blob: c72f801c3b38b48656b1804bd2ee1630d88e6e93 [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{
2775 struct weston_surface *fs_surface = surface->configure_private;
2776 int n;
2777 int rem;
2778 int ret;
2779
2780 n = snprintf(buf, len, "black background surface for ");
2781 if (n < 0)
2782 return n;
2783
2784 rem = (int)len - n;
2785 if (rem < 0)
2786 rem = 0;
2787
2788 if (fs_surface->get_label)
2789 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2790 else
2791 ret = snprintf(buf + n, rem, "<unknown>");
2792
2793 if (ret < 0)
2794 return n;
2795
2796 return n + ret;
2797}
2798
Alex Wu21858432012-04-01 20:13:08 +08002799static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002800black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002801
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002803create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002804 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002805 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002806{
2807 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002808 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002809
2810 surface = weston_surface_create(ec);
2811 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002812 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002813 return NULL;
2814 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002815 view = weston_view_create(surface);
2816 if (surface == NULL) {
2817 weston_log("no memory\n");
2818 weston_surface_destroy(surface);
2819 return NULL;
2820 }
Alex Wu4539b082012-03-01 12:57:46 +08002821
Alex Wu21858432012-04-01 20:13:08 +08002822 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002823 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002824 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002825 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002826 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002827 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002828 pixman_region32_fini(&surface->input);
2829 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002830
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002831 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002832 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833
2834 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002835}
2836
Philip Withnalled8f1a92013-11-25 18:01:43 +00002837static void
2838shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2839{
2840 struct weston_output *output = shsurf->fullscreen_output;
2841
Rafael Antognolli03b16592013-12-03 15:35:42 -02002842 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002843
2844 if (!shsurf->fullscreen.black_view)
2845 shsurf->fullscreen.black_view =
2846 create_black_surface(shsurf->surface->compositor,
2847 shsurf->surface,
2848 output->x, output->y,
2849 output->width,
2850 output->height);
2851
2852 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002853 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2854 weston_layer_entry_insert(&shsurf->view->layer_link,
2855 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002856 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2857 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002858
Armin Krezović4663aca2016-06-30 06:04:29 +02002859 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002860 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002861}
2862
Alex Wu4539b082012-03-01 12:57:46 +08002863/* Create black surface and append it to the associated fullscreen surface.
2864 * Handle size dismatch and positioning according to the method. */
2865static void
2866shell_configure_fullscreen(struct shell_surface *shsurf)
2867{
2868 struct weston_output *output = shsurf->fullscreen_output;
2869 struct weston_surface *surface = shsurf->surface;
2870 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002871 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002872 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002873
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002874 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2875 restore_output_mode(output);
2876
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002877 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002878 weston_layer_entry_remove(&shsurf->view->layer_link);
2879 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002880
Philip Withnalled8f1a92013-11-25 18:01:43 +00002881 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002882
Jason Ekstranda7af7042013-10-12 22:38:11 -05002883 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002884 &surf_width, &surf_height);
2885
Alex Wu4539b082012-03-01 12:57:46 +08002886 switch (shsurf->fullscreen.type) {
2887 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002888 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002890 break;
2891 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002892 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002893 if (output->width == surf_width &&
2894 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002895 weston_view_set_position(shsurf->view,
2896 output->x - surf_x,
2897 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002898 break;
2899 }
2900
Alex Wu4539b082012-03-01 12:57:46 +08002901 matrix = &shsurf->fullscreen.transform.matrix;
2902 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002903
Scott Moreau1bad5db2012-08-18 01:04:05 -06002904 output_aspect = (float) output->width /
2905 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002906 /* XXX: Use surf_width and surf_height here? */
2907 surface_aspect = (float) surface->width /
2908 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002909 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002910 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002911 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002912 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002913 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002914 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002915
Alex Wu4539b082012-03-01 12:57:46 +08002916 weston_matrix_scale(matrix, scale, scale, 1);
2917 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002918 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002919 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002920 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2921 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002922 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002923
Alex Wu4539b082012-03-01 12:57:46 +08002924 break;
2925 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002926 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002927 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002928 surf_width * surface->buffer_viewport.buffer.scale,
2929 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002930 shsurf->fullscreen.framerate};
2931
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002932 if (weston_output_mode_switch_to_temporary(output, &mode,
2933 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 weston_view_set_position(shsurf->view,
2935 output->x - surf_x,
2936 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002937 shsurf->fullscreen.black_view->surface->width = output->width;
2938 shsurf->fullscreen.black_view->surface->height = output->height;
2939 weston_view_set_position(shsurf->fullscreen.black_view,
2940 output->x - surf_x,
2941 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002942 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002943 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002944 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002945 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002946 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002947 }
Alex Wubd3354b2012-04-17 17:20:49 +08002948 }
Alex Wu4539b082012-03-01 12:57:46 +08002949 break;
2950 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002951 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002952 break;
2953 default:
2954 break;
2955 }
2956}
2957
Alex Wu4539b082012-03-01 12:57:46 +08002958static void
2959shell_map_fullscreen(struct shell_surface *shsurf)
2960{
Alex Wubd3354b2012-04-17 17:20:49 +08002961 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002962}
2963
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002964static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002965set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2966{
2967 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002968 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002969 shsurf->transient.x = x;
2970 shsurf->transient.y = y;
2971 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002972
2973 shell_surface_set_parent(shsurf, NULL);
2974
Pekka Paalanenb5026542014-11-12 15:09:24 +02002975 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002976 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002977}
2978
Kristian Høgsberg47792412014-05-04 13:47:06 -07002979static void
2980shell_interface_set_fullscreen(struct shell_surface *shsurf,
2981 uint32_t method,
2982 uint32_t framerate,
2983 struct weston_output *output)
2984{
2985 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002986 shsurf->next_state.fullscreen = true;
2987 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002988
2989 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002990}
2991
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002992static struct weston_output *
2993get_focused_output(struct weston_compositor *compositor)
2994{
2995 struct weston_seat *seat;
2996 struct weston_output *output = NULL;
2997
2998 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002999 struct weston_touch *touch = weston_seat_get_touch(seat);
3000 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3001 struct weston_keyboard *keyboard =
3002 weston_seat_get_keyboard(seat);
3003
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003004 /* Priority has touch focus, then pointer and
3005 * then keyboard focus. We should probably have
3006 * three for loops and check frist for touch,
3007 * then for pointer, etc. but unless somebody has some
3008 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003009 if (touch && touch->focus)
3010 output = touch->focus->output;
3011 else if (pointer && pointer->focus)
3012 output = pointer->focus->output;
3013 else if (keyboard && keyboard->focus)
3014 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003015
3016 if (output)
3017 break;
3018 }
3019
3020 return output;
3021}
3022
3023static void
3024shell_interface_set_maximized(struct shell_surface *shsurf)
3025{
3026 struct weston_output *output;
3027
3028 surface_clear_next_states(shsurf);
3029 shsurf->next_state.maximized = true;
3030 shsurf->state_changed = true;
3031 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3032
3033 if (!weston_surface_is_mapped(shsurf->surface))
3034 output = get_focused_output(shsurf->surface->compositor);
3035 else
3036 output = shsurf->surface->output;
3037
3038 shell_surface_set_output(shsurf, output);
3039 send_configure_for_surface(shsurf);
3040}
3041
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003042static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003043shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003044{
Derek Foremane4d6c832015-08-05 14:48:11 -05003045 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003046}
3047
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003048static int
3049shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003050 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003051 uint32_t edges)
3052{
Derek Foremane4d6c832015-08-05 14:48:11 -05003053 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003054}
3055
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003056static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003057
3058static void
3059destroy_shell_seat(struct wl_listener *listener, void *data)
3060{
3061 struct shell_seat *shseat =
3062 container_of(listener,
3063 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003064 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065
3066 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003067 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003068 shseat->popup_grab.client = NULL;
3069
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003070 wl_list_for_each_safe(shsurf, next,
3071 &shseat->popup_grab.surfaces_list,
3072 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003074 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003075 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076 }
3077
3078 wl_list_remove(&shseat->seat_destroy_listener.link);
3079 free(shseat);
3080}
3081
Jason Ekstrand024177c2014-04-21 19:42:58 -05003082static void
3083shell_seat_caps_changed(struct wl_listener *listener, void *data)
3084{
Derek Foreman1281a362015-07-31 16:55:32 -05003085 struct weston_keyboard *keyboard;
3086 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003087 struct shell_seat *seat;
3088
3089 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003090 keyboard = weston_seat_get_keyboard(seat->seat);
3091 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003092
Derek Foreman1281a362015-07-31 16:55:32 -05003093 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003094 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003095 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003096 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003097 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003098 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003099 wl_list_init(&seat->keyboard_focus_listener.link);
3100 }
3101
Derek Foreman1281a362015-07-31 16:55:32 -05003102 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003103 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003104 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003105 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003106 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003107 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003108 wl_list_init(&seat->pointer_focus_listener.link);
3109 }
3110}
3111
Giulio Camuffo5085a752013-03-25 21:42:45 +01003112static struct shell_seat *
3113create_shell_seat(struct weston_seat *seat)
3114{
3115 struct shell_seat *shseat;
3116
3117 shseat = calloc(1, sizeof *shseat);
3118 if (!shseat) {
3119 weston_log("no memory to allocate shell seat\n");
3120 return NULL;
3121 }
3122
3123 shseat->seat = seat;
3124 wl_list_init(&shseat->popup_grab.surfaces_list);
3125
3126 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3127 wl_signal_add(&seat->destroy_signal,
3128 &shseat->seat_destroy_listener);
3129
Jason Ekstrand024177c2014-04-21 19:42:58 -05003130 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3131 wl_list_init(&shseat->keyboard_focus_listener.link);
3132
3133 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3134 wl_list_init(&shseat->pointer_focus_listener.link);
3135
3136 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3137 wl_signal_add(&seat->updated_caps_signal,
3138 &shseat->caps_changed_listener);
3139 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3140
Giulio Camuffo5085a752013-03-25 21:42:45 +01003141 return shseat;
3142}
3143
3144static struct shell_seat *
3145get_shell_seat(struct weston_seat *seat)
3146{
3147 struct wl_listener *listener;
3148
3149 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003150 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003151
3152 return container_of(listener,
3153 struct shell_seat, seat_destroy_listener);
3154}
3155
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003156static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003157popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003158{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003159 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003161 struct shell_seat *shseat =
3162 container_of(grab, struct shell_seat, popup_grab.grab);
3163 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003164 wl_fixed_t sx, sy;
3165
Jason Ekstranda7af7042013-10-12 22:38:11 -05003166 view = weston_compositor_pick_view(pointer->seat->compositor,
3167 pointer->x, pointer->y,
3168 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003169
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 if (view && view->surface->resource &&
3171 wl_resource_get_client(view->surface->resource) == client) {
3172 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003173 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003174 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175 }
3176}
3177
3178static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003179popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003180 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003182 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003183 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003184 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003185 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003186 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003187
Jonas Ådahl61917c82014-08-11 22:44:02 +02003188 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003189 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003190 weston_view_from_global_fixed(pointer->focus, x, y,
3191 &pointer->sx, &pointer->sy);
3192 }
3193
Jonas Ådahld2510102014-10-05 21:39:14 +02003194 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003195
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003196 if (!pointer->focus_client)
3197 return;
3198
3199 resource_list = &pointer->focus_client->pointer_resources;
3200 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003201 weston_view_from_global_fixed(pointer->focus,
3202 pointer->x, pointer->y,
3203 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003204 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003205 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003206}
3207
3208static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003209popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003210 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003211{
3212 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003213 struct shell_seat *shseat =
3214 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003215 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003216 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003217 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003218 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003219
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003220 if (grab->pointer->focus_client)
3221 resource_list = &grab->pointer->focus_client->pointer_resources;
3222 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003223 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003224 wl_resource_for_each(resource, resource_list) {
3225 wl_pointer_send_button(resource, serial,
3226 time, button, state);
3227 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003228 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003229 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003230 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003231 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003232 }
3233
Daniel Stone4dbadb12012-05-30 16:31:51 +01003234 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003235 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003236}
3237
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003238static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003239popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003240 uint32_t time,
3241 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003242{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003243 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003244}
3245
3246static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003247popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3248{
3249 weston_pointer_send_axis_source(grab->pointer, source);
3250}
3251
3252static void
3253popup_grab_frame(struct weston_pointer_grab *grab)
3254{
3255 weston_pointer_send_frame(grab->pointer);
3256}
3257
3258static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003259popup_grab_cancel(struct weston_pointer_grab *grab)
3260{
3261 popup_grab_end(grab->pointer);
3262}
3263
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003264static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003265 popup_grab_focus,
3266 popup_grab_motion,
3267 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003268 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003269 popup_grab_axis_source,
3270 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003271 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003272};
3273
3274static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003275touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003276 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003277{
3278 struct wl_resource *resource;
3279 struct shell_seat *shseat =
3280 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3281 struct wl_display *display = shseat->seat->compositor->wl_display;
3282 uint32_t serial;
3283 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003284 wl_fixed_t sx, sy;
3285
3286 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003287
3288 resource_list = &grab->touch->focus_resource_list;
3289 if (!wl_list_empty(resource_list)) {
3290 serial = wl_display_get_serial(display);
3291 wl_resource_for_each(resource, resource_list) {
3292 wl_touch_send_down(resource, serial, time,
3293 grab->touch->focus->surface->resource,
3294 touch_id, sx, sy);
3295 }
3296 }
3297}
3298
3299static void
3300touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3301{
3302 struct wl_resource *resource;
3303 struct shell_seat *shseat =
3304 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3305 struct wl_display *display = shseat->seat->compositor->wl_display;
3306 uint32_t serial;
3307 struct wl_list *resource_list;
3308
3309 resource_list = &grab->touch->focus_resource_list;
3310 if (!wl_list_empty(resource_list)) {
3311 serial = wl_display_get_serial(display);
3312 wl_resource_for_each(resource, resource_list) {
3313 wl_touch_send_up(resource, serial, time, touch_id);
3314 }
3315 }
3316}
3317
3318static void
3319touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003320 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003321{
3322 struct wl_resource *resource;
3323 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003324 wl_fixed_t sx, sy;
3325
3326 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003327
3328 resource_list = &grab->touch->focus_resource_list;
3329 if (!wl_list_empty(resource_list)) {
3330 wl_resource_for_each(resource, resource_list) {
3331 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3332 }
3333 }
3334}
3335
3336static void
3337touch_popup_grab_frame(struct weston_touch_grab *grab)
3338{
3339}
3340
3341static void
3342touch_popup_grab_cancel(struct weston_touch_grab *grab)
3343{
3344 touch_popup_grab_end(grab->touch);
3345}
3346
3347static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3348 touch_popup_grab_down,
3349 touch_popup_grab_up,
3350 touch_popup_grab_motion,
3351 touch_popup_grab_frame,
3352 touch_popup_grab_cancel,
3353};
3354
3355static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003356shell_surface_send_popup_done(struct shell_surface *shsurf)
3357{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003358 if (shsurf->resource == NULL)
3359 return;
3360 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003361 wl_shell_surface_send_popup_done(shsurf->resource);
3362 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003363 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003364}
3365
3366static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003367popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003368{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003369 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003370 struct shell_seat *shseat =
3371 container_of(grab, struct shell_seat, popup_grab.grab);
3372 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003373 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003374
3375 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003376 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003377 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003378 shseat->popup_grab.grab.interface = NULL;
3379 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003380 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003381 wl_list_for_each_safe(shsurf, next,
3382 &shseat->popup_grab.surfaces_list,
3383 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003384 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003385 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003386 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003387 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003388 wl_list_init(&shseat->popup_grab.surfaces_list);
3389 }
3390}
3391
3392static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003393touch_popup_grab_end(struct weston_touch *touch)
3394{
3395 struct weston_touch_grab *grab = touch->grab;
3396 struct shell_seat *shseat =
3397 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3398 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003399 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003400
3401 if (touch->grab->interface == &touch_popup_grab_interface) {
3402 weston_touch_end_grab(grab->touch);
3403 shseat->popup_grab.client = NULL;
3404 shseat->popup_grab.touch_grab.interface = NULL;
3405 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3406 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003407 wl_list_for_each_safe(shsurf, next,
3408 &shseat->popup_grab.surfaces_list,
3409 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003410 shell_surface_send_popup_done(shsurf);
3411 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003412 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003413 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003414 wl_list_init(&shseat->popup_grab.surfaces_list);
3415 }
3416}
3417
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003418static struct shell_surface *
3419get_top_popup(struct shell_seat *shseat)
3420{
3421 struct shell_surface *shsurf;
3422
3423 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3424 return NULL;
3425 } else {
3426 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3427 struct shell_surface,
3428 popup.grab_link);
3429 return shsurf;
3430 }
3431}
3432
3433static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003434add_popup_grab(struct shell_surface *shsurf,
3435 struct shell_seat *shseat,
3436 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003437{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003438 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003439 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003440 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3441 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003442
3443 parent = get_shell_surface(shsurf->parent);
3444 top_surface = get_top_popup(shseat);
3445 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003446 (!parent ||
3447 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003448 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003449 wl_resource_post_error(shsurf->owner_resource,
3450 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003451 "xdg_popup was not created on the "
3452 "topmost popup");
3453 return -1;
3454 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003455
3456 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003457 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003458 shseat->popup_grab.client =
3459 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003460
3461 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003462 shseat->popup_grab.grab.interface =
3463 &popup_grab_interface;
3464
3465 /* We must make sure here that this popup was opened
3466 * after a mouse press, and not just by moving around
3467 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003468 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003469 shseat->popup_grab.initial_up = 0;
3470 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003471 shseat->popup_grab.touch_grab.interface =
3472 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003473 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003474
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003475 wl_list_insert(&shseat->popup_grab.surfaces_list,
3476 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003477
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003478 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003479 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003480 &shseat->popup_grab.grab);
3481 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003482 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003483 &shseat->popup_grab.touch_grab);
3484 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003485 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003486 wl_list_insert(&shseat->popup_grab.surfaces_list,
3487 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003488 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003489
3490 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003491}
3492
3493static void
3494remove_popup_grab(struct shell_surface *shsurf)
3495{
3496 struct shell_seat *shseat = shsurf->popup.shseat;
3497
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003498 if (shell_surface_is_xdg_popup(shsurf) &&
3499 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003500 wl_resource_post_error(shsurf->owner_resource,
3501 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003502 "xdg_popup was destroyed while it was "
3503 "not the topmost popup.");
3504 return;
3505 }
3506
Giulio Camuffo5085a752013-03-25 21:42:45 +01003507 wl_list_remove(&shsurf->popup.grab_link);
3508 wl_list_init(&shsurf->popup.grab_link);
3509 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003510 if (shseat->popup_grab.type == POINTER) {
3511 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3512 shseat->popup_grab.grab.interface = NULL;
3513 } else if (shseat->popup_grab.type == TOUCH) {
3514 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3515 shseat->popup_grab.touch_grab.interface = NULL;
3516 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003517 }
3518}
3519
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003520static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003521shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003522{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003523 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003524 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003525 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3526 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003527
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 shsurf->surface->output = parent_view->output;
Armin Krezović4663aca2016-06-30 06:04:29 +02003529 shsurf->surface->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003530 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003531
Jason Ekstranda7af7042013-10-12 22:38:11 -05003532 weston_view_set_transform_parent(shsurf->view, parent_view);
3533 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3534 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003535 shsurf->view->is_mapped = true;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003536
Derek Foreman1281a362015-07-31 16:55:32 -05003537 if (pointer &&
3538 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003539 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3540 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003541 } else if (touch &&
3542 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003543 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3544 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003545 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003546 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003547 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003548 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003549
3550 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003551}
3552
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003553static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003554 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003555 shell_surface_move,
3556 shell_surface_resize,
3557 shell_surface_set_toplevel,
3558 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003559 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003560 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003561 shell_surface_set_maximized,
3562 shell_surface_set_title,
3563 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003564};
3565
3566static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003567destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003568{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003569 struct shell_surface *child, *next;
3570
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003571 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3572
Giulio Camuffo5085a752013-03-25 21:42:45 +01003573 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3574 remove_popup_grab(shsurf);
3575 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003576
Alex Wubd3354b2012-04-17 17:20:49 +08003577 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003578 shell_surface_is_top_fullscreen(shsurf))
3579 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003580
Jason Ekstranda7af7042013-10-12 22:38:11 -05003581 if (shsurf->fullscreen.black_view)
3582 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003583
Alex Wubd3354b2012-04-17 17:20:49 +08003584 /* As destroy_resource() use wl_list_for_each_safe(),
3585 * we can always remove the listener.
3586 */
3587 wl_list_remove(&shsurf->surface_destroy_listener.link);
3588 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003589 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003590 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003591
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 weston_view_destroy(shsurf->view);
3593
Philip Withnall648a4dd2013-11-25 18:01:44 +00003594 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003595 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3596 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003597
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003598 wl_list_remove(&shsurf->link);
3599 free(shsurf);
3600}
3601
3602static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003603shell_destroy_shell_surface(struct wl_resource *resource)
3604{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003605 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003606
Bryan Caina46b9462014-04-04 17:41:24 -05003607 if (!wl_list_empty(&shsurf->popup.grab_link))
3608 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003609 if (shsurf->resource)
3610 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003611 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003612}
3613
3614static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003615shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003616{
3617 struct shell_surface *shsurf = container_of(listener,
3618 struct shell_surface,
3619 surface_destroy_listener);
3620
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003621 if (shsurf->resource)
3622 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003623
3624 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003625}
3626
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003627static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003628fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003629{
3630 struct shell_surface *shsurf = data;
3631
3632 weston_surface_destroy(shsurf->surface);
3633}
3634
3635static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003636fade_out_done(struct weston_view_animation *animation, void *data)
3637{
3638 struct shell_surface *shsurf = data;
3639 struct wl_event_loop *loop;
3640
3641 loop = wl_display_get_event_loop(
3642 shsurf->surface->compositor->wl_display);
3643
3644 if (!shsurf->destroying) {
3645 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3646 shsurf->destroying = true;
3647 }
3648}
3649
3650static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003651handle_resource_destroy(struct wl_listener *listener, void *data)
3652{
3653 struct shell_surface *shsurf =
3654 container_of(listener, struct shell_surface,
3655 resource_destroy_listener);
3656
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003657 if (!weston_surface_is_mapped(shsurf->surface))
3658 return;
3659
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003660 shsurf->surface->ref_count++;
3661
3662 pixman_region32_fini(&shsurf->surface->pending.input);
3663 pixman_region32_init(&shsurf->surface->pending.input);
3664 pixman_region32_fini(&shsurf->surface->input);
3665 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003666 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3667 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3668 fade_out_done, shsurf);
3669 } else {
3670 weston_surface_destroy(shsurf->surface);
3671 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003672}
3673
3674static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003675shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003676
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003677struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003678get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003679{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003680 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003681 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003682 else
3683 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003684}
3685
Rafael Antognollie2a34552013-12-03 15:35:45 -02003686static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003687create_common_surface(struct shell_client *owner, void *shell,
3688 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003689 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003690{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003691 struct shell_surface *shsurf;
3692
Pekka Paalanen50b67472014-10-01 15:02:41 +03003693 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003694
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003695 shsurf = calloc(1, sizeof *shsurf);
3696 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003697 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003698 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003699 }
3700
Jason Ekstranda7af7042013-10-12 22:38:11 -05003701 shsurf->view = weston_view_create(surface);
3702 if (!shsurf->view) {
3703 weston_log("no memory to allocate shell surface\n");
3704 free(shsurf);
3705 return NULL;
3706 }
3707
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003708 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003709 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003710 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003711
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003712 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3713 wl_resource_add_destroy_listener(surface->resource,
3714 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003715 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003716
Tiago Vignattibc052c92012-04-19 16:18:18 +03003717 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003718 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003719 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003720 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003721 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003722 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3723 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003725 wl_list_init(&shsurf->fullscreen.transform.link);
3726
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003727 shsurf->output = get_default_output(shsurf->shell->compositor);
3728
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003729 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003730 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003731 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003732 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003733
3734 /* init link so its safe to always remove it in destroy_shell_surface */
3735 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003736 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003737
Pekka Paalanen460099f2012-01-20 16:48:25 +02003738 /* empty when not in use */
3739 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003740 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003741
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003742 wl_list_init(&shsurf->workspace_transform.link);
3743
Philip Withnall648a4dd2013-11-25 18:01:44 +00003744 wl_list_init(&shsurf->children_link);
3745 wl_list_init(&shsurf->children_list);
3746 shsurf->parent = NULL;
3747
Pekka Paalanenb5026542014-11-12 15:09:24 +02003748 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003749
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003750 shsurf->client = client;
3751
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003752 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003753}
3754
Rafael Antognollie2a34552013-12-03 15:35:45 -02003755static struct shell_surface *
3756create_shell_surface(void *shell, struct weston_surface *surface,
3757 const struct weston_shell_client *client)
3758{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003759 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003760}
3761
Tiago Vignattibc052c92012-04-19 16:18:18 +03003762static void
3763shell_get_shell_surface(struct wl_client *client,
3764 struct wl_resource *resource,
3765 uint32_t id,
3766 struct wl_resource *surface_resource)
3767{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003768 struct weston_surface *surface =
3769 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003770 struct shell_client *sc = wl_resource_get_user_data(resource);
3771 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003772 struct shell_surface *shsurf;
3773
Pekka Paalanen50b67472014-10-01 15:02:41 +03003774 if (weston_surface_set_role(surface, "wl_shell_surface",
3775 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003776 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003777
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003778 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003779 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003780 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003781 return;
3782 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003783
Jason Ekstranda85118c2013-06-27 20:17:02 -05003784 shsurf->resource =
3785 wl_resource_create(client,
3786 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003787 if (!shsurf->resource) {
3788 wl_resource_post_no_memory(surface_resource);
3789 return;
3790 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003791 wl_resource_set_implementation(shsurf->resource,
3792 &shell_surface_implementation,
3793 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003794 wl_list_insert(&sc->surface_list,
3795 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003796}
3797
Rafael Antognollie2a34552013-12-03 15:35:45 -02003798static bool
3799shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3800{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003801 /* A shell surface without a resource is created from xwayland
3802 * and is considered a wl_shell surface for now. */
3803
3804 return shsurf->resource == NULL ||
3805 wl_resource_instance_of(shsurf->resource,
3806 &wl_shell_surface_interface,
3807 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003808}
3809
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003810static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003811 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003812};
3813
Rafael Antognollie2a34552013-12-03 15:35:45 -02003814/****************************
3815 * xdg-shell implementation */
3816
3817static void
3818xdg_surface_destroy(struct wl_client *client,
3819 struct wl_resource *resource)
3820{
3821 wl_resource_destroy(resource);
3822}
3823
3824static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003825xdg_surface_set_parent(struct wl_client *client,
3826 struct wl_resource *resource,
3827 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003828{
3829 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003830 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003831
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003832 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003833 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003834 shell_surface_set_parent(shsurf, parent->surface);
3835 } else {
3836 shell_surface_set_parent(shsurf, NULL);
3837 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003838}
3839
3840static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003841xdg_surface_set_app_id(struct wl_client *client,
3842 struct wl_resource *resource,
3843 const char *app_id)
3844{
3845 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3846
3847 free(shsurf->class);
3848 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003849 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003850}
3851
3852static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003853xdg_surface_show_window_menu(struct wl_client *client,
3854 struct wl_resource *surface_resource,
3855 struct wl_resource *seat_resource,
3856 uint32_t serial,
3857 int32_t x,
3858 int32_t y)
3859{
3860 /* TODO */
3861}
3862
3863static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003864xdg_surface_set_title(struct wl_client *client,
3865 struct wl_resource *resource, const char *title)
3866{
3867 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3868
3869 set_title(shsurf, title);
3870}
3871
3872static void
3873xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3874 struct wl_resource *seat_resource, uint32_t serial)
3875{
3876 common_surface_move(resource, seat_resource, serial);
3877}
3878
3879static void
3880xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3881 struct wl_resource *seat_resource, uint32_t serial,
3882 uint32_t edges)
3883{
3884 common_surface_resize(resource, seat_resource, serial, edges);
3885}
3886
3887static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003888xdg_surface_ack_configure(struct wl_client *client,
3889 struct wl_resource *resource,
3890 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003891{
3892 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3893
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003894 if (shsurf->state_requested) {
3895 shsurf->next_state = shsurf->requested_state;
3896 shsurf->state_changed = true;
3897 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003898 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003899}
3900
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003901static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003902xdg_surface_set_window_geometry(struct wl_client *client,
3903 struct wl_resource *resource,
3904 int32_t x,
3905 int32_t y,
3906 int32_t width,
3907 int32_t height)
3908{
3909 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3910
3911 set_window_geometry(shsurf, x, y, width, height);
3912}
3913
3914static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003915xdg_surface_set_maximized(struct wl_client *client,
3916 struct wl_resource *resource)
3917{
3918 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003919 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003920
3921 shsurf->state_requested = true;
3922 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003923
3924 if (!weston_surface_is_mapped(shsurf->surface))
3925 output = get_focused_output(shsurf->surface->compositor);
3926 else
3927 output = shsurf->surface->output;
3928
3929 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003930 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003931}
3932
3933static void
3934xdg_surface_unset_maximized(struct wl_client *client,
3935 struct wl_resource *resource)
3936{
3937 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3938
3939 shsurf->state_requested = true;
3940 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003941 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003942}
3943
3944static void
3945xdg_surface_set_fullscreen(struct wl_client *client,
3946 struct wl_resource *resource,
3947 struct wl_resource *output_resource)
3948{
3949 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3950 struct weston_output *output;
3951
3952 shsurf->state_requested = true;
3953 shsurf->requested_state.fullscreen = true;
3954
3955 if (output_resource)
3956 output = wl_resource_get_user_data(output_resource);
3957 else
3958 output = NULL;
3959
Marek Chalupa052917a2014-09-02 11:35:12 +02003960 /* handle clients launching in fullscreen */
3961 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3962 /* Set the output to the one that has focus currently. */
3963 assert(shsurf->surface);
3964 output = get_focused_output(shsurf->surface->compositor);
3965 }
3966
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003967 shell_surface_set_output(shsurf, output);
3968 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003969
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003970 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003971}
3972
3973static void
3974xdg_surface_unset_fullscreen(struct wl_client *client,
3975 struct wl_resource *resource)
3976{
3977 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3978
3979 shsurf->state_requested = true;
3980 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003981 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003982}
3983
3984static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003985xdg_surface_set_minimized(struct wl_client *client,
3986 struct wl_resource *resource)
3987{
3988 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3989
3990 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3991 return;
3992
3993 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003994 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003995}
3996
Rafael Antognollie2a34552013-12-03 15:35:45 -02003997static const struct xdg_surface_interface xdg_surface_implementation = {
3998 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003999 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004000 xdg_surface_set_title,
4001 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004002 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004003 xdg_surface_move,
4004 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004005 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004006 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004007 xdg_surface_set_maximized,
4008 xdg_surface_unset_maximized,
4009 xdg_surface_set_fullscreen,
4010 xdg_surface_unset_fullscreen,
4011 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004012};
4013
4014static void
4015xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004016 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004017{
4018 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004019 uint32_t *s;
4020 struct wl_array states;
4021 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004022
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004023 assert(shsurf);
4024
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004025 if (!shsurf->resource)
4026 return;
4027
4028 wl_array_init(&states);
4029 if (shsurf->requested_state.fullscreen) {
4030 s = wl_array_add(&states, sizeof *s);
4031 *s = XDG_SURFACE_STATE_FULLSCREEN;
4032 } else if (shsurf->requested_state.maximized) {
4033 s = wl_array_add(&states, sizeof *s);
4034 *s = XDG_SURFACE_STATE_MAXIMIZED;
4035 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004036 if (shsurf->resize_edges != 0) {
4037 s = wl_array_add(&states, sizeof *s);
4038 *s = XDG_SURFACE_STATE_RESIZING;
4039 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004040 if (shsurf->focus_count > 0) {
4041 s = wl_array_add(&states, sizeof *s);
4042 *s = XDG_SURFACE_STATE_ACTIVATED;
4043 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004044
4045 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4046 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4047
4048 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004049}
4050
4051static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004052 xdg_send_configure,
4053 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004054};
4055
4056static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004057xdg_shell_destroy(struct wl_client *client,
4058 struct wl_resource *resource)
4059{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004060 struct shell_client *sc = wl_resource_get_user_data(resource);
4061 struct wl_resource *shsurf_resource;
4062 struct shell_surface *shsurf;
4063
4064 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4065 shsurf = wl_resource_get_user_data(shsurf_resource);
4066 if (shsurf->owner_resource == resource) {
4067 wl_resource_post_error(
4068 resource,
4069 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4070 "not all child surface objects destroyed");
4071 return;
4072 }
4073 }
4074
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004075 wl_resource_destroy(resource);
4076}
4077
4078static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004079xdg_use_unstable_version(struct wl_client *client,
4080 struct wl_resource *resource,
4081 int32_t version)
4082{
4083 if (version > 1) {
4084 wl_resource_post_error(resource,
4085 1,
4086 "xdg-shell:: version not implemented yet.");
4087 return;
4088 }
4089}
4090
4091static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004092create_xdg_surface(struct shell_client *owner, void *shell,
4093 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004094 const struct weston_shell_client *client)
4095{
4096 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004097
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004098 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004099 if (!shsurf)
4100 return NULL;
4101
Pekka Paalanenb5026542014-11-12 15:09:24 +02004102 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004103
4104 return shsurf;
4105}
4106
4107static void
4108xdg_get_xdg_surface(struct wl_client *client,
4109 struct wl_resource *resource,
4110 uint32_t id,
4111 struct wl_resource *surface_resource)
4112{
4113 struct weston_surface *surface =
4114 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004115 struct shell_client *sc = wl_resource_get_user_data(resource);
4116 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004117 struct shell_surface *shsurf;
4118
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004119 shsurf = get_shell_surface(surface);
4120 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4121 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4122 "This wl_surface is already an "
4123 "xdg_surface");
4124 return;
4125 }
4126
Pekka Paalanen50b67472014-10-01 15:02:41 +03004127 if (weston_surface_set_role(surface, "xdg_surface",
4128 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004129 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004130
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004131 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004132 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004133 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004134 return;
4135 }
4136
4137 shsurf->resource =
4138 wl_resource_create(client,
4139 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004140 if (!shsurf->resource) {
4141 wl_resource_post_no_memory(surface_resource);
4142 return;
4143 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004144 wl_resource_set_implementation(shsurf->resource,
4145 &xdg_surface_implementation,
4146 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004147 shsurf->owner_resource = resource;
4148 wl_list_insert(&sc->surface_list,
4149 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004150}
4151
4152static bool
4153shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4154{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004155 return shsurf->resource &&
4156 wl_resource_instance_of(shsurf->resource,
4157 &xdg_surface_interface,
4158 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004159}
4160
4161/* xdg-popup implementation */
4162
4163static void
4164xdg_popup_destroy(struct wl_client *client,
4165 struct wl_resource *resource)
4166{
4167 wl_resource_destroy(resource);
4168}
4169
Rafael Antognollie2a34552013-12-03 15:35:45 -02004170static const struct xdg_popup_interface xdg_popup_implementation = {
4171 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004172};
4173
4174static void
4175xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004176 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004177{
4178}
4179
4180static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004181 xdg_popup_send_configure,
4182 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004183};
4184
4185static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004186create_xdg_popup(struct shell_client *owner, void *shell,
4187 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004188 const struct weston_shell_client *client,
4189 struct weston_surface *parent,
4190 struct shell_seat *seat,
4191 uint32_t serial,
4192 int32_t x, int32_t y)
4193{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004194 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004195
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004196 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004197 if (!shsurf)
4198 return NULL;
4199
Pekka Paalanenb5026542014-11-12 15:09:24 +02004200 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004201 shsurf->popup.shseat = seat;
4202 shsurf->popup.serial = serial;
4203 shsurf->popup.x = x;
4204 shsurf->popup.y = y;
4205 shell_surface_set_parent(shsurf, parent);
4206
4207 return shsurf;
4208}
4209
4210static void
4211xdg_get_xdg_popup(struct wl_client *client,
4212 struct wl_resource *resource,
4213 uint32_t id,
4214 struct wl_resource *surface_resource,
4215 struct wl_resource *parent_resource,
4216 struct wl_resource *seat_resource,
4217 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004218 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004219{
4220 struct weston_surface *surface =
4221 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004222 struct shell_client *sc = wl_resource_get_user_data(resource);
4223 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004224 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004225 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004226 struct weston_surface *parent;
4227 struct shell_seat *seat;
4228
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004229 shsurf = get_shell_surface(surface);
4230 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4231 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4232 "This wl_surface is already an "
4233 "xdg_popup");
4234 return;
4235 }
4236
Pekka Paalanen50b67472014-10-01 15:02:41 +03004237 if (weston_surface_set_role(surface, "xdg_popup",
4238 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004239 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004240
4241 if (!parent_resource) {
4242 wl_resource_post_error(surface_resource,
4243 WL_DISPLAY_ERROR_INVALID_OBJECT,
4244 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004245 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004246 }
4247
4248 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004249 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004250
Jonas Ådahlee45a552015-03-18 16:37:47 +08004251 /* Verify that we are creating the top most popup when mapping,
4252 * as it's not until then we know whether it was mapped as most
4253 * top level or not. */
4254
4255 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004256 if (!parent_shsurf ||
4257 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4258 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004259 wl_resource_post_error(resource,
4260 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4261 "xdg_popup parent was invalid");
4262 return;
4263 }
4264
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004265 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004266 parent, seat, serial, x, y);
4267 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004268 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004269 return;
4270 }
4271
4272 shsurf->resource =
4273 wl_resource_create(client,
4274 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004275 if (!shsurf->resource) {
4276 wl_resource_post_no_memory(surface_resource);
4277 return;
4278 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004279 wl_resource_set_implementation(shsurf->resource,
4280 &xdg_popup_implementation,
4281 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004282 shsurf->owner_resource = resource;
4283 wl_list_insert(&sc->surface_list,
4284 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004285}
4286
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004287static void
4288xdg_pong(struct wl_client *client,
4289 struct wl_resource *resource, uint32_t serial)
4290{
4291 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004292
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004293 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004294}
4295
Rafael Antognollie2a34552013-12-03 15:35:45 -02004296static bool
4297shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4298{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004299 return (shsurf->resource &&
4300 wl_resource_instance_of(shsurf->resource,
4301 &xdg_popup_interface,
4302 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004303}
4304
4305static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004306 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004307 xdg_use_unstable_version,
4308 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004309 xdg_get_xdg_popup,
4310 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004311};
4312
4313static int
4314xdg_shell_unversioned_dispatch(const void *implementation,
4315 void *_target, uint32_t opcode,
4316 const struct wl_message *message,
4317 union wl_argument *args)
4318{
4319 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004320 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004321
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004322 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004323 wl_resource_post_error(resource,
4324 WL_DISPLAY_ERROR_INVALID_OBJECT,
4325 "must call use_unstable_version first");
4326 return 0;
4327 }
4328
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004329#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004330
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004331 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4332 "shell implementation doesn't match protocol version");
4333
Rafael Antognollie2a34552013-12-03 15:35:45 -02004334 if (args[0].i != XDG_SERVER_VERSION) {
4335 wl_resource_post_error(resource,
4336 WL_DISPLAY_ERROR_INVALID_OBJECT,
4337 "incompatible version, server is %d "
4338 "client wants %d",
4339 XDG_SERVER_VERSION, args[0].i);
4340 return 0;
4341 }
4342
4343 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004344 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004345
4346 return 1;
4347}
4348
4349/* end of xdg-shell implementation */
4350/***********************************/
4351
Kristian Høgsberg07937562011-04-12 17:25:42 -04004352static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004353shell_fade(struct desktop_shell *shell, enum fade_type type);
4354
Pekka Paalanen77346a62011-11-30 16:26:35 +02004355static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004356configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004357{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004358 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004359
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004360 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 if (v->output == ev->output && v != ev) {
4362 weston_view_unmap(v);
4363 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004364 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004365 }
4366 }
4367
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004368 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02004369 ev->surface->is_mapped = true;
4370 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004371
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004372 if (wl_list_empty(&ev->layer_link.link)) {
4373 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004375 }
4376}
4377
David Fort50d962f2016-06-09 17:55:52 +02004378
4379static struct shell_output *
4380find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4381{
4382 struct shell_output *shell_output;
4383
4384 wl_list_for_each(shell_output, &shell->output_list, link) {
4385 if (shell_output->output == output)
4386 return shell_output;
4387 }
4388
4389 return NULL;
4390}
4391
Pekka Paalanen8274d902014-08-06 19:36:51 +03004392static int
4393background_get_label(struct weston_surface *surface, char *buf, size_t len)
4394{
4395 return snprintf(buf, len, "background for output %s",
4396 surface->output->name);
4397}
4398
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004399static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004400background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004401{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004402 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004404
Jason Ekstranda7af7042013-10-12 22:38:11 -05004405 view = container_of(es->views.next, struct weston_view, surface_link);
4406
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004407 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004408}
4409
4410static void
David Fort50d962f2016-06-09 17:55:52 +02004411handle_background_surface_destroy(struct wl_listener *listener, void *data)
4412{
4413 struct shell_output *output =
4414 container_of(listener, struct shell_output, background_surface_listener);
4415
4416 weston_log("background surface gone\n");
4417 output->background_surface = NULL;
4418}
4419
4420static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004421desktop_shell_set_background(struct wl_client *client,
4422 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004423 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004424 struct wl_resource *surface_resource)
4425{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004426 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004427 struct weston_surface *surface =
4428 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02004429 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004430 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004431
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004432 if (surface->configure) {
4433 wl_resource_post_error(surface_resource,
4434 WL_DISPLAY_ERROR_INVALID_OBJECT,
4435 "surface role already assigned");
4436 return;
4437 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004438
Jason Ekstranda7af7042013-10-12 22:38:11 -05004439 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4440 weston_view_destroy(view);
4441 view = weston_view_create(surface);
4442
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004443 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004444 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004445 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004446 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004447 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004448 weston_desktop_shell_send_configure(resource, 0,
4449 surface_resource,
4450 surface->output->width,
4451 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004452
4453 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4454 sh_output->background_surface = surface;
4455
4456 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4457 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004458}
4459
Pekka Paalanen8274d902014-08-06 19:36:51 +03004460static int
4461panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4462{
4463 return snprintf(buf, len, "panel for output %s",
4464 surface->output->name);
4465}
4466
Kristian Høgsberg75840622011-09-06 13:48:16 -04004467static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004468panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004469{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004470 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004471 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004472
Jason Ekstranda7af7042013-10-12 22:38:11 -05004473 view = container_of(es->views.next, struct weston_view, surface_link);
4474
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004475 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004476}
4477
4478static void
David Fort50d962f2016-06-09 17:55:52 +02004479handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4480{
4481 struct shell_output *output =
4482 container_of(listener, struct shell_output, panel_surface_listener);
4483
4484 weston_log("panel surface gone\n");
4485 output->panel_surface = NULL;
4486}
4487
4488
4489static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004490desktop_shell_set_panel(struct wl_client *client,
4491 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004492 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004493 struct wl_resource *surface_resource)
4494{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004495 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004496 struct weston_surface *surface =
4497 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004498 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02004499 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004500
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004501 if (surface->configure) {
4502 wl_resource_post_error(surface_resource,
4503 WL_DISPLAY_ERROR_INVALID_OBJECT,
4504 "surface role already assigned");
4505 return;
4506 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004507
Jason Ekstranda7af7042013-10-12 22:38:11 -05004508 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4509 weston_view_destroy(view);
4510 view = weston_view_create(surface);
4511
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004512 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004513 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004514 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004515 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004516 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004517 weston_desktop_shell_send_configure(resource, 0,
4518 surface_resource,
4519 surface->output->width,
4520 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004521
4522 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4523 sh_output->panel_surface = surface;
4524
4525 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4526 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004527}
4528
Pekka Paalanen8274d902014-08-06 19:36:51 +03004529static int
4530lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4531{
4532 return snprintf(buf, len, "lock window");
4533}
4534
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004535static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004536lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004537{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004538 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004539 struct weston_view *view;
4540
4541 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004542
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004543 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004544 return;
4545
Jason Ekstranda7af7042013-10-12 22:38:11 -05004546 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004547
4548 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004549 weston_layer_entry_insert(&shell->lock_layer.view_list,
4550 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02004552 surface->is_mapped = true;
4553 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004554 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004555 }
4556}
4557
4558static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004559handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004560{
Tiago Vignattibe143262012-04-16 17:31:41 +03004561 struct desktop_shell *shell =
4562 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004563
Martin Minarik6d118362012-06-07 18:01:59 +02004564 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004565 shell->lock_surface = NULL;
4566}
4567
4568static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004569desktop_shell_set_lock_surface(struct wl_client *client,
4570 struct wl_resource *resource,
4571 struct wl_resource *surface_resource)
4572{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004573 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004574 struct weston_surface *surface =
4575 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004576
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004577 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004578
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004579 if (!shell->locked)
4580 return;
4581
Pekka Paalanen98262232011-12-01 10:42:22 +02004582 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004583
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004584 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004585 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004586 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004587
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004588 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004589 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004590 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004591 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004592}
4593
4594static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004595resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004596{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004597 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004598
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004599 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004600 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004601 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004602 &shell->input_panel_layer.link);
4603 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004604 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004605 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004606 wl_list_insert(&shell->compositor->cursor_layer.link,
4607 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004608 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004609 wl_list_insert(&shell->fullscreen_layer.link,
4610 &shell->panel_layer.link);
4611 wl_list_insert(&shell->panel_layer.link,
4612 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004613
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004614 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004615
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004616 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004617 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004618 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004619}
4620
4621static void
4622desktop_shell_unlock(struct wl_client *client,
4623 struct wl_resource *resource)
4624{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004625 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004626
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004627 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004628
4629 if (shell->locked)
4630 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004631}
4632
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004633static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004634desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004635 struct wl_resource *resource,
4636 struct wl_resource *surface_resource)
4637{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004638 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004639
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004640 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004641 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004642}
4643
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004644static void
4645desktop_shell_desktop_ready(struct wl_client *client,
4646 struct wl_resource *resource)
4647{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004648 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004649
4650 shell_fade_startup(shell);
4651}
4652
Jonny Lamb765760d2014-08-20 15:53:19 +02004653static void
4654desktop_shell_set_panel_position(struct wl_client *client,
4655 struct wl_resource *resource,
4656 uint32_t position)
4657{
4658 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4659
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004660 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4661 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4662 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4663 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004664 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004665 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004666 "bad position argument");
4667 return;
4668 }
4669
4670 shell->panel_position = position;
4671}
4672
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004673static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004674 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004675 desktop_shell_set_panel,
4676 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004677 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004678 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004679 desktop_shell_desktop_ready,
4680 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004681};
4682
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004683static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004684get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004685{
4686 struct shell_surface *shsurf;
4687
4688 shsurf = get_shell_surface(surface);
4689 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004690 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004691 return shsurf->type;
4692}
4693
Kristian Høgsberg75840622011-09-06 13:48:16 -04004694static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004695move_binding(struct weston_pointer *pointer, uint32_t time,
4696 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004697{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004698 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004699 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004700 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004701
Derek Foreman8ae2db52015-07-15 13:00:36 -05004702 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004703 return;
4704
Derek Foreman8ae2db52015-07-15 13:00:36 -05004705 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004706
Pekka Paalanen01388e22013-04-25 13:57:44 +03004707 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004708 if (surface == NULL)
4709 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004710
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004711 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004712 if (shsurf == NULL || shsurf->state.fullscreen ||
4713 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004714 return;
4715
Derek Foreman794fa0e2015-07-15 13:00:38 -05004716 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004717}
4718
4719static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004720maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4721 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004722{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004723 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004724 struct weston_surface *surface;
4725 struct shell_surface *shsurf;
4726
4727 surface = weston_surface_get_main_surface(focus);
4728 if (surface == NULL)
4729 return;
4730
4731 shsurf = get_shell_surface(surface);
4732 if (shsurf == NULL)
4733 return;
4734
4735 if (!shell_surface_is_xdg_surface(shsurf))
4736 return;
4737
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004738 shsurf->state_requested = true;
4739 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004740 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004741}
4742
4743static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004744fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4745 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004746{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004747 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004748 struct weston_surface *surface;
4749 struct shell_surface *shsurf;
4750
4751 surface = weston_surface_get_main_surface(focus);
4752 if (surface == NULL)
4753 return;
4754
4755 shsurf = get_shell_surface(surface);
4756 if (shsurf == NULL)
4757 return;
4758
4759 if (!shell_surface_is_xdg_surface(shsurf))
4760 return;
4761
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004762 shsurf->state_requested = true;
4763 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004764 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004765 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004766}
4767
4768static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004769touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004770{
Derek Foreman362656b2014-09-04 10:23:05 -05004771 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004772 struct weston_surface *surface;
4773 struct shell_surface *shsurf;
4774
Derek Foreman8ae2db52015-07-15 13:00:36 -05004775 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004776 return;
4777
Derek Foreman8ae2db52015-07-15 13:00:36 -05004778 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004779 surface = weston_surface_get_main_surface(focus);
4780 if (surface == NULL)
4781 return;
4782
4783 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004784 if (shsurf == NULL || shsurf->state.fullscreen ||
4785 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004786 return;
4787
Derek Foremanb7674ae2015-07-15 13:00:37 -05004788 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004789}
4790
4791static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004792resize_binding(struct weston_pointer *pointer, uint32_t time,
4793 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004794{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004795 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004796 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004797 uint32_t edges = 0;
4798 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004799 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004800
Derek Foreman8ae2db52015-07-15 13:00:36 -05004801 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004802 return;
4803
Derek Foreman8ae2db52015-07-15 13:00:36 -05004804 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004805
Pekka Paalanen01388e22013-04-25 13:57:44 +03004806 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004807 if (surface == NULL)
4808 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004809
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004810 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004811 if (shsurf == NULL || shsurf->state.fullscreen ||
4812 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004813 return;
4814
Jason Ekstranda7af7042013-10-12 22:38:11 -05004815 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004816 wl_fixed_to_int(pointer->grab_x),
4817 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004818 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004819
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004820 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004821 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004822 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004823 edges |= 0;
4824 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004825 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004826
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004827 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004828 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004829 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004830 edges |= 0;
4831 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004832 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004833
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004834 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004835}
4836
4837static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004838surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004839 struct weston_pointer_axis_event *event,
4840 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004841{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004842 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004843 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004844 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004845 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004846
Pekka Paalanen01388e22013-04-25 13:57:44 +03004847 /* XXX: broken for windows containing sub-surfaces */
4848 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004849 if (surface == NULL)
4850 return;
4851
4852 shsurf = get_shell_surface(surface);
4853 if (!shsurf)
4854 return;
4855
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004856 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004857
Jason Ekstranda7af7042013-10-12 22:38:11 -05004858 if (shsurf->view->alpha > 1.0)
4859 shsurf->view->alpha = 1.0;
4860 if (shsurf->view->alpha < step)
4861 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004862
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004864 weston_surface_damage(surface);
4865}
4866
4867static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004868do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004869 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004870{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004871 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004872 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004873 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004874 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004875
Derek Foreman1281a362015-07-31 16:55:32 -05004876 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004877 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4878 return;
4879 }
4880
Scott Moreauccbf29d2012-02-22 14:21:41 -07004881 wl_list_for_each(output, &compositor->output_list, link) {
4882 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004883 wl_fixed_to_double(pointer->x),
4884 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004885 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004886 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004887 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004888 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004889 increment = -output->zoom.increment;
4890 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004891 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004892 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004893 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004894 else
4895 increment = 0;
4896
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004897 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004898
Scott Moreaue6603982012-06-11 13:07:51 -06004899 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004900 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004901 else if (output->zoom.level > output->zoom.max_level)
4902 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004903
4904 if (!output->zoom.active) {
4905 if (output->zoom.level <= 0.0)
4906 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004907 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004908 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004909
Scott Moreaue6603982012-06-11 13:07:51 -06004910 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004911
Jason Ekstranda7af7042013-10-12 22:38:11 -05004912 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004913 }
4914 }
4915}
4916
Scott Moreauccbf29d2012-02-22 14:21:41 -07004917static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004918zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004919 struct weston_pointer_axis_event *event,
4920 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004921{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004922 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004923}
4924
4925static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004926zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4927 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004928{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004929 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004930}
4931
4932static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004933terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4934 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004935{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004936 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004937
Daniel Stone325fc2d2012-05-30 16:31:58 +01004938 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004939}
4940
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004941static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004942rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004943 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004944{
4945 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004946 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004947 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004948 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004949 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004950
Jonas Ådahld2510102014-10-05 21:39:14 +02004951 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004952
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004953 if (!shsurf)
4954 return;
4955
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004956 cx = 0.5f * shsurf->surface->width;
4957 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004958
Daniel Stone37816df2012-05-16 18:45:18 +01004959 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4960 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004961 r = sqrtf(dx * dx + dy * dy);
4962
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004963 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004964 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004965
4966 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004967 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004968 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004969
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004970 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004971 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004972
4973 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004974 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004975 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004976 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004977 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004978
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004979 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004980 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004981 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004982 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004983 wl_list_init(&shsurf->rotation.transform.link);
4984 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004985 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004986 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004987
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004988 /* We need to adjust the position of the surface
4989 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004990 cposx = shsurf->view->geometry.x + cx;
4991 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004992 dposx = rotate->center.x - cposx;
4993 dposy = rotate->center.y - cposy;
4994 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004995 weston_view_set_position(shsurf->view,
4996 shsurf->view->geometry.x + dposx,
4997 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004998 }
4999
Derek Foreman4b1a0a12014-09-10 15:37:33 -05005000 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02005001 * lazily applies the damage due to rotation update.
5002 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005003 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005004}
5005
5006static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005007rotate_grab_button(struct weston_pointer_grab *grab,
5008 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005009{
5010 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005011 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005012 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005013 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01005014 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005015
Daniel Stone4dbadb12012-05-30 16:31:51 +01005016 if (pointer->button_count == 0 &&
5017 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005018 if (shsurf)
5019 weston_matrix_multiply(&shsurf->rotation.rotation,
5020 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005021 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005022 free(rotate);
5023 }
5024}
5025
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005026static void
5027rotate_grab_cancel(struct weston_pointer_grab *grab)
5028{
5029 struct rotate_grab *rotate =
5030 container_of(grab, struct rotate_grab, base.grab);
5031
5032 shell_grab_end(&rotate->base);
5033 free(rotate);
5034}
5035
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005036static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005037 noop_grab_focus,
5038 rotate_grab_motion,
5039 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005040 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10005041 noop_grab_axis_source,
5042 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005043 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005044};
5045
5046static void
Derek Foreman74de4692015-07-15 13:00:39 -05005047surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005048{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005049 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005050 float dx, dy;
5051 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005052
Derek Foreman45a7c272015-09-11 14:27:40 -05005053 surface = find_toplevel_surface(surface);
5054
Pekka Paalanen460099f2012-01-20 16:48:25 +02005055 rotate = malloc(sizeof *rotate);
5056 if (!rotate)
5057 return;
5058
Jason Ekstranda7af7042013-10-12 22:38:11 -05005059 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005060 surface->surface->width * 0.5f,
5061 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005062 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005063
Derek Foreman74de4692015-07-15 13:00:39 -05005064 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5065 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005066 r = sqrtf(dx * dx + dy * dy);
5067 if (r > 20.0f) {
5068 struct weston_matrix inverse;
5069
5070 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005071 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005072 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005073
5074 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005075 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005076 } else {
5077 weston_matrix_init(&surface->rotation.rotation);
5078 weston_matrix_init(&rotate->rotation);
5079 }
5080
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005081 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005082 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005083}
5084
5085static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005086rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005087 void *data)
5088{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005089 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005090 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005091 struct shell_surface *surface;
5092
Derek Foreman8ae2db52015-07-15 13:00:36 -05005093 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005094 return;
5095
Derek Foreman8ae2db52015-07-15 13:00:36 -05005096 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005097
Pekka Paalanen01388e22013-04-25 13:57:44 +03005098 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005099 if (base_surface == NULL)
5100 return;
5101
5102 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005103 if (surface == NULL || surface->state.fullscreen ||
5104 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005105 return;
5106
Derek Foreman74de4692015-07-15 13:00:39 -05005107 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005108}
5109
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005110/* Move all fullscreen layers down to the current workspace and hide their
5111 * black views. The surfaces' state is set to both fullscreen and lowered,
5112 * and this is reversed when such a surface is re-configured, see
5113 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5114 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005115 * lowering_output = NULL - Lower on all outputs, else only lower on the
5116 * specified output.
5117 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005118 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005119 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005120void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005121lower_fullscreen_layer(struct desktop_shell *shell,
5122 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005123{
5124 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005125 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005126
5127 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005128 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005129 &shell->fullscreen_layer.view_list.link,
5130 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005131 struct shell_surface *shsurf = get_shell_surface(view->surface);
5132
5133 if (!shsurf)
5134 continue;
5135
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005136 /* Only lower surfaces which have lowering_output as their fullscreen
5137 * output, unless a NULL output asks for lowering on all outputs.
5138 */
5139 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5140 continue;
5141
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005142 /* We can have a non-fullscreen popup for a fullscreen surface
5143 * in the fullscreen layer. */
5144 if (shsurf->state.fullscreen) {
5145 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005146 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5147 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005148 weston_view_damage_below(shsurf->fullscreen.black_view);
5149
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005150 }
5151
5152 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005153 weston_layer_entry_remove(&view->layer_link);
5154 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005155 weston_view_damage_below(view);
5156 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005157
5158 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005159 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005160}
5161
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005162void
Tiago Vignattibe143262012-04-16 17:31:41 +03005163activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005164 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005165{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005166 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005167 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005168 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005169 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005170 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005171
Pekka Paalanen01388e22013-04-25 13:57:44 +03005172 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005173 shsurf = get_shell_surface(main_surface);
5174 assert(shsurf);
5175
5176 /* Only demote fullscreen surfaces on the output of activated shsurf.
5177 * Leave fullscreen surfaces on unrelated outputs alone. */
5178 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005179
Bryce Harrington260c2ff2016-06-29 19:04:06 -07005180 weston_seat_set_keyboard_focus(seat, es);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005181
Jonas Ådahl8538b222012-08-29 22:13:03 +02005182 state = ensure_focus_state(shell, seat);
5183 if (state == NULL)
5184 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005185
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005186 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005187 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005188
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005189 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005190 shell_configure_fullscreen(shsurf);
5191 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005192 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005193
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005194 /* Update the surface’s layer. This brings it to the top of the stacking
5195 * order as appropriate. */
5196 shell_surface_update_layer(shsurf);
5197
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005198 if (shell->focus_animation_type != ANIMATION_NONE) {
5199 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005200 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005201 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005202}
5203
Alex Wu21858432012-04-01 20:13:08 +08005204/* no-op func for checking black surface */
5205static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005206black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005207{
5208}
5209
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005210static bool
Alex Wu21858432012-04-01 20:13:08 +08005211is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5212{
5213 if (es->configure == black_surface_configure) {
5214 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005215 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005216 return true;
5217 }
5218 return false;
5219}
5220
Kristian Høgsberg75840622011-09-06 13:48:16 -04005221static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005222activate_binding(struct weston_seat *seat,
5223 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005224 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005225{
Derek Foremanbbde6432015-12-10 13:08:01 -06005226 struct focus_state *state;
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005227 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005228 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005229
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005230 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005231
Pekka Paalanen01388e22013-04-25 13:57:44 +03005232 if (is_black_surface(focus, &main_surface))
5233 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005234
Pekka Paalanen01388e22013-04-25 13:57:44 +03005235 main_surface = weston_surface_get_main_surface(focus);
5236 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005237 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005238
Derek Foremanbbde6432015-12-10 13:08:01 -06005239 state = ensure_focus_state(shell, seat);
5240 if (state == NULL)
5241 return;
5242
5243 if (state->keyboard_focus == focus)
5244 return;
5245
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005246 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005247}
5248
5249static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005250click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5251 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005252{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005253 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005254 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005255 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005256 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005257
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005258 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005259}
5260
5261static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005262touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5263 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005264{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005265 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005266 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005267 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005268 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005269
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005270 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005271}
5272
5273static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005274unfocus_all_seats(struct desktop_shell *shell)
5275{
5276 struct weston_seat *seat, *next;
5277
5278 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005279 struct weston_keyboard *keyboard =
5280 weston_seat_get_keyboard(seat);
5281
5282 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005283 continue;
5284
Derek Foreman1281a362015-07-31 16:55:32 -05005285 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005286 }
5287}
5288
5289static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005290lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005291{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005292 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005293
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005294 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005295 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005296 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005297 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005298
5299 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005300
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005301 /* Hide all surfaces by removing the fullscreen, panel and
5302 * toplevel layers. This way nothing else can show or receive
5303 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005304
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005305 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005306 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005307 if (shell->showing_input_panels)
5308 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005309 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005310 wl_list_insert(&shell->compositor->cursor_layer.link,
5311 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005312
Derek Foreman004b4a12015-07-20 16:28:13 -05005313 weston_compositor_sleep(shell->compositor);
5314
Neil Robertsb4a91702014-04-09 16:33:32 +01005315 /* Remove the keyboard focus on all seats. This will be
5316 * restored to the workspace's saved state via
5317 * restore_focus_state when the compositor is unlocked */
5318 unfocus_all_seats(shell);
5319
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005320 /* TODO: disable bindings that should not work while locked. */
5321
5322 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005323}
5324
5325static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005326unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005327{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005328 struct wl_resource *shell_resource;
5329
Pekka Paalanend81c2162011-11-16 13:47:34 +02005330 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005331 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005332 return;
5333 }
5334
5335 /* If desktop-shell client has gone away, unlock immediately. */
5336 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005337 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005338 return;
5339 }
5340
5341 if (shell->prepare_event_sent)
5342 return;
5343
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005344 shell_resource = shell->child.desktop_shell;
5345 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005346 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005347}
5348
5349static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005350shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005351{
5352 struct desktop_shell *shell = data;
5353
5354 shell->fade.animation = NULL;
5355
5356 switch (shell->fade.type) {
5357 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005358 weston_surface_destroy(shell->fade.view->surface);
5359 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005360 break;
5361 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005362 lock(shell);
5363 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005364 default:
5365 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005366 }
5367}
5368
Jason Ekstranda7af7042013-10-12 22:38:11 -05005369static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005370shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005371{
5372 struct weston_compositor *compositor = shell->compositor;
5373 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005375
5376 surface = weston_surface_create(compositor);
5377 if (!surface)
5378 return NULL;
5379
Jason Ekstranda7af7042013-10-12 22:38:11 -05005380 view = weston_view_create(surface);
5381 if (!view) {
5382 weston_surface_destroy(surface);
5383 return NULL;
5384 }
5385
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005386 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005387 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005388 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005389 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5390 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005391 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02005392 surface->is_mapped = true;
5393 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005394
Jason Ekstranda7af7042013-10-12 22:38:11 -05005395 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005396}
5397
5398static void
5399shell_fade(struct desktop_shell *shell, enum fade_type type)
5400{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005401 float tint;
5402
5403 switch (type) {
5404 case FADE_IN:
5405 tint = 0.0;
5406 break;
5407 case FADE_OUT:
5408 tint = 1.0;
5409 break;
5410 default:
5411 weston_log("shell: invalid fade type\n");
5412 return;
5413 }
5414
5415 shell->fade.type = type;
5416
Jason Ekstranda7af7042013-10-12 22:38:11 -05005417 if (shell->fade.view == NULL) {
5418 shell->fade.view = shell_fade_create_surface(shell);
5419 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005420 return;
5421
Jason Ekstranda7af7042013-10-12 22:38:11 -05005422 shell->fade.view->alpha = 1.0 - tint;
5423 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005424 }
5425
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005426 if (shell->fade.view->output == NULL) {
5427 /* If the black view gets a NULL output, we lost the
5428 * last output and we'll just cancel the fade. This
5429 * happens when you close the last window under the
5430 * X11 or Wayland backends. */
5431 shell->locked = false;
5432 weston_surface_destroy(shell->fade.view->surface);
5433 shell->fade.view = NULL;
5434 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005435 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005436 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005437 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005438 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005439 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005440 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005441 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005442}
5443
5444static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005445do_shell_fade_startup(void *data)
5446{
5447 struct desktop_shell *shell = data;
5448
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005449 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005450 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005451 } else {
5452 weston_log("desktop shell: "
5453 "unexpected fade-in animation type %d\n",
5454 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005455 weston_surface_destroy(shell->fade.view->surface);
5456 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005457 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005458}
5459
5460static void
5461shell_fade_startup(struct desktop_shell *shell)
5462{
5463 struct wl_event_loop *loop;
5464
5465 if (!shell->fade.startup_timer)
5466 return;
5467
5468 wl_event_source_remove(shell->fade.startup_timer);
5469 shell->fade.startup_timer = NULL;
5470
5471 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5472 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5473}
5474
5475static int
5476fade_startup_timeout(void *data)
5477{
5478 struct desktop_shell *shell = data;
5479
5480 shell_fade_startup(shell);
5481 return 0;
5482}
5483
5484static void
5485shell_fade_init(struct desktop_shell *shell)
5486{
5487 /* Make compositor output all black, and wait for the desktop-shell
5488 * client to signal it is ready, then fade in. The timer triggers a
5489 * fade-in, in case the desktop-shell client takes too long.
5490 */
5491
5492 struct wl_event_loop *loop;
5493
Jason Ekstranda7af7042013-10-12 22:38:11 -05005494 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005495 weston_log("%s: warning: fade surface already exists\n",
5496 __func__);
5497 return;
5498 }
5499
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005500 if (shell->startup_animation_type == ANIMATION_NONE)
5501 return;
5502
Jason Ekstranda7af7042013-10-12 22:38:11 -05005503 shell->fade.view = shell_fade_create_surface(shell);
5504 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005505 return;
5506
Jason Ekstranda7af7042013-10-12 22:38:11 -05005507 weston_view_update_transform(shell->fade.view);
5508 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005509
5510 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5511 shell->fade.startup_timer =
5512 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5513 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5514}
5515
5516static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005517idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005518{
5519 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005520 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005521 struct weston_seat *seat;
5522
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005523 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005524 struct weston_touch *touch = weston_seat_get_touch(seat);
5525 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5526
5527 if (pointer)
5528 popup_grab_end(pointer);
5529 if (touch)
5530 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005531 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005532
5533 shell_fade(shell, FADE_OUT);
5534 /* lock() is called from shell_fade_done() */
5535}
5536
5537static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005538wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005539{
5540 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005541 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005542
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005543 unlock(shell);
5544}
5545
5546static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005547transform_handler(struct wl_listener *listener, void *data)
5548{
5549 struct weston_surface *surface = data;
5550 struct shell_surface *shsurf = get_shell_surface(surface);
5551 struct weston_view *view;;
5552 int x, y;
5553
5554 if (!shsurf || !shsurf->client->send_position)
5555 return;
5556
5557 view = shsurf->view;
5558 if (!view || !weston_view_is_mapped(view))
5559 return;
5560
5561 x = view->geometry.x;
5562 y = view->geometry.y;
5563
5564 shsurf->client->send_position(surface, x, y);
5565}
5566
5567static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005568center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005569{
Giulio Camuffob8366642013-04-25 13:57:46 +03005570 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005571 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005572
Jason Ekstranda7af7042013-10-12 22:38:11 -05005573 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005574
5575 x = output->x + (output->width - width) / 2 - surf_x / 2;
5576 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005577
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005578 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005579}
5580
5581static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005582weston_view_set_initial_position(struct weston_view *view,
5583 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005584{
5585 struct weston_compositor *compositor = shell->compositor;
5586 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005587 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005588 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005589 struct weston_output *output, *target_output = NULL;
5590 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005591 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005592
5593 /* As a heuristic place the new window on the same output as the
5594 * pointer. Falling back to the output containing 0, 0.
5595 *
5596 * TODO: Do something clever for touch too?
5597 */
5598 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005599 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5600
5601 if (pointer) {
5602 ix = wl_fixed_to_int(pointer->x);
5603 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005604 break;
5605 }
5606 }
5607
5608 wl_list_for_each(output, &compositor->output_list, link) {
5609 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5610 target_output = output;
5611 break;
5612 }
5613 }
5614
5615 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005616 weston_view_set_position(view, 10 + random() % 400,
5617 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005618 return;
5619 }
5620
5621 /* Valid range within output where the surface will still be onscreen.
5622 * If this is negative it means that the surface is bigger than
5623 * output.
5624 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005625 get_output_work_area(shell, target_output, &area);
5626
Derek Foremanf814c5d2015-04-15 12:23:54 -05005627 x = area.x;
5628 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005629 range_x = area.width - view->surface->width;
5630 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005631
Rob Bradford4cb88c72012-08-13 15:18:44 +01005632 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005633 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005634
5635 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005636 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005637
Jason Ekstranda7af7042013-10-12 22:38:11 -05005638 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005639}
5640
5641static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005642set_maximized_position(struct desktop_shell *shell,
5643 struct shell_surface *shsurf)
5644{
5645 int32_t surf_x, surf_y;
5646 pixman_rectangle32_t area;
5647
Jonny Lambd73c6942014-08-20 15:53:20 +02005648 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005649 if (shsurf->has_set_geometry) {
5650 surf_x = shsurf->geometry.x;
5651 surf_y = shsurf->geometry.y;
5652 } else {
5653 surface_subsurfaces_boundingbox(shsurf->surface,
5654 &surf_x, &surf_y, NULL, NULL);
5655 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005656
5657 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005658 area.x - surf_x,
5659 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005660}
5661
5662static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005663map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005664 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005665{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005666 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005667 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005668
Pekka Paalanen77346a62011-11-30 16:26:35 +02005669 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005670 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005671 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005672 if (shsurf->state.fullscreen) {
5673 center_on_output(shsurf->view, shsurf->fullscreen_output);
5674 shell_map_fullscreen(shsurf);
5675 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005676 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005677 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005678 weston_view_set_initial_position(shsurf->view, shell);
5679 }
Juan Zhao96879df2012-02-07 08:45:41 +08005680 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005681 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005682 if (shell_map_popup(shsurf) != 0)
5683 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005684 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005685 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005686 weston_view_set_position(shsurf->view,
5687 shsurf->view->geometry.x + sx,
5688 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005689 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005690 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005691 default:
5692 ;
5693 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005694
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005695 /* Surface stacking order, see also activate(). */
5696 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005697
Jason Ekstranda7af7042013-10-12 22:38:11 -05005698 if (shsurf->type != SHELL_SURFACE_NONE) {
5699 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02005700 shsurf->view->is_mapped = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005701 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005702 shsurf->surface->output = shsurf->output;
5703 shsurf->view->output = shsurf->output;
5704 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005705 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005706
Jason Ekstranda7af7042013-10-12 22:38:11 -05005707 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005708 /* XXX: xwayland's using the same fields for transient type */
5709 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005710 if (shsurf->transient.flags ==
5711 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5712 break;
5713 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005714 if (shsurf->state.relative &&
5715 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5716 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005717 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005718 break;
5719 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005720 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005721 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005722 case SHELL_SURFACE_POPUP:
5723 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005724 default:
5725 break;
5726 }
5727
Rafael Antognolli03b16592013-12-03 15:35:42 -02005728 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5729 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005730 {
5731 switch (shell->win_animation_type) {
5732 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005733 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005734 break;
5735 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005736 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005737 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005738 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005739 default:
5740 break;
5741 }
5742 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005743}
5744
5745static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005746configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005747 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005748{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005749 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005750 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005751
Pekka Paalanen77346a62011-11-30 16:26:35 +02005752 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005753
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005754 assert(shsurf);
5755
Rafael Antognolli03b16592013-12-03 15:35:42 -02005756 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005757 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005758 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005759 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005760 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005761 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005762 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005763
Alex Wu4539b082012-03-01 12:57:46 +08005764 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005765 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005766 wl_list_for_each(view, &surface->views, surface_link)
5767 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005768
Rafael Antognolli03b16592013-12-03 15:35:42 -02005769 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005770 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005771 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005772}
5773
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005774static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005775shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005776{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005777 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005778 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005779 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005780
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005781 assert(shsurf);
5782
5783 shell = shsurf->shell;
5784
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005785 if (!weston_surface_is_mapped(es) &&
5786 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005787 remove_popup_grab(shsurf);
5788 }
5789
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005790 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005791 return;
5792
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005793 if (shsurf->has_next_geometry) {
5794 shsurf->geometry = shsurf->next_geometry;
5795 shsurf->has_next_geometry = false;
5796 shsurf->has_set_geometry = true;
5797 } else if (!shsurf->has_set_geometry) {
5798 surface_subsurfaces_boundingbox(shsurf->surface,
5799 &shsurf->geometry.x,
5800 &shsurf->geometry.y,
5801 &shsurf->geometry.width,
5802 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005803 }
5804
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005805 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005806 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005807 type_changed = 1;
5808 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005809
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005810 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005811 map(shell, shsurf, sx, sy);
Armin Krezović4663aca2016-06-30 06:04:29 +02005812 es->is_mapped = true;
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005813 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005814 shsurf->last_width != es->width ||
5815 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005816 float from_x, from_y;
5817 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005818
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005819 if (shsurf->resize_edges) {
5820 sx = 0;
5821 sy = 0;
5822 }
5823
5824 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5825 sx = shsurf->last_width - es->width;
5826 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5827 sy = shsurf->last_height - es->height;
5828
5829 shsurf->last_width = es->width;
5830 shsurf->last_height = es->height;
5831
Jason Ekstranda7af7042013-10-12 22:38:11 -05005832 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5833 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005834 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005835 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005836 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005837 }
5838}
5839
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005840static bool
5841check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5842{
5843 struct timespec now;
5844
5845 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5846 return false;
5847
5848 /*
5849 * If the shell helper client dies before the session has been
5850 * up for roughly 30 seconds, better just make Weston shut down,
5851 * because the user likely has no way to interact with the desktop
5852 * anyway.
5853 */
5854 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5855 weston_log("Error: %s apparently cannot run at all.\n",
5856 shell->client);
5857 weston_log_continue(STAMP_SPACE "Quitting...");
5858 wl_display_terminate(shell->compositor->wl_display);
5859
5860 return true;
5861 }
5862
5863 return false;
5864}
5865
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005866static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005867
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005868static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005869respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005870{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005871 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005872
5873 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5874 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005875 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005876 shell->child.deathstamp = time;
5877 shell->child.deathcount = 0;
5878 }
5879
5880 shell->child.deathcount++;
5881 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005882 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005883 return;
5884 }
5885
Pekka Paalanen826dc142014-08-27 12:11:53 +03005886 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005887 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005888}
5889
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005890static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005891desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5892{
5893 struct desktop_shell *shell;
5894
5895 shell = container_of(listener, struct desktop_shell,
5896 child.client_destroy_listener);
5897
Pekka Paalanen826dc142014-08-27 12:11:53 +03005898 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005899 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005900 /*
5901 * unbind_desktop_shell() will reset shell->child.desktop_shell
5902 * before the respawned process has a chance to create a new
5903 * desktop_shell object, because we are being called from the
5904 * wl_client destructor which destroys all wl_resources before
5905 * returning.
5906 */
5907
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005908 if (!check_desktop_shell_crash_too_early(shell))
5909 respawn_desktop_shell_process(shell);
5910
Pekka Paalanen826dc142014-08-27 12:11:53 +03005911 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005912}
5913
5914static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005915launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005916{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005917 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005918
Pekka Paalanen826dc142014-08-27 12:11:53 +03005919 shell->child.client = weston_client_start(shell->compositor,
5920 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005921
Arnaud Vrac42631192014-08-25 20:56:46 +02005922 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005923 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005924 return;
5925 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005926
5927 shell->child.client_destroy_listener.notify =
5928 desktop_shell_client_destroy;
5929 wl_client_add_destroy_listener(shell->child.client,
5930 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005931}
5932
5933static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005934handle_shell_client_destroy(struct wl_listener *listener, void *data)
5935{
5936 struct shell_client *sc =
5937 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005938 struct wl_resource *shsurf_resource;
5939 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005940
5941 if (sc->ping_timer)
5942 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005943
5944 /* Since we're about to free shell_client, we remove it from the
5945 * head of the surface list so we don't use that freed list node
5946 * during surface clean up later on.
5947 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005948 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5949 shsurf = wl_resource_get_user_data(shsurf_resource);
5950 shsurf->owner = NULL;
5951 }
Derek Foremane42d7542015-05-29 10:46:44 -05005952 wl_list_remove(&sc->surface_list);
5953
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005954 free(sc);
5955}
5956
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005957static struct shell_client *
5958shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5959 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005960{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005961 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005962
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005963 sc = zalloc(sizeof *sc);
5964 if (sc == NULL) {
5965 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005966 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005967 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005968
5969 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005970 if (sc->resource == NULL) {
5971 free(sc);
5972 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005973 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005974 }
5975
5976 sc->client = client;
5977 sc->shell = shell;
5978 sc->destroy_listener.notify = handle_shell_client_destroy;
5979 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005980 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005981
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005982 return sc;
5983}
5984
5985static void
5986bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5987{
5988 struct desktop_shell *shell = data;
5989 struct shell_client *sc;
5990
5991 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5992 if (sc)
5993 wl_resource_set_implementation(sc->resource,
5994 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005995 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005996}
5997
5998static void
5999bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
6000{
6001 struct desktop_shell *shell = data;
6002 struct shell_client *sc;
6003
6004 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
6005 if (sc)
6006 wl_resource_set_dispatcher(sc->resource,
6007 xdg_shell_unversioned_dispatch,
6008 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02006009}
6010
6011static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006012unbind_desktop_shell(struct wl_resource *resource)
6013{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05006014 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05006015
6016 if (shell->locked)
6017 resume_desktop(shell);
6018
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006019 shell->child.desktop_shell = NULL;
6020 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006021}
6022
6023static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04006024bind_desktop_shell(struct wl_client *client,
6025 void *data, uint32_t version, uint32_t id)
6026{
Tiago Vignattibe143262012-04-16 17:31:41 +03006027 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006028 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04006029
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006030 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6031 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006032
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006033 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006034 wl_resource_set_implementation(resource,
6035 &desktop_shell_implementation,
6036 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006037 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006038 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006039 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006040
6041 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6042 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04006043}
6044
Kristian Høgsberg07045392012-02-19 18:52:44 -05006045struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006046 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047 struct weston_surface *current;
6048 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006049 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006050 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006051};
6052
6053static void
6054switcher_next(struct switcher *switcher)
6055{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006056 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006057 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006058 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006059 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006060
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006061 /* temporary re-display minimized surfaces */
6062 struct weston_view *tmp;
6063 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006064 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6065 weston_layer_entry_remove(&view->layer_link);
6066 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006067 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6068 *minimized = view;
6069 }
6070
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006071 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006072 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006073 if (shsurf &&
6074 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6075 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006076 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006077 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006078 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006079 next = view->surface;
6080 prev = view->surface;
6081 view->alpha = 0.25;
6082 weston_view_geometry_dirty(view);
6083 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006084 }
Alex Wu1659daa2012-04-01 20:13:09 +08006085
Jason Ekstranda7af7042013-10-12 22:38:11 -05006086 if (is_black_surface(view->surface, NULL)) {
6087 view->alpha = 0.25;
6088 weston_view_geometry_dirty(view);
6089 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006090 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006091 }
6092
6093 if (next == NULL)
6094 next = first;
6095
Alex Wu07b26062012-03-12 16:06:01 +08006096 if (next == NULL)
6097 return;
6098
Kristian Høgsberg07045392012-02-19 18:52:44 -05006099 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006100 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006101
6102 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006103 wl_list_for_each(view, &next->views, surface_link)
6104 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006105
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006106 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006107 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006108 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006109}
6110
6111static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006112switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006113{
6114 struct switcher *switcher =
6115 container_of(listener, struct switcher, listener);
6116
6117 switcher_next(switcher);
6118}
6119
6120static void
Daniel Stone351eb612012-05-31 15:27:47 -04006121switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006122{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006123 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006124 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006125 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006126
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006127 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006128 if (is_focus_view(view))
6129 continue;
6130
Jason Ekstranda7af7042013-10-12 22:38:11 -05006131 view->alpha = 1.0;
6132 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006133 }
6134
Alex Wu07b26062012-03-12 16:06:01 +08006135 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006136 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006137 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006138 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006139 weston_keyboard_end_grab(keyboard);
6140 if (keyboard->input_method_resource)
6141 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006142
6143 /* re-hide surfaces that were temporary shown during the switch */
6144 struct weston_view **minimized;
6145 wl_array_for_each(minimized, &switcher->minimized_array) {
6146 /* with the exception of the current selected */
6147 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006148 weston_layer_entry_remove(&(*minimized)->layer_link);
6149 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006150 weston_view_damage_below(*minimized);
6151 }
6152 }
6153 wl_array_release(&switcher->minimized_array);
6154
Kristian Høgsberg07045392012-02-19 18:52:44 -05006155 free(switcher);
6156}
6157
6158static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006159switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006160 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006161{
6162 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006163 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006164
Daniel Stonec9785ea2012-05-30 16:31:52 +01006165 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006166 switcher_next(switcher);
6167}
6168
6169static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006170switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006171 uint32_t mods_depressed, uint32_t mods_latched,
6172 uint32_t mods_locked, uint32_t group)
6173{
6174 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006175 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006176
Daniel Stone351eb612012-05-31 15:27:47 -04006177 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6178 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006179}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006180
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006181static void
6182switcher_cancel(struct weston_keyboard_grab *grab)
6183{
6184 struct switcher *switcher = container_of(grab, struct switcher, grab);
6185
6186 switcher_destroy(switcher);
6187}
6188
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006189static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006190 switcher_key,
6191 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006192 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006193};
6194
6195static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006196switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6197 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006198{
Tiago Vignattibe143262012-04-16 17:31:41 +03006199 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006200 struct switcher *switcher;
6201
6202 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006203 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006204 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006205 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006206 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006207 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006208
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006209 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006210 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006211 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006212 weston_keyboard_start_grab(keyboard, &switcher->grab);
6213 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006214 switcher_next(switcher);
6215}
6216
Pekka Paalanen3c647232011-12-22 13:43:43 +02006217static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006218backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6219 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006220{
6221 struct weston_compositor *compositor = data;
6222 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006223 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006224
6225 /* TODO: we're limiting to simple use cases, where we assume just
6226 * control on the primary display. We'd have to extend later if we
6227 * ever get support for setting backlights on random desktop LCD
6228 * panels though */
6229 output = get_default_output(compositor);
6230 if (!output)
6231 return;
6232
6233 if (!output->set_backlight)
6234 return;
6235
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006236 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6237 backlight_new = output->backlight_current - 25;
6238 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6239 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006240
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006241 if (backlight_new < 5)
6242 backlight_new = 5;
6243 if (backlight_new > 255)
6244 backlight_new = 255;
6245
6246 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006247 output->set_backlight(output, output->backlight_current);
6248}
6249
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006250static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006251force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6252 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006253{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006254 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006255 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006256 struct desktop_shell *shell = data;
6257 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006258 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006259
Derek Foreman8ae2db52015-07-15 13:00:36 -05006260 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006261 if (!focus_surface)
6262 return;
6263
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006264 wl_signal_emit(&compositor->kill_signal, focus_surface);
6265
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006266 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006267 wl_client_get_credentials(client, &pid, NULL, NULL);
6268
6269 /* Skip clients that we launched ourselves (the credentials of
6270 * the socketpair is ours) */
6271 if (pid == getpid())
6272 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006273
Daniel Stone325fc2d2012-05-30 16:31:58 +01006274 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006275}
6276
6277static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006278workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006279 uint32_t key, void *data)
6280{
6281 struct desktop_shell *shell = data;
6282 unsigned int new_index = shell->workspaces.current;
6283
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006284 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006285 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006286 if (new_index != 0)
6287 new_index--;
6288
6289 change_workspace(shell, new_index);
6290}
6291
6292static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006293workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006294 uint32_t key, void *data)
6295{
6296 struct desktop_shell *shell = data;
6297 unsigned int new_index = shell->workspaces.current;
6298
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006299 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006300 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006301 if (new_index < shell->workspaces.num - 1)
6302 new_index++;
6303
6304 change_workspace(shell, new_index);
6305}
6306
6307static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006308workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006309 uint32_t key, void *data)
6310{
6311 struct desktop_shell *shell = data;
6312 unsigned int new_index;
6313
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006314 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006315 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006316 new_index = key - KEY_F1;
6317 if (new_index >= shell->workspaces.num)
6318 new_index = shell->workspaces.num - 1;
6319
6320 change_workspace(shell, new_index);
6321}
6322
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006323static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006324workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6325 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006326{
6327 struct desktop_shell *shell = data;
6328 unsigned int new_index = shell->workspaces.current;
6329
6330 if (shell->locked)
6331 return;
6332
6333 if (new_index != 0)
6334 new_index--;
6335
Derek Foreman8ae2db52015-07-15 13:00:36 -05006336 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006337}
6338
6339static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006340workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6341 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006342{
6343 struct desktop_shell *shell = data;
6344 unsigned int new_index = shell->workspaces.current;
6345
6346 if (shell->locked)
6347 return;
6348
6349 if (new_index < shell->workspaces.num - 1)
6350 new_index++;
6351
Derek Foreman8ae2db52015-07-15 13:00:36 -05006352 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006353}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006354
6355static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006356shell_reposition_view_on_output_destroy(struct weston_view *view)
6357{
6358 struct weston_output *output, *first_output;
6359 struct weston_compositor *ec = view->surface->compositor;
6360 struct shell_surface *shsurf;
6361 float x, y;
6362 int visible;
6363
6364 x = view->geometry.x;
6365 y = view->geometry.y;
6366
6367 /* At this point the destroyed output is not in the list anymore.
6368 * If the view is still visible somewhere, we leave where it is,
6369 * otherwise, move it to the first output. */
6370 visible = 0;
6371 wl_list_for_each(output, &ec->output_list, link) {
6372 if (pixman_region32_contains_point(&output->region,
6373 x, y, NULL)) {
6374 visible = 1;
6375 break;
6376 }
6377 }
6378
6379 if (!visible) {
6380 first_output = container_of(ec->output_list.next,
6381 struct weston_output, link);
6382
6383 x = first_output->x + first_output->width / 4;
6384 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006385
6386 weston_view_set_position(view, x, y);
6387 } else {
6388 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006389 }
6390
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006391
6392 shsurf = get_shell_surface(view->surface);
6393
6394 if (shsurf) {
6395 shsurf->saved_position_valid = false;
6396 shsurf->next_state.maximized = false;
6397 shsurf->next_state.fullscreen = false;
6398 shsurf->state_changed = true;
6399 }
6400}
6401
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006402void
6403shell_for_each_layer(struct desktop_shell *shell,
6404 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006405{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006406 struct workspace **ws;
6407
6408 func(shell, &shell->fullscreen_layer, data);
6409 func(shell, &shell->panel_layer, data);
6410 func(shell, &shell->background_layer, data);
6411 func(shell, &shell->lock_layer, data);
6412 func(shell, &shell->input_panel_layer, data);
6413
6414 wl_array_for_each(ws, &shell->workspaces.array)
6415 func(shell, &(*ws)->layer, data);
6416}
6417
6418static void
6419shell_output_destroy_move_layer(struct desktop_shell *shell,
6420 struct weston_layer *layer,
6421 void *data)
6422{
6423 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006424 struct weston_view *view;
6425
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006426 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006427 if (view->output != output)
6428 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006429
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006430 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006431 }
6432}
6433
6434static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006435handle_output_destroy(struct wl_listener *listener, void *data)
6436{
6437 struct shell_output *output_listener =
6438 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006439 struct weston_output *output = output_listener->output;
6440 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006441
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006442 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006443
Xiong Zhang6b481422013-10-23 13:58:32 +08006444 wl_list_remove(&output_listener->destroy_listener.link);
6445 wl_list_remove(&output_listener->link);
6446 free(output_listener);
6447}
6448
6449static void
David Fort50d962f2016-06-09 17:55:52 +02006450shell_resize_surface_to_output(struct desktop_shell *shell,
6451 struct weston_surface *surface,
6452 const struct weston_output *output)
6453{
6454 if (!surface)
6455 return;
6456
6457 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6458 surface->resource,
6459 output->width,
6460 output->height);
6461}
6462
6463
6464static void
6465handle_output_resized(struct wl_listener *listener, void *data)
6466{
6467 struct desktop_shell *shell =
6468 container_of(listener, struct desktop_shell, resized_listener);
6469 struct weston_output *output = (struct weston_output *)data;
6470 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6471
6472 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6473 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6474}
6475
6476static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006477create_shell_output(struct desktop_shell *shell,
6478 struct weston_output *output)
6479{
6480 struct shell_output *shell_output;
6481
6482 shell_output = zalloc(sizeof *shell_output);
6483 if (shell_output == NULL)
6484 return;
6485
6486 shell_output->output = output;
6487 shell_output->shell = shell;
6488 shell_output->destroy_listener.notify = handle_output_destroy;
6489 wl_signal_add(&output->destroy_signal,
6490 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006491 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006492}
6493
6494static void
6495handle_output_create(struct wl_listener *listener, void *data)
6496{
6497 struct desktop_shell *shell =
6498 container_of(listener, struct desktop_shell, output_create_listener);
6499 struct weston_output *output = (struct weston_output *)data;
6500
6501 create_shell_output(shell, output);
6502}
6503
6504static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006505handle_output_move_layer(struct desktop_shell *shell,
6506 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006507{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006508 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006509 struct weston_view *view;
6510 float x, y;
6511
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006512 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006513 if (view->output != output)
6514 continue;
6515
6516 x = view->geometry.x + output->move_x;
6517 y = view->geometry.y + output->move_y;
6518 weston_view_set_position(view, x, y);
6519 }
6520}
6521
6522static void
6523handle_output_move(struct wl_listener *listener, void *data)
6524{
6525 struct desktop_shell *shell;
6526
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006527 shell = container_of(listener, struct desktop_shell,
6528 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006529
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006530 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006531}
6532
6533static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006534setup_output_destroy_handler(struct weston_compositor *ec,
6535 struct desktop_shell *shell)
6536{
6537 struct weston_output *output;
6538
6539 wl_list_init(&shell->output_list);
6540 wl_list_for_each(output, &ec->output_list, link)
6541 create_shell_output(shell, output);
6542
6543 shell->output_create_listener.notify = handle_output_create;
6544 wl_signal_add(&ec->output_created_signal,
6545 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006546
6547 shell->output_move_listener.notify = handle_output_move;
6548 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006549}
6550
6551static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006552shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006553{
Tiago Vignattibe143262012-04-16 17:31:41 +03006554 struct desktop_shell *shell =
6555 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006556 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006557 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006558
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006559 /* Force state to unlocked so we don't try to fade */
6560 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006561
6562 if (shell->child.client) {
6563 /* disable respawn */
6564 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006565 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006566 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006567
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006568 wl_list_remove(&shell->idle_listener.link);
6569 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006570 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006571
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006572 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006573 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006574
Xiong Zhang6b481422013-10-23 13:58:32 +08006575 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6576 wl_list_remove(&shell_output->destroy_listener.link);
6577 wl_list_remove(&shell_output->link);
6578 free(shell_output);
6579 }
6580
6581 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006582 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02006583 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006584
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006585 wl_array_for_each(ws, &shell->workspaces.array)
6586 workspace_destroy(*ws);
6587 wl_array_release(&shell->workspaces.array);
6588
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006589 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006590 free(shell);
6591}
6592
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006593static void
6594shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6595{
6596 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006597 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006598
Bob Ham744e6532016-01-12 10:21:48 +00006599 if (shell->allow_zap)
6600 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6601 MODIFIER_CTRL | MODIFIER_ALT,
6602 terminate_binding, ec);
6603
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006604 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006605 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6606 click_to_activate_binding,
6607 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006608 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6609 click_to_activate_binding,
6610 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006611 weston_compositor_add_touch_binding(ec, 0,
6612 touch_to_activate_binding,
6613 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006614 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6615 MODIFIER_SUPER | MODIFIER_ALT,
6616 surface_opacity_binding, NULL);
6617 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6618 MODIFIER_SUPER, zoom_axis_binding,
6619 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006620 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6621 backlight_binding, ec);
6622 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6623 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006624
6625 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006626 if (shell->exposay_modifier)
6627 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6628 exposay_binding, shell);
6629
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006630 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006631 if (!mod)
6632 return;
6633
Daniel Stone325fc2d2012-05-30 16:31:58 +01006634 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6635 zoom_key_binding, NULL);
6636 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6637 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006638 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6639 maximize_binding, NULL);
6640 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6641 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006642 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6643 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006644 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006645 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006646 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006647 weston_compositor_add_button_binding(ec, BTN_LEFT,
6648 mod | MODIFIER_SHIFT,
6649 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006650
6651 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006652 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006653 rotate_binding, NULL);
6654
Daniel Stone325fc2d2012-05-30 16:31:58 +01006655 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6656 shell);
6657 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6658 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006659 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6660 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006661 weston_compositor_add_key_binding(ec, KEY_K, mod,
6662 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006663 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6664 workspace_up_binding, shell);
6665 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6666 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006667 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6668 workspace_move_surface_up_binding,
6669 shell);
6670 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6671 workspace_move_surface_down_binding,
6672 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006673
6674 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6675 if (shell->workspaces.num > 1) {
6676 num_workspace_bindings = shell->workspaces.num;
6677 if (num_workspace_bindings > 6)
6678 num_workspace_bindings = 6;
6679 for (i = 0; i < num_workspace_bindings; i++)
6680 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6681 workspace_f_binding,
6682 shell);
6683 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006684
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006685 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006686}
6687
Jason Ekstrand024177c2014-04-21 19:42:58 -05006688static void
6689handle_seat_created(struct wl_listener *listener, void *data)
6690{
6691 struct weston_seat *seat = data;
6692
6693 create_shell_seat(seat);
6694}
6695
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006696WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006697module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006698 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006699{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006700 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006701 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006702 struct workspace **pws;
6703 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006704 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006705
Peter Huttererf3d62272013-08-08 11:57:05 +10006706 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006707 if (shell == NULL)
6708 return -1;
6709
Kristian Høgsberg75840622011-09-06 13:48:16 -04006710 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006711
6712 shell->destroy_listener.notify = shell_destroy;
6713 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006714 shell->idle_listener.notify = idle_handler;
6715 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6716 shell->wake_listener.notify = wake_handler;
6717 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006718 shell->transform_listener.notify = transform_handler;
6719 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006720
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006721 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006722 ec->shell_interface.create_shell_surface = create_shell_surface;
6723 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006724 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006725 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006726 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006727 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006728 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006729 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006730 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006731 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006732 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006733 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006734
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006735 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6736 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006737 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6738 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006739 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006740
6741 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006742 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006743
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006744 if (input_panel_setup(shell) < 0)
6745 return -1;
6746
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006747 shell->text_backend = text_backend_init(ec);
6748 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006749 return -1;
6750
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006751 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006752
Daniel Stonedf8133b2013-11-19 11:37:14 +01006753 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6754 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6755
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006756 for (i = 0; i < shell->workspaces.num; i++) {
6757 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6758 if (pws == NULL)
6759 return -1;
6760
6761 *pws = workspace_create();
6762 if (*pws == NULL)
6763 return -1;
6764 }
6765 activate_workspace(shell, 0);
6766
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006767 weston_layer_init(&shell->minimized_layer, NULL);
6768
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006769 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006770 wl_list_init(&shell->workspaces.animation.link);
6771 shell->workspaces.animation.frame = animate_workspace_change_frame;
6772
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006773 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006774 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006775 return -1;
6776
Rafael Antognollie2a34552013-12-03 15:35:45 -02006777 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6778 shell, bind_xdg_shell) == NULL)
6779 return -1;
6780
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006781 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006782 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006783 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006784 return -1;
6785
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006786 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006787
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006788 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006789
Xiong Zhang6b481422013-10-23 13:58:32 +08006790 setup_output_destroy_handler(ec, shell);
6791
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006792 loop = wl_display_get_event_loop(ec->wl_display);
6793 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006794
Jason Ekstrand024177c2014-04-21 19:42:58 -05006795 wl_list_for_each(seat, &ec->seat_list, link)
6796 handle_seat_created(NULL, seat);
6797 shell->seat_create_listener.notify = handle_seat_created;
6798 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006799
David Fort50d962f2016-06-09 17:55:52 +02006800 shell->resized_listener.notify = handle_output_resized;
6801 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6802
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006803 screenshooter_create(ec);
6804
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006805 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006806
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006807 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006808
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006809 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6810
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006811 return 0;
6812}