blob: 1b3247ad8cff4e983943c4516521077ff6a35fd9 [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;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500820 struct weston_surface *main_surface, *next;
821 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200822
Pekka Paalanen01388e22013-04-25 13:57:44 +0300823 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
824
Kristian Høgsberge3778222012-07-31 17:29:30 -0400825 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300826 wl_list_for_each(view,
827 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500828 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400829 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100830 if (is_focus_view(view))
831 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400832
Jason Ekstranda7af7042013-10-12 22:38:11 -0500833 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400834 break;
835 }
836
Pekka Paalanen01388e22013-04-25 13:57:44 +0300837 /* if the focus was a sub-surface, activate its main surface */
838 if (main_surface != state->keyboard_focus)
839 next = main_surface;
840
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400842 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100844 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400845 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
847 if (state->ws->focus_animation)
848 weston_view_animation_destroy(state->ws->focus_animation);
849
850 state->ws->focus_animation = weston_fade_run(
851 state->ws->fsurf_front->view,
852 state->ws->fsurf_front->view->alpha, 0.0, 300,
853 focus_animation_done, state->ws);
854 }
855
Kristian Høgsberge3778222012-07-31 17:29:30 -0400856 wl_list_remove(&state->link);
857 focus_state_destroy(state);
858 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200859}
860
861static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400862focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200863{
Jonas Ådahl04769742012-06-13 00:01:24 +0200864 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200865
866 state = malloc(sizeof *state);
867 if (state == NULL)
868 return NULL;
869
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400871 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200872 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400873 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200874
875 state->seat_destroy_listener.notify = focus_state_seat_destroy;
876 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400877 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200878 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400879 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200880
881 return state;
882}
883
Jonas Ådahl8538b222012-08-29 22:13:03 +0200884static struct focus_state *
885ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
886{
887 struct workspace *ws = get_current_workspace(shell);
888 struct focus_state *state;
889
890 wl_list_for_each(state, &ws->focus_list, link)
891 if (state->seat == seat)
892 break;
893
894 if (&state->link == &ws->focus_list)
895 state = focus_state_create(seat, ws);
896
897 return state;
898}
899
Jonas Ådahl04769742012-06-13 00:01:24 +0200900static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800901focus_state_set_focus(struct focus_state *state,
902 struct weston_surface *surface)
903{
904 if (state->keyboard_focus) {
905 wl_list_remove(&state->surface_destroy_listener.link);
906 wl_list_init(&state->surface_destroy_listener.link);
907 }
908
909 state->keyboard_focus = surface;
910 if (surface)
911 wl_signal_add(&surface->destroy_signal,
912 &state->surface_destroy_listener);
913}
914
915static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400916restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200917{
918 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400919 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100920 struct wl_list pending_seat_list;
921 struct weston_seat *seat, *next_seat;
922
923 /* Temporarily steal the list of seats so that we can keep
924 * track of the seats we've already processed */
925 wl_list_init(&pending_seat_list);
926 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
927 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200928
929 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500930 struct weston_keyboard *keyboard =
931 weston_seat_get_keyboard(state->seat);
932
Neil Roberts4237f502014-04-09 16:33:31 +0100933 wl_list_remove(&state->seat->link);
934 wl_list_insert(&shell->compositor->seat_list,
935 &state->seat->link);
936
Derek Foreman1281a362015-07-31 16:55:32 -0500937 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800938 continue;
939
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400940 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200941
Derek Foreman1281a362015-07-31 16:55:32 -0500942 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200943 }
Neil Roberts4237f502014-04-09 16:33:31 +0100944
945 /* For any remaining seats that we don't have a focus state
946 * for we'll reset the keyboard focus to NULL */
947 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500948 struct weston_keyboard *keyboard =
949 weston_seat_get_keyboard(seat);
950
Neil Roberts4237f502014-04-09 16:33:31 +0100951 wl_list_insert(&shell->compositor->seat_list, &seat->link);
952
Derek Foreman1281a362015-07-31 16:55:32 -0500953 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100954 continue;
955
Derek Foreman1281a362015-07-31 16:55:32 -0500956 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100957 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200958}
959
960static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200961replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
962 struct weston_seat *seat)
963{
Derek Foreman1281a362015-07-31 16:55:32 -0500964 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200965 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200966
967 wl_list_for_each(state, &ws->focus_list, link) {
968 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500969 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200970 return;
971 }
972 }
973}
974
975static void
976drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
977 struct weston_surface *surface)
978{
979 struct focus_state *state;
980
981 wl_list_for_each(state, &ws->focus_list, link)
982 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800983 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200984}
985
986static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100987animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
988 struct weston_view *from, struct weston_view *to)
989{
990 struct weston_output *output;
991 bool focus_surface_created = false;
992
993 /* FIXME: Only support dim animation using two layers */
994 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
995 return;
996
997 output = get_default_output(shell->compositor);
998 if (ws->fsurf_front == NULL && (from || to)) {
999 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001000 if (ws->fsurf_front == NULL)
1001 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001003
1004 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1005 if (ws->fsurf_back == NULL) {
1006 focus_surface_destroy(ws->fsurf_front);
1007 return;
1008 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001009 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001010
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001011 focus_surface_created = true;
1012 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001013 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1014 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 }
1016
1017 if (ws->focus_animation) {
1018 weston_view_animation_destroy(ws->focus_animation);
1019 ws->focus_animation = NULL;
1020 }
1021
1022 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001023 weston_layer_entry_insert(&to->layer_link,
1024 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001025 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001026 weston_layer_entry_insert(&ws->layer.view_list,
1027 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028
1029 if (focus_surface_created) {
1030 ws->focus_animation = weston_fade_run(
1031 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001032 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001033 focus_animation_done, ws);
1034 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001035 weston_layer_entry_insert(&from->layer_link,
1036 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 ws->focus_animation = weston_stable_fade_run(
1038 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001039 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001040 focus_animation_done, ws);
1041 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001042 weston_layer_entry_insert(&ws->layer.view_list,
1043 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001044 ws->focus_animation = weston_stable_fade_run(
1045 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001046 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001047 focus_animation_done, ws);
1048 }
1049}
1050
1051static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001052workspace_destroy(struct workspace *ws)
1053{
Jonas Ådahl04769742012-06-13 00:01:24 +02001054 struct focus_state *state, *next;
1055
1056 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1057 focus_state_destroy(state);
1058
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001059 if (ws->fsurf_front)
1060 focus_surface_destroy(ws->fsurf_front);
1061 if (ws->fsurf_back)
1062 focus_surface_destroy(ws->fsurf_back);
1063
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001064 free(ws);
1065}
1066
Jonas Ådahl04769742012-06-13 00:01:24 +02001067static void
1068seat_destroyed(struct wl_listener *listener, void *data)
1069{
1070 struct weston_seat *seat = data;
1071 struct focus_state *state, *next;
1072 struct workspace *ws = container_of(listener,
1073 struct workspace,
1074 seat_destroyed_listener);
1075
1076 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1077 if (state->seat == seat)
1078 wl_list_remove(&state->link);
1079}
1080
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001081static struct workspace *
1082workspace_create(void)
1083{
1084 struct workspace *ws = malloc(sizeof *ws);
1085 if (ws == NULL)
1086 return NULL;
1087
1088 weston_layer_init(&ws->layer, NULL);
1089
Jonas Ådahl04769742012-06-13 00:01:24 +02001090 wl_list_init(&ws->focus_list);
1091 wl_list_init(&ws->seat_destroyed_listener.link);
1092 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001093 ws->fsurf_front = NULL;
1094 ws->fsurf_back = NULL;
1095 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001096
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001097 return ws;
1098}
1099
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100static int
1101workspace_is_empty(struct workspace *ws)
1102{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001103 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001104}
1105
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001106static struct workspace *
1107get_workspace(struct desktop_shell *shell, unsigned int index)
1108{
1109 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001110 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001111 pws += index;
1112 return *pws;
1113}
1114
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001115struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001116get_current_workspace(struct desktop_shell *shell)
1117{
1118 return get_workspace(shell, shell->workspaces.current);
1119}
1120
1121static void
1122activate_workspace(struct desktop_shell *shell, unsigned int index)
1123{
1124 struct workspace *ws;
1125
1126 ws = get_workspace(shell, index);
1127 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1128
1129 shell->workspaces.current = index;
1130}
1131
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001132static unsigned int
1133get_output_height(struct weston_output *output)
1134{
1135 return abs(output->region.extents.y1 - output->region.extents.y2);
1136}
1137
1138static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001139view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001141 struct weston_transform *transform;
1142
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001143 if (is_focus_view(view)) {
1144 struct focus_surface *fsurf = get_focus_surface(view->surface);
1145 transform = &fsurf->workspace_transform;
1146 } else {
1147 struct shell_surface *shsurf = get_shell_surface(view->surface);
1148 transform = &shsurf->workspace_transform;
1149 }
1150
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001153 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001155 weston_matrix_init(&transform->matrix);
1156 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159}
1160
1161static void
1162workspace_translate_out(struct workspace *ws, double fraction)
1163{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001165 unsigned int height;
1166 double d;
1167
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001168 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001170 d = height * fraction;
1171
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001172 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173 }
1174}
1175
1176static void
1177workspace_translate_in(struct workspace *ws, double fraction)
1178{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001180 unsigned int height;
1181 double d;
1182
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001183 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185
1186 if (fraction > 0)
1187 d = -(height - height * fraction);
1188 else
1189 d = height + height * fraction;
1190
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001191 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192 }
1193}
1194
1195static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001196reverse_workspace_change_animation(struct desktop_shell *shell,
1197 unsigned int index,
1198 struct workspace *from,
1199 struct workspace *to)
1200{
1201 shell->workspaces.current = index;
1202
1203 shell->workspaces.anim_to = to;
1204 shell->workspaces.anim_from = from;
1205 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1206 shell->workspaces.anim_timestamp = 0;
1207
Scott Moreau4272e632012-08-13 09:58:41 -06001208 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001209}
1210
1211static void
1212workspace_deactivate_transforms(struct workspace *ws)
1213{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001215 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001216
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001217 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001218 if (is_focus_view(view)) {
1219 struct focus_surface *fsurf = get_focus_surface(view->surface);
1220 transform = &fsurf->workspace_transform;
1221 } else {
1222 struct shell_surface *shsurf = get_shell_surface(view->surface);
1223 transform = &shsurf->workspace_transform;
1224 }
1225
1226 if (!wl_list_empty(&transform->link)) {
1227 wl_list_remove(&transform->link);
1228 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001229 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001231 }
1232}
1233
1234static void
1235finish_workspace_change_animation(struct desktop_shell *shell,
1236 struct workspace *from,
1237 struct workspace *to)
1238{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001239 struct weston_view *view;
1240
Scott Moreau4272e632012-08-13 09:58:41 -06001241 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001242
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001243 /* Views that extend past the bottom of the output are still
1244 * visible after the workspace animation ends but before its layer
1245 * is hidden. In that case, we need to damage below those views so
1246 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001247 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001248 weston_view_damage_below(view);
1249
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001250 wl_list_remove(&shell->workspaces.animation.link);
1251 workspace_deactivate_transforms(from);
1252 workspace_deactivate_transforms(to);
1253 shell->workspaces.anim_to = NULL;
1254
1255 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1256}
1257
1258static void
1259animate_workspace_change_frame(struct weston_animation *animation,
1260 struct weston_output *output, uint32_t msecs)
1261{
1262 struct desktop_shell *shell =
1263 container_of(animation, struct desktop_shell,
1264 workspaces.animation);
1265 struct workspace *from = shell->workspaces.anim_from;
1266 struct workspace *to = shell->workspaces.anim_to;
1267 uint32_t t;
1268 double x, y;
1269
1270 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1271 finish_workspace_change_animation(shell, from, to);
1272 return;
1273 }
1274
1275 if (shell->workspaces.anim_timestamp == 0) {
1276 if (shell->workspaces.anim_current == 0.0)
1277 shell->workspaces.anim_timestamp = msecs;
1278 else
1279 shell->workspaces.anim_timestamp =
1280 msecs -
1281 /* Invers of movement function 'y' below. */
1282 (asin(1.0 - shell->workspaces.anim_current) *
1283 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1284 M_2_PI);
1285 }
1286
1287 t = msecs - shell->workspaces.anim_timestamp;
1288
1289 /*
1290 * x = [0, π/2]
1291 * y(x) = sin(x)
1292 */
1293 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1294 y = sin(x);
1295
1296 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001297 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001298
1299 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1300 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1301 shell->workspaces.anim_current = y;
1302
Scott Moreau4272e632012-08-13 09:58:41 -06001303 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001304 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001305 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001306 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001307}
1308
1309static void
1310animate_workspace_change(struct desktop_shell *shell,
1311 unsigned int index,
1312 struct workspace *from,
1313 struct workspace *to)
1314{
1315 struct weston_output *output;
1316
1317 int dir;
1318
1319 if (index > shell->workspaces.current)
1320 dir = -1;
1321 else
1322 dir = 1;
1323
1324 shell->workspaces.current = index;
1325
1326 shell->workspaces.anim_dir = dir;
1327 shell->workspaces.anim_from = from;
1328 shell->workspaces.anim_to = to;
1329 shell->workspaces.anim_current = 0.0;
1330 shell->workspaces.anim_timestamp = 0;
1331
1332 output = container_of(shell->compositor->output_list.next,
1333 struct weston_output, link);
1334 wl_list_insert(&output->animation_list,
1335 &shell->workspaces.animation.link);
1336
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001337 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001338
1339 workspace_translate_in(to, 0);
1340
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001341 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001342
Scott Moreau4272e632012-08-13 09:58:41 -06001343 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001344}
1345
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001346static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001347update_workspace(struct desktop_shell *shell, unsigned int index,
1348 struct workspace *from, struct workspace *to)
1349{
1350 shell->workspaces.current = index;
1351 wl_list_insert(&from->layer.link, &to->layer.link);
1352 wl_list_remove(&from->layer.link);
1353}
1354
1355static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001356change_workspace(struct desktop_shell *shell, unsigned int index)
1357{
1358 struct workspace *from;
1359 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001360 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001361
1362 if (index == shell->workspaces.current)
1363 return;
1364
1365 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001366 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001367 return;
1368
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001369 from = get_current_workspace(shell);
1370 to = get_workspace(shell, index);
1371
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001372 if (shell->workspaces.anim_from == to &&
1373 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001374 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001375 reverse_workspace_change_animation(shell, index, from, to);
1376 return;
1377 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001378
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001379 if (shell->workspaces.anim_to != NULL)
1380 finish_workspace_change_animation(shell,
1381 shell->workspaces.anim_from,
1382 shell->workspaces.anim_to);
1383
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001384 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001385
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001386 if (shell->focus_animation_type != ANIMATION_NONE) {
1387 wl_list_for_each(state, &from->focus_list, link)
1388 if (state->keyboard_focus)
1389 animate_focus_change(shell, from,
1390 get_default_view(state->keyboard_focus), NULL);
1391
1392 wl_list_for_each(state, &to->focus_list, link)
1393 if (state->keyboard_focus)
1394 animate_focus_change(shell, to,
1395 NULL, get_default_view(state->keyboard_focus));
1396 }
1397
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001398 if (workspace_is_empty(to) && workspace_is_empty(from))
1399 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001400 else
1401 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001402}
1403
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001404static bool
1405workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1406{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001407 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001408 struct wl_list *e;
1409
1410 if (wl_list_empty(list))
1411 return false;
1412
1413 e = list->next;
1414
1415 if (e->next != list)
1416 return false;
1417
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001418 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001419}
1420
1421static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001422surface_keyboard_focus_lost(struct weston_surface *surface)
1423{
1424 struct weston_compositor *compositor = surface->compositor;
1425 struct weston_seat *seat;
1426 struct weston_surface *focus;
1427
1428 wl_list_for_each(seat, &compositor->seat_list, link) {
1429 struct weston_keyboard *keyboard =
1430 weston_seat_get_keyboard(seat);
1431
1432 if (!keyboard)
1433 continue;
1434
1435 focus = weston_surface_get_main_surface(keyboard->focus);
1436 if (focus == surface)
1437 weston_keyboard_set_focus(keyboard, NULL);
1438 }
1439}
1440
1441static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001442take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001443 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001444 unsigned int index)
1445{
Derek Foreman1281a362015-07-31 16:55:32 -05001446 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001447 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001449 struct shell_surface *shsurf;
1450 struct workspace *from;
1451 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001452 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001453
Derek Foreman1281a362015-07-31 16:55:32 -05001454 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 view = get_default_view(surface);
1456 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001457 index == shell->workspaces.current ||
1458 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001459 return;
1460
1461 from = get_current_workspace(shell);
1462 to = get_workspace(shell, index);
1463
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001464 weston_layer_entry_remove(&view->layer_link);
1465 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001466
Philip Withnall659163d2013-11-25 18:01:36 +00001467 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001468 if (shsurf != NULL)
1469 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001470
Jonas Ådahle9d22502012-08-29 22:13:01 +02001471 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001472 drop_focus_state(shell, from, surface);
1473
1474 if (shell->workspaces.anim_from == to &&
1475 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001476 wl_list_remove(&to->layer.link);
1477 wl_list_insert(from->layer.link.prev, &to->layer.link);
1478
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001479 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001480
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001481 return;
1482 }
1483
1484 if (shell->workspaces.anim_to != NULL)
1485 finish_workspace_change_animation(shell,
1486 shell->workspaces.anim_from,
1487 shell->workspaces.anim_to);
1488
1489 if (workspace_is_empty(from) &&
1490 workspace_has_only(to, surface))
1491 update_workspace(shell, index, from, to);
1492 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001493 if (shsurf != NULL &&
1494 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001495 wl_list_insert(&shell->workspaces.anim_sticky_list,
1496 &shsurf->workspace_transform.link);
1497
1498 animate_workspace_change(shell, index, from, to);
1499 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001500
Jonas Ådahl8538b222012-08-29 22:13:03 +02001501 state = ensure_focus_state(shell, seat);
1502 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001503 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001504}
1505
1506static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001507touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001508 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001509{
1510}
1511
1512static void
1513touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1514{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001515 struct weston_touch_move_grab *move =
1516 (struct weston_touch_move_grab *) container_of(
1517 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001518
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001519 if (touch_id == 0)
1520 move->active = 0;
1521
Jonas Ådahl9484b692013-12-02 22:05:03 +01001522 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001523 shell_touch_grab_end(&move->base);
1524 free(move);
1525 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001526}
1527
1528static void
1529touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001530 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001531{
1532 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1533 struct shell_surface *shsurf = move->base.shsurf;
1534 struct weston_surface *es;
1535 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1536 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1537
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001538 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001539 return;
1540
1541 es = shsurf->surface;
1542
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001543 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001544
1545 weston_compositor_schedule_repaint(es->compositor);
1546}
1547
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001548static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001549touch_move_grab_frame(struct weston_touch_grab *grab)
1550{
1551}
1552
1553static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001554touch_move_grab_cancel(struct weston_touch_grab *grab)
1555{
1556 struct weston_touch_move_grab *move =
1557 (struct weston_touch_move_grab *) container_of(
1558 grab, struct shell_touch_grab, grab);
1559
1560 shell_touch_grab_end(&move->base);
1561 free(move);
1562}
1563
Rusty Lynch1084da52013-08-15 09:10:08 -07001564static const struct weston_touch_grab_interface touch_move_grab_interface = {
1565 touch_move_grab_down,
1566 touch_move_grab_up,
1567 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001568 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001569 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001570};
1571
1572static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001573surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001574{
1575 struct weston_touch_move_grab *move;
1576
1577 if (!shsurf)
1578 return -1;
1579
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001580 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001581 return 0;
1582
1583 move = malloc(sizeof *move);
1584 if (!move)
1585 return -1;
1586
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001587 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001589 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001591 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001592
1593 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001594 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001595
1596 return 0;
1597}
1598
1599static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001600noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001601{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001602}
1603
1604static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001605noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001606 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001607{
1608}
1609
1610static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001611noop_grab_axis_source(struct weston_pointer_grab *grab,
1612 uint32_t source)
1613{
1614}
1615
1616static void
1617noop_grab_frame(struct weston_pointer_grab *grab)
1618{
1619}
1620
1621static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001622constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1623{
1624 struct shell_surface *shsurf = move->base.shsurf;
1625 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001626 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001627 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001628 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001629
1630 x = wl_fixed_to_int(pointer->x + move->dx);
1631 y = wl_fixed_to_int(pointer->y + move->dy);
1632
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001633 if (shsurf->shell->panel_position ==
1634 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001635 get_output_work_area(shsurf->shell,
1636 shsurf->surface->output,
1637 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001638
Derek Foreman6feb0f92015-04-15 12:23:56 -05001639 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1640 if (bottom - safety < area.y)
1641 y = area.y + safety - shsurf->geometry.height
1642 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001643
1644 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001645 y + shsurf->geometry.y < area.y)
1646 y = area.y - shsurf->geometry.y;
1647
Jonny Lambd73c6942014-08-20 15:53:20 +02001648 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001649
1650 *cx = x;
1651 *cy = y;
1652}
1653
1654static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001655move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001656 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001657{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001658 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001659 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001660 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001661 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001662
Jonas Ådahld2510102014-10-05 21:39:14 +02001663 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001664 if (!shsurf)
1665 return;
1666
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001667 constrain_position(move, &cx, &cy);
1668
1669 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001670
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001672}
1673
1674static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001675move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001676 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001677{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001678 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1679 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001680 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001681 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001682
Daniel Stone4dbadb12012-05-30 16:31:51 +01001683 if (pointer->button_count == 0 &&
1684 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001685 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001686 free(grab);
1687 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001688}
1689
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001690static void
1691move_grab_cancel(struct weston_pointer_grab *grab)
1692{
1693 struct shell_grab *shell_grab =
1694 container_of(grab, struct shell_grab, grab);
1695
1696 shell_grab_end(shell_grab);
1697 free(grab);
1698}
1699
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001700static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001701 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001702 move_grab_motion,
1703 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001704 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001705 noop_grab_axis_source,
1706 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001707 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001708};
1709
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001711surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001712 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713{
1714 struct weston_move_grab *move;
1715
1716 if (!shsurf)
1717 return -1;
1718
Derek Foreman45a7c272015-09-11 14:27:40 -05001719 shsurf = find_toplevel_surface(shsurf);
1720
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001721 if (shsurf->grabbed ||
1722 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001723 return 0;
1724
1725 move = malloc(sizeof *move);
1726 if (!move)
1727 return -1;
1728
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001730 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001732 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001733 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734
1735 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001736 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737
1738 return 0;
1739}
1740
1741static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001742common_surface_move(struct wl_resource *resource,
1743 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001745 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001746 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1747 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001748 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001749 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750
Derek Foreman1281a362015-07-31 16:55:32 -05001751 if (pointer &&
1752 pointer->focus &&
1753 pointer->button_count > 0 &&
1754 pointer->grab_serial == serial) {
1755 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001756 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001757 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001758 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001759 } else if (touch &&
1760 touch->focus &&
1761 touch->grab_serial == serial) {
1762 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001763 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001764 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001765 wl_resource_post_no_memory(resource);
1766 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767}
1768
Rafael Antognollie2a34552013-12-03 15:35:45 -02001769static void
1770shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1771 struct wl_resource *seat_resource, uint32_t serial)
1772{
1773 common_surface_move(resource, seat_resource, serial);
1774}
1775
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776struct weston_resize_grab {
1777 struct shell_grab base;
1778 uint32_t edges;
1779 int32_t width, height;
1780};
1781
1782static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001783resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001784 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001785{
1786 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001787 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001788 struct shell_surface *shsurf = resize->base.shsurf;
1789 int32_t width, height;
1790 wl_fixed_t from_x, from_y;
1791 wl_fixed_t to_x, to_y;
1792
Jonas Ådahld2510102014-10-05 21:39:14 +02001793 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001794
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001795 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001796 return;
1797
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 weston_view_from_global_fixed(shsurf->view,
1799 pointer->grab_x, pointer->grab_y,
1800 &from_x, &from_y);
1801 weston_view_from_global_fixed(shsurf->view,
1802 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001803
1804 width = resize->width;
1805 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1806 width += wl_fixed_to_int(from_x - to_x);
1807 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1808 width += wl_fixed_to_int(to_x - from_x);
1809 }
1810
1811 height = resize->height;
1812 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1813 height += wl_fixed_to_int(from_y - to_y);
1814 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1815 height += wl_fixed_to_int(to_y - from_y);
1816 }
1817
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001818 if (width < 1)
1819 width = 1;
1820 if (height < 1)
1821 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001822 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001823}
1824
1825static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001826send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001827{
1828 struct shell_surface *shsurf = get_shell_surface(surface);
1829
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001830 assert(shsurf);
1831
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001832 if (shsurf->resource)
1833 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001834 shsurf->resize_edges,
1835 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001836}
1837
1838static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001839 send_configure,
1840 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001841};
1842
1843static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001844resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001845 uint32_t time, uint32_t button, uint32_t state_w)
1846{
1847 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001848 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001849 enum wl_pointer_button_state state = state_w;
1850
1851 if (pointer->button_count == 0 &&
1852 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1853 shell_grab_end(&resize->base);
1854 free(grab);
1855 }
1856}
1857
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001858static void
1859resize_grab_cancel(struct weston_pointer_grab *grab)
1860{
1861 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1862
1863 shell_grab_end(&resize->base);
1864 free(grab);
1865}
1866
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001867static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001868 noop_grab_focus,
1869 resize_grab_motion,
1870 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001871 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001872 noop_grab_axis_source,
1873 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001874 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001875};
1876
Giulio Camuffob8366642013-04-25 13:57:46 +03001877/*
1878 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001879 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001880static void
1881surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1882 int32_t *y, int32_t *w, int32_t *h) {
1883 pixman_region32_t region;
1884 pixman_box32_t *box;
1885 struct weston_subsurface *subsurface;
1886
1887 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 surface->width,
1889 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001890
1891 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1892 pixman_region32_union_rect(&region, &region,
1893 subsurface->position.x,
1894 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895 subsurface->surface->width,
1896 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001897 }
1898
1899 box = pixman_region32_extents(&region);
1900 if (x)
1901 *x = box->x1;
1902 if (y)
1903 *y = box->y1;
1904 if (w)
1905 *w = box->x2 - box->x1;
1906 if (h)
1907 *h = box->y2 - box->y1;
1908
1909 pixman_region32_fini(&region);
1910}
1911
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001912static int
1913surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001914 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001915{
1916 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001917 const unsigned resize_topbottom =
1918 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1919 const unsigned resize_leftright =
1920 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1921 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001922
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001923 if (shsurf->grabbed ||
1924 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001925 return 0;
1926
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001927 /* Check for invalid edge combinations. */
1928 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1929 (edges & resize_topbottom) == resize_topbottom ||
1930 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001931 return 0;
1932
1933 resize = malloc(sizeof *resize);
1934 if (!resize)
1935 return -1;
1936
1937 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001938
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001939 resize->width = shsurf->geometry.width;
1940 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001941
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001942 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001943 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001944 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001945 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001946
1947 return 0;
1948}
1949
1950static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001951common_surface_resize(struct wl_resource *resource,
1952 struct wl_resource *seat_resource, uint32_t serial,
1953 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001954{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001955 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001956 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001957 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001958 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001959
Derek Foreman1281a362015-07-31 16:55:32 -05001960 if (!pointer ||
1961 pointer->button_count == 0 ||
1962 pointer->grab_serial != serial ||
1963 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001964 return;
1965
Derek Foreman1281a362015-07-31 16:55:32 -05001966 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001967 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001968 return;
1969
Derek Foreman1281a362015-07-31 16:55:32 -05001970 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001971 wl_resource_post_no_memory(resource);
1972}
1973
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001974static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001975shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1976 struct wl_resource *seat_resource, uint32_t serial,
1977 uint32_t edges)
1978{
1979 common_surface_resize(resource, seat_resource, serial, edges);
1980}
1981
1982static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001983busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001984{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001985 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001986 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001988 wl_fixed_t sx, sy;
1989
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 view = weston_compositor_pick_view(pointer->seat->compositor,
1991 pointer->x, pointer->y,
1992 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001993
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001994 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1995 shell_grab_end(grab);
1996 free(grab);
1997 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001998}
1999
2000static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002001busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02002002 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002003{
Jonas Ådahld2510102014-10-05 21:39:14 +02002004 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002005}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002006
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002007static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002008busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002009 uint32_t time, uint32_t button, uint32_t state)
2010{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002011 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002012 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002013 struct weston_pointer *pointer = grab->grab.pointer;
2014 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002015
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002016 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002017 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002018 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002019 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002020 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002021 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002022 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002023}
2024
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002025static void
2026busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2027{
2028 struct shell_grab *grab = (struct shell_grab *) base;
2029
2030 shell_grab_end(grab);
2031 free(grab);
2032}
2033
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002034static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002035 busy_cursor_grab_focus,
2036 busy_cursor_grab_motion,
2037 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002038 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002039 noop_grab_axis_source,
2040 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002041 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002042};
2043
2044static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002045set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002046{
2047 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002048
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002049 if (pointer->grab->interface == &busy_cursor_grab_interface)
2050 return;
2051
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052 grab = malloc(sizeof *grab);
2053 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002054 return;
2055
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002056 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002057 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002058 /* Mark the shsurf as ungrabbed so that button binding is able
2059 * to move it. */
2060 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002061}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002062
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002063static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002064end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002065{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002066 struct shell_grab *grab;
2067 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002068
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002069 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002070 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2071
2072 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002073 continue;
2074
Derek Foreman1281a362015-07-31 16:55:32 -05002075 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002076 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002077 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002078 wl_resource_get_client(grab->shsurf->resource) == client) {
2079 shell_grab_end(grab);
2080 free(grab);
2081 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002082 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002083}
2084
Scott Moreau9521d5e2012-04-19 13:06:17 -06002085static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002086handle_shell_client_destroy(struct wl_listener *listener, void *data);
2087
2088static int
2089xdg_ping_timeout_handler(void *data)
2090{
2091 struct shell_client *sc = data;
2092 struct weston_seat *seat;
2093 struct shell_surface *shsurf;
2094
2095 /* Client is not responding */
2096 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002097 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002098 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2099
2100 if (!pointer ||
2101 !pointer->focus ||
2102 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002103 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002104
Derek Foreman1281a362015-07-31 16:55:32 -05002105 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002106 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002107 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002108 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002109 }
2110
2111 return 1;
2112}
2113
2114static void
2115handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2116{
2117 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002118 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002119 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002120 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002121
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002122 if (sc->unresponsive) {
2123 xdg_ping_timeout_handler(sc);
2124 return;
2125 }
2126
2127 sc->ping_serial = serial;
2128 loop = wl_display_get_event_loop(compositor->wl_display);
2129 if (sc->ping_timer == NULL)
2130 sc->ping_timer =
2131 wl_event_loop_add_timer(loop,
2132 xdg_ping_timeout_handler, sc);
2133 if (sc->ping_timer == NULL)
2134 return;
2135
2136 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2137
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002138 if (shell_surface_is_xdg_surface(shsurf) ||
2139 shell_surface_is_xdg_popup(shsurf))
2140 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002141 else if (shell_surface_is_wl_shell_surface(shsurf)
2142 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002143 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002144}
2145
Scott Moreauff1db4a2012-04-17 19:06:18 -06002146static void
2147ping_handler(struct weston_surface *surface, uint32_t serial)
2148{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002149 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002150
2151 if (!shsurf)
2152 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002153 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002154 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002155 if (shsurf->surface == shsurf->shell->grab_surface)
2156 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002157 if (!shsurf->owner)
2158 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002159
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002160 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002161}
2162
2163static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002164handle_pointer_focus(struct wl_listener *listener, void *data)
2165{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002166 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002168 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002169 uint32_t serial;
2170
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002172 return;
2173
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002175 serial = wl_display_next_serial(compositor->wl_display);
2176 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002177}
2178
2179static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002180shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2181{
2182 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002183 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002184}
2185
2186static void
2187shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2188{
2189 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002190 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002191}
2192
2193static void
2194handle_keyboard_focus(struct wl_listener *listener, void *data)
2195{
2196 struct weston_keyboard *keyboard = data;
2197 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2198
2199 if (seat->focused_surface) {
2200 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2201 if (shsurf)
2202 shell_surface_lose_keyboard_focus(shsurf);
2203 }
2204
2205 seat->focused_surface = keyboard->focus;
2206
2207 if (seat->focused_surface) {
2208 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2209 if (shsurf)
2210 shell_surface_gain_keyboard_focus(shsurf);
2211 }
2212}
2213
2214static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002215shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002216{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002217 if (sc->ping_serial != serial)
2218 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002219
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002220 sc->unresponsive = 0;
2221 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002222
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002223 if (sc->ping_timer) {
2224 wl_event_source_remove(sc->ping_timer);
2225 sc->ping_timer = NULL;
2226 }
2227
2228}
2229
2230static void
2231shell_surface_pong(struct wl_client *client,
2232 struct wl_resource *resource, uint32_t serial)
2233{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002234 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2235 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002236
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002237 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002238}
2239
2240static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002241set_title(struct shell_surface *shsurf, const char *title)
2242{
2243 free(shsurf->title);
2244 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002245 shsurf->surface->timeline.force_refresh = 1;
2246}
2247
2248static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002249set_pid(struct shell_surface *shsurf, pid_t pid)
2250{
2251 /* We have no use for it */
2252}
2253
2254static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002255set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2256{
2257 shsurf->type = t;
2258 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002259}
2260
2261static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002262set_window_geometry(struct shell_surface *shsurf,
2263 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002264{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002265 shsurf->next_geometry.x = x;
2266 shsurf->next_geometry.y = y;
2267 shsurf->next_geometry.width = width;
2268 shsurf->next_geometry.height = height;
2269 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002270}
2271
2272static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002273shell_surface_set_title(struct wl_client *client,
2274 struct wl_resource *resource, const char *title)
2275{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002276 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002277
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002278 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002279}
2280
2281static void
2282shell_surface_set_class(struct wl_client *client,
2283 struct wl_resource *resource, const char *class)
2284{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002285 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002286
2287 free(shsurf->class);
2288 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002289 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002290}
2291
Alex Wu4539b082012-03-01 12:57:46 +08002292static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002293restore_output_mode(struct weston_output *output)
2294{
Armin Krezovićc77f2582016-06-23 11:59:32 +02002295 if (output && output->original_mode)
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002296 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002297}
2298
2299static void
2300restore_all_output_modes(struct weston_compositor *compositor)
2301{
2302 struct weston_output *output;
2303
2304 wl_list_for_each(output, &compositor->output_list, link)
2305 restore_output_mode(output);
2306}
2307
Philip Withnall07926d92013-11-25 18:01:40 +00002308/* The surface will be inserted into the list immediately after the link
2309 * returned by this function (i.e. will be stacked immediately above the
2310 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002311static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002312shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2313{
2314 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002315 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002316
2317 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002318 case SHELL_SURFACE_XWAYLAND:
2319 return &shsurf->shell->fullscreen_layer.view_list;
2320
2321 case SHELL_SURFACE_NONE:
2322 return NULL;
2323
Kristian Høgsbergead52422014-01-01 13:51:52 -08002324 case SHELL_SURFACE_POPUP:
2325 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002326 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002327 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002328 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002329 /* Move the surface to its parent layer so
2330 * that surfaces which are transient for
2331 * fullscreen surfaces don't get hidden by the
2332 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002333
2334 /* TODO: Handle a parent with multiple views */
2335 parent = get_default_view(shsurf->parent);
2336 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002337 return container_of(parent->layer_link.link.prev,
2338 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002339 }
Philip Withnall07926d92013-11-25 18:01:40 +00002340
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002341 /* Move the surface to a normal workspace layer so that surfaces
2342 * which were previously fullscreen or transient are no longer
2343 * rendered on top. */
2344 ws = get_current_workspace(shsurf->shell);
2345 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002346 }
2347
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002348 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002349}
2350
Philip Withnall648a4dd2013-11-25 18:01:44 +00002351static void
2352shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2353{
2354 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002355 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002356
2357 /* Move the child layers to the same workspace as shsurf. They will be
2358 * stacked above shsurf. */
2359 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002360 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002361 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002362 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002363 prev = container_of(shsurf->view->layer_link.link.prev,
2364 struct weston_layer_entry, link);
2365 weston_layer_entry_remove(&child->view->layer_link);
2366 weston_layer_entry_insert(prev,
2367 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002368 weston_view_geometry_dirty(child->view);
2369 weston_surface_damage(child->surface);
2370
2371 /* Recurse. We don’t expect this to recurse very far (if
2372 * at all) because that would imply we have transient
2373 * (or popup) children of transient surfaces, which
2374 * would be unusual. */
2375 shell_surface_update_child_surface_layers(child);
2376 }
2377 }
2378}
2379
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002380/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002381 * geometry to ensure the changes are redrawn.
2382 *
2383 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2384 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002385static void
2386shell_surface_update_layer(struct shell_surface *shsurf)
2387{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002388 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002389
2390 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2391
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002392 if (new_layer_link == NULL)
2393 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002394 if (new_layer_link == &shsurf->view->layer_link)
2395 return;
2396
2397 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002398 weston_layer_entry_remove(&shsurf->view->layer_link);
2399 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002400 weston_view_geometry_dirty(shsurf->view);
2401 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002402
2403 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002404}
2405
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002406static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002407shell_surface_set_parent(struct shell_surface *shsurf,
2408 struct weston_surface *parent)
2409{
2410 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002411
2412 wl_list_remove(&shsurf->children_link);
2413 wl_list_init(&shsurf->children_link);
2414
2415 /* Insert into the parent surface’s child list. */
2416 if (parent != NULL) {
2417 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2418 if (parent_shsurf != NULL)
2419 wl_list_insert(&parent_shsurf->children_list,
2420 &shsurf->children_link);
2421 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002422}
2423
2424static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002425shell_surface_set_output(struct shell_surface *shsurf,
2426 struct weston_output *output)
2427{
2428 struct weston_surface *es = shsurf->surface;
2429
2430 /* get the default output, if the client set it as NULL
2431 check whether the ouput is available */
2432 if (output)
2433 shsurf->output = output;
2434 else if (es->output)
2435 shsurf->output = es->output;
2436 else
2437 shsurf->output = get_default_output(es->compositor);
2438}
2439
2440static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002441surface_clear_next_states(struct shell_surface *shsurf)
2442{
2443 shsurf->next_state.maximized = false;
2444 shsurf->next_state.fullscreen = false;
2445
2446 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2447 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2448 shsurf->state_changed = true;
2449}
2450
2451static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002452set_toplevel(struct shell_surface *shsurf)
2453{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002454 shell_surface_set_parent(shsurf, NULL);
2455 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002456 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002457
2458 /* The layer_link is updated in set_surface_type(),
2459 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460}
2461
2462static void
2463shell_surface_set_toplevel(struct wl_client *client,
2464 struct wl_resource *resource)
2465{
2466 struct shell_surface *surface = wl_resource_get_user_data(resource);
2467
2468 set_toplevel(surface);
2469}
2470
2471static void
2472set_transient(struct shell_surface *shsurf,
2473 struct weston_surface *parent, int x, int y, uint32_t flags)
2474{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002475 assert(parent != NULL);
2476
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002477 shell_surface_set_parent(shsurf, parent);
2478
2479 surface_clear_next_states(shsurf);
2480
Philip Withnallbecb77e2013-11-25 18:01:30 +00002481 shsurf->transient.x = x;
2482 shsurf->transient.y = y;
2483 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002484
Rafael Antognollied207b42013-12-03 15:35:43 -02002485 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002486 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002487 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002488
2489 /* The layer_link is updated in set_surface_type(),
2490 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002491}
2492
2493static void
2494shell_surface_set_transient(struct wl_client *client,
2495 struct wl_resource *resource,
2496 struct wl_resource *parent_resource,
2497 int x, int y, uint32_t flags)
2498{
2499 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2500 struct weston_surface *parent =
2501 wl_resource_get_user_data(parent_resource);
2502
2503 set_transient(shsurf, parent, x, y, flags);
2504}
2505
2506static void
2507set_fullscreen(struct shell_surface *shsurf,
2508 uint32_t method,
2509 uint32_t framerate,
2510 struct weston_output *output)
2511{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002512 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002513 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002514
2515 shsurf->fullscreen_output = shsurf->output;
2516 shsurf->fullscreen.type = method;
2517 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002518
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002519 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002520}
2521
2522static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002523weston_view_set_initial_position(struct weston_view *view,
2524 struct desktop_shell *shell);
2525
2526static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002527unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002528{
Philip Withnallf85fe842013-11-25 18:01:37 +00002529 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002530 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2531 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002532 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002533 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002534
Alex Wu4539b082012-03-01 12:57:46 +08002535 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2536 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002537
Alex Wu4539b082012-03-01 12:57:46 +08002538 wl_list_remove(&shsurf->fullscreen.transform.link);
2539 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002540
Jason Ekstranda7af7042013-10-12 22:38:11 -05002541 if (shsurf->fullscreen.black_view)
2542 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2543 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002544
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002545 if (shsurf->saved_position_valid)
2546 weston_view_set_position(shsurf->view,
2547 shsurf->saved_x, shsurf->saved_y);
2548 else
2549 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2550
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002551 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002553 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002554 shsurf->saved_rotation_valid = false;
2555 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002556
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002557 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002558}
2559
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002560static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002561shell_surface_set_fullscreen(struct wl_client *client,
2562 struct wl_resource *resource,
2563 uint32_t method,
2564 uint32_t framerate,
2565 struct wl_resource *output_resource)
2566{
2567 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2568 struct weston_output *output;
2569
Emmanuel Gil Peyrot28834b62016-05-10 03:22:43 +02002570 if (shsurf->fullscreen_output == shsurf->output &&
2571 shsurf->fullscreen.type == method &&
2572 shsurf->fullscreen.framerate == framerate) {
2573 send_configure_for_surface(shsurf);
2574 return;
2575 }
2576
Philip Withnallbecb77e2013-11-25 18:01:30 +00002577 if (output_resource)
2578 output = wl_resource_get_user_data(output_resource);
2579 else
2580 output = NULL;
2581
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002582 shell_surface_set_parent(shsurf, NULL);
2583
Rafael Antognolli03b16592013-12-03 15:35:42 -02002584 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002585 shsurf->next_state.fullscreen = true;
2586 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002587 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002588}
2589
2590static void
2591set_popup(struct shell_surface *shsurf,
2592 struct weston_surface *parent,
2593 struct weston_seat *seat,
2594 uint32_t serial,
2595 int32_t x,
2596 int32_t y)
2597{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002598 assert(parent != NULL);
2599
Philip Withnallbecb77e2013-11-25 18:01:30 +00002600 shsurf->popup.shseat = get_shell_seat(seat);
2601 shsurf->popup.serial = serial;
2602 shsurf->popup.x = x;
2603 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002604
Pekka Paalanenb5026542014-11-12 15:09:24 +02002605 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002606}
2607
2608static void
2609shell_surface_set_popup(struct wl_client *client,
2610 struct wl_resource *resource,
2611 struct wl_resource *seat_resource,
2612 uint32_t serial,
2613 struct wl_resource *parent_resource,
2614 int32_t x, int32_t y, uint32_t flags)
2615{
2616 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002617 struct weston_surface *parent =
2618 wl_resource_get_user_data(parent_resource);
2619
2620 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002621
Rafael Antognolli03b16592013-12-03 15:35:42 -02002622 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002623 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002624 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002625 wl_resource_get_user_data(seat_resource),
2626 serial, x, y);
2627}
2628
2629static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002630unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002631{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002632 /* undo all maximized things here */
2633 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002634
2635 if (shsurf->saved_position_valid)
2636 weston_view_set_position(shsurf->view,
2637 shsurf->saved_x, shsurf->saved_y);
2638 else
2639 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002640
2641 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642 wl_list_insert(&shsurf->view->geometry.transformation_list,
2643 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002644 shsurf->saved_rotation_valid = false;
2645 }
2646
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002647 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002648}
2649
Philip Withnallbecb77e2013-11-25 18:01:30 +00002650static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002651set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002652{
2653 struct shell_surface *shsurf;
2654 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002655 struct weston_view *view;
2656
2657 view = get_default_view(surface);
2658 if (!view)
2659 return;
2660
2661 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2662
2663 shsurf = get_shell_surface(surface);
2664 current_ws = get_current_workspace(shsurf->shell);
2665
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002666 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002667 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002668
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002669 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002670 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002671
2672 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002673 weston_view_damage_below(view);
2674}
2675
2676static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677shell_surface_set_maximized(struct wl_client *client,
2678 struct wl_resource *resource,
2679 struct wl_resource *output_resource)
2680{
2681 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2682 struct weston_output *output;
2683
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002684 surface_clear_next_states(shsurf);
2685 shsurf->next_state.maximized = true;
2686 shsurf->state_changed = true;
2687
Pekka Paalanenb5026542014-11-12 15:09:24 +02002688 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002689 shell_surface_set_parent(shsurf, NULL);
2690
Philip Withnallbecb77e2013-11-25 18:01:30 +00002691 if (output_resource)
2692 output = wl_resource_get_user_data(output_resource);
2693 else
2694 output = NULL;
2695
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002696 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002697
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002698 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002699}
2700
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002701/* This is only ever called from set_surface_type(), so there’s no need to
2702 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002703static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002704reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002705{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002706 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002707 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002708 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002709 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002710
Pekka Paalanen98262232011-12-01 10:42:22 +02002711 return 0;
2712}
2713
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002714static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002715set_full_output(struct shell_surface *shsurf)
2716{
2717 shsurf->saved_x = shsurf->view->geometry.x;
2718 shsurf->saved_y = shsurf->view->geometry.y;
2719 shsurf->saved_position_valid = true;
2720
2721 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2722 wl_list_remove(&shsurf->rotation.transform.link);
2723 wl_list_init(&shsurf->rotation.transform.link);
2724 weston_view_geometry_dirty(shsurf->view);
2725 shsurf->saved_rotation_valid = true;
2726 }
2727}
2728
2729static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002730set_surface_type(struct shell_surface *shsurf)
2731{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002732 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002734
Philip Withnallbecb77e2013-11-25 18:01:30 +00002735 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002736
Rafael Antognolli03b16592013-12-03 15:35:42 -02002737 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002738 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002739
2740 switch (shsurf->type) {
2741 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002742 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002743 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002744 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745 weston_view_set_position(shsurf->view,
2746 pev->geometry.x + shsurf->transient.x,
2747 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002748 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002749 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002750
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002751 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 weston_view_set_position(shsurf->view, shsurf->transient.x,
2753 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002754 break;
2755
Philip Withnall0f640e22013-11-25 18:01:31 +00002756 case SHELL_SURFACE_POPUP:
2757 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002758 default:
2759 break;
2760 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002761
2762 /* Update the surface’s layer. */
2763 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002764}
2765
Tiago Vignattibe143262012-04-16 17:31:41 +03002766static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002767shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002768{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002769 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002770}
2771
Pekka Paalanen8274d902014-08-06 19:36:51 +03002772static int
2773black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2774{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002775 struct weston_view *fs_view = surface->configure_private;
2776 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002777 int n;
2778 int rem;
2779 int ret;
2780
2781 n = snprintf(buf, len, "black background surface for ");
2782 if (n < 0)
2783 return n;
2784
2785 rem = (int)len - n;
2786 if (rem < 0)
2787 rem = 0;
2788
2789 if (fs_surface->get_label)
2790 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2791 else
2792 ret = snprintf(buf + n, rem, "<unknown>");
2793
2794 if (ret < 0)
2795 return n;
2796
2797 return n + ret;
2798}
2799
Alex Wu21858432012-04-01 20:13:08 +08002800static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002801black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002802
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002804create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002805 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002806 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002807{
2808 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002810
2811 surface = weston_surface_create(ec);
2812 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002813 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002814 return NULL;
2815 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002816 view = weston_view_create(surface);
2817 if (surface == NULL) {
2818 weston_log("no memory\n");
2819 weston_surface_destroy(surface);
2820 return NULL;
2821 }
Alex Wu4539b082012-03-01 12:57:46 +08002822
Alex Wu21858432012-04-01 20:13:08 +08002823 surface->configure = black_surface_configure;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002824 surface->configure_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002825 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002826 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002827 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002828 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002829 pixman_region32_fini(&surface->input);
2830 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002831
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002832 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002833 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002834
2835 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002836}
2837
Philip Withnalled8f1a92013-11-25 18:01:43 +00002838static void
2839shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2840{
2841 struct weston_output *output = shsurf->fullscreen_output;
2842
Rafael Antognolli03b16592013-12-03 15:35:42 -02002843 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002844
2845 if (!shsurf->fullscreen.black_view)
2846 shsurf->fullscreen.black_view =
2847 create_black_surface(shsurf->surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002848 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002849 output->x, output->y,
2850 output->width,
2851 output->height);
2852
2853 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002854 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2855 weston_layer_entry_insert(&shsurf->view->layer_link,
2856 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002857 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2858 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002859
Armin Krezović4663aca2016-06-30 06:04:29 +02002860 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002861 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002862}
2863
Alex Wu4539b082012-03-01 12:57:46 +08002864/* Create black surface and append it to the associated fullscreen surface.
2865 * Handle size dismatch and positioning according to the method. */
2866static void
2867shell_configure_fullscreen(struct shell_surface *shsurf)
2868{
2869 struct weston_output *output = shsurf->fullscreen_output;
2870 struct weston_surface *surface = shsurf->surface;
2871 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002872 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002873 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002874
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002875 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2876 restore_output_mode(output);
2877
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002878 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002879 weston_layer_entry_remove(&shsurf->view->layer_link);
2880 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002881
Philip Withnalled8f1a92013-11-25 18:01:43 +00002882 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002883
Jason Ekstranda7af7042013-10-12 22:38:11 -05002884 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002885 &surf_width, &surf_height);
2886
Alex Wu4539b082012-03-01 12:57:46 +08002887 switch (shsurf->fullscreen.type) {
2888 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002889 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002890 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002891 break;
2892 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002893 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002894 if (output->width == surf_width &&
2895 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002896 weston_view_set_position(shsurf->view,
2897 output->x - surf_x,
2898 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002899 break;
2900 }
2901
Alex Wu4539b082012-03-01 12:57:46 +08002902 matrix = &shsurf->fullscreen.transform.matrix;
2903 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002904
Scott Moreau1bad5db2012-08-18 01:04:05 -06002905 output_aspect = (float) output->width /
2906 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 /* XXX: Use surf_width and surf_height here? */
2908 surface_aspect = (float) surface->width /
2909 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002910 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002911 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002912 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002913 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002914 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002915 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002916
Alex Wu4539b082012-03-01 12:57:46 +08002917 weston_matrix_scale(matrix, scale, scale, 1);
2918 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002919 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002920 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002921 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2922 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002924
Alex Wu4539b082012-03-01 12:57:46 +08002925 break;
2926 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002927 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002928 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002929 surf_width * surface->buffer_viewport.buffer.scale,
2930 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002931 shsurf->fullscreen.framerate};
2932
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002933 if (weston_output_mode_switch_to_temporary(output, &mode,
2934 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 weston_view_set_position(shsurf->view,
2936 output->x - surf_x,
2937 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002938 shsurf->fullscreen.black_view->surface->width = output->width;
2939 shsurf->fullscreen.black_view->surface->height = output->height;
2940 weston_view_set_position(shsurf->fullscreen.black_view,
2941 output->x - surf_x,
2942 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002943 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002944 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002945 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002946 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002947 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002948 }
Alex Wubd3354b2012-04-17 17:20:49 +08002949 }
Alex Wu4539b082012-03-01 12:57:46 +08002950 break;
2951 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002953 break;
2954 default:
2955 break;
2956 }
2957}
2958
Alex Wu4539b082012-03-01 12:57:46 +08002959static void
2960shell_map_fullscreen(struct shell_surface *shsurf)
2961{
Alex Wubd3354b2012-04-17 17:20:49 +08002962 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002963}
2964
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002965static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002966set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2967{
2968 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002969 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002970 shsurf->transient.x = x;
2971 shsurf->transient.y = y;
2972 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002973
2974 shell_surface_set_parent(shsurf, NULL);
2975
Pekka Paalanenb5026542014-11-12 15:09:24 +02002976 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002977 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002978}
2979
Kristian Høgsberg47792412014-05-04 13:47:06 -07002980static void
2981shell_interface_set_fullscreen(struct shell_surface *shsurf,
2982 uint32_t method,
2983 uint32_t framerate,
2984 struct weston_output *output)
2985{
2986 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002987 shsurf->next_state.fullscreen = true;
2988 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002989
2990 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002991}
2992
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002993static struct weston_output *
2994get_focused_output(struct weston_compositor *compositor)
2995{
2996 struct weston_seat *seat;
2997 struct weston_output *output = NULL;
2998
2999 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003000 struct weston_touch *touch = weston_seat_get_touch(seat);
3001 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3002 struct weston_keyboard *keyboard =
3003 weston_seat_get_keyboard(seat);
3004
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003005 /* Priority has touch focus, then pointer and
3006 * then keyboard focus. We should probably have
3007 * three for loops and check frist for touch,
3008 * then for pointer, etc. but unless somebody has some
3009 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003010 if (touch && touch->focus)
3011 output = touch->focus->output;
3012 else if (pointer && pointer->focus)
3013 output = pointer->focus->output;
3014 else if (keyboard && keyboard->focus)
3015 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003016
3017 if (output)
3018 break;
3019 }
3020
3021 return output;
3022}
3023
3024static void
3025shell_interface_set_maximized(struct shell_surface *shsurf)
3026{
3027 struct weston_output *output;
3028
3029 surface_clear_next_states(shsurf);
3030 shsurf->next_state.maximized = true;
3031 shsurf->state_changed = true;
3032 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3033
3034 if (!weston_surface_is_mapped(shsurf->surface))
3035 output = get_focused_output(shsurf->surface->compositor);
3036 else
3037 output = shsurf->surface->output;
3038
3039 shell_surface_set_output(shsurf, output);
3040 send_configure_for_surface(shsurf);
3041}
3042
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003043static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003044shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003045{
Derek Foremane4d6c832015-08-05 14:48:11 -05003046 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003047}
3048
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003049static int
3050shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003051 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003052 uint32_t edges)
3053{
Derek Foremane4d6c832015-08-05 14:48:11 -05003054 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003055}
3056
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003057static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003058
3059static void
3060destroy_shell_seat(struct wl_listener *listener, void *data)
3061{
3062 struct shell_seat *shseat =
3063 container_of(listener,
3064 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003065 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003066
3067 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003068 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069 shseat->popup_grab.client = NULL;
3070
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003071 wl_list_for_each_safe(shsurf, next,
3072 &shseat->popup_grab.surfaces_list,
3073 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003075 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003077 }
3078
3079 wl_list_remove(&shseat->seat_destroy_listener.link);
3080 free(shseat);
3081}
3082
Jason Ekstrand024177c2014-04-21 19:42:58 -05003083static void
3084shell_seat_caps_changed(struct wl_listener *listener, void *data)
3085{
Derek Foreman1281a362015-07-31 16:55:32 -05003086 struct weston_keyboard *keyboard;
3087 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003088 struct shell_seat *seat;
3089
3090 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003091 keyboard = weston_seat_get_keyboard(seat->seat);
3092 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003093
Derek Foreman1281a362015-07-31 16:55:32 -05003094 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003095 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003096 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003097 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003098 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003099 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003100 wl_list_init(&seat->keyboard_focus_listener.link);
3101 }
3102
Derek Foreman1281a362015-07-31 16:55:32 -05003103 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003104 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003105 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003106 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003107 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003108 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003109 wl_list_init(&seat->pointer_focus_listener.link);
3110 }
3111}
3112
Giulio Camuffo5085a752013-03-25 21:42:45 +01003113static struct shell_seat *
3114create_shell_seat(struct weston_seat *seat)
3115{
3116 struct shell_seat *shseat;
3117
3118 shseat = calloc(1, sizeof *shseat);
3119 if (!shseat) {
3120 weston_log("no memory to allocate shell seat\n");
3121 return NULL;
3122 }
3123
3124 shseat->seat = seat;
3125 wl_list_init(&shseat->popup_grab.surfaces_list);
3126
3127 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3128 wl_signal_add(&seat->destroy_signal,
3129 &shseat->seat_destroy_listener);
3130
Jason Ekstrand024177c2014-04-21 19:42:58 -05003131 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3132 wl_list_init(&shseat->keyboard_focus_listener.link);
3133
3134 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3135 wl_list_init(&shseat->pointer_focus_listener.link);
3136
3137 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3138 wl_signal_add(&seat->updated_caps_signal,
3139 &shseat->caps_changed_listener);
3140 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3141
Giulio Camuffo5085a752013-03-25 21:42:45 +01003142 return shseat;
3143}
3144
3145static struct shell_seat *
3146get_shell_seat(struct weston_seat *seat)
3147{
3148 struct wl_listener *listener;
3149
3150 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003151 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003152
3153 return container_of(listener,
3154 struct shell_seat, seat_destroy_listener);
3155}
3156
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003157static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003158popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003159{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003160 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003161 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003162 struct shell_seat *shseat =
3163 container_of(grab, struct shell_seat, popup_grab.grab);
3164 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003165 wl_fixed_t sx, sy;
3166
Jason Ekstranda7af7042013-10-12 22:38:11 -05003167 view = weston_compositor_pick_view(pointer->seat->compositor,
3168 pointer->x, pointer->y,
3169 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003170
Jason Ekstranda7af7042013-10-12 22:38:11 -05003171 if (view && view->surface->resource &&
3172 wl_resource_get_client(view->surface->resource) == client) {
3173 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003174 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003175 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003176 }
3177}
3178
3179static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003180popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003181 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003182{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003183 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003184 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003185 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003186 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003187 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003188
Jonas Ådahl61917c82014-08-11 22:44:02 +02003189 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003190 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003191 weston_view_from_global_fixed(pointer->focus, x, y,
3192 &pointer->sx, &pointer->sy);
3193 }
3194
Jonas Ådahld2510102014-10-05 21:39:14 +02003195 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003196
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003197 if (!pointer->focus_client)
3198 return;
3199
3200 resource_list = &pointer->focus_client->pointer_resources;
3201 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003202 weston_view_from_global_fixed(pointer->focus,
3203 pointer->x, pointer->y,
3204 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003205 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003206 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003207}
3208
3209static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003210popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003211 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212{
3213 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003214 struct shell_seat *shseat =
3215 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003216 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003217 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003218 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003219 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003220
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003221 if (grab->pointer->focus_client)
3222 resource_list = &grab->pointer->focus_client->pointer_resources;
3223 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003224 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003225 wl_resource_for_each(resource, resource_list) {
3226 wl_pointer_send_button(resource, serial,
3227 time, button, state);
3228 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003229 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003230 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003231 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003232 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003233 }
3234
Daniel Stone4dbadb12012-05-30 16:31:51 +01003235 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003236 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003237}
3238
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003239static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003240popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003241 uint32_t time,
3242 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003243{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003244 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003245}
3246
3247static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003248popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3249{
3250 weston_pointer_send_axis_source(grab->pointer, source);
3251}
3252
3253static void
3254popup_grab_frame(struct weston_pointer_grab *grab)
3255{
3256 weston_pointer_send_frame(grab->pointer);
3257}
3258
3259static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003260popup_grab_cancel(struct weston_pointer_grab *grab)
3261{
3262 popup_grab_end(grab->pointer);
3263}
3264
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003265static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003266 popup_grab_focus,
3267 popup_grab_motion,
3268 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003269 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003270 popup_grab_axis_source,
3271 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003272 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003273};
3274
3275static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003276touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003277 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003278{
3279 struct wl_resource *resource;
3280 struct shell_seat *shseat =
3281 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3282 struct wl_display *display = shseat->seat->compositor->wl_display;
3283 uint32_t serial;
3284 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003285 wl_fixed_t sx, sy;
3286
3287 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003288
3289 resource_list = &grab->touch->focus_resource_list;
3290 if (!wl_list_empty(resource_list)) {
3291 serial = wl_display_get_serial(display);
3292 wl_resource_for_each(resource, resource_list) {
3293 wl_touch_send_down(resource, serial, time,
3294 grab->touch->focus->surface->resource,
3295 touch_id, sx, sy);
3296 }
3297 }
3298}
3299
3300static void
3301touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3302{
3303 struct wl_resource *resource;
3304 struct shell_seat *shseat =
3305 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3306 struct wl_display *display = shseat->seat->compositor->wl_display;
3307 uint32_t serial;
3308 struct wl_list *resource_list;
3309
3310 resource_list = &grab->touch->focus_resource_list;
3311 if (!wl_list_empty(resource_list)) {
3312 serial = wl_display_get_serial(display);
3313 wl_resource_for_each(resource, resource_list) {
3314 wl_touch_send_up(resource, serial, time, touch_id);
3315 }
3316 }
3317}
3318
3319static void
3320touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003321 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003322{
3323 struct wl_resource *resource;
3324 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003325 wl_fixed_t sx, sy;
3326
3327 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003328
3329 resource_list = &grab->touch->focus_resource_list;
3330 if (!wl_list_empty(resource_list)) {
3331 wl_resource_for_each(resource, resource_list) {
3332 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3333 }
3334 }
3335}
3336
3337static void
3338touch_popup_grab_frame(struct weston_touch_grab *grab)
3339{
3340}
3341
3342static void
3343touch_popup_grab_cancel(struct weston_touch_grab *grab)
3344{
3345 touch_popup_grab_end(grab->touch);
3346}
3347
3348static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3349 touch_popup_grab_down,
3350 touch_popup_grab_up,
3351 touch_popup_grab_motion,
3352 touch_popup_grab_frame,
3353 touch_popup_grab_cancel,
3354};
3355
3356static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003357shell_surface_send_popup_done(struct shell_surface *shsurf)
3358{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003359 if (shsurf->resource == NULL)
3360 return;
3361 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362 wl_shell_surface_send_popup_done(shsurf->resource);
3363 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003364 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003365}
3366
3367static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003368popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003369{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003370 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003371 struct shell_seat *shseat =
3372 container_of(grab, struct shell_seat, popup_grab.grab);
3373 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003374 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003375
3376 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003377 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003378 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003379 shseat->popup_grab.grab.interface = NULL;
3380 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003381 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003382 wl_list_for_each_safe(shsurf, next,
3383 &shseat->popup_grab.surfaces_list,
3384 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003385 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003386 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003387 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003388 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003389 wl_list_init(&shseat->popup_grab.surfaces_list);
3390 }
3391}
3392
3393static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003394touch_popup_grab_end(struct weston_touch *touch)
3395{
3396 struct weston_touch_grab *grab = touch->grab;
3397 struct shell_seat *shseat =
3398 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3399 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003400 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003401
3402 if (touch->grab->interface == &touch_popup_grab_interface) {
3403 weston_touch_end_grab(grab->touch);
3404 shseat->popup_grab.client = NULL;
3405 shseat->popup_grab.touch_grab.interface = NULL;
3406 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3407 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003408 wl_list_for_each_safe(shsurf, next,
3409 &shseat->popup_grab.surfaces_list,
3410 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003411 shell_surface_send_popup_done(shsurf);
3412 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003413 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003414 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003415 wl_list_init(&shseat->popup_grab.surfaces_list);
3416 }
3417}
3418
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003419static struct shell_surface *
3420get_top_popup(struct shell_seat *shseat)
3421{
3422 struct shell_surface *shsurf;
3423
3424 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3425 return NULL;
3426 } else {
3427 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3428 struct shell_surface,
3429 popup.grab_link);
3430 return shsurf;
3431 }
3432}
3433
3434static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003435add_popup_grab(struct shell_surface *shsurf,
3436 struct shell_seat *shseat,
3437 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003438{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003439 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003440 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003441 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3442 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003443
3444 parent = get_shell_surface(shsurf->parent);
3445 top_surface = get_top_popup(shseat);
3446 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003447 (!parent ||
3448 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003449 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003450 wl_resource_post_error(shsurf->owner_resource,
3451 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003452 "xdg_popup was not created on the "
3453 "topmost popup");
3454 return -1;
3455 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003456
3457 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003458 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003459 shseat->popup_grab.client =
3460 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003461
3462 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003463 shseat->popup_grab.grab.interface =
3464 &popup_grab_interface;
3465
3466 /* We must make sure here that this popup was opened
3467 * after a mouse press, and not just by moving around
3468 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003469 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003470 shseat->popup_grab.initial_up = 0;
3471 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003472 shseat->popup_grab.touch_grab.interface =
3473 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003474 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003475
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003476 wl_list_insert(&shseat->popup_grab.surfaces_list,
3477 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003478
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003479 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003480 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003481 &shseat->popup_grab.grab);
3482 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003483 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003484 &shseat->popup_grab.touch_grab);
3485 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003486 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003487 wl_list_insert(&shseat->popup_grab.surfaces_list,
3488 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003489 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003490
3491 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003492}
3493
3494static void
3495remove_popup_grab(struct shell_surface *shsurf)
3496{
3497 struct shell_seat *shseat = shsurf->popup.shseat;
3498
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003499 if (shell_surface_is_xdg_popup(shsurf) &&
3500 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003501 wl_resource_post_error(shsurf->owner_resource,
3502 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003503 "xdg_popup was destroyed while it was "
3504 "not the topmost popup.");
3505 return;
3506 }
3507
Giulio Camuffo5085a752013-03-25 21:42:45 +01003508 wl_list_remove(&shsurf->popup.grab_link);
3509 wl_list_init(&shsurf->popup.grab_link);
3510 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003511 if (shseat->popup_grab.type == POINTER) {
3512 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3513 shseat->popup_grab.grab.interface = NULL;
3514 } else if (shseat->popup_grab.type == TOUCH) {
3515 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3516 shseat->popup_grab.touch_grab.interface = NULL;
3517 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003518 }
3519}
3520
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003521static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003522shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003523{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003524 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003526 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3527 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003528
Jason Ekstranda7af7042013-10-12 22:38:11 -05003529 shsurf->surface->output = parent_view->output;
Armin Krezović4663aca2016-06-30 06:04:29 +02003530 shsurf->surface->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 weston_view_set_transform_parent(shsurf->view, parent_view);
3534 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3535 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003536 shsurf->view->is_mapped = true;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003537
Derek Foreman1281a362015-07-31 16:55:32 -05003538 if (pointer &&
3539 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003540 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3541 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003542 } else if (touch &&
3543 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003544 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3545 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003546 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003547 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003548 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003549 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003550
3551 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003552}
3553
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003554static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003555 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003556 shell_surface_move,
3557 shell_surface_resize,
3558 shell_surface_set_toplevel,
3559 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003560 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003561 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003562 shell_surface_set_maximized,
3563 shell_surface_set_title,
3564 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003565};
3566
3567static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003568destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003569{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003570 struct shell_surface *child, *next;
3571
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003572 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3573
Giulio Camuffo5085a752013-03-25 21:42:45 +01003574 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3575 remove_popup_grab(shsurf);
3576 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003577
Alex Wubd3354b2012-04-17 17:20:49 +08003578 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003579 shell_surface_is_top_fullscreen(shsurf))
3580 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003581
Jason Ekstranda7af7042013-10-12 22:38:11 -05003582 if (shsurf->fullscreen.black_view)
3583 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003584
Alex Wubd3354b2012-04-17 17:20:49 +08003585 /* As destroy_resource() use wl_list_for_each_safe(),
3586 * we can always remove the listener.
3587 */
3588 wl_list_remove(&shsurf->surface_destroy_listener.link);
3589 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003590 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003591 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003592
Jason Ekstranda7af7042013-10-12 22:38:11 -05003593 weston_view_destroy(shsurf->view);
3594
Philip Withnall648a4dd2013-11-25 18:01:44 +00003595 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003596 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3597 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003598
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003599 wl_list_remove(&shsurf->link);
3600 free(shsurf);
3601}
3602
3603static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003604shell_destroy_shell_surface(struct wl_resource *resource)
3605{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003606 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003607
Bryan Caina46b9462014-04-04 17:41:24 -05003608 if (!wl_list_empty(&shsurf->popup.grab_link))
3609 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003610 if (shsurf->resource)
3611 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003612 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003613}
3614
3615static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003616shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003617{
3618 struct shell_surface *shsurf = container_of(listener,
3619 struct shell_surface,
3620 surface_destroy_listener);
3621
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003622 if (shsurf->resource)
3623 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003624
3625 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003626}
3627
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003628static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003629fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003630{
3631 struct shell_surface *shsurf = data;
3632
3633 weston_surface_destroy(shsurf->surface);
3634}
3635
3636static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003637fade_out_done(struct weston_view_animation *animation, void *data)
3638{
3639 struct shell_surface *shsurf = data;
3640 struct wl_event_loop *loop;
3641
3642 loop = wl_display_get_event_loop(
3643 shsurf->surface->compositor->wl_display);
3644
3645 if (!shsurf->destroying) {
3646 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3647 shsurf->destroying = true;
3648 }
3649}
3650
3651static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003652handle_resource_destroy(struct wl_listener *listener, void *data)
3653{
3654 struct shell_surface *shsurf =
3655 container_of(listener, struct shell_surface,
3656 resource_destroy_listener);
3657
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003658 if (!weston_surface_is_mapped(shsurf->surface))
3659 return;
3660
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003661 shsurf->surface->ref_count++;
3662
3663 pixman_region32_fini(&shsurf->surface->pending.input);
3664 pixman_region32_init(&shsurf->surface->pending.input);
3665 pixman_region32_fini(&shsurf->surface->input);
3666 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003667 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3668 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3669 fade_out_done, shsurf);
3670 } else {
3671 weston_surface_destroy(shsurf->surface);
3672 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003673}
3674
3675static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003676shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003677
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003678struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003679get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003680{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003681 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003682 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003683 else
3684 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003685}
3686
Rafael Antognollie2a34552013-12-03 15:35:45 -02003687static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003688create_common_surface(struct shell_client *owner, void *shell,
3689 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003690 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003691{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003692 struct shell_surface *shsurf;
3693
Pekka Paalanen50b67472014-10-01 15:02:41 +03003694 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003695
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003696 shsurf = calloc(1, sizeof *shsurf);
3697 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003698 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003699 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003700 }
3701
Jason Ekstranda7af7042013-10-12 22:38:11 -05003702 shsurf->view = weston_view_create(surface);
3703 if (!shsurf->view) {
3704 weston_log("no memory to allocate shell surface\n");
3705 free(shsurf);
3706 return NULL;
3707 }
3708
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003709 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003710 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003711 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003712
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003713 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3714 wl_resource_add_destroy_listener(surface->resource,
3715 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003716 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003717
Tiago Vignattibc052c92012-04-19 16:18:18 +03003718 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003719 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003720 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003721 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003722 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003723 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3724 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003725 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003726 wl_list_init(&shsurf->fullscreen.transform.link);
3727
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003728 shsurf->output = get_default_output(shsurf->shell->compositor);
3729
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003730 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003731 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003732 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003733 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003734
3735 /* init link so its safe to always remove it in destroy_shell_surface */
3736 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003737 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003738
Pekka Paalanen460099f2012-01-20 16:48:25 +02003739 /* empty when not in use */
3740 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003741 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003742
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003743 wl_list_init(&shsurf->workspace_transform.link);
3744
Philip Withnall648a4dd2013-11-25 18:01:44 +00003745 wl_list_init(&shsurf->children_link);
3746 wl_list_init(&shsurf->children_list);
3747 shsurf->parent = NULL;
3748
Pekka Paalanenb5026542014-11-12 15:09:24 +02003749 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003750
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003751 shsurf->client = client;
3752
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003753 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003754}
3755
Rafael Antognollie2a34552013-12-03 15:35:45 -02003756static struct shell_surface *
3757create_shell_surface(void *shell, struct weston_surface *surface,
3758 const struct weston_shell_client *client)
3759{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003760 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003761}
3762
Tiago Vignattibc052c92012-04-19 16:18:18 +03003763static void
3764shell_get_shell_surface(struct wl_client *client,
3765 struct wl_resource *resource,
3766 uint32_t id,
3767 struct wl_resource *surface_resource)
3768{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003769 struct weston_surface *surface =
3770 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003771 struct shell_client *sc = wl_resource_get_user_data(resource);
3772 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003773 struct shell_surface *shsurf;
3774
Pekka Paalanen50b67472014-10-01 15:02:41 +03003775 if (weston_surface_set_role(surface, "wl_shell_surface",
3776 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003777 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003778
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003779 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003780 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003781 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003782 return;
3783 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003784
Jason Ekstranda85118c2013-06-27 20:17:02 -05003785 shsurf->resource =
3786 wl_resource_create(client,
3787 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003788 if (!shsurf->resource) {
3789 wl_resource_post_no_memory(surface_resource);
3790 return;
3791 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003792 wl_resource_set_implementation(shsurf->resource,
3793 &shell_surface_implementation,
3794 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003795 wl_list_insert(&sc->surface_list,
3796 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003797}
3798
Rafael Antognollie2a34552013-12-03 15:35:45 -02003799static bool
3800shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3801{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003802 /* A shell surface without a resource is created from xwayland
3803 * and is considered a wl_shell surface for now. */
3804
3805 return shsurf->resource == NULL ||
3806 wl_resource_instance_of(shsurf->resource,
3807 &wl_shell_surface_interface,
3808 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003809}
3810
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003811static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003812 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003813};
3814
Rafael Antognollie2a34552013-12-03 15:35:45 -02003815/****************************
3816 * xdg-shell implementation */
3817
3818static void
3819xdg_surface_destroy(struct wl_client *client,
3820 struct wl_resource *resource)
3821{
3822 wl_resource_destroy(resource);
3823}
3824
3825static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003826xdg_surface_set_parent(struct wl_client *client,
3827 struct wl_resource *resource,
3828 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003829{
3830 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003831 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003832
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003833 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003834 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003835 shell_surface_set_parent(shsurf, parent->surface);
3836 } else {
3837 shell_surface_set_parent(shsurf, NULL);
3838 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003839}
3840
3841static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003842xdg_surface_set_app_id(struct wl_client *client,
3843 struct wl_resource *resource,
3844 const char *app_id)
3845{
3846 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3847
3848 free(shsurf->class);
3849 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003850 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003851}
3852
3853static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003854xdg_surface_show_window_menu(struct wl_client *client,
3855 struct wl_resource *surface_resource,
3856 struct wl_resource *seat_resource,
3857 uint32_t serial,
3858 int32_t x,
3859 int32_t y)
3860{
3861 /* TODO */
3862}
3863
3864static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003865xdg_surface_set_title(struct wl_client *client,
3866 struct wl_resource *resource, const char *title)
3867{
3868 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3869
3870 set_title(shsurf, title);
3871}
3872
3873static void
3874xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3875 struct wl_resource *seat_resource, uint32_t serial)
3876{
3877 common_surface_move(resource, seat_resource, serial);
3878}
3879
3880static void
3881xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3882 struct wl_resource *seat_resource, uint32_t serial,
3883 uint32_t edges)
3884{
3885 common_surface_resize(resource, seat_resource, serial, edges);
3886}
3887
3888static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003889xdg_surface_ack_configure(struct wl_client *client,
3890 struct wl_resource *resource,
3891 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003892{
3893 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3894
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003895 if (shsurf->state_requested) {
3896 shsurf->next_state = shsurf->requested_state;
3897 shsurf->state_changed = true;
3898 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003899 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003900}
3901
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003902static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003903xdg_surface_set_window_geometry(struct wl_client *client,
3904 struct wl_resource *resource,
3905 int32_t x,
3906 int32_t y,
3907 int32_t width,
3908 int32_t height)
3909{
3910 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3911
3912 set_window_geometry(shsurf, x, y, width, height);
3913}
3914
3915static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003916xdg_surface_set_maximized(struct wl_client *client,
3917 struct wl_resource *resource)
3918{
3919 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003920 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003921
3922 shsurf->state_requested = true;
3923 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003924
3925 if (!weston_surface_is_mapped(shsurf->surface))
3926 output = get_focused_output(shsurf->surface->compositor);
3927 else
3928 output = shsurf->surface->output;
3929
3930 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003931 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003932}
3933
3934static void
3935xdg_surface_unset_maximized(struct wl_client *client,
3936 struct wl_resource *resource)
3937{
3938 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3939
3940 shsurf->state_requested = true;
3941 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003942 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003943}
3944
3945static void
3946xdg_surface_set_fullscreen(struct wl_client *client,
3947 struct wl_resource *resource,
3948 struct wl_resource *output_resource)
3949{
3950 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3951 struct weston_output *output;
3952
3953 shsurf->state_requested = true;
3954 shsurf->requested_state.fullscreen = true;
3955
3956 if (output_resource)
3957 output = wl_resource_get_user_data(output_resource);
3958 else
3959 output = NULL;
3960
Marek Chalupa052917a2014-09-02 11:35:12 +02003961 /* handle clients launching in fullscreen */
3962 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3963 /* Set the output to the one that has focus currently. */
3964 assert(shsurf->surface);
3965 output = get_focused_output(shsurf->surface->compositor);
3966 }
3967
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003968 shell_surface_set_output(shsurf, output);
3969 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003970
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003971 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003972}
3973
3974static void
3975xdg_surface_unset_fullscreen(struct wl_client *client,
3976 struct wl_resource *resource)
3977{
3978 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3979
3980 shsurf->state_requested = true;
3981 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003982 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003983}
3984
3985static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003986xdg_surface_set_minimized(struct wl_client *client,
3987 struct wl_resource *resource)
3988{
3989 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3990
3991 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3992 return;
3993
3994 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003995 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003996}
3997
Rafael Antognollie2a34552013-12-03 15:35:45 -02003998static const struct xdg_surface_interface xdg_surface_implementation = {
3999 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004000 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004001 xdg_surface_set_title,
4002 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004003 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004004 xdg_surface_move,
4005 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004006 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004007 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004008 xdg_surface_set_maximized,
4009 xdg_surface_unset_maximized,
4010 xdg_surface_set_fullscreen,
4011 xdg_surface_unset_fullscreen,
4012 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004013};
4014
4015static void
4016xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004017 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004018{
4019 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004020 uint32_t *s;
4021 struct wl_array states;
4022 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004023
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004024 assert(shsurf);
4025
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004026 if (!shsurf->resource)
4027 return;
4028
4029 wl_array_init(&states);
4030 if (shsurf->requested_state.fullscreen) {
4031 s = wl_array_add(&states, sizeof *s);
4032 *s = XDG_SURFACE_STATE_FULLSCREEN;
4033 } else if (shsurf->requested_state.maximized) {
4034 s = wl_array_add(&states, sizeof *s);
4035 *s = XDG_SURFACE_STATE_MAXIMIZED;
4036 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004037 if (shsurf->resize_edges != 0) {
4038 s = wl_array_add(&states, sizeof *s);
4039 *s = XDG_SURFACE_STATE_RESIZING;
4040 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004041 if (shsurf->focus_count > 0) {
4042 s = wl_array_add(&states, sizeof *s);
4043 *s = XDG_SURFACE_STATE_ACTIVATED;
4044 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004045
4046 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4047 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4048
4049 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004050}
4051
4052static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004053 xdg_send_configure,
4054 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004055};
4056
4057static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004058xdg_shell_destroy(struct wl_client *client,
4059 struct wl_resource *resource)
4060{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004061 struct shell_client *sc = wl_resource_get_user_data(resource);
4062 struct wl_resource *shsurf_resource;
4063 struct shell_surface *shsurf;
4064
4065 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4066 shsurf = wl_resource_get_user_data(shsurf_resource);
4067 if (shsurf->owner_resource == resource) {
4068 wl_resource_post_error(
4069 resource,
4070 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4071 "not all child surface objects destroyed");
4072 return;
4073 }
4074 }
4075
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004076 wl_resource_destroy(resource);
4077}
4078
4079static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004080xdg_use_unstable_version(struct wl_client *client,
4081 struct wl_resource *resource,
4082 int32_t version)
4083{
4084 if (version > 1) {
4085 wl_resource_post_error(resource,
4086 1,
4087 "xdg-shell:: version not implemented yet.");
4088 return;
4089 }
4090}
4091
4092static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004093create_xdg_surface(struct shell_client *owner, void *shell,
4094 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004095 const struct weston_shell_client *client)
4096{
4097 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004098
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004099 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004100 if (!shsurf)
4101 return NULL;
4102
Pekka Paalanenb5026542014-11-12 15:09:24 +02004103 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004104
4105 return shsurf;
4106}
4107
4108static void
4109xdg_get_xdg_surface(struct wl_client *client,
4110 struct wl_resource *resource,
4111 uint32_t id,
4112 struct wl_resource *surface_resource)
4113{
4114 struct weston_surface *surface =
4115 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004116 struct shell_client *sc = wl_resource_get_user_data(resource);
4117 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004118 struct shell_surface *shsurf;
4119
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004120 shsurf = get_shell_surface(surface);
4121 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4122 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4123 "This wl_surface is already an "
4124 "xdg_surface");
4125 return;
4126 }
4127
Pekka Paalanen50b67472014-10-01 15:02:41 +03004128 if (weston_surface_set_role(surface, "xdg_surface",
4129 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004130 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004131
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004132 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004133 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004134 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004135 return;
4136 }
4137
4138 shsurf->resource =
4139 wl_resource_create(client,
4140 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004141 if (!shsurf->resource) {
4142 wl_resource_post_no_memory(surface_resource);
4143 return;
4144 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004145 wl_resource_set_implementation(shsurf->resource,
4146 &xdg_surface_implementation,
4147 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004148 shsurf->owner_resource = resource;
4149 wl_list_insert(&sc->surface_list,
4150 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004151}
4152
4153static bool
4154shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4155{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004156 return shsurf->resource &&
4157 wl_resource_instance_of(shsurf->resource,
4158 &xdg_surface_interface,
4159 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004160}
4161
4162/* xdg-popup implementation */
4163
4164static void
4165xdg_popup_destroy(struct wl_client *client,
4166 struct wl_resource *resource)
4167{
4168 wl_resource_destroy(resource);
4169}
4170
Rafael Antognollie2a34552013-12-03 15:35:45 -02004171static const struct xdg_popup_interface xdg_popup_implementation = {
4172 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004173};
4174
4175static void
4176xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004177 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004178{
4179}
4180
4181static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004182 xdg_popup_send_configure,
4183 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004184};
4185
4186static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004187create_xdg_popup(struct shell_client *owner, void *shell,
4188 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189 const struct weston_shell_client *client,
4190 struct weston_surface *parent,
4191 struct shell_seat *seat,
4192 uint32_t serial,
4193 int32_t x, int32_t y)
4194{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004195 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004196
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004197 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004198 if (!shsurf)
4199 return NULL;
4200
Pekka Paalanenb5026542014-11-12 15:09:24 +02004201 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004202 shsurf->popup.shseat = seat;
4203 shsurf->popup.serial = serial;
4204 shsurf->popup.x = x;
4205 shsurf->popup.y = y;
4206 shell_surface_set_parent(shsurf, parent);
4207
4208 return shsurf;
4209}
4210
4211static void
4212xdg_get_xdg_popup(struct wl_client *client,
4213 struct wl_resource *resource,
4214 uint32_t id,
4215 struct wl_resource *surface_resource,
4216 struct wl_resource *parent_resource,
4217 struct wl_resource *seat_resource,
4218 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004219 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004220{
4221 struct weston_surface *surface =
4222 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004223 struct shell_client *sc = wl_resource_get_user_data(resource);
4224 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004225 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004226 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004227 struct weston_surface *parent;
4228 struct shell_seat *seat;
4229
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004230 shsurf = get_shell_surface(surface);
4231 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4232 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4233 "This wl_surface is already an "
4234 "xdg_popup");
4235 return;
4236 }
4237
Pekka Paalanen50b67472014-10-01 15:02:41 +03004238 if (weston_surface_set_role(surface, "xdg_popup",
4239 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004240 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004241
4242 if (!parent_resource) {
4243 wl_resource_post_error(surface_resource,
4244 WL_DISPLAY_ERROR_INVALID_OBJECT,
4245 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004246 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004247 }
4248
4249 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004250 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004251
Jonas Ådahlee45a552015-03-18 16:37:47 +08004252 /* Verify that we are creating the top most popup when mapping,
4253 * as it's not until then we know whether it was mapped as most
4254 * top level or not. */
4255
4256 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004257 if (!parent_shsurf ||
4258 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4259 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004260 wl_resource_post_error(resource,
4261 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4262 "xdg_popup parent was invalid");
4263 return;
4264 }
4265
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004266 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004267 parent, seat, serial, x, y);
4268 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004269 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004270 return;
4271 }
4272
4273 shsurf->resource =
4274 wl_resource_create(client,
4275 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004276 if (!shsurf->resource) {
4277 wl_resource_post_no_memory(surface_resource);
4278 return;
4279 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004280 wl_resource_set_implementation(shsurf->resource,
4281 &xdg_popup_implementation,
4282 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004283 shsurf->owner_resource = resource;
4284 wl_list_insert(&sc->surface_list,
4285 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004286}
4287
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004288static void
4289xdg_pong(struct wl_client *client,
4290 struct wl_resource *resource, uint32_t serial)
4291{
4292 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004293
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004294 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004295}
4296
Rafael Antognollie2a34552013-12-03 15:35:45 -02004297static bool
4298shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4299{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004300 return (shsurf->resource &&
4301 wl_resource_instance_of(shsurf->resource,
4302 &xdg_popup_interface,
4303 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004304}
4305
4306static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004307 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004308 xdg_use_unstable_version,
4309 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004310 xdg_get_xdg_popup,
4311 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004312};
4313
4314static int
4315xdg_shell_unversioned_dispatch(const void *implementation,
4316 void *_target, uint32_t opcode,
4317 const struct wl_message *message,
4318 union wl_argument *args)
4319{
4320 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004321 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004322
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004323 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004324 wl_resource_post_error(resource,
4325 WL_DISPLAY_ERROR_INVALID_OBJECT,
4326 "must call use_unstable_version first");
4327 return 0;
4328 }
4329
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004330#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004331
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004332 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4333 "shell implementation doesn't match protocol version");
4334
Rafael Antognollie2a34552013-12-03 15:35:45 -02004335 if (args[0].i != XDG_SERVER_VERSION) {
4336 wl_resource_post_error(resource,
4337 WL_DISPLAY_ERROR_INVALID_OBJECT,
4338 "incompatible version, server is %d "
4339 "client wants %d",
4340 XDG_SERVER_VERSION, args[0].i);
4341 return 0;
4342 }
4343
4344 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004345 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004346
4347 return 1;
4348}
4349
4350/* end of xdg-shell implementation */
4351/***********************************/
4352
Kristian Høgsberg07937562011-04-12 17:25:42 -04004353static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004354shell_fade(struct desktop_shell *shell, enum fade_type type);
4355
Pekka Paalanen77346a62011-11-30 16:26:35 +02004356static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004357configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004358{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004359 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004360
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004361 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004362 if (v->output == ev->output && v != ev) {
4363 weston_view_unmap(v);
4364 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004365 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004366 }
4367 }
4368
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004369 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02004370 ev->surface->is_mapped = true;
4371 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004372
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004373 if (wl_list_empty(&ev->layer_link.link)) {
4374 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004376 }
4377}
4378
David Fort50d962f2016-06-09 17:55:52 +02004379
4380static struct shell_output *
4381find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4382{
4383 struct shell_output *shell_output;
4384
4385 wl_list_for_each(shell_output, &shell->output_list, link) {
4386 if (shell_output->output == output)
4387 return shell_output;
4388 }
4389
4390 return NULL;
4391}
4392
Pekka Paalanen8274d902014-08-06 19:36:51 +03004393static int
4394background_get_label(struct weston_surface *surface, char *buf, size_t len)
4395{
4396 return snprintf(buf, len, "background for output %s",
4397 surface->output->name);
4398}
4399
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004400static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004401background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004402{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004403 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004405
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 view = container_of(es->views.next, struct weston_view, surface_link);
4407
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004408 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004409}
4410
4411static void
David Fort50d962f2016-06-09 17:55:52 +02004412handle_background_surface_destroy(struct wl_listener *listener, void *data)
4413{
4414 struct shell_output *output =
4415 container_of(listener, struct shell_output, background_surface_listener);
4416
4417 weston_log("background surface gone\n");
4418 output->background_surface = NULL;
4419}
4420
4421static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004422desktop_shell_set_background(struct wl_client *client,
4423 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004424 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004425 struct wl_resource *surface_resource)
4426{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004427 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004428 struct weston_surface *surface =
4429 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02004430 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004431 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004432
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004433 if (surface->configure) {
4434 wl_resource_post_error(surface_resource,
4435 WL_DISPLAY_ERROR_INVALID_OBJECT,
4436 "surface role already assigned");
4437 return;
4438 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004439
Jason Ekstranda7af7042013-10-12 22:38:11 -05004440 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4441 weston_view_destroy(view);
4442 view = weston_view_create(surface);
4443
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004444 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004445 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004446 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004447 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004448 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004449 weston_desktop_shell_send_configure(resource, 0,
4450 surface_resource,
4451 surface->output->width,
4452 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004453
4454 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4455 sh_output->background_surface = surface;
4456
4457 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4458 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004459}
4460
Pekka Paalanen8274d902014-08-06 19:36:51 +03004461static int
4462panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4463{
4464 return snprintf(buf, len, "panel for output %s",
4465 surface->output->name);
4466}
4467
Kristian Høgsberg75840622011-09-06 13:48:16 -04004468static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004469panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004470{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004471 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004472 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004473
Jason Ekstranda7af7042013-10-12 22:38:11 -05004474 view = container_of(es->views.next, struct weston_view, surface_link);
4475
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004476 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004477}
4478
4479static void
David Fort50d962f2016-06-09 17:55:52 +02004480handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4481{
4482 struct shell_output *output =
4483 container_of(listener, struct shell_output, panel_surface_listener);
4484
4485 weston_log("panel surface gone\n");
4486 output->panel_surface = NULL;
4487}
4488
4489
4490static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004491desktop_shell_set_panel(struct wl_client *client,
4492 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004493 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004494 struct wl_resource *surface_resource)
4495{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004496 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004497 struct weston_surface *surface =
4498 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004499 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02004500 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004501
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004502 if (surface->configure) {
4503 wl_resource_post_error(surface_resource,
4504 WL_DISPLAY_ERROR_INVALID_OBJECT,
4505 "surface role already assigned");
4506 return;
4507 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004508
Jason Ekstranda7af7042013-10-12 22:38:11 -05004509 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4510 weston_view_destroy(view);
4511 view = weston_view_create(surface);
4512
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004513 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004514 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004515 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004516 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004517 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004518 weston_desktop_shell_send_configure(resource, 0,
4519 surface_resource,
4520 surface->output->width,
4521 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004522
4523 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4524 sh_output->panel_surface = surface;
4525
4526 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4527 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004528}
4529
Pekka Paalanen8274d902014-08-06 19:36:51 +03004530static int
4531lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4532{
4533 return snprintf(buf, len, "lock window");
4534}
4535
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004536static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004537lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004538{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004539 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004540 struct weston_view *view;
4541
4542 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004543
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004544 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004545 return;
4546
Jason Ekstranda7af7042013-10-12 22:38:11 -05004547 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004548
4549 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004550 weston_layer_entry_insert(&shell->lock_layer.view_list,
4551 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004552 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02004553 surface->is_mapped = true;
4554 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004555 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004556 }
4557}
4558
4559static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004560handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004561{
Tiago Vignattibe143262012-04-16 17:31:41 +03004562 struct desktop_shell *shell =
4563 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004564
Martin Minarik6d118362012-06-07 18:01:59 +02004565 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004566 shell->lock_surface = NULL;
4567}
4568
4569static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004570desktop_shell_set_lock_surface(struct wl_client *client,
4571 struct wl_resource *resource,
4572 struct wl_resource *surface_resource)
4573{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004574 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004575 struct weston_surface *surface =
4576 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004577
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004578 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004579
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004580 if (!shell->locked)
4581 return;
4582
Pekka Paalanen98262232011-12-01 10:42:22 +02004583 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004584
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004585 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004586 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004587 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004588
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004589 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004590 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004591 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004592 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004593}
4594
4595static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004596resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004597{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004598 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004599
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004600 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004601 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004602 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004603 &shell->input_panel_layer.link);
4604 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004605 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004606 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004607 wl_list_insert(&shell->compositor->cursor_layer.link,
4608 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004609 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004610 wl_list_insert(&shell->fullscreen_layer.link,
4611 &shell->panel_layer.link);
4612 wl_list_insert(&shell->panel_layer.link,
4613 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004614
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004615 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004616
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004617 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004618 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004619 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004620}
4621
4622static void
4623desktop_shell_unlock(struct wl_client *client,
4624 struct wl_resource *resource)
4625{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004626 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004627
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004628 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004629
4630 if (shell->locked)
4631 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004632}
4633
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004634static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004635desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004636 struct wl_resource *resource,
4637 struct wl_resource *surface_resource)
4638{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004639 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004640
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004641 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004642 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004643}
4644
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004645static void
4646desktop_shell_desktop_ready(struct wl_client *client,
4647 struct wl_resource *resource)
4648{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004649 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004650
4651 shell_fade_startup(shell);
4652}
4653
Jonny Lamb765760d2014-08-20 15:53:19 +02004654static void
4655desktop_shell_set_panel_position(struct wl_client *client,
4656 struct wl_resource *resource,
4657 uint32_t position)
4658{
4659 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4660
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004661 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4662 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4663 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4664 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004665 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004666 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004667 "bad position argument");
4668 return;
4669 }
4670
4671 shell->panel_position = position;
4672}
4673
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004674static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004675 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004676 desktop_shell_set_panel,
4677 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004678 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004679 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004680 desktop_shell_desktop_ready,
4681 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004682};
4683
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004684static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004685get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004686{
4687 struct shell_surface *shsurf;
4688
4689 shsurf = get_shell_surface(surface);
4690 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004691 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004692 return shsurf->type;
4693}
4694
Kristian Høgsberg75840622011-09-06 13:48:16 -04004695static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004696move_binding(struct weston_pointer *pointer, uint32_t time,
4697 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004698{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004699 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004700 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004701 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004702
Derek Foreman8ae2db52015-07-15 13:00:36 -05004703 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004704 return;
4705
Derek Foreman8ae2db52015-07-15 13:00:36 -05004706 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004707
Pekka Paalanen01388e22013-04-25 13:57:44 +03004708 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004709 if (surface == NULL)
4710 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004711
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004712 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004713 if (shsurf == NULL || shsurf->state.fullscreen ||
4714 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004715 return;
4716
Derek Foreman794fa0e2015-07-15 13:00:38 -05004717 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004718}
4719
4720static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004721maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4722 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004723{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004724 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004725 struct weston_surface *surface;
4726 struct shell_surface *shsurf;
4727
4728 surface = weston_surface_get_main_surface(focus);
4729 if (surface == NULL)
4730 return;
4731
4732 shsurf = get_shell_surface(surface);
4733 if (shsurf == NULL)
4734 return;
4735
4736 if (!shell_surface_is_xdg_surface(shsurf))
4737 return;
4738
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004739 shsurf->state_requested = true;
4740 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004741 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004742}
4743
4744static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004745fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4746 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004747{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004748 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004749 struct weston_surface *surface;
4750 struct shell_surface *shsurf;
4751
4752 surface = weston_surface_get_main_surface(focus);
4753 if (surface == NULL)
4754 return;
4755
4756 shsurf = get_shell_surface(surface);
4757 if (shsurf == NULL)
4758 return;
4759
4760 if (!shell_surface_is_xdg_surface(shsurf))
4761 return;
4762
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004763 shsurf->state_requested = true;
4764 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004765 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004766 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004767}
4768
4769static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004770touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004771{
Derek Foreman362656b2014-09-04 10:23:05 -05004772 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004773 struct weston_surface *surface;
4774 struct shell_surface *shsurf;
4775
Derek Foreman8ae2db52015-07-15 13:00:36 -05004776 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004777 return;
4778
Derek Foreman8ae2db52015-07-15 13:00:36 -05004779 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004780 surface = weston_surface_get_main_surface(focus);
4781 if (surface == NULL)
4782 return;
4783
4784 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004785 if (shsurf == NULL || shsurf->state.fullscreen ||
4786 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004787 return;
4788
Derek Foremanb7674ae2015-07-15 13:00:37 -05004789 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004790}
4791
4792static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004793resize_binding(struct weston_pointer *pointer, uint32_t time,
4794 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004795{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004796 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004797 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004798 uint32_t edges = 0;
4799 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004800 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004801
Derek Foreman8ae2db52015-07-15 13:00:36 -05004802 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004803 return;
4804
Derek Foreman8ae2db52015-07-15 13:00:36 -05004805 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004806
Pekka Paalanen01388e22013-04-25 13:57:44 +03004807 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004808 if (surface == NULL)
4809 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004810
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004811 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004812 if (shsurf == NULL || shsurf->state.fullscreen ||
4813 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004814 return;
4815
Jason Ekstranda7af7042013-10-12 22:38:11 -05004816 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004817 wl_fixed_to_int(pointer->grab_x),
4818 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004819 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004820
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004821 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004822 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004823 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004824 edges |= 0;
4825 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004826 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004827
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004828 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004829 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004830 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004831 edges |= 0;
4832 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004833 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004834
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004835 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004836}
4837
4838static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004839surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004840 struct weston_pointer_axis_event *event,
4841 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004842{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004843 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004844 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004845 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004846 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004847
Pekka Paalanen01388e22013-04-25 13:57:44 +03004848 /* XXX: broken for windows containing sub-surfaces */
4849 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004850 if (surface == NULL)
4851 return;
4852
4853 shsurf = get_shell_surface(surface);
4854 if (!shsurf)
4855 return;
4856
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004857 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004858
Jason Ekstranda7af7042013-10-12 22:38:11 -05004859 if (shsurf->view->alpha > 1.0)
4860 shsurf->view->alpha = 1.0;
4861 if (shsurf->view->alpha < step)
4862 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004863
Jason Ekstranda7af7042013-10-12 22:38:11 -05004864 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004865 weston_surface_damage(surface);
4866}
4867
4868static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004869do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004870 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004871{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004872 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004873 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004874 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004875 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004876
Derek Foreman1281a362015-07-31 16:55:32 -05004877 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004878 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4879 return;
4880 }
4881
Scott Moreauccbf29d2012-02-22 14:21:41 -07004882 wl_list_for_each(output, &compositor->output_list, link) {
4883 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004884 wl_fixed_to_double(pointer->x),
4885 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004886 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004887 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004888 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004889 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004890 increment = -output->zoom.increment;
4891 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004892 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004893 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004894 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004895 else
4896 increment = 0;
4897
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004898 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004899
Scott Moreaue6603982012-06-11 13:07:51 -06004900 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004901 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004902 else if (output->zoom.level > output->zoom.max_level)
4903 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004904
4905 if (!output->zoom.active) {
4906 if (output->zoom.level <= 0.0)
4907 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004908 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004909 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004910
Scott Moreaue6603982012-06-11 13:07:51 -06004911 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004912
Jason Ekstranda7af7042013-10-12 22:38:11 -05004913 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004914 }
4915 }
4916}
4917
Scott Moreauccbf29d2012-02-22 14:21:41 -07004918static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004919zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004920 struct weston_pointer_axis_event *event,
4921 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004922{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004923 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004924}
4925
4926static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004927zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4928 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004929{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004930 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004931}
4932
4933static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004934terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4935 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004936{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004937 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004938
Daniel Stone325fc2d2012-05-30 16:31:58 +01004939 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004940}
4941
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004942static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004943rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004944 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004945{
4946 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004947 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004948 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004949 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004950 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004951
Jonas Ådahld2510102014-10-05 21:39:14 +02004952 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004953
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004954 if (!shsurf)
4955 return;
4956
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004957 cx = 0.5f * shsurf->surface->width;
4958 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004959
Daniel Stone37816df2012-05-16 18:45:18 +01004960 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4961 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004962 r = sqrtf(dx * dx + dy * dy);
4963
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004964 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004965 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004966
4967 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004968 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004969 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004970
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004971 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004972 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004973
4974 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004975 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004976 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004977 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004978 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004979
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004980 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004981 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004982 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004983 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004984 wl_list_init(&shsurf->rotation.transform.link);
4985 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004986 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004987 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004988
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004989 /* We need to adjust the position of the surface
4990 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004991 cposx = shsurf->view->geometry.x + cx;
4992 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004993 dposx = rotate->center.x - cposx;
4994 dposy = rotate->center.y - cposy;
4995 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004996 weston_view_set_position(shsurf->view,
4997 shsurf->view->geometry.x + dposx,
4998 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004999 }
5000
Derek Foreman4b1a0a12014-09-10 15:37:33 -05005001 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02005002 * lazily applies the damage due to rotation update.
5003 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005004 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005005}
5006
5007static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005008rotate_grab_button(struct weston_pointer_grab *grab,
5009 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005010{
5011 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005012 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005013 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005014 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01005015 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005016
Daniel Stone4dbadb12012-05-30 16:31:51 +01005017 if (pointer->button_count == 0 &&
5018 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005019 if (shsurf)
5020 weston_matrix_multiply(&shsurf->rotation.rotation,
5021 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005022 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005023 free(rotate);
5024 }
5025}
5026
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005027static void
5028rotate_grab_cancel(struct weston_pointer_grab *grab)
5029{
5030 struct rotate_grab *rotate =
5031 container_of(grab, struct rotate_grab, base.grab);
5032
5033 shell_grab_end(&rotate->base);
5034 free(rotate);
5035}
5036
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005037static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005038 noop_grab_focus,
5039 rotate_grab_motion,
5040 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005041 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10005042 noop_grab_axis_source,
5043 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005044 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005045};
5046
5047static void
Derek Foreman74de4692015-07-15 13:00:39 -05005048surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005049{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005050 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005051 float dx, dy;
5052 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005053
Derek Foreman45a7c272015-09-11 14:27:40 -05005054 surface = find_toplevel_surface(surface);
5055
Pekka Paalanen460099f2012-01-20 16:48:25 +02005056 rotate = malloc(sizeof *rotate);
5057 if (!rotate)
5058 return;
5059
Jason Ekstranda7af7042013-10-12 22:38:11 -05005060 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005061 surface->surface->width * 0.5f,
5062 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005063 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005064
Derek Foreman74de4692015-07-15 13:00:39 -05005065 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5066 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005067 r = sqrtf(dx * dx + dy * dy);
5068 if (r > 20.0f) {
5069 struct weston_matrix inverse;
5070
5071 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005072 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005073 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005074
5075 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005076 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005077 } else {
5078 weston_matrix_init(&surface->rotation.rotation);
5079 weston_matrix_init(&rotate->rotation);
5080 }
5081
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005082 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005083 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005084}
5085
5086static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005087rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005088 void *data)
5089{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005090 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005091 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005092 struct shell_surface *surface;
5093
Derek Foreman8ae2db52015-07-15 13:00:36 -05005094 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005095 return;
5096
Derek Foreman8ae2db52015-07-15 13:00:36 -05005097 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005098
Pekka Paalanen01388e22013-04-25 13:57:44 +03005099 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005100 if (base_surface == NULL)
5101 return;
5102
5103 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005104 if (surface == NULL || surface->state.fullscreen ||
5105 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005106 return;
5107
Derek Foreman74de4692015-07-15 13:00:39 -05005108 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005109}
5110
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005111/* Move all fullscreen layers down to the current workspace and hide their
5112 * black views. The surfaces' state is set to both fullscreen and lowered,
5113 * and this is reversed when such a surface is re-configured, see
5114 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5115 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005116 * lowering_output = NULL - Lower on all outputs, else only lower on the
5117 * specified output.
5118 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005119 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005120 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005121void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005122lower_fullscreen_layer(struct desktop_shell *shell,
5123 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005124{
5125 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005126 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005127
5128 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005129 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005130 &shell->fullscreen_layer.view_list.link,
5131 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005132 struct shell_surface *shsurf = get_shell_surface(view->surface);
5133
5134 if (!shsurf)
5135 continue;
5136
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005137 /* Only lower surfaces which have lowering_output as their fullscreen
5138 * output, unless a NULL output asks for lowering on all outputs.
5139 */
5140 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5141 continue;
5142
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005143 /* We can have a non-fullscreen popup for a fullscreen surface
5144 * in the fullscreen layer. */
5145 if (shsurf->state.fullscreen) {
5146 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005147 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5148 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005149 weston_view_damage_below(shsurf->fullscreen.black_view);
5150
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005151 }
5152
5153 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005154 weston_layer_entry_remove(&view->layer_link);
5155 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005156 weston_view_damage_below(view);
5157 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005158
5159 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005160 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005161}
5162
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005163void
Tiago Vignattibe143262012-04-16 17:31:41 +03005164activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005165 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005166{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005167 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005168 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005169 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005170 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005171 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005172
Pekka Paalanen01388e22013-04-25 13:57:44 +03005173 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005174 shsurf = get_shell_surface(main_surface);
5175 assert(shsurf);
5176
5177 /* Only demote fullscreen surfaces on the output of activated shsurf.
5178 * Leave fullscreen surfaces on unrelated outputs alone. */
5179 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005180
Bryce Harrington260c2ff2016-06-29 19:04:06 -07005181 weston_seat_set_keyboard_focus(seat, es);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005182
Jonas Ådahl8538b222012-08-29 22:13:03 +02005183 state = ensure_focus_state(shell, seat);
5184 if (state == NULL)
5185 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005186
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005187 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005188 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005189
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005190 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005191 shell_configure_fullscreen(shsurf);
5192 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005193 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005194
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005195 /* Update the surface’s layer. This brings it to the top of the stacking
5196 * order as appropriate. */
5197 shell_surface_update_layer(shsurf);
5198
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005199 if (shell->focus_animation_type != ANIMATION_NONE) {
5200 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005201 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005202 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005203}
5204
Alex Wu21858432012-04-01 20:13:08 +08005205/* no-op func for checking black surface */
5206static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005207black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005208{
5209}
5210
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005211static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005212is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08005213{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005214 struct weston_surface *surface = view->surface;
5215
5216 if (surface->configure == black_surface_configure) {
5217 if (fs_view)
5218 *fs_view = surface->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005219 return true;
5220 }
5221 return false;
5222}
5223
Kristian Høgsberg75840622011-09-06 13:48:16 -04005224static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005225activate_binding(struct weston_seat *seat,
5226 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005227 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005228{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005229 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005230 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005231
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005232 if (!focus_view)
5233 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005234
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005235 if (is_black_surface_view(focus_view, &main_view))
5236 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08005237
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005238 main_surface = weston_surface_get_main_surface(focus_view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005239 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005240 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005241
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005242 activate(shell, focus_view->surface, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005243}
5244
5245static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005246click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5247 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005248{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005249 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005250 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005251 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005252 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005253
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005254 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005255}
5256
5257static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005258touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5259 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005260{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005261 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005262 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005263 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005264 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005265
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005266 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005267}
5268
5269static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005270unfocus_all_seats(struct desktop_shell *shell)
5271{
5272 struct weston_seat *seat, *next;
5273
5274 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005275 struct weston_keyboard *keyboard =
5276 weston_seat_get_keyboard(seat);
5277
5278 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005279 continue;
5280
Derek Foreman1281a362015-07-31 16:55:32 -05005281 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005282 }
5283}
5284
5285static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005286lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005287{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005288 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005289
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005290 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005291 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005292 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005293 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005294
5295 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005296
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005297 /* Hide all surfaces by removing the fullscreen, panel and
5298 * toplevel layers. This way nothing else can show or receive
5299 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005300
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005301 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005302 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005303 if (shell->showing_input_panels)
5304 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005305 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005306 wl_list_insert(&shell->compositor->cursor_layer.link,
5307 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005308
Derek Foreman004b4a12015-07-20 16:28:13 -05005309 weston_compositor_sleep(shell->compositor);
5310
Neil Robertsb4a91702014-04-09 16:33:32 +01005311 /* Remove the keyboard focus on all seats. This will be
5312 * restored to the workspace's saved state via
5313 * restore_focus_state when the compositor is unlocked */
5314 unfocus_all_seats(shell);
5315
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005316 /* TODO: disable bindings that should not work while locked. */
5317
5318 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005319}
5320
5321static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005322unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005323{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005324 struct wl_resource *shell_resource;
5325
Pekka Paalanend81c2162011-11-16 13:47:34 +02005326 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005327 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005328 return;
5329 }
5330
5331 /* If desktop-shell client has gone away, unlock immediately. */
5332 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005333 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005334 return;
5335 }
5336
5337 if (shell->prepare_event_sent)
5338 return;
5339
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005340 shell_resource = shell->child.desktop_shell;
5341 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005342 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005343}
5344
5345static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005346shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005347{
5348 struct desktop_shell *shell = data;
5349
5350 shell->fade.animation = NULL;
5351
5352 switch (shell->fade.type) {
5353 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005354 weston_surface_destroy(shell->fade.view->surface);
5355 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005356 break;
5357 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005358 lock(shell);
5359 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005360 default:
5361 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005362 }
5363}
5364
Jason Ekstranda7af7042013-10-12 22:38:11 -05005365static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005366shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005367{
5368 struct weston_compositor *compositor = shell->compositor;
5369 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005370 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005371
5372 surface = weston_surface_create(compositor);
5373 if (!surface)
5374 return NULL;
5375
Jason Ekstranda7af7042013-10-12 22:38:11 -05005376 view = weston_view_create(surface);
5377 if (!view) {
5378 weston_surface_destroy(surface);
5379 return NULL;
5380 }
5381
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005382 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005383 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005384 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005385 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5386 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005387 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02005388 surface->is_mapped = true;
5389 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005390
Jason Ekstranda7af7042013-10-12 22:38:11 -05005391 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005392}
5393
5394static void
5395shell_fade(struct desktop_shell *shell, enum fade_type type)
5396{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005397 float tint;
5398
5399 switch (type) {
5400 case FADE_IN:
5401 tint = 0.0;
5402 break;
5403 case FADE_OUT:
5404 tint = 1.0;
5405 break;
5406 default:
5407 weston_log("shell: invalid fade type\n");
5408 return;
5409 }
5410
5411 shell->fade.type = type;
5412
Jason Ekstranda7af7042013-10-12 22:38:11 -05005413 if (shell->fade.view == NULL) {
5414 shell->fade.view = shell_fade_create_surface(shell);
5415 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005416 return;
5417
Jason Ekstranda7af7042013-10-12 22:38:11 -05005418 shell->fade.view->alpha = 1.0 - tint;
5419 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005420 }
5421
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005422 if (shell->fade.view->output == NULL) {
5423 /* If the black view gets a NULL output, we lost the
5424 * last output and we'll just cancel the fade. This
5425 * happens when you close the last window under the
5426 * X11 or Wayland backends. */
5427 shell->locked = false;
5428 weston_surface_destroy(shell->fade.view->surface);
5429 shell->fade.view = NULL;
5430 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005431 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005432 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005433 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005434 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005435 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005436 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005437 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005438}
5439
5440static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005441do_shell_fade_startup(void *data)
5442{
5443 struct desktop_shell *shell = data;
5444
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005445 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005446 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005447 } else {
5448 weston_log("desktop shell: "
5449 "unexpected fade-in animation type %d\n",
5450 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005451 weston_surface_destroy(shell->fade.view->surface);
5452 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005453 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005454}
5455
5456static void
5457shell_fade_startup(struct desktop_shell *shell)
5458{
5459 struct wl_event_loop *loop;
5460
5461 if (!shell->fade.startup_timer)
5462 return;
5463
5464 wl_event_source_remove(shell->fade.startup_timer);
5465 shell->fade.startup_timer = NULL;
5466
5467 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5468 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5469}
5470
5471static int
5472fade_startup_timeout(void *data)
5473{
5474 struct desktop_shell *shell = data;
5475
5476 shell_fade_startup(shell);
5477 return 0;
5478}
5479
5480static void
5481shell_fade_init(struct desktop_shell *shell)
5482{
5483 /* Make compositor output all black, and wait for the desktop-shell
5484 * client to signal it is ready, then fade in. The timer triggers a
5485 * fade-in, in case the desktop-shell client takes too long.
5486 */
5487
5488 struct wl_event_loop *loop;
5489
Jason Ekstranda7af7042013-10-12 22:38:11 -05005490 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005491 weston_log("%s: warning: fade surface already exists\n",
5492 __func__);
5493 return;
5494 }
5495
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005496 if (shell->startup_animation_type == ANIMATION_NONE)
5497 return;
5498
Jason Ekstranda7af7042013-10-12 22:38:11 -05005499 shell->fade.view = shell_fade_create_surface(shell);
5500 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005501 return;
5502
Jason Ekstranda7af7042013-10-12 22:38:11 -05005503 weston_view_update_transform(shell->fade.view);
5504 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005505
5506 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5507 shell->fade.startup_timer =
5508 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5509 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5510}
5511
5512static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005513idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005514{
5515 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005516 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005517 struct weston_seat *seat;
5518
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005519 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005520 struct weston_touch *touch = weston_seat_get_touch(seat);
5521 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5522
5523 if (pointer)
5524 popup_grab_end(pointer);
5525 if (touch)
5526 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005527 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005528
5529 shell_fade(shell, FADE_OUT);
5530 /* lock() is called from shell_fade_done() */
5531}
5532
5533static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005534wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005535{
5536 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005537 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005538
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005539 unlock(shell);
5540}
5541
5542static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005543transform_handler(struct wl_listener *listener, void *data)
5544{
5545 struct weston_surface *surface = data;
5546 struct shell_surface *shsurf = get_shell_surface(surface);
5547 struct weston_view *view;;
5548 int x, y;
5549
5550 if (!shsurf || !shsurf->client->send_position)
5551 return;
5552
5553 view = shsurf->view;
5554 if (!view || !weston_view_is_mapped(view))
5555 return;
5556
5557 x = view->geometry.x;
5558 y = view->geometry.y;
5559
5560 shsurf->client->send_position(surface, x, y);
5561}
5562
5563static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005564center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005565{
Giulio Camuffob8366642013-04-25 13:57:46 +03005566 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005567 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005568
Jason Ekstranda7af7042013-10-12 22:38:11 -05005569 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005570
5571 x = output->x + (output->width - width) / 2 - surf_x / 2;
5572 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005573
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005574 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005575}
5576
5577static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005578weston_view_set_initial_position(struct weston_view *view,
5579 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005580{
5581 struct weston_compositor *compositor = shell->compositor;
5582 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005583 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005584 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005585 struct weston_output *output, *target_output = NULL;
5586 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005587 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005588
5589 /* As a heuristic place the new window on the same output as the
5590 * pointer. Falling back to the output containing 0, 0.
5591 *
5592 * TODO: Do something clever for touch too?
5593 */
5594 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005595 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5596
5597 if (pointer) {
5598 ix = wl_fixed_to_int(pointer->x);
5599 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005600 break;
5601 }
5602 }
5603
5604 wl_list_for_each(output, &compositor->output_list, link) {
5605 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5606 target_output = output;
5607 break;
5608 }
5609 }
5610
5611 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005612 weston_view_set_position(view, 10 + random() % 400,
5613 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005614 return;
5615 }
5616
5617 /* Valid range within output where the surface will still be onscreen.
5618 * If this is negative it means that the surface is bigger than
5619 * output.
5620 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005621 get_output_work_area(shell, target_output, &area);
5622
Derek Foremanf814c5d2015-04-15 12:23:54 -05005623 x = area.x;
5624 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005625 range_x = area.width - view->surface->width;
5626 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005627
Rob Bradford4cb88c72012-08-13 15:18:44 +01005628 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005629 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005630
5631 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005632 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005633
Jason Ekstranda7af7042013-10-12 22:38:11 -05005634 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005635}
5636
5637static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005638set_maximized_position(struct desktop_shell *shell,
5639 struct shell_surface *shsurf)
5640{
5641 int32_t surf_x, surf_y;
5642 pixman_rectangle32_t area;
5643
Jonny Lambd73c6942014-08-20 15:53:20 +02005644 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005645 if (shsurf->has_set_geometry) {
5646 surf_x = shsurf->geometry.x;
5647 surf_y = shsurf->geometry.y;
5648 } else {
5649 surface_subsurfaces_boundingbox(shsurf->surface,
5650 &surf_x, &surf_y, NULL, NULL);
5651 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005652
5653 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005654 area.x - surf_x,
5655 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005656}
5657
5658static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005659map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005660 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005661{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005662 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005663 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005664
Pekka Paalanen77346a62011-11-30 16:26:35 +02005665 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005666 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005667 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005668 if (shsurf->state.fullscreen) {
5669 center_on_output(shsurf->view, shsurf->fullscreen_output);
5670 shell_map_fullscreen(shsurf);
5671 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005672 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005673 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005674 weston_view_set_initial_position(shsurf->view, shell);
5675 }
Juan Zhao96879df2012-02-07 08:45:41 +08005676 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005677 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005678 if (shell_map_popup(shsurf) != 0)
5679 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005680 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005681 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005682 weston_view_set_position(shsurf->view,
5683 shsurf->view->geometry.x + sx,
5684 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005685 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005686 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005687 default:
5688 ;
5689 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005690
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005691 /* Surface stacking order, see also activate(). */
5692 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005693
Jason Ekstranda7af7042013-10-12 22:38:11 -05005694 if (shsurf->type != SHELL_SURFACE_NONE) {
5695 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02005696 shsurf->view->is_mapped = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005697 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005698 shsurf->surface->output = shsurf->output;
5699 shsurf->view->output = shsurf->output;
5700 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005701 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005702
Jason Ekstranda7af7042013-10-12 22:38:11 -05005703 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005704 /* XXX: xwayland's using the same fields for transient type */
5705 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005706 if (shsurf->transient.flags ==
5707 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5708 break;
5709 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005710 if (shsurf->state.relative &&
5711 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5712 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005713 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005714 break;
5715 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005716 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005717 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005718 case SHELL_SURFACE_POPUP:
5719 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005720 default:
5721 break;
5722 }
5723
Rafael Antognolli03b16592013-12-03 15:35:42 -02005724 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5725 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005726 {
5727 switch (shell->win_animation_type) {
5728 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005729 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005730 break;
5731 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005732 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005733 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005734 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005735 default:
5736 break;
5737 }
5738 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005739}
5740
5741static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005742configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005743 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005744{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005745 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005746 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005747
Pekka Paalanen77346a62011-11-30 16:26:35 +02005748 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005749
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005750 assert(shsurf);
5751
Rafael Antognolli03b16592013-12-03 15:35:42 -02005752 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005753 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005754 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005755 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005756 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005757 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005758 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005759
Alex Wu4539b082012-03-01 12:57:46 +08005760 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005761 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005762 wl_list_for_each(view, &surface->views, surface_link)
5763 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005764
Rafael Antognolli03b16592013-12-03 15:35:42 -02005765 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005766 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005767 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005768}
5769
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005770static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005771shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005772{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005773 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005774 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005775 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005776
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005777 assert(shsurf);
5778
5779 shell = shsurf->shell;
5780
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005781 if (!weston_surface_is_mapped(es) &&
5782 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005783 remove_popup_grab(shsurf);
5784 }
5785
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005786 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005787 return;
5788
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005789 if (shsurf->has_next_geometry) {
5790 shsurf->geometry = shsurf->next_geometry;
5791 shsurf->has_next_geometry = false;
5792 shsurf->has_set_geometry = true;
5793 } else if (!shsurf->has_set_geometry) {
5794 surface_subsurfaces_boundingbox(shsurf->surface,
5795 &shsurf->geometry.x,
5796 &shsurf->geometry.y,
5797 &shsurf->geometry.width,
5798 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005799 }
5800
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005801 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005802 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005803 type_changed = 1;
5804 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005805
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005806 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005807 map(shell, shsurf, sx, sy);
Armin Krezović4663aca2016-06-30 06:04:29 +02005808 es->is_mapped = true;
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005809 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005810 shsurf->last_width != es->width ||
5811 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005812 float from_x, from_y;
5813 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005814
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005815 if (shsurf->resize_edges) {
5816 sx = 0;
5817 sy = 0;
5818 }
5819
5820 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5821 sx = shsurf->last_width - es->width;
5822 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5823 sy = shsurf->last_height - es->height;
5824
5825 shsurf->last_width = es->width;
5826 shsurf->last_height = es->height;
5827
Jason Ekstranda7af7042013-10-12 22:38:11 -05005828 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5829 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005830 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005831 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005832 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005833 }
5834}
5835
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005836static bool
5837check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5838{
5839 struct timespec now;
5840
5841 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5842 return false;
5843
5844 /*
5845 * If the shell helper client dies before the session has been
5846 * up for roughly 30 seconds, better just make Weston shut down,
5847 * because the user likely has no way to interact with the desktop
5848 * anyway.
5849 */
5850 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5851 weston_log("Error: %s apparently cannot run at all.\n",
5852 shell->client);
5853 weston_log_continue(STAMP_SPACE "Quitting...");
5854 wl_display_terminate(shell->compositor->wl_display);
5855
5856 return true;
5857 }
5858
5859 return false;
5860}
5861
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005862static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005863
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005864static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005865respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005866{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005867 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005868
5869 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5870 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005871 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005872 shell->child.deathstamp = time;
5873 shell->child.deathcount = 0;
5874 }
5875
5876 shell->child.deathcount++;
5877 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005878 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005879 return;
5880 }
5881
Pekka Paalanen826dc142014-08-27 12:11:53 +03005882 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005883 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005884}
5885
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005886static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005887desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5888{
5889 struct desktop_shell *shell;
5890
5891 shell = container_of(listener, struct desktop_shell,
5892 child.client_destroy_listener);
5893
Pekka Paalanen826dc142014-08-27 12:11:53 +03005894 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005895 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005896 /*
5897 * unbind_desktop_shell() will reset shell->child.desktop_shell
5898 * before the respawned process has a chance to create a new
5899 * desktop_shell object, because we are being called from the
5900 * wl_client destructor which destroys all wl_resources before
5901 * returning.
5902 */
5903
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005904 if (!check_desktop_shell_crash_too_early(shell))
5905 respawn_desktop_shell_process(shell);
5906
Pekka Paalanen826dc142014-08-27 12:11:53 +03005907 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005908}
5909
5910static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005911launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005912{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005913 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005914
Pekka Paalanen826dc142014-08-27 12:11:53 +03005915 shell->child.client = weston_client_start(shell->compositor,
5916 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005917
Arnaud Vrac42631192014-08-25 20:56:46 +02005918 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005919 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005920 return;
5921 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005922
5923 shell->child.client_destroy_listener.notify =
5924 desktop_shell_client_destroy;
5925 wl_client_add_destroy_listener(shell->child.client,
5926 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005927}
5928
5929static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005930handle_shell_client_destroy(struct wl_listener *listener, void *data)
5931{
5932 struct shell_client *sc =
5933 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005934 struct wl_resource *shsurf_resource;
5935 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005936
5937 if (sc->ping_timer)
5938 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005939
5940 /* Since we're about to free shell_client, we remove it from the
5941 * head of the surface list so we don't use that freed list node
5942 * during surface clean up later on.
5943 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005944 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5945 shsurf = wl_resource_get_user_data(shsurf_resource);
5946 shsurf->owner = NULL;
5947 }
Derek Foremane42d7542015-05-29 10:46:44 -05005948 wl_list_remove(&sc->surface_list);
5949
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005950 free(sc);
5951}
5952
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005953static struct shell_client *
5954shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5955 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005956{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005957 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005958
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005959 sc = zalloc(sizeof *sc);
5960 if (sc == NULL) {
5961 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005962 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005963 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005964
5965 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005966 if (sc->resource == NULL) {
5967 free(sc);
5968 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005969 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005970 }
5971
5972 sc->client = client;
5973 sc->shell = shell;
5974 sc->destroy_listener.notify = handle_shell_client_destroy;
5975 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005976 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005977
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005978 return sc;
5979}
5980
5981static void
5982bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5983{
5984 struct desktop_shell *shell = data;
5985 struct shell_client *sc;
5986
5987 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5988 if (sc)
5989 wl_resource_set_implementation(sc->resource,
5990 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005991 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005992}
5993
5994static void
5995bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5996{
5997 struct desktop_shell *shell = data;
5998 struct shell_client *sc;
5999
6000 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
6001 if (sc)
6002 wl_resource_set_dispatcher(sc->resource,
6003 xdg_shell_unversioned_dispatch,
6004 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02006005}
6006
6007static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006008unbind_desktop_shell(struct wl_resource *resource)
6009{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05006010 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05006011
6012 if (shell->locked)
6013 resume_desktop(shell);
6014
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006015 shell->child.desktop_shell = NULL;
6016 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006017}
6018
6019static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04006020bind_desktop_shell(struct wl_client *client,
6021 void *data, uint32_t version, uint32_t id)
6022{
Tiago Vignattibe143262012-04-16 17:31:41 +03006023 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006024 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04006025
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006026 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6027 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006028
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006029 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006030 wl_resource_set_implementation(resource,
6031 &desktop_shell_implementation,
6032 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006033 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006034 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006035 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006036
6037 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6038 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04006039}
6040
Kristian Høgsberg07045392012-02-19 18:52:44 -05006041struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006042 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006043 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006045 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006046 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047};
6048
6049static void
6050switcher_next(struct switcher *switcher)
6051{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006052 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006053 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006054 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006055 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006056
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006057 /* temporary re-display minimized surfaces */
6058 struct weston_view *tmp;
6059 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006060 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6061 weston_layer_entry_remove(&view->layer_link);
6062 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006063 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6064 *minimized = view;
6065 }
6066
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006067 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006068 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006069 if (shsurf &&
6070 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6071 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006072 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006073 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006074 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006075 next = view;
6076 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006077 view->alpha = 0.25;
6078 weston_view_geometry_dirty(view);
6079 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006080 }
Alex Wu1659daa2012-04-01 20:13:09 +08006081
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02006082 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05006083 view->alpha = 0.25;
6084 weston_view_geometry_dirty(view);
6085 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006086 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006087 }
6088
6089 if (next == NULL)
6090 next = first;
6091
Alex Wu07b26062012-03-12 16:06:01 +08006092 if (next == NULL)
6093 return;
6094
Kristian Høgsberg07045392012-02-19 18:52:44 -05006095 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006096 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006097
6098 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006099 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006100 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006101
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006102 shsurf = get_shell_surface(switcher->current->surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006103 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006104 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006105}
6106
6107static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006108switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006109{
6110 struct switcher *switcher =
6111 container_of(listener, struct switcher, listener);
6112
6113 switcher_next(switcher);
6114}
6115
6116static void
Daniel Stone351eb612012-05-31 15:27:47 -04006117switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006118{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006119 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006120 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006121 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006122
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006123 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006124 if (is_focus_view(view))
6125 continue;
6126
Jason Ekstranda7af7042013-10-12 22:38:11 -05006127 view->alpha = 1.0;
6128 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006129 }
6130
Alex Wu07b26062012-03-12 16:06:01 +08006131 if (switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006132 activate(switcher->shell, switcher->current->surface,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006133 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006134 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006135 weston_keyboard_end_grab(keyboard);
6136 if (keyboard->input_method_resource)
6137 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006138
6139 /* re-hide surfaces that were temporary shown during the switch */
6140 struct weston_view **minimized;
6141 wl_array_for_each(minimized, &switcher->minimized_array) {
6142 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006143 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006144 weston_layer_entry_remove(&(*minimized)->layer_link);
6145 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006146 weston_view_damage_below(*minimized);
6147 }
6148 }
6149 wl_array_release(&switcher->minimized_array);
6150
Kristian Høgsberg07045392012-02-19 18:52:44 -05006151 free(switcher);
6152}
6153
6154static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006155switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006156 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006157{
6158 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006159 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006160
Daniel Stonec9785ea2012-05-30 16:31:52 +01006161 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006162 switcher_next(switcher);
6163}
6164
6165static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006166switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006167 uint32_t mods_depressed, uint32_t mods_latched,
6168 uint32_t mods_locked, uint32_t group)
6169{
6170 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006171 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006172
Daniel Stone351eb612012-05-31 15:27:47 -04006173 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6174 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006175}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006176
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006177static void
6178switcher_cancel(struct weston_keyboard_grab *grab)
6179{
6180 struct switcher *switcher = container_of(grab, struct switcher, grab);
6181
6182 switcher_destroy(switcher);
6183}
6184
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006185static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006186 switcher_key,
6187 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006188 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006189};
6190
6191static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006192switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6193 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006194{
Tiago Vignattibe143262012-04-16 17:31:41 +03006195 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006196 struct switcher *switcher;
6197
6198 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006199 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006200 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006201 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006202 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006203 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006204
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006205 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006206 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006207 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006208 weston_keyboard_start_grab(keyboard, &switcher->grab);
6209 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006210 switcher_next(switcher);
6211}
6212
Pekka Paalanen3c647232011-12-22 13:43:43 +02006213static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006214backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6215 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006216{
6217 struct weston_compositor *compositor = data;
6218 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006219 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006220
6221 /* TODO: we're limiting to simple use cases, where we assume just
6222 * control on the primary display. We'd have to extend later if we
6223 * ever get support for setting backlights on random desktop LCD
6224 * panels though */
6225 output = get_default_output(compositor);
6226 if (!output)
6227 return;
6228
6229 if (!output->set_backlight)
6230 return;
6231
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006232 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6233 backlight_new = output->backlight_current - 25;
6234 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6235 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006236
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006237 if (backlight_new < 5)
6238 backlight_new = 5;
6239 if (backlight_new > 255)
6240 backlight_new = 255;
6241
6242 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006243 output->set_backlight(output, output->backlight_current);
6244}
6245
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006246static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006247force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6248 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006249{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006250 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006251 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006252 struct desktop_shell *shell = data;
6253 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006254 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006255
Derek Foreman8ae2db52015-07-15 13:00:36 -05006256 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006257 if (!focus_surface)
6258 return;
6259
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006260 wl_signal_emit(&compositor->kill_signal, focus_surface);
6261
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006262 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006263 wl_client_get_credentials(client, &pid, NULL, NULL);
6264
6265 /* Skip clients that we launched ourselves (the credentials of
6266 * the socketpair is ours) */
6267 if (pid == getpid())
6268 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006269
Daniel Stone325fc2d2012-05-30 16:31:58 +01006270 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006271}
6272
6273static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006274workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006275 uint32_t key, void *data)
6276{
6277 struct desktop_shell *shell = data;
6278 unsigned int new_index = shell->workspaces.current;
6279
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006280 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006281 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006282 if (new_index != 0)
6283 new_index--;
6284
6285 change_workspace(shell, new_index);
6286}
6287
6288static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006289workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006290 uint32_t key, void *data)
6291{
6292 struct desktop_shell *shell = data;
6293 unsigned int new_index = shell->workspaces.current;
6294
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006295 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006296 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006297 if (new_index < shell->workspaces.num - 1)
6298 new_index++;
6299
6300 change_workspace(shell, new_index);
6301}
6302
6303static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006304workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006305 uint32_t key, void *data)
6306{
6307 struct desktop_shell *shell = data;
6308 unsigned int new_index;
6309
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006310 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006311 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006312 new_index = key - KEY_F1;
6313 if (new_index >= shell->workspaces.num)
6314 new_index = shell->workspaces.num - 1;
6315
6316 change_workspace(shell, new_index);
6317}
6318
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006319static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006320workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6321 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006322{
6323 struct desktop_shell *shell = data;
6324 unsigned int new_index = shell->workspaces.current;
6325
6326 if (shell->locked)
6327 return;
6328
6329 if (new_index != 0)
6330 new_index--;
6331
Derek Foreman8ae2db52015-07-15 13:00:36 -05006332 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006333}
6334
6335static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006336workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6337 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006338{
6339 struct desktop_shell *shell = data;
6340 unsigned int new_index = shell->workspaces.current;
6341
6342 if (shell->locked)
6343 return;
6344
6345 if (new_index < shell->workspaces.num - 1)
6346 new_index++;
6347
Derek Foreman8ae2db52015-07-15 13:00:36 -05006348 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006349}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006350
6351static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006352shell_reposition_view_on_output_destroy(struct weston_view *view)
6353{
6354 struct weston_output *output, *first_output;
6355 struct weston_compositor *ec = view->surface->compositor;
6356 struct shell_surface *shsurf;
6357 float x, y;
6358 int visible;
6359
6360 x = view->geometry.x;
6361 y = view->geometry.y;
6362
6363 /* At this point the destroyed output is not in the list anymore.
6364 * If the view is still visible somewhere, we leave where it is,
6365 * otherwise, move it to the first output. */
6366 visible = 0;
6367 wl_list_for_each(output, &ec->output_list, link) {
6368 if (pixman_region32_contains_point(&output->region,
6369 x, y, NULL)) {
6370 visible = 1;
6371 break;
6372 }
6373 }
6374
6375 if (!visible) {
6376 first_output = container_of(ec->output_list.next,
6377 struct weston_output, link);
6378
6379 x = first_output->x + first_output->width / 4;
6380 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006381
6382 weston_view_set_position(view, x, y);
6383 } else {
6384 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006385 }
6386
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006387
6388 shsurf = get_shell_surface(view->surface);
6389
6390 if (shsurf) {
6391 shsurf->saved_position_valid = false;
6392 shsurf->next_state.maximized = false;
6393 shsurf->next_state.fullscreen = false;
6394 shsurf->state_changed = true;
6395 }
6396}
6397
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006398void
6399shell_for_each_layer(struct desktop_shell *shell,
6400 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006401{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006402 struct workspace **ws;
6403
6404 func(shell, &shell->fullscreen_layer, data);
6405 func(shell, &shell->panel_layer, data);
6406 func(shell, &shell->background_layer, data);
6407 func(shell, &shell->lock_layer, data);
6408 func(shell, &shell->input_panel_layer, data);
6409
6410 wl_array_for_each(ws, &shell->workspaces.array)
6411 func(shell, &(*ws)->layer, data);
6412}
6413
6414static void
6415shell_output_destroy_move_layer(struct desktop_shell *shell,
6416 struct weston_layer *layer,
6417 void *data)
6418{
6419 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006420 struct weston_view *view;
6421
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006422 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006423 if (view->output != output)
6424 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006425
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006426 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006427 }
6428}
6429
6430static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006431handle_output_destroy(struct wl_listener *listener, void *data)
6432{
6433 struct shell_output *output_listener =
6434 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006435 struct weston_output *output = output_listener->output;
6436 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006437
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006438 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006439
Xiong Zhang6b481422013-10-23 13:58:32 +08006440 wl_list_remove(&output_listener->destroy_listener.link);
6441 wl_list_remove(&output_listener->link);
6442 free(output_listener);
6443}
6444
6445static void
David Fort50d962f2016-06-09 17:55:52 +02006446shell_resize_surface_to_output(struct desktop_shell *shell,
6447 struct weston_surface *surface,
6448 const struct weston_output *output)
6449{
6450 if (!surface)
6451 return;
6452
6453 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6454 surface->resource,
6455 output->width,
6456 output->height);
6457}
6458
6459
6460static void
6461handle_output_resized(struct wl_listener *listener, void *data)
6462{
6463 struct desktop_shell *shell =
6464 container_of(listener, struct desktop_shell, resized_listener);
6465 struct weston_output *output = (struct weston_output *)data;
6466 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6467
6468 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6469 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6470}
6471
6472static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006473create_shell_output(struct desktop_shell *shell,
6474 struct weston_output *output)
6475{
6476 struct shell_output *shell_output;
6477
6478 shell_output = zalloc(sizeof *shell_output);
6479 if (shell_output == NULL)
6480 return;
6481
6482 shell_output->output = output;
6483 shell_output->shell = shell;
6484 shell_output->destroy_listener.notify = handle_output_destroy;
6485 wl_signal_add(&output->destroy_signal,
6486 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006487 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006488}
6489
6490static void
6491handle_output_create(struct wl_listener *listener, void *data)
6492{
6493 struct desktop_shell *shell =
6494 container_of(listener, struct desktop_shell, output_create_listener);
6495 struct weston_output *output = (struct weston_output *)data;
6496
6497 create_shell_output(shell, output);
6498}
6499
6500static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006501handle_output_move_layer(struct desktop_shell *shell,
6502 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006503{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006504 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006505 struct weston_view *view;
6506 float x, y;
6507
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006508 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006509 if (view->output != output)
6510 continue;
6511
6512 x = view->geometry.x + output->move_x;
6513 y = view->geometry.y + output->move_y;
6514 weston_view_set_position(view, x, y);
6515 }
6516}
6517
6518static void
6519handle_output_move(struct wl_listener *listener, void *data)
6520{
6521 struct desktop_shell *shell;
6522
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006523 shell = container_of(listener, struct desktop_shell,
6524 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006525
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006526 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006527}
6528
6529static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006530setup_output_destroy_handler(struct weston_compositor *ec,
6531 struct desktop_shell *shell)
6532{
6533 struct weston_output *output;
6534
6535 wl_list_init(&shell->output_list);
6536 wl_list_for_each(output, &ec->output_list, link)
6537 create_shell_output(shell, output);
6538
6539 shell->output_create_listener.notify = handle_output_create;
6540 wl_signal_add(&ec->output_created_signal,
6541 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006542
6543 shell->output_move_listener.notify = handle_output_move;
6544 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006545}
6546
6547static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006548shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006549{
Tiago Vignattibe143262012-04-16 17:31:41 +03006550 struct desktop_shell *shell =
6551 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006552 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006553 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006554
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006555 /* Force state to unlocked so we don't try to fade */
6556 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006557
6558 if (shell->child.client) {
6559 /* disable respawn */
6560 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006561 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006562 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006563
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006564 wl_list_remove(&shell->idle_listener.link);
6565 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006566 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006567
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006568 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006569 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006570
Xiong Zhang6b481422013-10-23 13:58:32 +08006571 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6572 wl_list_remove(&shell_output->destroy_listener.link);
6573 wl_list_remove(&shell_output->link);
6574 free(shell_output);
6575 }
6576
6577 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006578 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02006579 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006580
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006581 wl_array_for_each(ws, &shell->workspaces.array)
6582 workspace_destroy(*ws);
6583 wl_array_release(&shell->workspaces.array);
6584
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006585 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006586 free(shell);
6587}
6588
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006589static void
6590shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6591{
6592 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006593 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006594
Bob Ham744e6532016-01-12 10:21:48 +00006595 if (shell->allow_zap)
6596 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6597 MODIFIER_CTRL | MODIFIER_ALT,
6598 terminate_binding, ec);
6599
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006600 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006601 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6602 click_to_activate_binding,
6603 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006604 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6605 click_to_activate_binding,
6606 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006607 weston_compositor_add_touch_binding(ec, 0,
6608 touch_to_activate_binding,
6609 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006610 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6611 MODIFIER_SUPER | MODIFIER_ALT,
6612 surface_opacity_binding, NULL);
6613 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6614 MODIFIER_SUPER, zoom_axis_binding,
6615 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006616 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6617 backlight_binding, ec);
6618 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6619 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006620
6621 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006622 if (shell->exposay_modifier)
6623 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6624 exposay_binding, shell);
6625
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006626 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006627 if (!mod)
6628 return;
6629
Daniel Stone325fc2d2012-05-30 16:31:58 +01006630 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6631 zoom_key_binding, NULL);
6632 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6633 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006634 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6635 maximize_binding, NULL);
6636 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6637 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006638 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6639 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006640 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006641 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006642 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006643 weston_compositor_add_button_binding(ec, BTN_LEFT,
6644 mod | MODIFIER_SHIFT,
6645 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006646
6647 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006648 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006649 rotate_binding, NULL);
6650
Daniel Stone325fc2d2012-05-30 16:31:58 +01006651 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6652 shell);
6653 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6654 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006655 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6656 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006657 weston_compositor_add_key_binding(ec, KEY_K, mod,
6658 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006659 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6660 workspace_up_binding, shell);
6661 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6662 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006663 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6664 workspace_move_surface_up_binding,
6665 shell);
6666 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6667 workspace_move_surface_down_binding,
6668 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006669
6670 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6671 if (shell->workspaces.num > 1) {
6672 num_workspace_bindings = shell->workspaces.num;
6673 if (num_workspace_bindings > 6)
6674 num_workspace_bindings = 6;
6675 for (i = 0; i < num_workspace_bindings; i++)
6676 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6677 workspace_f_binding,
6678 shell);
6679 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006680
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006681 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006682}
6683
Jason Ekstrand024177c2014-04-21 19:42:58 -05006684static void
6685handle_seat_created(struct wl_listener *listener, void *data)
6686{
6687 struct weston_seat *seat = data;
6688
6689 create_shell_seat(seat);
6690}
6691
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006692WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006693module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006694 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006695{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006696 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006697 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006698 struct workspace **pws;
6699 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006700 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006701
Peter Huttererf3d62272013-08-08 11:57:05 +10006702 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006703 if (shell == NULL)
6704 return -1;
6705
Kristian Høgsberg75840622011-09-06 13:48:16 -04006706 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006707
6708 shell->destroy_listener.notify = shell_destroy;
6709 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006710 shell->idle_listener.notify = idle_handler;
6711 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6712 shell->wake_listener.notify = wake_handler;
6713 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006714 shell->transform_listener.notify = transform_handler;
6715 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006716
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006717 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006718 ec->shell_interface.create_shell_surface = create_shell_surface;
6719 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006720 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006721 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006722 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006723 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006724 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006725 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006726 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006727 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006728 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006729 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006730
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006731 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6732 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006733 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6734 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006735 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006736
6737 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006738 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006739
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006740 if (input_panel_setup(shell) < 0)
6741 return -1;
6742
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006743 shell->text_backend = text_backend_init(ec);
6744 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006745 return -1;
6746
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006747 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006748
Daniel Stonedf8133b2013-11-19 11:37:14 +01006749 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6750 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6751
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006752 for (i = 0; i < shell->workspaces.num; i++) {
6753 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6754 if (pws == NULL)
6755 return -1;
6756
6757 *pws = workspace_create();
6758 if (*pws == NULL)
6759 return -1;
6760 }
6761 activate_workspace(shell, 0);
6762
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006763 weston_layer_init(&shell->minimized_layer, NULL);
6764
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006765 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006766 wl_list_init(&shell->workspaces.animation.link);
6767 shell->workspaces.animation.frame = animate_workspace_change_frame;
6768
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006769 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006770 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006771 return -1;
6772
Rafael Antognollie2a34552013-12-03 15:35:45 -02006773 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6774 shell, bind_xdg_shell) == NULL)
6775 return -1;
6776
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006777 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006778 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006779 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006780 return -1;
6781
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006782 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006783
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006784 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006785
Xiong Zhang6b481422013-10-23 13:58:32 +08006786 setup_output_destroy_handler(ec, shell);
6787
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006788 loop = wl_display_get_event_loop(ec->wl_display);
6789 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006790
Jason Ekstrand024177c2014-04-21 19:42:58 -05006791 wl_list_for_each(seat, &ec->seat_list, link)
6792 handle_seat_created(NULL, seat);
6793 shell->seat_create_listener.notify = handle_seat_created;
6794 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006795
David Fort50d962f2016-06-09 17:55:52 +02006796 shell->resized_listener.notify = handle_output_resized;
6797 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6798
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006799 screenshooter_create(ec);
6800
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006801 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006802
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006803 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006804
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006805 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6806
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006807 return 0;
6808}