blob: e81ecdda8a2735942cb60256a27c8162dc5bad53 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040029#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080038#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030039#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080040#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070041#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070042#include "shared/helpers.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080043#include "xdg-shell-unstable-v5-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050044
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020046#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010048#ifndef static_assert
49#define static_assert(cond, msg)
50#endif
51
Jonas Ådahl04769742012-06-13 00:01:24 +020052struct focus_state {
53 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020062 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050063 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030064 SHELL_SURFACE_POPUP,
65 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020066};
67
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050068struct shell_client;
69
Philip Withnall648a4dd2013-11-25 18:01:44 +000070/*
71 * Surface stacking and ordering.
72 *
73 * This is handled using several linked lists of surfaces, organised into
74 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
75 * above all of the surfaces in the layer below. The set of layers is static and
76 * in the following order (top-most first):
77 * • Lock layer (only ever displayed on its own)
78 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010079 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Fullscreen layer
81 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000082 * • Workspace layers
83 * • Background layer
84 *
85 * The list of layers may be manipulated to remove whole layers of surfaces from
86 * display. For example, when locking the screen, all layers except the lock
87 * layer are removed.
88 *
89 * A surface’s layer is modified on configuring the surface, in
90 * set_surface_type() (which is only called when the surface’s type change is
91 * _committed_). If a surface’s type changes (e.g. when making a window
92 * fullscreen) its layer changes too.
93 *
94 * In order to allow popup and transient surfaces to be correctly stacked above
95 * their parent surfaces, each surface tracks both its parent surface, and a
96 * linked list of its children. When a surface’s layer is updated, so are the
97 * layers of its children. Note that child surfaces are *not* the same as
98 * subsurfaces — child/parent surfaces are purely for maintaining stacking
99 * order.
100 *
101 * The children_link list of siblings of a surface (i.e. those surfaces which
102 * have the same parent) only contains weston_surfaces which have a
103 * shell_surface. Stacking is not implemented for non-shell_surface
104 * weston_surfaces. This means that the following implication does *not* hold:
105 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
106 */
107
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200108struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500109 struct wl_resource *resource;
110 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500111 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800112 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500115 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600116 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700118 struct wl_listener resource_destroy_listener;
119
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400120 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000121 struct wl_list children_list; /* child surfaces of this one */
122 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300123 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200124
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800125 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400126 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500127 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800128 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800129 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700130 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800131 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100132
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200134 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500135 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 } rotation;
137
138 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100139 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500140 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100141 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400142 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 } popup;
144
Alex Wu4539b082012-03-01 12:57:46 +0800145 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300146 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400147 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300148 } transient;
149
150 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800151 enum wl_shell_surface_fullscreen_method type;
152 struct weston_transform transform; /* matrix from x, y */
153 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500154 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800155 } fullscreen;
156
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200157 struct weston_transform workspace_transform;
158
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500159 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700165 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100169 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500170 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200171 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500173
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700174 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400175 int32_t x, y, width, height;
176 } geometry, next_geometry;
177 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700178
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500179 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500180
181 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200182};
183
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300184struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400185 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186 struct shell_surface *shsurf;
187 struct wl_listener shsurf_destroy_listener;
188};
189
Rusty Lynch1084da52013-08-15 09:10:08 -0700190struct shell_touch_grab {
191 struct weston_touch_grab grab;
192 struct shell_surface *shsurf;
193 struct wl_listener shsurf_destroy_listener;
194 struct weston_touch *touch;
195};
196
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197struct weston_move_grab {
198 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100199 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500200 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500201};
202
Rusty Lynch1084da52013-08-15 09:10:08 -0700203struct weston_touch_move_grab {
204 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800205 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700206 wl_fixed_t dx, dy;
207};
208
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300210 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500211 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200213 float x;
214 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 } center;
216};
217
Giulio Camuffo5085a752013-03-25 21:42:45 +0100218struct shell_seat {
219 struct weston_seat *seat;
220 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500221 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100222
Jason Ekstrand024177c2014-04-21 19:42:58 -0500223 struct wl_listener caps_changed_listener;
224 struct wl_listener pointer_focus_listener;
225 struct wl_listener keyboard_focus_listener;
226
Giulio Camuffo5085a752013-03-25 21:42:45 +0100227 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400228 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200229 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100230 struct wl_list surfaces_list;
231 struct wl_client *client;
232 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200233 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100234 } popup_grab;
235};
236
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800237struct shell_client {
238 struct wl_resource *resource;
239 struct wl_client *client;
240 struct desktop_shell *shell;
241 struct wl_listener destroy_listener;
242 struct wl_event_source *ping_timer;
243 uint32_t ping_serial;
244 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800245 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800246};
247
Alex Wubd3354b2012-04-17 17:20:49 +0800248static struct desktop_shell *
249shell_surface_get_shell(struct shell_surface *shsurf);
250
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500251static void
Derek Foreman74de4692015-07-15 13:00:39 -0500252surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500253
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300254static void
255shell_fade_startup(struct desktop_shell *shell);
256
Philip Withnallbecb77e2013-11-25 18:01:30 +0000257static struct shell_seat *
258get_shell_seat(struct weston_seat *seat);
259
Jonny Lambd73c6942014-08-20 15:53:20 +0200260static void
261get_output_panel_size(struct desktop_shell *shell,
262 struct weston_output *output,
263 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700264
Philip Withnall648a4dd2013-11-25 18:01:44 +0000265static void
266shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
267
Alex Wubd3354b2012-04-17 17:20:49 +0800268static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200269shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
270
271static bool
272shell_surface_is_xdg_surface(struct shell_surface *shsurf);
273
274static bool
275shell_surface_is_xdg_popup(struct shell_surface *shsurf);
276
277static void
278shell_surface_set_parent(struct shell_surface *shsurf,
279 struct weston_surface *parent);
280
Pekka Paalanen8274d902014-08-06 19:36:51 +0300281static int
282shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
283{
284 struct shell_surface *shsurf;
285 const char *typestr[] = {
286 [SHELL_SURFACE_NONE] = "unidentified",
287 [SHELL_SURFACE_TOPLEVEL] = "top-level",
288 [SHELL_SURFACE_POPUP] = "popup",
289 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
290 };
291 const char *t, *c;
292
293 shsurf = get_shell_surface(surface);
294 if (!shsurf)
295 return snprintf(buf, len, "unidentified window");
296
297 t = shsurf->title;
298 c = shsurf->class;
299
300 return snprintf(buf, len, "%s window%s%s%s%s%s",
301 typestr[shsurf->type],
302 t ? " '" : "", t ?: "", t ? "'" : "",
303 c ? " of " : "", c ?: "");
304}
305
Rafael Antognollie2a34552013-12-03 15:35:45 -0200306static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800307shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
308{
309 struct desktop_shell *shell;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200310 struct weston_view *view;
311 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800312
313 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100314
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300315 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800316 return false;
317
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200318 /* Find topmost shsurf on the same fullscreen output on which shsurf
319 * is displaying. We don't care about other outputs.
320 */
321 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
322 layer_link.link) {
323 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
324
325 if (cand_shsurf &&
326 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
327 top_fs_shsurf = cand_shsurf;
328 break;
329 }
330 }
331
332 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800333}
334
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500335static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400336destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337{
338 struct shell_grab *grab;
339
340 grab = container_of(listener, struct shell_grab,
341 shsurf_destroy_listener);
342
343 grab->shsurf = NULL;
344}
345
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800346struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500347get_default_view(struct weston_surface *surface)
348{
349 struct shell_surface *shsurf;
350 struct weston_view *view;
351
352 if (!surface || wl_list_empty(&surface->views))
353 return NULL;
354
355 shsurf = get_shell_surface(surface);
356 if (shsurf)
357 return shsurf->view;
358
359 wl_list_for_each(view, &surface->views, surface_link)
360 if (weston_view_is_mapped(view))
361 return view;
362
363 return container_of(surface->views.next, struct weston_view, surface_link);
364}
365
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300366static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400367popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200368static void
369touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400370
371static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300372shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400373 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800376 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300377{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500379 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400381 popup_grab_end(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -0500382 if (touch)
383 touch_popup_grab_end(touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400384
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300385 grab->grab.interface = interface;
386 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400387 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500388 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400389 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300390
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700391 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400392 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400393 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800394 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400395 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500396 weston_pointer_set_focus(pointer,
397 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400398 wl_fixed_from_int(0),
399 wl_fixed_from_int(0));
400 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300401}
402
Jonny Lambd73c6942014-08-20 15:53:20 +0200403static void
404get_output_panel_size(struct desktop_shell *shell,
405 struct weston_output *output,
406 int *width,
407 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700408{
409 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200410
411 *width = 0;
412 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700413
414 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200415 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700416
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300417 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200418 float x, y;
419
420 if (view->surface->output != output)
421 continue;
422
423 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800424 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
425 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200426 weston_view_to_global_float(view,
427 view->surface->width, 0,
428 &x, &y);
429
Derek Foreman612341f2015-04-15 12:23:55 -0500430 *width = (int)x - output->x;
431 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200432 return;
433
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800434 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
435 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200436 weston_view_to_global_float(view,
437 0, view->surface->height,
438 &x, &y);
439
Derek Foreman612341f2015-04-15 12:23:55 -0500440 *width = view->surface->width + (int)x - output->x;
441 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200442 return;
443
444 default:
445 /* we've already set width and height to
446 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700447 break;
448 }
449 }
450
Jonny Lambd73c6942014-08-20 15:53:20 +0200451 /* the correct view wasn't found */
452}
453
454static void
Quentin Glidic5c201952016-03-23 13:50:49 +0100455get_output_work_area(void *data,
Jonny Lambd73c6942014-08-20 15:53:20 +0200456 struct weston_output *output,
457 pixman_rectangle32_t *area)
458{
Quentin Glidic5c201952016-03-23 13:50:49 +0100459 struct desktop_shell *shell = data;
Jonny Lambd73c6942014-08-20 15:53:20 +0200460 int32_t panel_width = 0, panel_height = 0;
461
Derek Foremanf814c5d2015-04-15 12:23:54 -0500462 area->x = output->x;
463 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200464
465 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200466 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800467 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200468 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500469 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800470 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200471 area->width = output->width;
472 area->height = output->height - panel_height;
473 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800474 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500475 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800476 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200477 area->width = output->width - panel_width;
478 area->height = output->height;
479 break;
480 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700481}
482
Derek Foreman45a7c272015-09-11 14:27:40 -0500483static struct shell_surface *
484find_toplevel_surface(struct shell_surface *in_surface)
485{
486 struct shell_surface *surface = in_surface;
487
Ben Hummon445e44c2015-10-06 11:48:14 -0500488 while (surface) {
489 if (surface->type == SHELL_SURFACE_TOPLEVEL)
490 return surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500491 surface = get_shell_surface(surface->parent);
Ben Hummon445e44c2015-10-06 11:48:14 -0500492 }
Derek Foreman45a7c272015-09-11 14:27:40 -0500493
494 /* If no top level surface was found, just use whatever surface was
495 originally provided. */
Ben Hummon445e44c2015-10-06 11:48:14 -0500496 return in_surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500497}
498
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700499static void
500send_configure_for_surface(struct shell_surface *shsurf)
501{
502 int32_t width, height;
503 struct surface_state *state;
504
505 if (shsurf->state_requested)
506 state = &shsurf->requested_state;
507 else if (shsurf->state_changed)
508 state = &shsurf->next_state;
509 else
510 state = &shsurf->state;
511
512 if (state->fullscreen) {
513 width = shsurf->output->width;
514 height = shsurf->output->height;
515 } else if (state->maximized) {
516 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200517 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700518
519 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200520 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700521
Jonny Lambd73c6942014-08-20 15:53:20 +0200522 width = area.width;
523 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900524 } else if (shsurf->resize_edges) {
525 width = shsurf->geometry.width;
526 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700527 } else {
528 width = 0;
529 height = 0;
530 }
531
532 shsurf->client->send_configure(shsurf->surface, width, height);
533}
534
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300535static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400536shell_surface_state_changed(struct shell_surface *shsurf)
537{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700538 if (shell_surface_is_xdg_surface(shsurf))
539 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400540}
541
542static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300543shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300544{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700545 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400546 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700547 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400548
549 if (grab->shsurf->resize_edges) {
550 grab->shsurf->resize_edges = 0;
551 shell_surface_state_changed(grab->shsurf);
552 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700553 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300554
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700555 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300556}
557
558static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700559shell_touch_grab_start(struct shell_touch_grab *grab,
560 const struct weston_touch_grab_interface *interface,
561 struct shell_surface *shsurf,
562 struct weston_touch *touch)
563{
564 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500565 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800566
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200567 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500568 if (pointer)
569 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700570
Rusty Lynch1084da52013-08-15 09:10:08 -0700571 grab->grab.interface = interface;
572 grab->shsurf = shsurf;
573 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
574 wl_signal_add(&shsurf->destroy_signal,
575 &grab->shsurf_destroy_listener);
576
577 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700578 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700579
580 weston_touch_start_grab(touch, &grab->grab);
581 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500582 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700584}
585
586static void
587shell_touch_grab_end(struct shell_touch_grab *grab)
588{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700589 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700590 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700591 grab->shsurf->grabbed = 0;
592 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700593
594 weston_touch_end_grab(grab->touch);
595}
596
597static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800599 struct weston_output *output);
600
Daniel Stone496ca172012-05-30 16:31:42 +0100601static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300602get_modifier(char *modifier)
603{
604 if (!modifier)
605 return MODIFIER_SUPER;
606
607 if (!strcmp("ctrl", modifier))
608 return MODIFIER_CTRL;
609 else if (!strcmp("alt", modifier))
610 return MODIFIER_ALT;
611 else if (!strcmp("super", modifier))
612 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000613 else if (!strcmp("none", modifier))
614 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300615 else
616 return MODIFIER_SUPER;
617}
618
Juan Zhaoe10d2792012-04-25 19:09:52 +0800619static enum animation_type
620get_animation_type(char *animation)
621{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800622 if (!animation)
623 return ANIMATION_NONE;
624
Juan Zhaoe10d2792012-04-25 19:09:52 +0800625 if (!strcmp("zoom", animation))
626 return ANIMATION_ZOOM;
627 else if (!strcmp("fade", animation))
628 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100629 else if (!strcmp("dim-layer", animation))
630 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800631 else
632 return ANIMATION_NONE;
633}
634
Alex Wu4539b082012-03-01 12:57:46 +0800635static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400636shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200637{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400638 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500639 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300640 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000641 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200642
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300643 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400644 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300645 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
646 WESTON_SHELL_CLIENT);
647 if (ret < 0)
648 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400649 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500650 "client", &s, client);
651 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100652 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000653
654 weston_config_section_get_bool(section,
655 "allow-zap", &allow_zap, true);
656 shell->allow_zap = allow_zap;
657
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100658 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400659 "binding-modifier", &s, "super");
660 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200661 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800662
663 weston_config_section_get_string(section,
664 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000665 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800666 free(s);
667
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400668 weston_config_section_get_string(section, "animation", &s, "none");
669 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200670 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200671 weston_config_section_get_string(section, "close-animation", &s, "fade");
672 shell->win_close_animation_type = get_animation_type(s);
673 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700674 weston_config_section_get_string(section,
675 "startup-animation", &s, "fade");
676 shell->startup_animation_type = get_animation_type(s);
677 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700678 if (shell->startup_animation_type == ANIMATION_ZOOM)
679 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100680 weston_config_section_get_string(section, "focus-animation", &s, "none");
681 shell->focus_animation_type = get_animation_type(s);
682 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400683 weston_config_section_get_uint(section, "num-workspaces",
684 &shell->workspaces.num,
685 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200686}
687
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800688struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100689get_default_output(struct weston_compositor *compositor)
690{
Armin Krezović10b06182016-06-23 11:59:30 +0200691 if (wl_list_empty(&compositor->output_list))
692 return NULL;
693
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 return container_of(compositor->output_list.next,
695 struct weston_output, link);
696}
697
Pekka Paalanen8274d902014-08-06 19:36:51 +0300698static int
699focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
700{
701 return snprintf(buf, len, "focus highlight effect for output %s",
702 surface->output->name);
703}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100704
705/* no-op func for checking focus surface */
706static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600707focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100708{
709}
710
711static struct focus_surface *
712get_focus_surface(struct weston_surface *surface)
713{
714 if (surface->configure == focus_surface_configure)
715 return surface->configure_private;
716 else
717 return NULL;
718}
719
720static bool
721is_focus_surface (struct weston_surface *es)
722{
723 return (es->configure == focus_surface_configure);
724}
725
726static bool
727is_focus_view (struct weston_view *view)
728{
729 return is_focus_surface (view->surface);
730}
731
732static struct focus_surface *
733create_focus_surface(struct weston_compositor *ec,
734 struct weston_output *output)
735{
736 struct focus_surface *fsurf = NULL;
737 struct weston_surface *surface = NULL;
738
739 fsurf = malloc(sizeof *fsurf);
740 if (!fsurf)
741 return NULL;
742
743 fsurf->surface = weston_surface_create(ec);
744 surface = fsurf->surface;
745 if (surface == NULL) {
746 free(fsurf);
747 return NULL;
748 }
749
750 surface->configure = focus_surface_configure;
751 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200752 surface->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100753 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300754 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100755
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800756 fsurf->view = weston_view_create(surface);
757 if (fsurf->view == NULL) {
758 weston_surface_destroy(surface);
759 free(fsurf);
760 return NULL;
761 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100762 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200763 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100764
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600765 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600766 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100767 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
768 pixman_region32_fini(&surface->opaque);
769 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
770 output->width, output->height);
771 pixman_region32_fini(&surface->input);
772 pixman_region32_init(&surface->input);
773
774 wl_list_init(&fsurf->workspace_transform.link);
775
776 return fsurf;
777}
778
779static void
780focus_surface_destroy(struct focus_surface *fsurf)
781{
782 weston_surface_destroy(fsurf->surface);
783 free(fsurf);
784}
785
786static void
787focus_animation_done(struct weston_view_animation *animation, void *data)
788{
789 struct workspace *ws = data;
790
791 ws->focus_animation = NULL;
792}
793
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200794static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200795focus_state_destroy(struct focus_state *state)
796{
797 wl_list_remove(&state->seat_destroy_listener.link);
798 wl_list_remove(&state->surface_destroy_listener.link);
799 free(state);
800}
801
802static void
803focus_state_seat_destroy(struct wl_listener *listener, void *data)
804{
805 struct focus_state *state = container_of(listener,
806 struct focus_state,
807 seat_destroy_listener);
808
809 wl_list_remove(&state->link);
810 focus_state_destroy(state);
811}
812
813static void
814focus_state_surface_destroy(struct wl_listener *listener, void *data)
815{
816 struct focus_state *state = container_of(listener,
817 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400818 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400819 struct desktop_shell *shell;
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200820 struct weston_surface *main_surface;
821 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500822 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200823
Pekka Paalanen01388e22013-04-25 13:57:44 +0300824 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
825
Kristian Høgsberge3778222012-07-31 17:29:30 -0400826 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300827 wl_list_for_each(view,
828 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500829 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400830 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831 if (is_focus_view(view))
832 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400833
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200834 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400835 break;
836 }
837
Pekka Paalanen01388e22013-04-25 13:57:44 +0300838 /* if the focus was a sub-surface, activate its main surface */
839 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200840 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300841
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100842 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400843 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100845 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400846 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100847 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
848 if (state->ws->focus_animation)
849 weston_view_animation_destroy(state->ws->focus_animation);
850
851 state->ws->focus_animation = weston_fade_run(
852 state->ws->fsurf_front->view,
853 state->ws->fsurf_front->view->alpha, 0.0, 300,
854 focus_animation_done, state->ws);
855 }
856
Kristian Høgsberge3778222012-07-31 17:29:30 -0400857 wl_list_remove(&state->link);
858 focus_state_destroy(state);
859 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200860}
861
862static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400863focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200864{
Jonas Ådahl04769742012-06-13 00:01:24 +0200865 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200866
867 state = malloc(sizeof *state);
868 if (state == NULL)
869 return NULL;
870
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100871 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400872 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200873 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400874 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200875
876 state->seat_destroy_listener.notify = focus_state_seat_destroy;
877 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400878 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200879 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400880 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200881
882 return state;
883}
884
Jonas Ådahl8538b222012-08-29 22:13:03 +0200885static struct focus_state *
886ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
887{
888 struct workspace *ws = get_current_workspace(shell);
889 struct focus_state *state;
890
891 wl_list_for_each(state, &ws->focus_list, link)
892 if (state->seat == seat)
893 break;
894
895 if (&state->link == &ws->focus_list)
896 state = focus_state_create(seat, ws);
897
898 return state;
899}
900
Jonas Ådahl04769742012-06-13 00:01:24 +0200901static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800902focus_state_set_focus(struct focus_state *state,
903 struct weston_surface *surface)
904{
905 if (state->keyboard_focus) {
906 wl_list_remove(&state->surface_destroy_listener.link);
907 wl_list_init(&state->surface_destroy_listener.link);
908 }
909
910 state->keyboard_focus = surface;
911 if (surface)
912 wl_signal_add(&surface->destroy_signal,
913 &state->surface_destroy_listener);
914}
915
916static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400917restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200918{
919 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400920 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100921 struct wl_list pending_seat_list;
922 struct weston_seat *seat, *next_seat;
923
924 /* Temporarily steal the list of seats so that we can keep
925 * track of the seats we've already processed */
926 wl_list_init(&pending_seat_list);
927 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
928 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200929
930 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500931 struct weston_keyboard *keyboard =
932 weston_seat_get_keyboard(state->seat);
933
Neil Roberts4237f502014-04-09 16:33:31 +0100934 wl_list_remove(&state->seat->link);
935 wl_list_insert(&shell->compositor->seat_list,
936 &state->seat->link);
937
Derek Foreman1281a362015-07-31 16:55:32 -0500938 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800939 continue;
940
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400941 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200942
Derek Foreman1281a362015-07-31 16:55:32 -0500943 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200944 }
Neil Roberts4237f502014-04-09 16:33:31 +0100945
946 /* For any remaining seats that we don't have a focus state
947 * for we'll reset the keyboard focus to NULL */
948 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500949 struct weston_keyboard *keyboard =
950 weston_seat_get_keyboard(seat);
951
Neil Roberts4237f502014-04-09 16:33:31 +0100952 wl_list_insert(&shell->compositor->seat_list, &seat->link);
953
Derek Foreman1281a362015-07-31 16:55:32 -0500954 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100955 continue;
956
Derek Foreman1281a362015-07-31 16:55:32 -0500957 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100958 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200959}
960
961static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200962replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
963 struct weston_seat *seat)
964{
Derek Foreman1281a362015-07-31 16:55:32 -0500965 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200966 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200967
968 wl_list_for_each(state, &ws->focus_list, link) {
969 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500970 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200971 return;
972 }
973 }
974}
975
976static void
977drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
978 struct weston_surface *surface)
979{
980 struct focus_state *state;
981
982 wl_list_for_each(state, &ws->focus_list, link)
983 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800984 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200985}
986
987static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100988animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
989 struct weston_view *from, struct weston_view *to)
990{
991 struct weston_output *output;
992 bool focus_surface_created = false;
993
994 /* FIXME: Only support dim animation using two layers */
995 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
996 return;
997
998 output = get_default_output(shell->compositor);
999 if (ws->fsurf_front == NULL && (from || to)) {
1000 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001001 if (ws->fsurf_front == NULL)
1002 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001004
1005 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1006 if (ws->fsurf_back == NULL) {
1007 focus_surface_destroy(ws->fsurf_front);
1008 return;
1009 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001010 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001011
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 focus_surface_created = true;
1013 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001014 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1015 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 }
1017
1018 if (ws->focus_animation) {
1019 weston_view_animation_destroy(ws->focus_animation);
1020 ws->focus_animation = NULL;
1021 }
1022
1023 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001024 weston_layer_entry_insert(&to->layer_link,
1025 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001027 weston_layer_entry_insert(&ws->layer.view_list,
1028 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001029
1030 if (focus_surface_created) {
1031 ws->focus_animation = weston_fade_run(
1032 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001033 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 focus_animation_done, ws);
1035 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001036 weston_layer_entry_insert(&from->layer_link,
1037 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001038 ws->focus_animation = weston_stable_fade_run(
1039 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001040 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001041 focus_animation_done, ws);
1042 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001043 weston_layer_entry_insert(&ws->layer.view_list,
1044 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001045 ws->focus_animation = weston_stable_fade_run(
1046 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001047 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001048 focus_animation_done, ws);
1049 }
1050}
1051
1052static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001053workspace_destroy(struct workspace *ws)
1054{
Jonas Ådahl04769742012-06-13 00:01:24 +02001055 struct focus_state *state, *next;
1056
1057 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1058 focus_state_destroy(state);
1059
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001060 if (ws->fsurf_front)
1061 focus_surface_destroy(ws->fsurf_front);
1062 if (ws->fsurf_back)
1063 focus_surface_destroy(ws->fsurf_back);
1064
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001065 free(ws);
1066}
1067
Jonas Ådahl04769742012-06-13 00:01:24 +02001068static void
1069seat_destroyed(struct wl_listener *listener, void *data)
1070{
1071 struct weston_seat *seat = data;
1072 struct focus_state *state, *next;
1073 struct workspace *ws = container_of(listener,
1074 struct workspace,
1075 seat_destroyed_listener);
1076
1077 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1078 if (state->seat == seat)
1079 wl_list_remove(&state->link);
1080}
1081
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001082static struct workspace *
1083workspace_create(void)
1084{
1085 struct workspace *ws = malloc(sizeof *ws);
1086 if (ws == NULL)
1087 return NULL;
1088
1089 weston_layer_init(&ws->layer, NULL);
1090
Jonas Ådahl04769742012-06-13 00:01:24 +02001091 wl_list_init(&ws->focus_list);
1092 wl_list_init(&ws->seat_destroyed_listener.link);
1093 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001094 ws->fsurf_front = NULL;
1095 ws->fsurf_back = NULL;
1096 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001097
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001098 return ws;
1099}
1100
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101static int
1102workspace_is_empty(struct workspace *ws)
1103{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001104 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105}
1106
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001107static struct workspace *
1108get_workspace(struct desktop_shell *shell, unsigned int index)
1109{
1110 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001111 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001112 pws += index;
1113 return *pws;
1114}
1115
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001116struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001117get_current_workspace(struct desktop_shell *shell)
1118{
1119 return get_workspace(shell, shell->workspaces.current);
1120}
1121
1122static void
1123activate_workspace(struct desktop_shell *shell, unsigned int index)
1124{
1125 struct workspace *ws;
1126
1127 ws = get_workspace(shell, index);
1128 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1129
1130 shell->workspaces.current = index;
1131}
1132
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133static unsigned int
1134get_output_height(struct weston_output *output)
1135{
1136 return abs(output->region.extents.y1 - output->region.extents.y2);
1137}
1138
1139static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001140view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001141{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001142 struct weston_transform *transform;
1143
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001144 if (is_focus_view(view)) {
1145 struct focus_surface *fsurf = get_focus_surface(view->surface);
1146 transform = &fsurf->workspace_transform;
1147 } else {
1148 struct shell_surface *shsurf = get_shell_surface(view->surface);
1149 transform = &shsurf->workspace_transform;
1150 }
1151
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001154 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001156 weston_matrix_init(&transform->matrix);
1157 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001158 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160}
1161
1162static void
1163workspace_translate_out(struct workspace *ws, double fraction)
1164{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166 unsigned int height;
1167 double d;
1168
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001169 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001171 d = height * fraction;
1172
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001173 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174 }
1175}
1176
1177static void
1178workspace_translate_in(struct workspace *ws, double fraction)
1179{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001181 unsigned int height;
1182 double d;
1183
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001184 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001186
1187 if (fraction > 0)
1188 d = -(height - height * fraction);
1189 else
1190 d = height + height * fraction;
1191
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001192 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001193 }
1194}
1195
1196static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001197reverse_workspace_change_animation(struct desktop_shell *shell,
1198 unsigned int index,
1199 struct workspace *from,
1200 struct workspace *to)
1201{
1202 shell->workspaces.current = index;
1203
1204 shell->workspaces.anim_to = to;
1205 shell->workspaces.anim_from = from;
1206 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1207 shell->workspaces.anim_timestamp = 0;
1208
Scott Moreau4272e632012-08-13 09:58:41 -06001209 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001210}
1211
1212static void
1213workspace_deactivate_transforms(struct workspace *ws)
1214{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001216 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001217
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001218 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001219 if (is_focus_view(view)) {
1220 struct focus_surface *fsurf = get_focus_surface(view->surface);
1221 transform = &fsurf->workspace_transform;
1222 } else {
1223 struct shell_surface *shsurf = get_shell_surface(view->surface);
1224 transform = &shsurf->workspace_transform;
1225 }
1226
1227 if (!wl_list_empty(&transform->link)) {
1228 wl_list_remove(&transform->link);
1229 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001230 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001232 }
1233}
1234
1235static void
1236finish_workspace_change_animation(struct desktop_shell *shell,
1237 struct workspace *from,
1238 struct workspace *to)
1239{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001240 struct weston_view *view;
1241
Scott Moreau4272e632012-08-13 09:58:41 -06001242 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001243
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001244 /* Views that extend past the bottom of the output are still
1245 * visible after the workspace animation ends but before its layer
1246 * is hidden. In that case, we need to damage below those views so
1247 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001248 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001249 weston_view_damage_below(view);
1250
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001251 wl_list_remove(&shell->workspaces.animation.link);
1252 workspace_deactivate_transforms(from);
1253 workspace_deactivate_transforms(to);
1254 shell->workspaces.anim_to = NULL;
1255
1256 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1257}
1258
1259static void
1260animate_workspace_change_frame(struct weston_animation *animation,
1261 struct weston_output *output, uint32_t msecs)
1262{
1263 struct desktop_shell *shell =
1264 container_of(animation, struct desktop_shell,
1265 workspaces.animation);
1266 struct workspace *from = shell->workspaces.anim_from;
1267 struct workspace *to = shell->workspaces.anim_to;
1268 uint32_t t;
1269 double x, y;
1270
1271 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1272 finish_workspace_change_animation(shell, from, to);
1273 return;
1274 }
1275
1276 if (shell->workspaces.anim_timestamp == 0) {
1277 if (shell->workspaces.anim_current == 0.0)
1278 shell->workspaces.anim_timestamp = msecs;
1279 else
1280 shell->workspaces.anim_timestamp =
1281 msecs -
1282 /* Invers of movement function 'y' below. */
1283 (asin(1.0 - shell->workspaces.anim_current) *
1284 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1285 M_2_PI);
1286 }
1287
1288 t = msecs - shell->workspaces.anim_timestamp;
1289
1290 /*
1291 * x = [0, π/2]
1292 * y(x) = sin(x)
1293 */
1294 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1295 y = sin(x);
1296
1297 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001298 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001299
1300 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1301 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1302 shell->workspaces.anim_current = y;
1303
Scott Moreau4272e632012-08-13 09:58:41 -06001304 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001305 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001306 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001307 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001308}
1309
1310static void
1311animate_workspace_change(struct desktop_shell *shell,
1312 unsigned int index,
1313 struct workspace *from,
1314 struct workspace *to)
1315{
1316 struct weston_output *output;
1317
1318 int dir;
1319
1320 if (index > shell->workspaces.current)
1321 dir = -1;
1322 else
1323 dir = 1;
1324
1325 shell->workspaces.current = index;
1326
1327 shell->workspaces.anim_dir = dir;
1328 shell->workspaces.anim_from = from;
1329 shell->workspaces.anim_to = to;
1330 shell->workspaces.anim_current = 0.0;
1331 shell->workspaces.anim_timestamp = 0;
1332
1333 output = container_of(shell->compositor->output_list.next,
1334 struct weston_output, link);
1335 wl_list_insert(&output->animation_list,
1336 &shell->workspaces.animation.link);
1337
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001338 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001339
1340 workspace_translate_in(to, 0);
1341
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001342 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001343
Scott Moreau4272e632012-08-13 09:58:41 -06001344 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001345}
1346
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001347static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001348update_workspace(struct desktop_shell *shell, unsigned int index,
1349 struct workspace *from, struct workspace *to)
1350{
1351 shell->workspaces.current = index;
1352 wl_list_insert(&from->layer.link, &to->layer.link);
1353 wl_list_remove(&from->layer.link);
1354}
1355
1356static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001357change_workspace(struct desktop_shell *shell, unsigned int index)
1358{
1359 struct workspace *from;
1360 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001361 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001362
1363 if (index == shell->workspaces.current)
1364 return;
1365
1366 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001367 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001368 return;
1369
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001370 from = get_current_workspace(shell);
1371 to = get_workspace(shell, index);
1372
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001373 if (shell->workspaces.anim_from == to &&
1374 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001375 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001376 reverse_workspace_change_animation(shell, index, from, to);
1377 return;
1378 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001379
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001380 if (shell->workspaces.anim_to != NULL)
1381 finish_workspace_change_animation(shell,
1382 shell->workspaces.anim_from,
1383 shell->workspaces.anim_to);
1384
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001385 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001386
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001387 if (shell->focus_animation_type != ANIMATION_NONE) {
1388 wl_list_for_each(state, &from->focus_list, link)
1389 if (state->keyboard_focus)
1390 animate_focus_change(shell, from,
1391 get_default_view(state->keyboard_focus), NULL);
1392
1393 wl_list_for_each(state, &to->focus_list, link)
1394 if (state->keyboard_focus)
1395 animate_focus_change(shell, to,
1396 NULL, get_default_view(state->keyboard_focus));
1397 }
1398
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001399 if (workspace_is_empty(to) && workspace_is_empty(from))
1400 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001401 else
1402 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001403}
1404
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001405static bool
1406workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1407{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001408 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001409 struct wl_list *e;
1410
1411 if (wl_list_empty(list))
1412 return false;
1413
1414 e = list->next;
1415
1416 if (e->next != list)
1417 return false;
1418
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001419 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001420}
1421
1422static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001423surface_keyboard_focus_lost(struct weston_surface *surface)
1424{
1425 struct weston_compositor *compositor = surface->compositor;
1426 struct weston_seat *seat;
1427 struct weston_surface *focus;
1428
1429 wl_list_for_each(seat, &compositor->seat_list, link) {
1430 struct weston_keyboard *keyboard =
1431 weston_seat_get_keyboard(seat);
1432
1433 if (!keyboard)
1434 continue;
1435
1436 focus = weston_surface_get_main_surface(keyboard->focus);
1437 if (focus == surface)
1438 weston_keyboard_set_focus(keyboard, NULL);
1439 }
1440}
1441
1442static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001443take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001444 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001445 unsigned int index)
1446{
Derek Foreman1281a362015-07-31 16:55:32 -05001447 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001448 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001450 struct shell_surface *shsurf;
1451 struct workspace *from;
1452 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001453 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001454
Derek Foreman1281a362015-07-31 16:55:32 -05001455 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 view = get_default_view(surface);
1457 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001458 index == shell->workspaces.current ||
1459 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001460 return;
1461
1462 from = get_current_workspace(shell);
1463 to = get_workspace(shell, index);
1464
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001465 weston_layer_entry_remove(&view->layer_link);
1466 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001467
Philip Withnall659163d2013-11-25 18:01:36 +00001468 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001469 if (shsurf != NULL)
1470 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001471
Jonas Ådahle9d22502012-08-29 22:13:01 +02001472 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001473 drop_focus_state(shell, from, surface);
1474
1475 if (shell->workspaces.anim_from == to &&
1476 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001477 wl_list_remove(&to->layer.link);
1478 wl_list_insert(from->layer.link.prev, &to->layer.link);
1479
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001480 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001481
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001482 return;
1483 }
1484
1485 if (shell->workspaces.anim_to != NULL)
1486 finish_workspace_change_animation(shell,
1487 shell->workspaces.anim_from,
1488 shell->workspaces.anim_to);
1489
1490 if (workspace_is_empty(from) &&
1491 workspace_has_only(to, surface))
1492 update_workspace(shell, index, from, to);
1493 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001494 if (shsurf != NULL &&
1495 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001496 wl_list_insert(&shell->workspaces.anim_sticky_list,
1497 &shsurf->workspace_transform.link);
1498
1499 animate_workspace_change(shell, index, from, to);
1500 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001501
Jonas Ådahl8538b222012-08-29 22:13:03 +02001502 state = ensure_focus_state(shell, seat);
1503 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001504 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001505}
1506
1507static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001508touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001509 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001510{
1511}
1512
1513static void
1514touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1515{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001516 struct weston_touch_move_grab *move =
1517 (struct weston_touch_move_grab *) container_of(
1518 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001519
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001520 if (touch_id == 0)
1521 move->active = 0;
1522
Jonas Ådahl9484b692013-12-02 22:05:03 +01001523 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001524 shell_touch_grab_end(&move->base);
1525 free(move);
1526 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001527}
1528
1529static void
1530touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001531 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001532{
1533 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1534 struct shell_surface *shsurf = move->base.shsurf;
1535 struct weston_surface *es;
1536 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1537 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1538
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001539 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001540 return;
1541
1542 es = shsurf->surface;
1543
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001544 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001545
1546 weston_compositor_schedule_repaint(es->compositor);
1547}
1548
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001549static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001550touch_move_grab_frame(struct weston_touch_grab *grab)
1551{
1552}
1553
1554static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001555touch_move_grab_cancel(struct weston_touch_grab *grab)
1556{
1557 struct weston_touch_move_grab *move =
1558 (struct weston_touch_move_grab *) container_of(
1559 grab, struct shell_touch_grab, grab);
1560
1561 shell_touch_grab_end(&move->base);
1562 free(move);
1563}
1564
Rusty Lynch1084da52013-08-15 09:10:08 -07001565static const struct weston_touch_grab_interface touch_move_grab_interface = {
1566 touch_move_grab_down,
1567 touch_move_grab_up,
1568 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001569 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001570 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001571};
1572
1573static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001574surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001575{
1576 struct weston_touch_move_grab *move;
1577
1578 if (!shsurf)
1579 return -1;
1580
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001581 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001582 return 0;
1583
1584 move = malloc(sizeof *move);
1585 if (!move)
1586 return -1;
1587
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001588 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001590 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001592 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001593
1594 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001595 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001596
1597 return 0;
1598}
1599
1600static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001601noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001602{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001603}
1604
1605static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001606noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001607 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001608{
1609}
1610
1611static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001612noop_grab_axis_source(struct weston_pointer_grab *grab,
1613 uint32_t source)
1614{
1615}
1616
1617static void
1618noop_grab_frame(struct weston_pointer_grab *grab)
1619{
1620}
1621
1622static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001623constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1624{
1625 struct shell_surface *shsurf = move->base.shsurf;
1626 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001627 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001628 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001629 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001630
1631 x = wl_fixed_to_int(pointer->x + move->dx);
1632 y = wl_fixed_to_int(pointer->y + move->dy);
1633
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001634 if (shsurf->shell->panel_position ==
1635 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001636 get_output_work_area(shsurf->shell,
1637 shsurf->surface->output,
1638 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001639
Derek Foreman6feb0f92015-04-15 12:23:56 -05001640 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1641 if (bottom - safety < area.y)
1642 y = area.y + safety - shsurf->geometry.height
1643 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001644
1645 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001646 y + shsurf->geometry.y < area.y)
1647 y = area.y - shsurf->geometry.y;
1648
Jonny Lambd73c6942014-08-20 15:53:20 +02001649 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001650
1651 *cx = x;
1652 *cy = y;
1653}
1654
1655static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001656move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001657 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001658{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001659 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001660 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001661 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001662 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001663
Jonas Ådahld2510102014-10-05 21:39:14 +02001664 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001665 if (!shsurf)
1666 return;
1667
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001668 constrain_position(move, &cx, &cy);
1669
1670 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001671
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001673}
1674
1675static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001676move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001677 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001678{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001679 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1680 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001681 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001682 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001683
Daniel Stone4dbadb12012-05-30 16:31:51 +01001684 if (pointer->button_count == 0 &&
1685 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001686 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001687 free(grab);
1688 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001689}
1690
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001691static void
1692move_grab_cancel(struct weston_pointer_grab *grab)
1693{
1694 struct shell_grab *shell_grab =
1695 container_of(grab, struct shell_grab, grab);
1696
1697 shell_grab_end(shell_grab);
1698 free(grab);
1699}
1700
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001701static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001702 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001703 move_grab_motion,
1704 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001705 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001706 noop_grab_axis_source,
1707 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001708 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001709};
1710
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001712surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001713 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714{
1715 struct weston_move_grab *move;
1716
1717 if (!shsurf)
1718 return -1;
1719
Derek Foreman45a7c272015-09-11 14:27:40 -05001720 shsurf = find_toplevel_surface(shsurf);
1721
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001722 if (shsurf->grabbed ||
1723 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724 return 0;
1725
1726 move = malloc(sizeof *move);
1727 if (!move)
1728 return -1;
1729
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001731 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001733 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001734 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001735
1736 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001737 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738
1739 return 0;
1740}
1741
1742static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001743common_surface_move(struct wl_resource *resource,
1744 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001746 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001747 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1748 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001749 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001750 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751
Derek Foreman1281a362015-07-31 16:55:32 -05001752 if (pointer &&
1753 pointer->focus &&
1754 pointer->button_count > 0 &&
1755 pointer->grab_serial == serial) {
1756 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001757 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001758 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001759 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001760 } else if (touch &&
1761 touch->focus &&
1762 touch->grab_serial == serial) {
1763 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001764 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001765 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001766 wl_resource_post_no_memory(resource);
1767 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001768}
1769
Rafael Antognollie2a34552013-12-03 15:35:45 -02001770static void
1771shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1772 struct wl_resource *seat_resource, uint32_t serial)
1773{
1774 common_surface_move(resource, seat_resource, serial);
1775}
1776
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001777struct weston_resize_grab {
1778 struct shell_grab base;
1779 uint32_t edges;
1780 int32_t width, height;
1781};
1782
1783static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001784resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001785 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786{
1787 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001788 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001789 struct shell_surface *shsurf = resize->base.shsurf;
1790 int32_t width, height;
1791 wl_fixed_t from_x, from_y;
1792 wl_fixed_t to_x, to_y;
1793
Jonas Ådahld2510102014-10-05 21:39:14 +02001794 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001795
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001796 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001797 return;
1798
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 weston_view_from_global_fixed(shsurf->view,
1800 pointer->grab_x, pointer->grab_y,
1801 &from_x, &from_y);
1802 weston_view_from_global_fixed(shsurf->view,
1803 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001804
1805 width = resize->width;
1806 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1807 width += wl_fixed_to_int(from_x - to_x);
1808 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1809 width += wl_fixed_to_int(to_x - from_x);
1810 }
1811
1812 height = resize->height;
1813 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1814 height += wl_fixed_to_int(from_y - to_y);
1815 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1816 height += wl_fixed_to_int(to_y - from_y);
1817 }
1818
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001819 if (width < 1)
1820 width = 1;
1821 if (height < 1)
1822 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001823 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001824}
1825
1826static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001827send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001828{
1829 struct shell_surface *shsurf = get_shell_surface(surface);
1830
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001831 assert(shsurf);
1832
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001833 if (shsurf->resource)
1834 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001835 shsurf->resize_edges,
1836 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001837}
1838
1839static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001840 send_configure,
1841 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001842};
1843
1844static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001845resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001846 uint32_t time, uint32_t button, uint32_t state_w)
1847{
1848 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001849 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001850 enum wl_pointer_button_state state = state_w;
1851
1852 if (pointer->button_count == 0 &&
1853 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1854 shell_grab_end(&resize->base);
1855 free(grab);
1856 }
1857}
1858
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001859static void
1860resize_grab_cancel(struct weston_pointer_grab *grab)
1861{
1862 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1863
1864 shell_grab_end(&resize->base);
1865 free(grab);
1866}
1867
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001868static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001869 noop_grab_focus,
1870 resize_grab_motion,
1871 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001872 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001873 noop_grab_axis_source,
1874 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001875 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001876};
1877
Giulio Camuffob8366642013-04-25 13:57:46 +03001878/*
1879 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001880 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001881static void
1882surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1883 int32_t *y, int32_t *w, int32_t *h) {
1884 pixman_region32_t region;
1885 pixman_box32_t *box;
1886 struct weston_subsurface *subsurface;
1887
1888 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889 surface->width,
1890 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001891
1892 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1893 pixman_region32_union_rect(&region, &region,
1894 subsurface->position.x,
1895 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896 subsurface->surface->width,
1897 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001898 }
1899
1900 box = pixman_region32_extents(&region);
1901 if (x)
1902 *x = box->x1;
1903 if (y)
1904 *y = box->y1;
1905 if (w)
1906 *w = box->x2 - box->x1;
1907 if (h)
1908 *h = box->y2 - box->y1;
1909
1910 pixman_region32_fini(&region);
1911}
1912
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001913static int
1914surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001915 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001916{
1917 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001918 const unsigned resize_topbottom =
1919 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1920 const unsigned resize_leftright =
1921 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1922 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001923
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001924 if (shsurf->grabbed ||
1925 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001926 return 0;
1927
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001928 /* Check for invalid edge combinations. */
1929 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1930 (edges & resize_topbottom) == resize_topbottom ||
1931 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001932 return 0;
1933
1934 resize = malloc(sizeof *resize);
1935 if (!resize)
1936 return -1;
1937
1938 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001939
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001940 resize->width = shsurf->geometry.width;
1941 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001942
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001943 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001944 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001945 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001946 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001947
1948 return 0;
1949}
1950
1951static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001952common_surface_resize(struct wl_resource *resource,
1953 struct wl_resource *seat_resource, uint32_t serial,
1954 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001955{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001956 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001957 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001958 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001959 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001960
Derek Foreman1281a362015-07-31 16:55:32 -05001961 if (!pointer ||
1962 pointer->button_count == 0 ||
1963 pointer->grab_serial != serial ||
1964 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001965 return;
1966
Derek Foreman1281a362015-07-31 16:55:32 -05001967 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001968 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001969 return;
1970
Derek Foreman1281a362015-07-31 16:55:32 -05001971 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001972 wl_resource_post_no_memory(resource);
1973}
1974
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001975static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001976shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1977 struct wl_resource *seat_resource, uint32_t serial,
1978 uint32_t edges)
1979{
1980 common_surface_resize(resource, seat_resource, serial, edges);
1981}
1982
1983static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001984busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001985{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001986 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001987 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001989 wl_fixed_t sx, sy;
1990
Jason Ekstranda7af7042013-10-12 22:38:11 -05001991 view = weston_compositor_pick_view(pointer->seat->compositor,
1992 pointer->x, pointer->y,
1993 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001994
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001995 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1996 shell_grab_end(grab);
1997 free(grab);
1998 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001999}
2000
2001static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002002busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02002003 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002004{
Jonas Ådahld2510102014-10-05 21:39:14 +02002005 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002006}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002007
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002008static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002009busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002010 uint32_t time, uint32_t button, uint32_t state)
2011{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002012 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002013 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002014 struct weston_pointer *pointer = grab->grab.pointer;
2015 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002016
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002017 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02002018 activate(shsurf->shell, shsurf->view, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002019 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002020 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02002021 activate(shsurf->shell, shsurf->view, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002022 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002023 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024}
2025
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002026static void
2027busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2028{
2029 struct shell_grab *grab = (struct shell_grab *) base;
2030
2031 shell_grab_end(grab);
2032 free(grab);
2033}
2034
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002035static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036 busy_cursor_grab_focus,
2037 busy_cursor_grab_motion,
2038 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002039 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002040 noop_grab_axis_source,
2041 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002042 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043};
2044
2045static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002046set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047{
2048 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002049
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002050 if (pointer->grab->interface == &busy_cursor_grab_interface)
2051 return;
2052
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002053 grab = malloc(sizeof *grab);
2054 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002055 return;
2056
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002057 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002058 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002059 /* Mark the shsurf as ungrabbed so that button binding is able
2060 * to move it. */
2061 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002062}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002063
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002064static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002065end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002066{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002067 struct shell_grab *grab;
2068 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002069
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002070 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002071 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2072
2073 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002074 continue;
2075
Derek Foreman1281a362015-07-31 16:55:32 -05002076 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002077 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002078 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002079 wl_resource_get_client(grab->shsurf->resource) == client) {
2080 shell_grab_end(grab);
2081 free(grab);
2082 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002083 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002084}
2085
Scott Moreau9521d5e2012-04-19 13:06:17 -06002086static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002087handle_shell_client_destroy(struct wl_listener *listener, void *data);
2088
2089static int
2090xdg_ping_timeout_handler(void *data)
2091{
2092 struct shell_client *sc = data;
2093 struct weston_seat *seat;
2094 struct shell_surface *shsurf;
2095
2096 /* Client is not responding */
2097 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002098 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002099 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2100
2101 if (!pointer ||
2102 !pointer->focus ||
2103 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002104 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002105
Derek Foreman1281a362015-07-31 16:55:32 -05002106 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002107 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002108 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002109 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002110 }
2111
2112 return 1;
2113}
2114
2115static void
2116handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2117{
2118 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002119 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002120 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002121 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002122
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002123 if (sc->unresponsive) {
2124 xdg_ping_timeout_handler(sc);
2125 return;
2126 }
2127
2128 sc->ping_serial = serial;
2129 loop = wl_display_get_event_loop(compositor->wl_display);
2130 if (sc->ping_timer == NULL)
2131 sc->ping_timer =
2132 wl_event_loop_add_timer(loop,
2133 xdg_ping_timeout_handler, sc);
2134 if (sc->ping_timer == NULL)
2135 return;
2136
2137 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2138
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002139 if (shell_surface_is_xdg_surface(shsurf) ||
2140 shell_surface_is_xdg_popup(shsurf))
2141 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002142 else if (shell_surface_is_wl_shell_surface(shsurf)
2143 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002144 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002145}
2146
Scott Moreauff1db4a2012-04-17 19:06:18 -06002147static void
2148ping_handler(struct weston_surface *surface, uint32_t serial)
2149{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002150 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002151
2152 if (!shsurf)
2153 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002154 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002155 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002156 if (shsurf->surface == shsurf->shell->grab_surface)
2157 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002158 if (!shsurf->owner)
2159 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002160
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002161 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002162}
2163
2164static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002165handle_pointer_focus(struct wl_listener *listener, void *data)
2166{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002167 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002169 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002170 uint32_t serial;
2171
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002173 return;
2174
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002176 serial = wl_display_next_serial(compositor->wl_display);
2177 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002178}
2179
2180static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002181shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2182{
2183 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002184 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002185}
2186
2187static void
2188shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2189{
2190 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002191 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002192}
2193
2194static void
2195handle_keyboard_focus(struct wl_listener *listener, void *data)
2196{
2197 struct weston_keyboard *keyboard = data;
2198 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2199
2200 if (seat->focused_surface) {
2201 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2202 if (shsurf)
2203 shell_surface_lose_keyboard_focus(shsurf);
2204 }
2205
2206 seat->focused_surface = keyboard->focus;
2207
2208 if (seat->focused_surface) {
2209 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2210 if (shsurf)
2211 shell_surface_gain_keyboard_focus(shsurf);
2212 }
2213}
2214
2215static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002216shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002217{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002218 if (sc->ping_serial != serial)
2219 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002220
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002221 sc->unresponsive = 0;
2222 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002223
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002224 if (sc->ping_timer) {
2225 wl_event_source_remove(sc->ping_timer);
2226 sc->ping_timer = NULL;
2227 }
2228
2229}
2230
2231static void
2232shell_surface_pong(struct wl_client *client,
2233 struct wl_resource *resource, uint32_t serial)
2234{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002235 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2236 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002237
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002238 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002239}
2240
2241static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002242set_title(struct shell_surface *shsurf, const char *title)
2243{
2244 free(shsurf->title);
2245 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002246 shsurf->surface->timeline.force_refresh = 1;
2247}
2248
2249static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002250set_pid(struct shell_surface *shsurf, pid_t pid)
2251{
2252 /* We have no use for it */
2253}
2254
2255static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002256set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2257{
2258 shsurf->type = t;
2259 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002260}
2261
2262static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002263set_window_geometry(struct shell_surface *shsurf,
2264 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002265{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002266 shsurf->next_geometry.x = x;
2267 shsurf->next_geometry.y = y;
2268 shsurf->next_geometry.width = width;
2269 shsurf->next_geometry.height = height;
2270 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002271}
2272
2273static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002274shell_surface_set_title(struct wl_client *client,
2275 struct wl_resource *resource, const char *title)
2276{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002277 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002278
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002279 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002280}
2281
2282static void
2283shell_surface_set_class(struct wl_client *client,
2284 struct wl_resource *resource, const char *class)
2285{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002286 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002287
2288 free(shsurf->class);
2289 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002290 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002291}
2292
Alex Wu4539b082012-03-01 12:57:46 +08002293static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002294restore_output_mode(struct weston_output *output)
2295{
Armin Krezovićc77f2582016-06-23 11:59:32 +02002296 if (output && output->original_mode)
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002297 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002298}
2299
2300static void
2301restore_all_output_modes(struct weston_compositor *compositor)
2302{
2303 struct weston_output *output;
2304
2305 wl_list_for_each(output, &compositor->output_list, link)
2306 restore_output_mode(output);
2307}
2308
Philip Withnall07926d92013-11-25 18:01:40 +00002309/* The surface will be inserted into the list immediately after the link
2310 * returned by this function (i.e. will be stacked immediately above the
2311 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002312static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002313shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2314{
2315 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002316 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002317
2318 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002319 case SHELL_SURFACE_XWAYLAND:
2320 return &shsurf->shell->fullscreen_layer.view_list;
2321
2322 case SHELL_SURFACE_NONE:
2323 return NULL;
2324
Kristian Høgsbergead52422014-01-01 13:51:52 -08002325 case SHELL_SURFACE_POPUP:
2326 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002327 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002328 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002329 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002330 /* Move the surface to its parent layer so
2331 * that surfaces which are transient for
2332 * fullscreen surfaces don't get hidden by the
2333 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002334
2335 /* TODO: Handle a parent with multiple views */
2336 parent = get_default_view(shsurf->parent);
2337 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002338 return container_of(parent->layer_link.link.prev,
2339 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002340 }
Philip Withnall07926d92013-11-25 18:01:40 +00002341
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002342 /* Move the surface to a normal workspace layer so that surfaces
2343 * which were previously fullscreen or transient are no longer
2344 * rendered on top. */
2345 ws = get_current_workspace(shsurf->shell);
2346 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002347 }
2348
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002349 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002350}
2351
Philip Withnall648a4dd2013-11-25 18:01:44 +00002352static void
2353shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2354{
2355 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002356 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002357
2358 /* Move the child layers to the same workspace as shsurf. They will be
2359 * stacked above shsurf. */
2360 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002361 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002362 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002363 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002364 prev = container_of(shsurf->view->layer_link.link.prev,
2365 struct weston_layer_entry, link);
2366 weston_layer_entry_remove(&child->view->layer_link);
2367 weston_layer_entry_insert(prev,
2368 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002369 weston_view_geometry_dirty(child->view);
2370 weston_surface_damage(child->surface);
2371
2372 /* Recurse. We don’t expect this to recurse very far (if
2373 * at all) because that would imply we have transient
2374 * (or popup) children of transient surfaces, which
2375 * would be unusual. */
2376 shell_surface_update_child_surface_layers(child);
2377 }
2378 }
2379}
2380
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002381/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002382 * geometry to ensure the changes are redrawn.
2383 *
2384 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2385 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002386static void
2387shell_surface_update_layer(struct shell_surface *shsurf)
2388{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002389 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002390
2391 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2392
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002393 if (new_layer_link == NULL)
2394 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002395 if (new_layer_link == &shsurf->view->layer_link)
2396 return;
2397
2398 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002399 weston_layer_entry_remove(&shsurf->view->layer_link);
2400 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002401 weston_view_geometry_dirty(shsurf->view);
2402 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002403
2404 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002405}
2406
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002407static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002408shell_surface_set_parent(struct shell_surface *shsurf,
2409 struct weston_surface *parent)
2410{
2411 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002412
2413 wl_list_remove(&shsurf->children_link);
2414 wl_list_init(&shsurf->children_link);
2415
2416 /* Insert into the parent surface’s child list. */
2417 if (parent != NULL) {
2418 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2419 if (parent_shsurf != NULL)
2420 wl_list_insert(&parent_shsurf->children_list,
2421 &shsurf->children_link);
2422 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002423}
2424
2425static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002426shell_surface_set_output(struct shell_surface *shsurf,
2427 struct weston_output *output)
2428{
2429 struct weston_surface *es = shsurf->surface;
2430
2431 /* get the default output, if the client set it as NULL
2432 check whether the ouput is available */
2433 if (output)
2434 shsurf->output = output;
2435 else if (es->output)
2436 shsurf->output = es->output;
2437 else
2438 shsurf->output = get_default_output(es->compositor);
2439}
2440
2441static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002442surface_clear_next_states(struct shell_surface *shsurf)
2443{
2444 shsurf->next_state.maximized = false;
2445 shsurf->next_state.fullscreen = false;
2446
2447 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2448 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2449 shsurf->state_changed = true;
2450}
2451
2452static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002453set_toplevel(struct shell_surface *shsurf)
2454{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002455 shell_surface_set_parent(shsurf, NULL);
2456 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002457 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002458
2459 /* The layer_link is updated in set_surface_type(),
2460 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002461}
2462
2463static void
2464shell_surface_set_toplevel(struct wl_client *client,
2465 struct wl_resource *resource)
2466{
2467 struct shell_surface *surface = wl_resource_get_user_data(resource);
2468
2469 set_toplevel(surface);
2470}
2471
2472static void
2473set_transient(struct shell_surface *shsurf,
2474 struct weston_surface *parent, int x, int y, uint32_t flags)
2475{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002476 assert(parent != NULL);
2477
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002478 shell_surface_set_parent(shsurf, parent);
2479
2480 surface_clear_next_states(shsurf);
2481
Philip Withnallbecb77e2013-11-25 18:01:30 +00002482 shsurf->transient.x = x;
2483 shsurf->transient.y = y;
2484 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002485
Rafael Antognollied207b42013-12-03 15:35:43 -02002486 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002487 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002488 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002489
2490 /* The layer_link is updated in set_surface_type(),
2491 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002492}
2493
2494static void
2495shell_surface_set_transient(struct wl_client *client,
2496 struct wl_resource *resource,
2497 struct wl_resource *parent_resource,
2498 int x, int y, uint32_t flags)
2499{
2500 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2501 struct weston_surface *parent =
2502 wl_resource_get_user_data(parent_resource);
2503
2504 set_transient(shsurf, parent, x, y, flags);
2505}
2506
2507static void
2508set_fullscreen(struct shell_surface *shsurf,
2509 uint32_t method,
2510 uint32_t framerate,
2511 struct weston_output *output)
2512{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002513 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002514 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002515
2516 shsurf->fullscreen_output = shsurf->output;
2517 shsurf->fullscreen.type = method;
2518 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002519
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002520 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002521}
2522
2523static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002524weston_view_set_initial_position(struct weston_view *view,
2525 struct desktop_shell *shell);
2526
2527static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002528unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002529{
Philip Withnallf85fe842013-11-25 18:01:37 +00002530 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002531 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2532 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002533 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002534 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002535
Alex Wu4539b082012-03-01 12:57:46 +08002536 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2537 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002538
Alex Wu4539b082012-03-01 12:57:46 +08002539 wl_list_remove(&shsurf->fullscreen.transform.link);
2540 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002541
Jason Ekstranda7af7042013-10-12 22:38:11 -05002542 if (shsurf->fullscreen.black_view)
2543 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2544 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002545
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002546 if (shsurf->saved_position_valid)
2547 weston_view_set_position(shsurf->view,
2548 shsurf->saved_x, shsurf->saved_y);
2549 else
2550 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2551
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002552 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002553 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002554 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002555 shsurf->saved_rotation_valid = false;
2556 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002557
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002558 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002559}
2560
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002561static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002562shell_surface_set_fullscreen(struct wl_client *client,
2563 struct wl_resource *resource,
2564 uint32_t method,
2565 uint32_t framerate,
2566 struct wl_resource *output_resource)
2567{
2568 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2569 struct weston_output *output;
2570
Emmanuel Gil Peyrot28834b62016-05-10 03:22:43 +02002571 if (shsurf->fullscreen_output == shsurf->output &&
2572 shsurf->fullscreen.type == method &&
2573 shsurf->fullscreen.framerate == framerate) {
2574 send_configure_for_surface(shsurf);
2575 return;
2576 }
2577
Philip Withnallbecb77e2013-11-25 18:01:30 +00002578 if (output_resource)
2579 output = wl_resource_get_user_data(output_resource);
2580 else
2581 output = NULL;
2582
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002583 shell_surface_set_parent(shsurf, NULL);
2584
Rafael Antognolli03b16592013-12-03 15:35:42 -02002585 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002586 shsurf->next_state.fullscreen = true;
2587 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002588 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002589}
2590
2591static void
2592set_popup(struct shell_surface *shsurf,
2593 struct weston_surface *parent,
2594 struct weston_seat *seat,
2595 uint32_t serial,
2596 int32_t x,
2597 int32_t y)
2598{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002599 assert(parent != NULL);
2600
Philip Withnallbecb77e2013-11-25 18:01:30 +00002601 shsurf->popup.shseat = get_shell_seat(seat);
2602 shsurf->popup.serial = serial;
2603 shsurf->popup.x = x;
2604 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002605
Pekka Paalanenb5026542014-11-12 15:09:24 +02002606 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002607}
2608
2609static void
2610shell_surface_set_popup(struct wl_client *client,
2611 struct wl_resource *resource,
2612 struct wl_resource *seat_resource,
2613 uint32_t serial,
2614 struct wl_resource *parent_resource,
2615 int32_t x, int32_t y, uint32_t flags)
2616{
2617 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002618 struct weston_surface *parent =
2619 wl_resource_get_user_data(parent_resource);
2620
2621 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002622
Rafael Antognolli03b16592013-12-03 15:35:42 -02002623 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002624 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002625 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002626 wl_resource_get_user_data(seat_resource),
2627 serial, x, y);
2628}
2629
2630static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002631unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002632{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002633 /* undo all maximized things here */
2634 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002635
2636 if (shsurf->saved_position_valid)
2637 weston_view_set_position(shsurf->view,
2638 shsurf->saved_x, shsurf->saved_y);
2639 else
2640 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002641
2642 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002643 wl_list_insert(&shsurf->view->geometry.transformation_list,
2644 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002645 shsurf->saved_rotation_valid = false;
2646 }
2647
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002648 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002649}
2650
Philip Withnallbecb77e2013-11-25 18:01:30 +00002651static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002652set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002653{
2654 struct shell_surface *shsurf;
2655 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002656 struct weston_view *view;
2657
2658 view = get_default_view(surface);
2659 if (!view)
2660 return;
2661
2662 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2663
2664 shsurf = get_shell_surface(surface);
2665 current_ws = get_current_workspace(shsurf->shell);
2666
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002667 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002668 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002669
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002670 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002671 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002672
2673 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002674 weston_view_damage_below(view);
2675}
2676
2677static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002678shell_surface_set_maximized(struct wl_client *client,
2679 struct wl_resource *resource,
2680 struct wl_resource *output_resource)
2681{
2682 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2683 struct weston_output *output;
2684
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002685 surface_clear_next_states(shsurf);
2686 shsurf->next_state.maximized = true;
2687 shsurf->state_changed = true;
2688
Pekka Paalanenb5026542014-11-12 15:09:24 +02002689 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002690 shell_surface_set_parent(shsurf, NULL);
2691
Philip Withnallbecb77e2013-11-25 18:01:30 +00002692 if (output_resource)
2693 output = wl_resource_get_user_data(output_resource);
2694 else
2695 output = NULL;
2696
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002697 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002698
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002699 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002700}
2701
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002702/* This is only ever called from set_surface_type(), so there’s no need to
2703 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002704static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002705reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002706{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002707 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002708 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002709 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002710 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002711
Pekka Paalanen98262232011-12-01 10:42:22 +02002712 return 0;
2713}
2714
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002715static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002716set_full_output(struct shell_surface *shsurf)
2717{
2718 shsurf->saved_x = shsurf->view->geometry.x;
2719 shsurf->saved_y = shsurf->view->geometry.y;
2720 shsurf->saved_position_valid = true;
2721
2722 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2723 wl_list_remove(&shsurf->rotation.transform.link);
2724 wl_list_init(&shsurf->rotation.transform.link);
2725 weston_view_geometry_dirty(shsurf->view);
2726 shsurf->saved_rotation_valid = true;
2727 }
2728}
2729
2730static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002731set_surface_type(struct shell_surface *shsurf)
2732{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002733 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002735
Philip Withnallbecb77e2013-11-25 18:01:30 +00002736 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002737
Rafael Antognolli03b16592013-12-03 15:35:42 -02002738 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002739 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002740
2741 switch (shsurf->type) {
2742 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002743 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002744 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002745 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 weston_view_set_position(shsurf->view,
2747 pev->geometry.x + shsurf->transient.x,
2748 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002749 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002750 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002751
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002752 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753 weston_view_set_position(shsurf->view, shsurf->transient.x,
2754 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002755 break;
2756
Philip Withnall0f640e22013-11-25 18:01:31 +00002757 case SHELL_SURFACE_POPUP:
2758 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002759 default:
2760 break;
2761 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002762
2763 /* Update the surface’s layer. */
2764 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002765}
2766
Tiago Vignattibe143262012-04-16 17:31:41 +03002767static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002768shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002769{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002770 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002771}
2772
Pekka Paalanen8274d902014-08-06 19:36:51 +03002773static int
2774black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2775{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002776 struct weston_view *fs_view = surface->configure_private;
2777 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002778 int n;
2779 int rem;
2780 int ret;
2781
2782 n = snprintf(buf, len, "black background surface for ");
2783 if (n < 0)
2784 return n;
2785
2786 rem = (int)len - n;
2787 if (rem < 0)
2788 rem = 0;
2789
2790 if (fs_surface->get_label)
2791 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2792 else
2793 ret = snprintf(buf + n, rem, "<unknown>");
2794
2795 if (ret < 0)
2796 return n;
2797
2798 return n + ret;
2799}
2800
Alex Wu21858432012-04-01 20:13:08 +08002801static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002802black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002803
Jason Ekstranda7af7042013-10-12 22:38:11 -05002804static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002805create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002806 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002807 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002808{
2809 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002811
2812 surface = weston_surface_create(ec);
2813 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002814 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002815 return NULL;
2816 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002817 view = weston_view_create(surface);
2818 if (surface == NULL) {
2819 weston_log("no memory\n");
2820 weston_surface_destroy(surface);
2821 return NULL;
2822 }
Alex Wu4539b082012-03-01 12:57:46 +08002823
Alex Wu21858432012-04-01 20:13:08 +08002824 surface->configure = black_surface_configure;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002825 surface->configure_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002826 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002827 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002828 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002829 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002830 pixman_region32_fini(&surface->input);
2831 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002832
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002833 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002834 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002835
2836 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002837}
2838
Philip Withnalled8f1a92013-11-25 18:01:43 +00002839static void
2840shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2841{
2842 struct weston_output *output = shsurf->fullscreen_output;
2843
Rafael Antognolli03b16592013-12-03 15:35:42 -02002844 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002845
2846 if (!shsurf->fullscreen.black_view)
2847 shsurf->fullscreen.black_view =
2848 create_black_surface(shsurf->surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002849 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002850 output->x, output->y,
2851 output->width,
2852 output->height);
2853
2854 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002855 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2856 weston_layer_entry_insert(&shsurf->view->layer_link,
2857 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002858 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2859 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002860
Armin Krezović4663aca2016-06-30 06:04:29 +02002861 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002862 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002863}
2864
Alex Wu4539b082012-03-01 12:57:46 +08002865/* Create black surface and append it to the associated fullscreen surface.
2866 * Handle size dismatch and positioning according to the method. */
2867static void
2868shell_configure_fullscreen(struct shell_surface *shsurf)
2869{
2870 struct weston_output *output = shsurf->fullscreen_output;
2871 struct weston_surface *surface = shsurf->surface;
2872 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002873 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002874 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002875
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002876 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2877 restore_output_mode(output);
2878
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002879 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002880 weston_layer_entry_remove(&shsurf->view->layer_link);
2881 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002882
Philip Withnalled8f1a92013-11-25 18:01:43 +00002883 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002884
Jason Ekstranda7af7042013-10-12 22:38:11 -05002885 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002886 &surf_width, &surf_height);
2887
Alex Wu4539b082012-03-01 12:57:46 +08002888 switch (shsurf->fullscreen.type) {
2889 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002890 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002892 break;
2893 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002894 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002895 if (output->width == surf_width &&
2896 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002897 weston_view_set_position(shsurf->view,
2898 output->x - surf_x,
2899 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002900 break;
2901 }
2902
Alex Wu4539b082012-03-01 12:57:46 +08002903 matrix = &shsurf->fullscreen.transform.matrix;
2904 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002905
Scott Moreau1bad5db2012-08-18 01:04:05 -06002906 output_aspect = (float) output->width /
2907 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002908 /* XXX: Use surf_width and surf_height here? */
2909 surface_aspect = (float) surface->width /
2910 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002911 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002912 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002913 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002914 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002915 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002916 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002917
Alex Wu4539b082012-03-01 12:57:46 +08002918 weston_matrix_scale(matrix, scale, scale, 1);
2919 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002920 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002921 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002922 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2923 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002924 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002925
Alex Wu4539b082012-03-01 12:57:46 +08002926 break;
2927 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002928 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002929 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002930 surf_width * surface->buffer_viewport.buffer.scale,
2931 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002932 shsurf->fullscreen.framerate};
2933
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002934 if (weston_output_mode_switch_to_temporary(output, &mode,
2935 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 weston_view_set_position(shsurf->view,
2937 output->x - surf_x,
2938 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002939 shsurf->fullscreen.black_view->surface->width = output->width;
2940 shsurf->fullscreen.black_view->surface->height = output->height;
2941 weston_view_set_position(shsurf->fullscreen.black_view,
2942 output->x - surf_x,
2943 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002944 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002945 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002946 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002947 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002948 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002949 }
Alex Wubd3354b2012-04-17 17:20:49 +08002950 }
Alex Wu4539b082012-03-01 12:57:46 +08002951 break;
2952 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002953 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002954 break;
2955 default:
2956 break;
2957 }
2958}
2959
Alex Wu4539b082012-03-01 12:57:46 +08002960static void
2961shell_map_fullscreen(struct shell_surface *shsurf)
2962{
Alex Wubd3354b2012-04-17 17:20:49 +08002963 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002964}
2965
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002966static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002967set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2968{
2969 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002970 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002971 shsurf->transient.x = x;
2972 shsurf->transient.y = y;
2973 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002974
2975 shell_surface_set_parent(shsurf, NULL);
2976
Pekka Paalanenb5026542014-11-12 15:09:24 +02002977 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002978 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002979}
2980
Kristian Høgsberg47792412014-05-04 13:47:06 -07002981static void
2982shell_interface_set_fullscreen(struct shell_surface *shsurf,
2983 uint32_t method,
2984 uint32_t framerate,
2985 struct weston_output *output)
2986{
2987 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002988 shsurf->next_state.fullscreen = true;
2989 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002990
2991 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002992}
2993
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002994static struct weston_output *
2995get_focused_output(struct weston_compositor *compositor)
2996{
2997 struct weston_seat *seat;
2998 struct weston_output *output = NULL;
2999
3000 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003001 struct weston_touch *touch = weston_seat_get_touch(seat);
3002 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3003 struct weston_keyboard *keyboard =
3004 weston_seat_get_keyboard(seat);
3005
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003006 /* Priority has touch focus, then pointer and
3007 * then keyboard focus. We should probably have
3008 * three for loops and check frist for touch,
3009 * then for pointer, etc. but unless somebody has some
3010 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003011 if (touch && touch->focus)
3012 output = touch->focus->output;
3013 else if (pointer && pointer->focus)
3014 output = pointer->focus->output;
3015 else if (keyboard && keyboard->focus)
3016 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003017
3018 if (output)
3019 break;
3020 }
3021
3022 return output;
3023}
3024
3025static void
3026shell_interface_set_maximized(struct shell_surface *shsurf)
3027{
3028 struct weston_output *output;
3029
3030 surface_clear_next_states(shsurf);
3031 shsurf->next_state.maximized = true;
3032 shsurf->state_changed = true;
3033 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3034
3035 if (!weston_surface_is_mapped(shsurf->surface))
3036 output = get_focused_output(shsurf->surface->compositor);
3037 else
3038 output = shsurf->surface->output;
3039
3040 shell_surface_set_output(shsurf, output);
3041 send_configure_for_surface(shsurf);
3042}
3043
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003044static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003045shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003046{
Derek Foremane4d6c832015-08-05 14:48:11 -05003047 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003048}
3049
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003050static int
3051shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003052 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003053 uint32_t edges)
3054{
Derek Foremane4d6c832015-08-05 14:48:11 -05003055 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003056}
3057
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003058static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003059
3060static void
3061destroy_shell_seat(struct wl_listener *listener, void *data)
3062{
3063 struct shell_seat *shseat =
3064 container_of(listener,
3065 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003066 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003067
3068 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003069 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003070 shseat->popup_grab.client = NULL;
3071
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003072 wl_list_for_each_safe(shsurf, next,
3073 &shseat->popup_grab.surfaces_list,
3074 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003075 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003076 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003077 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003078 }
3079
3080 wl_list_remove(&shseat->seat_destroy_listener.link);
3081 free(shseat);
3082}
3083
Jason Ekstrand024177c2014-04-21 19:42:58 -05003084static void
3085shell_seat_caps_changed(struct wl_listener *listener, void *data)
3086{
Derek Foreman1281a362015-07-31 16:55:32 -05003087 struct weston_keyboard *keyboard;
3088 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003089 struct shell_seat *seat;
3090
3091 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003092 keyboard = weston_seat_get_keyboard(seat->seat);
3093 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003094
Derek Foreman1281a362015-07-31 16:55:32 -05003095 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003096 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003097 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003098 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003099 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003100 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003101 wl_list_init(&seat->keyboard_focus_listener.link);
3102 }
3103
Derek Foreman1281a362015-07-31 16:55:32 -05003104 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003105 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003106 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003107 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003108 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003109 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003110 wl_list_init(&seat->pointer_focus_listener.link);
3111 }
3112}
3113
Giulio Camuffo5085a752013-03-25 21:42:45 +01003114static struct shell_seat *
3115create_shell_seat(struct weston_seat *seat)
3116{
3117 struct shell_seat *shseat;
3118
3119 shseat = calloc(1, sizeof *shseat);
3120 if (!shseat) {
3121 weston_log("no memory to allocate shell seat\n");
3122 return NULL;
3123 }
3124
3125 shseat->seat = seat;
3126 wl_list_init(&shseat->popup_grab.surfaces_list);
3127
3128 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3129 wl_signal_add(&seat->destroy_signal,
3130 &shseat->seat_destroy_listener);
3131
Jason Ekstrand024177c2014-04-21 19:42:58 -05003132 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3133 wl_list_init(&shseat->keyboard_focus_listener.link);
3134
3135 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3136 wl_list_init(&shseat->pointer_focus_listener.link);
3137
3138 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3139 wl_signal_add(&seat->updated_caps_signal,
3140 &shseat->caps_changed_listener);
3141 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3142
Giulio Camuffo5085a752013-03-25 21:42:45 +01003143 return shseat;
3144}
3145
3146static struct shell_seat *
3147get_shell_seat(struct weston_seat *seat)
3148{
3149 struct wl_listener *listener;
3150
3151 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003152 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003153
3154 return container_of(listener,
3155 struct shell_seat, seat_destroy_listener);
3156}
3157
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003158static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003159popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003160{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003161 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003162 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003163 struct shell_seat *shseat =
3164 container_of(grab, struct shell_seat, popup_grab.grab);
3165 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003166 wl_fixed_t sx, sy;
3167
Jason Ekstranda7af7042013-10-12 22:38:11 -05003168 view = weston_compositor_pick_view(pointer->seat->compositor,
3169 pointer->x, pointer->y,
3170 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003171
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 if (view && view->surface->resource &&
3173 wl_resource_get_client(view->surface->resource) == client) {
3174 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003176 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003177 }
3178}
3179
3180static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003181popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003182 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003183{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003184 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003185 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003186 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003187 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003188 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003189
Jonas Ådahl61917c82014-08-11 22:44:02 +02003190 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003191 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003192 weston_view_from_global_fixed(pointer->focus, x, y,
3193 &pointer->sx, &pointer->sy);
3194 }
3195
Jonas Ådahld2510102014-10-05 21:39:14 +02003196 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003197
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003198 if (!pointer->focus_client)
3199 return;
3200
3201 resource_list = &pointer->focus_client->pointer_resources;
3202 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003203 weston_view_from_global_fixed(pointer->focus,
3204 pointer->x, pointer->y,
3205 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003206 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003207 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003208}
3209
3210static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003211popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003212 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003213{
3214 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003215 struct shell_seat *shseat =
3216 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003217 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003218 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003219 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003220 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003221
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003222 if (grab->pointer->focus_client)
3223 resource_list = &grab->pointer->focus_client->pointer_resources;
3224 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003225 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003226 wl_resource_for_each(resource, resource_list) {
3227 wl_pointer_send_button(resource, serial,
3228 time, button, state);
3229 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003230 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003231 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003232 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003233 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003234 }
3235
Daniel Stone4dbadb12012-05-30 16:31:51 +01003236 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003237 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003238}
3239
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003240static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003241popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003242 uint32_t time,
3243 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003244{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003245 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003246}
3247
3248static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003249popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3250{
3251 weston_pointer_send_axis_source(grab->pointer, source);
3252}
3253
3254static void
3255popup_grab_frame(struct weston_pointer_grab *grab)
3256{
3257 weston_pointer_send_frame(grab->pointer);
3258}
3259
3260static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003261popup_grab_cancel(struct weston_pointer_grab *grab)
3262{
3263 popup_grab_end(grab->pointer);
3264}
3265
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003266static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003267 popup_grab_focus,
3268 popup_grab_motion,
3269 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003270 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003271 popup_grab_axis_source,
3272 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003273 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003274};
3275
3276static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003277touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003278 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003279{
3280 struct wl_resource *resource;
3281 struct shell_seat *shseat =
3282 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3283 struct wl_display *display = shseat->seat->compositor->wl_display;
3284 uint32_t serial;
3285 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003286 wl_fixed_t sx, sy;
3287
3288 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003289
3290 resource_list = &grab->touch->focus_resource_list;
3291 if (!wl_list_empty(resource_list)) {
3292 serial = wl_display_get_serial(display);
3293 wl_resource_for_each(resource, resource_list) {
3294 wl_touch_send_down(resource, serial, time,
3295 grab->touch->focus->surface->resource,
3296 touch_id, sx, sy);
3297 }
3298 }
3299}
3300
3301static void
3302touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3303{
3304 struct wl_resource *resource;
3305 struct shell_seat *shseat =
3306 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3307 struct wl_display *display = shseat->seat->compositor->wl_display;
3308 uint32_t serial;
3309 struct wl_list *resource_list;
3310
3311 resource_list = &grab->touch->focus_resource_list;
3312 if (!wl_list_empty(resource_list)) {
3313 serial = wl_display_get_serial(display);
3314 wl_resource_for_each(resource, resource_list) {
3315 wl_touch_send_up(resource, serial, time, touch_id);
3316 }
3317 }
3318}
3319
3320static void
3321touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003322 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003323{
3324 struct wl_resource *resource;
3325 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003326 wl_fixed_t sx, sy;
3327
3328 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003329
3330 resource_list = &grab->touch->focus_resource_list;
3331 if (!wl_list_empty(resource_list)) {
3332 wl_resource_for_each(resource, resource_list) {
3333 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3334 }
3335 }
3336}
3337
3338static void
3339touch_popup_grab_frame(struct weston_touch_grab *grab)
3340{
3341}
3342
3343static void
3344touch_popup_grab_cancel(struct weston_touch_grab *grab)
3345{
3346 touch_popup_grab_end(grab->touch);
3347}
3348
3349static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3350 touch_popup_grab_down,
3351 touch_popup_grab_up,
3352 touch_popup_grab_motion,
3353 touch_popup_grab_frame,
3354 touch_popup_grab_cancel,
3355};
3356
3357static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003358shell_surface_send_popup_done(struct shell_surface *shsurf)
3359{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003360 if (shsurf->resource == NULL)
3361 return;
3362 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003363 wl_shell_surface_send_popup_done(shsurf->resource);
3364 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003365 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003366}
3367
3368static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003369popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003370{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003371 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003372 struct shell_seat *shseat =
3373 container_of(grab, struct shell_seat, popup_grab.grab);
3374 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003375 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003376
3377 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003378 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003379 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003380 shseat->popup_grab.grab.interface = NULL;
3381 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003382 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003383 wl_list_for_each_safe(shsurf, next,
3384 &shseat->popup_grab.surfaces_list,
3385 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003386 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003387 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003388 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003389 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003390 wl_list_init(&shseat->popup_grab.surfaces_list);
3391 }
3392}
3393
3394static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003395touch_popup_grab_end(struct weston_touch *touch)
3396{
3397 struct weston_touch_grab *grab = touch->grab;
3398 struct shell_seat *shseat =
3399 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3400 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003401 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003402
3403 if (touch->grab->interface == &touch_popup_grab_interface) {
3404 weston_touch_end_grab(grab->touch);
3405 shseat->popup_grab.client = NULL;
3406 shseat->popup_grab.touch_grab.interface = NULL;
3407 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3408 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003409 wl_list_for_each_safe(shsurf, next,
3410 &shseat->popup_grab.surfaces_list,
3411 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003412 shell_surface_send_popup_done(shsurf);
3413 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003414 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003415 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003416 wl_list_init(&shseat->popup_grab.surfaces_list);
3417 }
3418}
3419
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003420static struct shell_surface *
3421get_top_popup(struct shell_seat *shseat)
3422{
3423 struct shell_surface *shsurf;
3424
3425 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3426 return NULL;
3427 } else {
3428 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3429 struct shell_surface,
3430 popup.grab_link);
3431 return shsurf;
3432 }
3433}
3434
3435static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003436add_popup_grab(struct shell_surface *shsurf,
3437 struct shell_seat *shseat,
3438 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003439{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003440 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003441 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003442 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3443 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003444
3445 parent = get_shell_surface(shsurf->parent);
3446 top_surface = get_top_popup(shseat);
3447 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003448 (!parent ||
3449 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003450 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003451 wl_resource_post_error(shsurf->owner_resource,
3452 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003453 "xdg_popup was not created on the "
3454 "topmost popup");
3455 return -1;
3456 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003457
3458 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003459 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003460 shseat->popup_grab.client =
3461 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003462
3463 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003464 shseat->popup_grab.grab.interface =
3465 &popup_grab_interface;
3466
3467 /* We must make sure here that this popup was opened
3468 * after a mouse press, and not just by moving around
3469 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003470 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003471 shseat->popup_grab.initial_up = 0;
3472 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003473 shseat->popup_grab.touch_grab.interface =
3474 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003475 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003476
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003477 wl_list_insert(&shseat->popup_grab.surfaces_list,
3478 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003479
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003480 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003481 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003482 &shseat->popup_grab.grab);
3483 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003484 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003485 &shseat->popup_grab.touch_grab);
3486 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003487 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003488 wl_list_insert(&shseat->popup_grab.surfaces_list,
3489 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003490 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003491
3492 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003493}
3494
3495static void
3496remove_popup_grab(struct shell_surface *shsurf)
3497{
3498 struct shell_seat *shseat = shsurf->popup.shseat;
3499
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003500 if (shell_surface_is_xdg_popup(shsurf) &&
3501 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003502 wl_resource_post_error(shsurf->owner_resource,
3503 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003504 "xdg_popup was destroyed while it was "
3505 "not the topmost popup.");
3506 return;
3507 }
3508
Giulio Camuffo5085a752013-03-25 21:42:45 +01003509 wl_list_remove(&shsurf->popup.grab_link);
3510 wl_list_init(&shsurf->popup.grab_link);
3511 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003512 if (shseat->popup_grab.type == POINTER) {
3513 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3514 shseat->popup_grab.grab.interface = NULL;
3515 } else if (shseat->popup_grab.type == TOUCH) {
3516 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3517 shseat->popup_grab.touch_grab.interface = NULL;
3518 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003519 }
3520}
3521
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003522static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003523shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003524{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003525 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003527 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3528 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003529
Jason Ekstranda7af7042013-10-12 22:38:11 -05003530 shsurf->surface->output = parent_view->output;
Armin Krezović4663aca2016-06-30 06:04:29 +02003531 shsurf->surface->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003532 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003533
Jason Ekstranda7af7042013-10-12 22:38:11 -05003534 weston_view_set_transform_parent(shsurf->view, parent_view);
3535 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3536 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003537 shsurf->view->is_mapped = true;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003538
Derek Foreman1281a362015-07-31 16:55:32 -05003539 if (pointer &&
3540 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003541 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3542 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003543 } else if (touch &&
3544 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003545 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3546 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003547 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003548 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003549 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003550 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003551
3552 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003553}
3554
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003555static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003556 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003557 shell_surface_move,
3558 shell_surface_resize,
3559 shell_surface_set_toplevel,
3560 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003561 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003562 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003563 shell_surface_set_maximized,
3564 shell_surface_set_title,
3565 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003566};
3567
3568static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003569destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003570{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003571 struct shell_surface *child, *next;
3572
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003573 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3574
Giulio Camuffo5085a752013-03-25 21:42:45 +01003575 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3576 remove_popup_grab(shsurf);
3577 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003578
Alex Wubd3354b2012-04-17 17:20:49 +08003579 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003580 shell_surface_is_top_fullscreen(shsurf))
3581 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003582
Jason Ekstranda7af7042013-10-12 22:38:11 -05003583 if (shsurf->fullscreen.black_view)
3584 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003585
Alex Wubd3354b2012-04-17 17:20:49 +08003586 /* As destroy_resource() use wl_list_for_each_safe(),
3587 * we can always remove the listener.
3588 */
3589 wl_list_remove(&shsurf->surface_destroy_listener.link);
3590 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003591 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003592 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003593
Jason Ekstranda7af7042013-10-12 22:38:11 -05003594 weston_view_destroy(shsurf->view);
3595
Philip Withnall648a4dd2013-11-25 18:01:44 +00003596 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003597 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3598 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003599
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003600 wl_list_remove(&shsurf->link);
3601 free(shsurf);
3602}
3603
3604static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003605shell_destroy_shell_surface(struct wl_resource *resource)
3606{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003607 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003608
Bryan Caina46b9462014-04-04 17:41:24 -05003609 if (!wl_list_empty(&shsurf->popup.grab_link))
3610 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003611 if (shsurf->resource)
3612 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003613 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003614}
3615
3616static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003617shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003618{
3619 struct shell_surface *shsurf = container_of(listener,
3620 struct shell_surface,
3621 surface_destroy_listener);
3622
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003623 if (shsurf->resource)
3624 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003625
3626 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003627}
3628
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003629static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003630fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003631{
3632 struct shell_surface *shsurf = data;
3633
3634 weston_surface_destroy(shsurf->surface);
3635}
3636
3637static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003638fade_out_done(struct weston_view_animation *animation, void *data)
3639{
3640 struct shell_surface *shsurf = data;
3641 struct wl_event_loop *loop;
3642
3643 loop = wl_display_get_event_loop(
3644 shsurf->surface->compositor->wl_display);
3645
3646 if (!shsurf->destroying) {
3647 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3648 shsurf->destroying = true;
3649 }
3650}
3651
3652static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003653handle_resource_destroy(struct wl_listener *listener, void *data)
3654{
3655 struct shell_surface *shsurf =
3656 container_of(listener, struct shell_surface,
3657 resource_destroy_listener);
3658
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003659 if (!weston_surface_is_mapped(shsurf->surface))
3660 return;
3661
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003662 shsurf->surface->ref_count++;
3663
3664 pixman_region32_fini(&shsurf->surface->pending.input);
3665 pixman_region32_init(&shsurf->surface->pending.input);
3666 pixman_region32_fini(&shsurf->surface->input);
3667 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003668 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3669 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3670 fade_out_done, shsurf);
3671 } else {
3672 weston_surface_destroy(shsurf->surface);
3673 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003674}
3675
3676static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003677shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003678
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003679struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003680get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003681{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003682 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003683 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003684 else
3685 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003686}
3687
Rafael Antognollie2a34552013-12-03 15:35:45 -02003688static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003689create_common_surface(struct shell_client *owner, void *shell,
3690 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003691 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003692{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003693 struct shell_surface *shsurf;
3694
Pekka Paalanen50b67472014-10-01 15:02:41 +03003695 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003696
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003697 shsurf = calloc(1, sizeof *shsurf);
3698 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003699 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003700 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003701 }
3702
Jason Ekstranda7af7042013-10-12 22:38:11 -05003703 shsurf->view = weston_view_create(surface);
3704 if (!shsurf->view) {
3705 weston_log("no memory to allocate shell surface\n");
3706 free(shsurf);
3707 return NULL;
3708 }
3709
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003710 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003711 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003712 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003713
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003714 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3715 wl_resource_add_destroy_listener(surface->resource,
3716 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003717 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003718
Tiago Vignattibc052c92012-04-19 16:18:18 +03003719 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003720 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003721 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003722 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003723 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003724 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3725 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003726 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003727 wl_list_init(&shsurf->fullscreen.transform.link);
3728
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003729 shsurf->output = get_default_output(shsurf->shell->compositor);
3730
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003731 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003732 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003733 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003734 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003735
3736 /* init link so its safe to always remove it in destroy_shell_surface */
3737 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003738 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003739
Pekka Paalanen460099f2012-01-20 16:48:25 +02003740 /* empty when not in use */
3741 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003742 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003743
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003744 wl_list_init(&shsurf->workspace_transform.link);
3745
Philip Withnall648a4dd2013-11-25 18:01:44 +00003746 wl_list_init(&shsurf->children_link);
3747 wl_list_init(&shsurf->children_list);
3748 shsurf->parent = NULL;
3749
Pekka Paalanenb5026542014-11-12 15:09:24 +02003750 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003751
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003752 shsurf->client = client;
3753
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003754 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003755}
3756
Rafael Antognollie2a34552013-12-03 15:35:45 -02003757static struct shell_surface *
3758create_shell_surface(void *shell, struct weston_surface *surface,
3759 const struct weston_shell_client *client)
3760{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003761 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003762}
3763
Tiago Vignattibc052c92012-04-19 16:18:18 +03003764static void
3765shell_get_shell_surface(struct wl_client *client,
3766 struct wl_resource *resource,
3767 uint32_t id,
3768 struct wl_resource *surface_resource)
3769{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003770 struct weston_surface *surface =
3771 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003772 struct shell_client *sc = wl_resource_get_user_data(resource);
3773 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003774 struct shell_surface *shsurf;
3775
Pekka Paalanen50b67472014-10-01 15:02:41 +03003776 if (weston_surface_set_role(surface, "wl_shell_surface",
3777 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003778 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003779
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003780 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003781 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003782 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003783 return;
3784 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003785
Jason Ekstranda85118c2013-06-27 20:17:02 -05003786 shsurf->resource =
3787 wl_resource_create(client,
3788 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003789 if (!shsurf->resource) {
3790 wl_resource_post_no_memory(surface_resource);
3791 return;
3792 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003793 wl_resource_set_implementation(shsurf->resource,
3794 &shell_surface_implementation,
3795 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003796 wl_list_insert(&sc->surface_list,
3797 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003798}
3799
Rafael Antognollie2a34552013-12-03 15:35:45 -02003800static bool
3801shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3802{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003803 /* A shell surface without a resource is created from xwayland
3804 * and is considered a wl_shell surface for now. */
3805
3806 return shsurf->resource == NULL ||
3807 wl_resource_instance_of(shsurf->resource,
3808 &wl_shell_surface_interface,
3809 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003810}
3811
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003812static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003813 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003814};
3815
Rafael Antognollie2a34552013-12-03 15:35:45 -02003816/****************************
3817 * xdg-shell implementation */
3818
3819static void
3820xdg_surface_destroy(struct wl_client *client,
3821 struct wl_resource *resource)
3822{
3823 wl_resource_destroy(resource);
3824}
3825
3826static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003827xdg_surface_set_parent(struct wl_client *client,
3828 struct wl_resource *resource,
3829 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003830{
3831 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003832 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003833
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003834 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003835 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003836 shell_surface_set_parent(shsurf, parent->surface);
3837 } else {
3838 shell_surface_set_parent(shsurf, NULL);
3839 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003840}
3841
3842static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003843xdg_surface_set_app_id(struct wl_client *client,
3844 struct wl_resource *resource,
3845 const char *app_id)
3846{
3847 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3848
3849 free(shsurf->class);
3850 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003851 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003852}
3853
3854static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003855xdg_surface_show_window_menu(struct wl_client *client,
3856 struct wl_resource *surface_resource,
3857 struct wl_resource *seat_resource,
3858 uint32_t serial,
3859 int32_t x,
3860 int32_t y)
3861{
3862 /* TODO */
3863}
3864
3865static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003866xdg_surface_set_title(struct wl_client *client,
3867 struct wl_resource *resource, const char *title)
3868{
3869 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3870
3871 set_title(shsurf, title);
3872}
3873
3874static void
3875xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3876 struct wl_resource *seat_resource, uint32_t serial)
3877{
3878 common_surface_move(resource, seat_resource, serial);
3879}
3880
3881static void
3882xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3883 struct wl_resource *seat_resource, uint32_t serial,
3884 uint32_t edges)
3885{
3886 common_surface_resize(resource, seat_resource, serial, edges);
3887}
3888
3889static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003890xdg_surface_ack_configure(struct wl_client *client,
3891 struct wl_resource *resource,
3892 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003893{
3894 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3895
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003896 if (shsurf->state_requested) {
3897 shsurf->next_state = shsurf->requested_state;
3898 shsurf->state_changed = true;
3899 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003900 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003901}
3902
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003903static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003904xdg_surface_set_window_geometry(struct wl_client *client,
3905 struct wl_resource *resource,
3906 int32_t x,
3907 int32_t y,
3908 int32_t width,
3909 int32_t height)
3910{
3911 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3912
3913 set_window_geometry(shsurf, x, y, width, height);
3914}
3915
3916static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003917xdg_surface_set_maximized(struct wl_client *client,
3918 struct wl_resource *resource)
3919{
3920 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003921 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003922
3923 shsurf->state_requested = true;
3924 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003925
3926 if (!weston_surface_is_mapped(shsurf->surface))
3927 output = get_focused_output(shsurf->surface->compositor);
3928 else
3929 output = shsurf->surface->output;
3930
3931 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003932 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003933}
3934
3935static void
3936xdg_surface_unset_maximized(struct wl_client *client,
3937 struct wl_resource *resource)
3938{
3939 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3940
3941 shsurf->state_requested = true;
3942 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003943 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003944}
3945
3946static void
3947xdg_surface_set_fullscreen(struct wl_client *client,
3948 struct wl_resource *resource,
3949 struct wl_resource *output_resource)
3950{
3951 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3952 struct weston_output *output;
3953
3954 shsurf->state_requested = true;
3955 shsurf->requested_state.fullscreen = true;
3956
3957 if (output_resource)
3958 output = wl_resource_get_user_data(output_resource);
3959 else
3960 output = NULL;
3961
Marek Chalupa052917a2014-09-02 11:35:12 +02003962 /* handle clients launching in fullscreen */
3963 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3964 /* Set the output to the one that has focus currently. */
3965 assert(shsurf->surface);
3966 output = get_focused_output(shsurf->surface->compositor);
3967 }
3968
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003969 shell_surface_set_output(shsurf, output);
3970 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003971
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003972 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003973}
3974
3975static void
3976xdg_surface_unset_fullscreen(struct wl_client *client,
3977 struct wl_resource *resource)
3978{
3979 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3980
3981 shsurf->state_requested = true;
3982 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003983 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003984}
3985
3986static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003987xdg_surface_set_minimized(struct wl_client *client,
3988 struct wl_resource *resource)
3989{
3990 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3991
3992 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3993 return;
3994
3995 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003996 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003997}
3998
Rafael Antognollie2a34552013-12-03 15:35:45 -02003999static const struct xdg_surface_interface xdg_surface_implementation = {
4000 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004001 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004002 xdg_surface_set_title,
4003 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004004 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004005 xdg_surface_move,
4006 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004007 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004008 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004009 xdg_surface_set_maximized,
4010 xdg_surface_unset_maximized,
4011 xdg_surface_set_fullscreen,
4012 xdg_surface_unset_fullscreen,
4013 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004014};
4015
4016static void
4017xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004018 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004019{
4020 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004021 uint32_t *s;
4022 struct wl_array states;
4023 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004024
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004025 assert(shsurf);
4026
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004027 if (!shsurf->resource)
4028 return;
4029
4030 wl_array_init(&states);
4031 if (shsurf->requested_state.fullscreen) {
4032 s = wl_array_add(&states, sizeof *s);
4033 *s = XDG_SURFACE_STATE_FULLSCREEN;
4034 } else if (shsurf->requested_state.maximized) {
4035 s = wl_array_add(&states, sizeof *s);
4036 *s = XDG_SURFACE_STATE_MAXIMIZED;
4037 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004038 if (shsurf->resize_edges != 0) {
4039 s = wl_array_add(&states, sizeof *s);
4040 *s = XDG_SURFACE_STATE_RESIZING;
4041 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004042 if (shsurf->focus_count > 0) {
4043 s = wl_array_add(&states, sizeof *s);
4044 *s = XDG_SURFACE_STATE_ACTIVATED;
4045 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004046
4047 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4048 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4049
4050 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004051}
4052
4053static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004054 xdg_send_configure,
4055 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004056};
4057
4058static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004059xdg_shell_destroy(struct wl_client *client,
4060 struct wl_resource *resource)
4061{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004062 struct shell_client *sc = wl_resource_get_user_data(resource);
4063 struct wl_resource *shsurf_resource;
4064 struct shell_surface *shsurf;
4065
4066 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4067 shsurf = wl_resource_get_user_data(shsurf_resource);
4068 if (shsurf->owner_resource == resource) {
4069 wl_resource_post_error(
4070 resource,
4071 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4072 "not all child surface objects destroyed");
4073 return;
4074 }
4075 }
4076
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004077 wl_resource_destroy(resource);
4078}
4079
4080static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004081xdg_use_unstable_version(struct wl_client *client,
4082 struct wl_resource *resource,
4083 int32_t version)
4084{
4085 if (version > 1) {
4086 wl_resource_post_error(resource,
4087 1,
4088 "xdg-shell:: version not implemented yet.");
4089 return;
4090 }
4091}
4092
4093static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004094create_xdg_surface(struct shell_client *owner, void *shell,
4095 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004096 const struct weston_shell_client *client)
4097{
4098 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004099
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004100 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004101 if (!shsurf)
4102 return NULL;
4103
Pekka Paalanenb5026542014-11-12 15:09:24 +02004104 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004105
4106 return shsurf;
4107}
4108
4109static void
4110xdg_get_xdg_surface(struct wl_client *client,
4111 struct wl_resource *resource,
4112 uint32_t id,
4113 struct wl_resource *surface_resource)
4114{
4115 struct weston_surface *surface =
4116 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004117 struct shell_client *sc = wl_resource_get_user_data(resource);
4118 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004119 struct shell_surface *shsurf;
4120
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004121 shsurf = get_shell_surface(surface);
4122 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4123 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4124 "This wl_surface is already an "
4125 "xdg_surface");
4126 return;
4127 }
4128
Pekka Paalanen50b67472014-10-01 15:02:41 +03004129 if (weston_surface_set_role(surface, "xdg_surface",
4130 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004131 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004132
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004133 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004134 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004135 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004136 return;
4137 }
4138
4139 shsurf->resource =
4140 wl_resource_create(client,
4141 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004142 if (!shsurf->resource) {
4143 wl_resource_post_no_memory(surface_resource);
4144 return;
4145 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004146 wl_resource_set_implementation(shsurf->resource,
4147 &xdg_surface_implementation,
4148 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004149 shsurf->owner_resource = resource;
4150 wl_list_insert(&sc->surface_list,
4151 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004152}
4153
4154static bool
4155shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4156{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004157 return shsurf->resource &&
4158 wl_resource_instance_of(shsurf->resource,
4159 &xdg_surface_interface,
4160 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004161}
4162
4163/* xdg-popup implementation */
4164
4165static void
4166xdg_popup_destroy(struct wl_client *client,
4167 struct wl_resource *resource)
4168{
4169 wl_resource_destroy(resource);
4170}
4171
Rafael Antognollie2a34552013-12-03 15:35:45 -02004172static const struct xdg_popup_interface xdg_popup_implementation = {
4173 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004174};
4175
4176static void
4177xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004178 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004179{
4180}
4181
4182static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004183 xdg_popup_send_configure,
4184 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004185};
4186
4187static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004188create_xdg_popup(struct shell_client *owner, void *shell,
4189 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004190 const struct weston_shell_client *client,
4191 struct weston_surface *parent,
4192 struct shell_seat *seat,
4193 uint32_t serial,
4194 int32_t x, int32_t y)
4195{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004196 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004197
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004198 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004199 if (!shsurf)
4200 return NULL;
4201
Pekka Paalanenb5026542014-11-12 15:09:24 +02004202 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004203 shsurf->popup.shseat = seat;
4204 shsurf->popup.serial = serial;
4205 shsurf->popup.x = x;
4206 shsurf->popup.y = y;
4207 shell_surface_set_parent(shsurf, parent);
4208
4209 return shsurf;
4210}
4211
4212static void
4213xdg_get_xdg_popup(struct wl_client *client,
4214 struct wl_resource *resource,
4215 uint32_t id,
4216 struct wl_resource *surface_resource,
4217 struct wl_resource *parent_resource,
4218 struct wl_resource *seat_resource,
4219 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004220 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004221{
4222 struct weston_surface *surface =
4223 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004224 struct shell_client *sc = wl_resource_get_user_data(resource);
4225 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004226 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004227 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004228 struct weston_surface *parent;
4229 struct shell_seat *seat;
4230
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004231 shsurf = get_shell_surface(surface);
4232 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4233 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4234 "This wl_surface is already an "
4235 "xdg_popup");
4236 return;
4237 }
4238
Pekka Paalanen50b67472014-10-01 15:02:41 +03004239 if (weston_surface_set_role(surface, "xdg_popup",
4240 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004241 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004242
4243 if (!parent_resource) {
4244 wl_resource_post_error(surface_resource,
4245 WL_DISPLAY_ERROR_INVALID_OBJECT,
4246 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004247 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004248 }
4249
4250 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004251 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004252
Jonas Ådahlee45a552015-03-18 16:37:47 +08004253 /* Verify that we are creating the top most popup when mapping,
4254 * as it's not until then we know whether it was mapped as most
4255 * top level or not. */
4256
4257 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004258 if (!parent_shsurf ||
4259 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4260 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004261 wl_resource_post_error(resource,
4262 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4263 "xdg_popup parent was invalid");
4264 return;
4265 }
4266
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004267 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004268 parent, seat, serial, x, y);
4269 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004270 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004271 return;
4272 }
4273
4274 shsurf->resource =
4275 wl_resource_create(client,
4276 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004277 if (!shsurf->resource) {
4278 wl_resource_post_no_memory(surface_resource);
4279 return;
4280 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004281 wl_resource_set_implementation(shsurf->resource,
4282 &xdg_popup_implementation,
4283 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004284 shsurf->owner_resource = resource;
4285 wl_list_insert(&sc->surface_list,
4286 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004287}
4288
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004289static void
4290xdg_pong(struct wl_client *client,
4291 struct wl_resource *resource, uint32_t serial)
4292{
4293 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004294
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004295 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004296}
4297
Rafael Antognollie2a34552013-12-03 15:35:45 -02004298static bool
4299shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4300{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004301 return (shsurf->resource &&
4302 wl_resource_instance_of(shsurf->resource,
4303 &xdg_popup_interface,
4304 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004305}
4306
4307static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004308 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004309 xdg_use_unstable_version,
4310 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004311 xdg_get_xdg_popup,
4312 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004313};
4314
4315static int
4316xdg_shell_unversioned_dispatch(const void *implementation,
4317 void *_target, uint32_t opcode,
4318 const struct wl_message *message,
4319 union wl_argument *args)
4320{
4321 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004322 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004323
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004324 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004325 wl_resource_post_error(resource,
4326 WL_DISPLAY_ERROR_INVALID_OBJECT,
4327 "must call use_unstable_version first");
4328 return 0;
4329 }
4330
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004331#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004332
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004333 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4334 "shell implementation doesn't match protocol version");
4335
Rafael Antognollie2a34552013-12-03 15:35:45 -02004336 if (args[0].i != XDG_SERVER_VERSION) {
4337 wl_resource_post_error(resource,
4338 WL_DISPLAY_ERROR_INVALID_OBJECT,
4339 "incompatible version, server is %d "
4340 "client wants %d",
4341 XDG_SERVER_VERSION, args[0].i);
4342 return 0;
4343 }
4344
4345 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004346 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004347
4348 return 1;
4349}
4350
4351/* end of xdg-shell implementation */
4352/***********************************/
4353
Kristian Høgsberg07937562011-04-12 17:25:42 -04004354static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004355shell_fade(struct desktop_shell *shell, enum fade_type type);
4356
Pekka Paalanen77346a62011-11-30 16:26:35 +02004357static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004358configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004359{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004361
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004362 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 if (v->output == ev->output && v != ev) {
4364 weston_view_unmap(v);
4365 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004366 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004367 }
4368 }
4369
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004370 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02004371 ev->surface->is_mapped = true;
4372 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004373
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004374 if (wl_list_empty(&ev->layer_link.link)) {
4375 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004377 }
4378}
4379
David Fort50d962f2016-06-09 17:55:52 +02004380
4381static struct shell_output *
4382find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4383{
4384 struct shell_output *shell_output;
4385
4386 wl_list_for_each(shell_output, &shell->output_list, link) {
4387 if (shell_output->output == output)
4388 return shell_output;
4389 }
4390
4391 return NULL;
4392}
4393
Pekka Paalanen8274d902014-08-06 19:36:51 +03004394static int
4395background_get_label(struct weston_surface *surface, char *buf, size_t len)
4396{
4397 return snprintf(buf, len, "background for output %s",
4398 surface->output->name);
4399}
4400
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004401static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004402background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004403{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004404 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004405 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004406
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407 view = container_of(es->views.next, struct weston_view, surface_link);
4408
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004409 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004410}
4411
4412static void
David Fort50d962f2016-06-09 17:55:52 +02004413handle_background_surface_destroy(struct wl_listener *listener, void *data)
4414{
4415 struct shell_output *output =
4416 container_of(listener, struct shell_output, background_surface_listener);
4417
4418 weston_log("background surface gone\n");
4419 output->background_surface = NULL;
4420}
4421
4422static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004423desktop_shell_set_background(struct wl_client *client,
4424 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004425 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004426 struct wl_resource *surface_resource)
4427{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004428 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004429 struct weston_surface *surface =
4430 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02004431 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004432 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004433
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004434 if (surface->configure) {
4435 wl_resource_post_error(surface_resource,
4436 WL_DISPLAY_ERROR_INVALID_OBJECT,
4437 "surface role already assigned");
4438 return;
4439 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004440
Jason Ekstranda7af7042013-10-12 22:38:11 -05004441 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4442 weston_view_destroy(view);
4443 view = weston_view_create(surface);
4444
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004445 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004446 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004447 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004448 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004450 weston_desktop_shell_send_configure(resource, 0,
4451 surface_resource,
4452 surface->output->width,
4453 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004454
4455 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4456 sh_output->background_surface = surface;
4457
4458 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4459 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004460}
4461
Pekka Paalanen8274d902014-08-06 19:36:51 +03004462static int
4463panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4464{
4465 return snprintf(buf, len, "panel for output %s",
4466 surface->output->name);
4467}
4468
Kristian Høgsberg75840622011-09-06 13:48:16 -04004469static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004470panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004471{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004472 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004473 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004474
Jason Ekstranda7af7042013-10-12 22:38:11 -05004475 view = container_of(es->views.next, struct weston_view, surface_link);
4476
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004477 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004478}
4479
4480static void
David Fort50d962f2016-06-09 17:55:52 +02004481handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4482{
4483 struct shell_output *output =
4484 container_of(listener, struct shell_output, panel_surface_listener);
4485
4486 weston_log("panel surface gone\n");
4487 output->panel_surface = NULL;
4488}
4489
4490
4491static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004492desktop_shell_set_panel(struct wl_client *client,
4493 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004494 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004495 struct wl_resource *surface_resource)
4496{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004497 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004498 struct weston_surface *surface =
4499 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004500 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02004501 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004502
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004503 if (surface->configure) {
4504 wl_resource_post_error(surface_resource,
4505 WL_DISPLAY_ERROR_INVALID_OBJECT,
4506 "surface role already assigned");
4507 return;
4508 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004509
Jason Ekstranda7af7042013-10-12 22:38:11 -05004510 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4511 weston_view_destroy(view);
4512 view = weston_view_create(surface);
4513
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004514 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004515 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004516 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004517 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004518 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004519 weston_desktop_shell_send_configure(resource, 0,
4520 surface_resource,
4521 surface->output->width,
4522 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004523
4524 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4525 sh_output->panel_surface = surface;
4526
4527 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4528 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004529}
4530
Pekka Paalanen8274d902014-08-06 19:36:51 +03004531static int
4532lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4533{
4534 return snprintf(buf, len, "lock window");
4535}
4536
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004537static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004538lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004539{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004540 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541 struct weston_view *view;
4542
4543 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004544
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004545 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004546 return;
4547
Jason Ekstranda7af7042013-10-12 22:38:11 -05004548 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004549
4550 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004551 weston_layer_entry_insert(&shell->lock_layer.view_list,
4552 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004553 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02004554 surface->is_mapped = true;
4555 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004556 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004557 }
4558}
4559
4560static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004561handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004562{
Tiago Vignattibe143262012-04-16 17:31:41 +03004563 struct desktop_shell *shell =
4564 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004565
Martin Minarik6d118362012-06-07 18:01:59 +02004566 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004567 shell->lock_surface = NULL;
4568}
4569
4570static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004571desktop_shell_set_lock_surface(struct wl_client *client,
4572 struct wl_resource *resource,
4573 struct wl_resource *surface_resource)
4574{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004575 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004576 struct weston_surface *surface =
4577 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004578
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004579 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004580
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004581 if (!shell->locked)
4582 return;
4583
Pekka Paalanen98262232011-12-01 10:42:22 +02004584 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004585
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004586 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004587 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004588 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004589
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004590 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004591 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004592 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004593 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004594}
4595
4596static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004597resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004598{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004599 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004600
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004601 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004602 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004603 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004604 &shell->input_panel_layer.link);
4605 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004606 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004607 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004608 wl_list_insert(&shell->compositor->cursor_layer.link,
4609 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004610 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004611 wl_list_insert(&shell->fullscreen_layer.link,
4612 &shell->panel_layer.link);
4613 wl_list_insert(&shell->panel_layer.link,
4614 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004615
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004616 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004617
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004618 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004619 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004620 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004621}
4622
4623static void
4624desktop_shell_unlock(struct wl_client *client,
4625 struct wl_resource *resource)
4626{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004627 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004628
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004629 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004630
4631 if (shell->locked)
4632 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004633}
4634
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004635static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004636desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004637 struct wl_resource *resource,
4638 struct wl_resource *surface_resource)
4639{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004640 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004641
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004642 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004643 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004644}
4645
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004646static void
4647desktop_shell_desktop_ready(struct wl_client *client,
4648 struct wl_resource *resource)
4649{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004650 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004651
4652 shell_fade_startup(shell);
4653}
4654
Jonny Lamb765760d2014-08-20 15:53:19 +02004655static void
4656desktop_shell_set_panel_position(struct wl_client *client,
4657 struct wl_resource *resource,
4658 uint32_t position)
4659{
4660 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4661
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004662 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4663 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4664 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4665 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004666 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004667 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004668 "bad position argument");
4669 return;
4670 }
4671
4672 shell->panel_position = position;
4673}
4674
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004675static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004676 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004677 desktop_shell_set_panel,
4678 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004679 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004680 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004681 desktop_shell_desktop_ready,
4682 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004683};
4684
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004685static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004686get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004687{
4688 struct shell_surface *shsurf;
4689
4690 shsurf = get_shell_surface(surface);
4691 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004692 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004693 return shsurf->type;
4694}
4695
Kristian Høgsberg75840622011-09-06 13:48:16 -04004696static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004697move_binding(struct weston_pointer *pointer, uint32_t time,
4698 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004699{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004700 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004701 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004702 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004703
Derek Foreman8ae2db52015-07-15 13:00:36 -05004704 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004705 return;
4706
Derek Foreman8ae2db52015-07-15 13:00:36 -05004707 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004708
Pekka Paalanen01388e22013-04-25 13:57:44 +03004709 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004710 if (surface == NULL)
4711 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004712
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004713 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004714 if (shsurf == NULL || shsurf->state.fullscreen ||
4715 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004716 return;
4717
Derek Foreman794fa0e2015-07-15 13:00:38 -05004718 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004719}
4720
4721static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004722maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4723 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004724{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004725 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004726 struct weston_surface *surface;
4727 struct shell_surface *shsurf;
4728
4729 surface = weston_surface_get_main_surface(focus);
4730 if (surface == NULL)
4731 return;
4732
4733 shsurf = get_shell_surface(surface);
4734 if (shsurf == NULL)
4735 return;
4736
4737 if (!shell_surface_is_xdg_surface(shsurf))
4738 return;
4739
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004740 shsurf->state_requested = true;
4741 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004742 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004743}
4744
4745static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004746fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4747 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004748{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004749 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004750 struct weston_surface *surface;
4751 struct shell_surface *shsurf;
4752
4753 surface = weston_surface_get_main_surface(focus);
4754 if (surface == NULL)
4755 return;
4756
4757 shsurf = get_shell_surface(surface);
4758 if (shsurf == NULL)
4759 return;
4760
4761 if (!shell_surface_is_xdg_surface(shsurf))
4762 return;
4763
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004764 shsurf->state_requested = true;
4765 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004766 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004767 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004768}
4769
4770static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004771touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004772{
Derek Foreman362656b2014-09-04 10:23:05 -05004773 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004774 struct weston_surface *surface;
4775 struct shell_surface *shsurf;
4776
Derek Foreman8ae2db52015-07-15 13:00:36 -05004777 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004778 return;
4779
Derek Foreman8ae2db52015-07-15 13:00:36 -05004780 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004781 surface = weston_surface_get_main_surface(focus);
4782 if (surface == NULL)
4783 return;
4784
4785 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004786 if (shsurf == NULL || shsurf->state.fullscreen ||
4787 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004788 return;
4789
Derek Foremanb7674ae2015-07-15 13:00:37 -05004790 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004791}
4792
4793static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004794resize_binding(struct weston_pointer *pointer, uint32_t time,
4795 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004796{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004797 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004798 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004799 uint32_t edges = 0;
4800 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004801 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004802
Derek Foreman8ae2db52015-07-15 13:00:36 -05004803 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004804 return;
4805
Derek Foreman8ae2db52015-07-15 13:00:36 -05004806 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004807
Pekka Paalanen01388e22013-04-25 13:57:44 +03004808 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004809 if (surface == NULL)
4810 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004811
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004812 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004813 if (shsurf == NULL || shsurf->state.fullscreen ||
4814 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004815 return;
4816
Jason Ekstranda7af7042013-10-12 22:38:11 -05004817 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004818 wl_fixed_to_int(pointer->grab_x),
4819 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004820 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004821
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004822 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004823 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004824 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004825 edges |= 0;
4826 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004827 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004828
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004829 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004830 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004831 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004832 edges |= 0;
4833 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004834 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004835
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004836 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004837}
4838
4839static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004840surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004841 struct weston_pointer_axis_event *event,
4842 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004843{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004844 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004845 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004846 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004847 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004848
Pekka Paalanen01388e22013-04-25 13:57:44 +03004849 /* XXX: broken for windows containing sub-surfaces */
4850 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004851 if (surface == NULL)
4852 return;
4853
4854 shsurf = get_shell_surface(surface);
4855 if (!shsurf)
4856 return;
4857
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004858 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004859
Jason Ekstranda7af7042013-10-12 22:38:11 -05004860 if (shsurf->view->alpha > 1.0)
4861 shsurf->view->alpha = 1.0;
4862 if (shsurf->view->alpha < step)
4863 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004864
Jason Ekstranda7af7042013-10-12 22:38:11 -05004865 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004866 weston_surface_damage(surface);
4867}
4868
4869static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004870do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004871 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004872{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004873 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004874 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004875 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004876 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004877
Derek Foreman1281a362015-07-31 16:55:32 -05004878 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004879 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4880 return;
4881 }
4882
Scott Moreauccbf29d2012-02-22 14:21:41 -07004883 wl_list_for_each(output, &compositor->output_list, link) {
4884 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004885 wl_fixed_to_double(pointer->x),
4886 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004887 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004888 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004889 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004890 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004891 increment = -output->zoom.increment;
4892 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004893 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004894 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004895 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004896 else
4897 increment = 0;
4898
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004899 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004900
Scott Moreaue6603982012-06-11 13:07:51 -06004901 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004902 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004903 else if (output->zoom.level > output->zoom.max_level)
4904 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004905
4906 if (!output->zoom.active) {
4907 if (output->zoom.level <= 0.0)
4908 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004909 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004910 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004911
Scott Moreaue6603982012-06-11 13:07:51 -06004912 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004913
Jason Ekstranda7af7042013-10-12 22:38:11 -05004914 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004915 }
4916 }
4917}
4918
Scott Moreauccbf29d2012-02-22 14:21:41 -07004919static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004920zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004921 struct weston_pointer_axis_event *event,
4922 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004923{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004924 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004925}
4926
4927static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004928zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4929 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004930{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004931 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004932}
4933
4934static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004935terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4936 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004937{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004938 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004939
Daniel Stone325fc2d2012-05-30 16:31:58 +01004940 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004941}
4942
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004943static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004944rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004945 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004946{
4947 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004948 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004949 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004950 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004951 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004952
Jonas Ådahld2510102014-10-05 21:39:14 +02004953 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004954
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004955 if (!shsurf)
4956 return;
4957
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004958 cx = 0.5f * shsurf->surface->width;
4959 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004960
Daniel Stone37816df2012-05-16 18:45:18 +01004961 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4962 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963 r = sqrtf(dx * dx + dy * dy);
4964
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004965 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004966 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004967
4968 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004969 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004970 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004971
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004972 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004973 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004974
4975 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004976 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004977 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004978 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004979 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004980
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004981 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004982 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004983 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004984 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004985 wl_list_init(&shsurf->rotation.transform.link);
4986 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004987 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004988 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004989
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004990 /* We need to adjust the position of the surface
4991 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004992 cposx = shsurf->view->geometry.x + cx;
4993 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004994 dposx = rotate->center.x - cposx;
4995 dposy = rotate->center.y - cposy;
4996 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004997 weston_view_set_position(shsurf->view,
4998 shsurf->view->geometry.x + dposx,
4999 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005000 }
5001
Derek Foreman4b1a0a12014-09-10 15:37:33 -05005002 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02005003 * lazily applies the damage due to rotation update.
5004 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005005 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005006}
5007
5008static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005009rotate_grab_button(struct weston_pointer_grab *grab,
5010 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005011{
5012 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005013 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005014 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005015 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01005016 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005017
Daniel Stone4dbadb12012-05-30 16:31:51 +01005018 if (pointer->button_count == 0 &&
5019 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005020 if (shsurf)
5021 weston_matrix_multiply(&shsurf->rotation.rotation,
5022 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005023 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005024 free(rotate);
5025 }
5026}
5027
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005028static void
5029rotate_grab_cancel(struct weston_pointer_grab *grab)
5030{
5031 struct rotate_grab *rotate =
5032 container_of(grab, struct rotate_grab, base.grab);
5033
5034 shell_grab_end(&rotate->base);
5035 free(rotate);
5036}
5037
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005038static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005039 noop_grab_focus,
5040 rotate_grab_motion,
5041 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005042 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10005043 noop_grab_axis_source,
5044 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005045 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005046};
5047
5048static void
Derek Foreman74de4692015-07-15 13:00:39 -05005049surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005050{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005051 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005052 float dx, dy;
5053 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005054
Derek Foreman45a7c272015-09-11 14:27:40 -05005055 surface = find_toplevel_surface(surface);
5056
Pekka Paalanen460099f2012-01-20 16:48:25 +02005057 rotate = malloc(sizeof *rotate);
5058 if (!rotate)
5059 return;
5060
Jason Ekstranda7af7042013-10-12 22:38:11 -05005061 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005062 surface->surface->width * 0.5f,
5063 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005064 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005065
Derek Foreman74de4692015-07-15 13:00:39 -05005066 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5067 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005068 r = sqrtf(dx * dx + dy * dy);
5069 if (r > 20.0f) {
5070 struct weston_matrix inverse;
5071
5072 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005073 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005074 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005075
5076 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005077 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005078 } else {
5079 weston_matrix_init(&surface->rotation.rotation);
5080 weston_matrix_init(&rotate->rotation);
5081 }
5082
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005083 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005084 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005085}
5086
5087static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005088rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005089 void *data)
5090{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005091 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005092 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005093 struct shell_surface *surface;
5094
Derek Foreman8ae2db52015-07-15 13:00:36 -05005095 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005096 return;
5097
Derek Foreman8ae2db52015-07-15 13:00:36 -05005098 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005099
Pekka Paalanen01388e22013-04-25 13:57:44 +03005100 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005101 if (base_surface == NULL)
5102 return;
5103
5104 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005105 if (surface == NULL || surface->state.fullscreen ||
5106 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005107 return;
5108
Derek Foreman74de4692015-07-15 13:00:39 -05005109 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005110}
5111
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005112/* Move all fullscreen layers down to the current workspace and hide their
5113 * black views. The surfaces' state is set to both fullscreen and lowered,
5114 * and this is reversed when such a surface is re-configured, see
5115 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5116 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005117 * lowering_output = NULL - Lower on all outputs, else only lower on the
5118 * specified output.
5119 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005120 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005121 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005122void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005123lower_fullscreen_layer(struct desktop_shell *shell,
5124 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005125{
5126 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005127 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005128
5129 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005130 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005131 &shell->fullscreen_layer.view_list.link,
5132 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005133 struct shell_surface *shsurf = get_shell_surface(view->surface);
5134
5135 if (!shsurf)
5136 continue;
5137
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005138 /* Only lower surfaces which have lowering_output as their fullscreen
5139 * output, unless a NULL output asks for lowering on all outputs.
5140 */
5141 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5142 continue;
5143
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005144 /* We can have a non-fullscreen popup for a fullscreen surface
5145 * in the fullscreen layer. */
5146 if (shsurf->state.fullscreen) {
5147 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005148 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5149 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005150 weston_view_damage_below(shsurf->fullscreen.black_view);
5151
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005152 }
5153
5154 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005155 weston_layer_entry_remove(&view->layer_link);
5156 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005157 weston_view_damage_below(view);
5158 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005159
5160 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005161 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005162}
5163
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005164void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005165activate(struct desktop_shell *shell, struct weston_view *view,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005166 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005167{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005168 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005169 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005170 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005171 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005172 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005173 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005174
Pekka Paalanen01388e22013-04-25 13:57:44 +03005175 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005176 shsurf = get_shell_surface(main_surface);
5177 assert(shsurf);
5178
5179 /* Only demote fullscreen surfaces on the output of activated shsurf.
5180 * Leave fullscreen surfaces on unrelated outputs alone. */
5181 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005182
Bryce Harrington260c2ff2016-06-29 19:04:06 -07005183 weston_seat_set_keyboard_focus(seat, es);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005184
Jonas Ådahl8538b222012-08-29 22:13:03 +02005185 state = ensure_focus_state(shell, seat);
5186 if (state == NULL)
5187 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005188
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005189 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005190 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005191
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005192 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005193 shell_configure_fullscreen(shsurf);
5194 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005195 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005196
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005197 /* Update the surface’s layer. This brings it to the top of the stacking
5198 * order as appropriate. */
5199 shell_surface_update_layer(shsurf);
5200
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005201 if (shell->focus_animation_type != ANIMATION_NONE) {
5202 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005203 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005204 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005205}
5206
Alex Wu21858432012-04-01 20:13:08 +08005207/* no-op func for checking black surface */
5208static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005209black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005210{
5211}
5212
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005213static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005214is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08005215{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005216 struct weston_surface *surface = view->surface;
5217
5218 if (surface->configure == black_surface_configure) {
5219 if (fs_view)
5220 *fs_view = surface->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005221 return true;
5222 }
5223 return false;
5224}
5225
Kristian Høgsberg75840622011-09-06 13:48:16 -04005226static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005227activate_binding(struct weston_seat *seat,
5228 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005229 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005230{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005231 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005232 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005233
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005234 if (!focus_view)
5235 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005236
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005237 if (is_black_surface_view(focus_view, &main_view))
5238 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08005239
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005240 main_surface = weston_surface_get_main_surface(focus_view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005241 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005242 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005243
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005244 activate(shell, focus_view, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005245}
5246
5247static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005248click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5249 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005250{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005251 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005252 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005253 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005254 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005255
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005256 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005257}
5258
5259static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005260touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5261 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005262{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005263 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005264 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005265 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005266 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005267
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005268 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005269}
5270
5271static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005272unfocus_all_seats(struct desktop_shell *shell)
5273{
5274 struct weston_seat *seat, *next;
5275
5276 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005277 struct weston_keyboard *keyboard =
5278 weston_seat_get_keyboard(seat);
5279
5280 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005281 continue;
5282
Derek Foreman1281a362015-07-31 16:55:32 -05005283 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005284 }
5285}
5286
5287static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005288lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005289{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005290 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005291
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005292 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005293 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005294 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005295 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005296
5297 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005298
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005299 /* Hide all surfaces by removing the fullscreen, panel and
5300 * toplevel layers. This way nothing else can show or receive
5301 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005302
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005303 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005304 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005305 if (shell->showing_input_panels)
5306 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005307 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005308 wl_list_insert(&shell->compositor->cursor_layer.link,
5309 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005310
Derek Foreman004b4a12015-07-20 16:28:13 -05005311 weston_compositor_sleep(shell->compositor);
5312
Neil Robertsb4a91702014-04-09 16:33:32 +01005313 /* Remove the keyboard focus on all seats. This will be
5314 * restored to the workspace's saved state via
5315 * restore_focus_state when the compositor is unlocked */
5316 unfocus_all_seats(shell);
5317
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005318 /* TODO: disable bindings that should not work while locked. */
5319
5320 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005321}
5322
5323static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005324unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005325{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005326 struct wl_resource *shell_resource;
5327
Pekka Paalanend81c2162011-11-16 13:47:34 +02005328 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005329 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005330 return;
5331 }
5332
5333 /* If desktop-shell client has gone away, unlock immediately. */
5334 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005335 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005336 return;
5337 }
5338
5339 if (shell->prepare_event_sent)
5340 return;
5341
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005342 shell_resource = shell->child.desktop_shell;
5343 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005344 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005345}
5346
5347static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005348shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005349{
5350 struct desktop_shell *shell = data;
5351
5352 shell->fade.animation = NULL;
5353
5354 switch (shell->fade.type) {
5355 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005356 weston_surface_destroy(shell->fade.view->surface);
5357 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005358 break;
5359 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005360 lock(shell);
5361 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005362 default:
5363 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005364 }
5365}
5366
Jason Ekstranda7af7042013-10-12 22:38:11 -05005367static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005368shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005369{
5370 struct weston_compositor *compositor = shell->compositor;
5371 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005372 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005373
5374 surface = weston_surface_create(compositor);
5375 if (!surface)
5376 return NULL;
5377
Jason Ekstranda7af7042013-10-12 22:38:11 -05005378 view = weston_view_create(surface);
5379 if (!view) {
5380 weston_surface_destroy(surface);
5381 return NULL;
5382 }
5383
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005384 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005385 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005386 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005387 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5388 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005389 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02005390 surface->is_mapped = true;
5391 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005392
Jason Ekstranda7af7042013-10-12 22:38:11 -05005393 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005394}
5395
5396static void
5397shell_fade(struct desktop_shell *shell, enum fade_type type)
5398{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005399 float tint;
5400
5401 switch (type) {
5402 case FADE_IN:
5403 tint = 0.0;
5404 break;
5405 case FADE_OUT:
5406 tint = 1.0;
5407 break;
5408 default:
5409 weston_log("shell: invalid fade type\n");
5410 return;
5411 }
5412
5413 shell->fade.type = type;
5414
Jason Ekstranda7af7042013-10-12 22:38:11 -05005415 if (shell->fade.view == NULL) {
5416 shell->fade.view = shell_fade_create_surface(shell);
5417 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005418 return;
5419
Jason Ekstranda7af7042013-10-12 22:38:11 -05005420 shell->fade.view->alpha = 1.0 - tint;
5421 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005422 }
5423
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005424 if (shell->fade.view->output == NULL) {
5425 /* If the black view gets a NULL output, we lost the
5426 * last output and we'll just cancel the fade. This
5427 * happens when you close the last window under the
5428 * X11 or Wayland backends. */
5429 shell->locked = false;
5430 weston_surface_destroy(shell->fade.view->surface);
5431 shell->fade.view = NULL;
5432 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005433 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005434 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005435 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005436 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005437 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005438 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005439 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005440}
5441
5442static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005443do_shell_fade_startup(void *data)
5444{
5445 struct desktop_shell *shell = data;
5446
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005447 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005448 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005449 } else {
5450 weston_log("desktop shell: "
5451 "unexpected fade-in animation type %d\n",
5452 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005453 weston_surface_destroy(shell->fade.view->surface);
5454 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005455 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005456}
5457
5458static void
5459shell_fade_startup(struct desktop_shell *shell)
5460{
5461 struct wl_event_loop *loop;
5462
5463 if (!shell->fade.startup_timer)
5464 return;
5465
5466 wl_event_source_remove(shell->fade.startup_timer);
5467 shell->fade.startup_timer = NULL;
5468
5469 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5470 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5471}
5472
5473static int
5474fade_startup_timeout(void *data)
5475{
5476 struct desktop_shell *shell = data;
5477
5478 shell_fade_startup(shell);
5479 return 0;
5480}
5481
5482static void
5483shell_fade_init(struct desktop_shell *shell)
5484{
5485 /* Make compositor output all black, and wait for the desktop-shell
5486 * client to signal it is ready, then fade in. The timer triggers a
5487 * fade-in, in case the desktop-shell client takes too long.
5488 */
5489
5490 struct wl_event_loop *loop;
5491
Jason Ekstranda7af7042013-10-12 22:38:11 -05005492 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005493 weston_log("%s: warning: fade surface already exists\n",
5494 __func__);
5495 return;
5496 }
5497
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005498 if (shell->startup_animation_type == ANIMATION_NONE)
5499 return;
5500
Jason Ekstranda7af7042013-10-12 22:38:11 -05005501 shell->fade.view = shell_fade_create_surface(shell);
5502 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005503 return;
5504
Jason Ekstranda7af7042013-10-12 22:38:11 -05005505 weston_view_update_transform(shell->fade.view);
5506 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005507
5508 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5509 shell->fade.startup_timer =
5510 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5511 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5512}
5513
5514static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005515idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005516{
5517 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005518 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005519 struct weston_seat *seat;
5520
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005521 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005522 struct weston_touch *touch = weston_seat_get_touch(seat);
5523 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5524
5525 if (pointer)
5526 popup_grab_end(pointer);
5527 if (touch)
5528 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005529 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005530
5531 shell_fade(shell, FADE_OUT);
5532 /* lock() is called from shell_fade_done() */
5533}
5534
5535static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005536wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005537{
5538 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005539 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005540
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005541 unlock(shell);
5542}
5543
5544static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005545transform_handler(struct wl_listener *listener, void *data)
5546{
5547 struct weston_surface *surface = data;
5548 struct shell_surface *shsurf = get_shell_surface(surface);
5549 struct weston_view *view;;
5550 int x, y;
5551
5552 if (!shsurf || !shsurf->client->send_position)
5553 return;
5554
5555 view = shsurf->view;
5556 if (!view || !weston_view_is_mapped(view))
5557 return;
5558
5559 x = view->geometry.x;
5560 y = view->geometry.y;
5561
5562 shsurf->client->send_position(surface, x, y);
5563}
5564
5565static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005566center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005567{
Giulio Camuffob8366642013-04-25 13:57:46 +03005568 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005569 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005570
Jason Ekstranda7af7042013-10-12 22:38:11 -05005571 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005572
5573 x = output->x + (output->width - width) / 2 - surf_x / 2;
5574 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005575
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005576 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005577}
5578
5579static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005580weston_view_set_initial_position(struct weston_view *view,
5581 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005582{
5583 struct weston_compositor *compositor = shell->compositor;
5584 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005585 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005586 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005587 struct weston_output *output, *target_output = NULL;
5588 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005589 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005590
5591 /* As a heuristic place the new window on the same output as the
5592 * pointer. Falling back to the output containing 0, 0.
5593 *
5594 * TODO: Do something clever for touch too?
5595 */
5596 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005597 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5598
5599 if (pointer) {
5600 ix = wl_fixed_to_int(pointer->x);
5601 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005602 break;
5603 }
5604 }
5605
5606 wl_list_for_each(output, &compositor->output_list, link) {
5607 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5608 target_output = output;
5609 break;
5610 }
5611 }
5612
5613 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005614 weston_view_set_position(view, 10 + random() % 400,
5615 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005616 return;
5617 }
5618
5619 /* Valid range within output where the surface will still be onscreen.
5620 * If this is negative it means that the surface is bigger than
5621 * output.
5622 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005623 get_output_work_area(shell, target_output, &area);
5624
Derek Foremanf814c5d2015-04-15 12:23:54 -05005625 x = area.x;
5626 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005627 range_x = area.width - view->surface->width;
5628 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005629
Rob Bradford4cb88c72012-08-13 15:18:44 +01005630 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005631 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005632
5633 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005634 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005635
Jason Ekstranda7af7042013-10-12 22:38:11 -05005636 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005637}
5638
5639static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005640set_maximized_position(struct desktop_shell *shell,
5641 struct shell_surface *shsurf)
5642{
5643 int32_t surf_x, surf_y;
5644 pixman_rectangle32_t area;
5645
Jonny Lambd73c6942014-08-20 15:53:20 +02005646 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005647 if (shsurf->has_set_geometry) {
5648 surf_x = shsurf->geometry.x;
5649 surf_y = shsurf->geometry.y;
5650 } else {
5651 surface_subsurfaces_boundingbox(shsurf->surface,
5652 &surf_x, &surf_y, NULL, NULL);
5653 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005654
5655 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005656 area.x - surf_x,
5657 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005658}
5659
5660static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005661map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005662 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005663{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005664 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005665 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005666
Pekka Paalanen77346a62011-11-30 16:26:35 +02005667 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005668 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005669 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005670 if (shsurf->state.fullscreen) {
5671 center_on_output(shsurf->view, shsurf->fullscreen_output);
5672 shell_map_fullscreen(shsurf);
5673 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005674 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005675 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005676 weston_view_set_initial_position(shsurf->view, shell);
5677 }
Juan Zhao96879df2012-02-07 08:45:41 +08005678 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005679 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005680 if (shell_map_popup(shsurf) != 0)
5681 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005682 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005683 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005684 weston_view_set_position(shsurf->view,
5685 shsurf->view->geometry.x + sx,
5686 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005687 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005688 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005689 default:
5690 ;
5691 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005692
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005693 /* Surface stacking order, see also activate(). */
5694 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005695
Jason Ekstranda7af7042013-10-12 22:38:11 -05005696 if (shsurf->type != SHELL_SURFACE_NONE) {
5697 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02005698 shsurf->view->is_mapped = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005699 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005700 shsurf->surface->output = shsurf->output;
5701 shsurf->view->output = shsurf->output;
5702 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005703 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005704
Jason Ekstranda7af7042013-10-12 22:38:11 -05005705 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005706 /* XXX: xwayland's using the same fields for transient type */
5707 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005708 if (shsurf->transient.flags ==
5709 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5710 break;
5711 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005712 if (shsurf->state.relative &&
5713 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5714 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005715 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005716 break;
5717 wl_list_for_each(seat, &compositor->seat_list, link)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005718 activate(shell, shsurf->view, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005719 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005720 case SHELL_SURFACE_POPUP:
5721 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005722 default:
5723 break;
5724 }
5725
Rafael Antognolli03b16592013-12-03 15:35:42 -02005726 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5727 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005728 {
5729 switch (shell->win_animation_type) {
5730 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005731 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005732 break;
5733 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005734 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005735 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005736 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005737 default:
5738 break;
5739 }
5740 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005741}
5742
5743static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005744configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005745 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005746{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005747 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005748 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005749
Pekka Paalanen77346a62011-11-30 16:26:35 +02005750 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005751
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005752 assert(shsurf);
5753
Rafael Antognolli03b16592013-12-03 15:35:42 -02005754 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005755 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005756 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005757 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005758 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005759 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005760 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005761
Alex Wu4539b082012-03-01 12:57:46 +08005762 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005763 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005764 wl_list_for_each(view, &surface->views, surface_link)
5765 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005766
Rafael Antognolli03b16592013-12-03 15:35:42 -02005767 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005768 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005769 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005770}
5771
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005772static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005773shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005774{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005775 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005776 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005777 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005778
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005779 assert(shsurf);
5780
5781 shell = shsurf->shell;
5782
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005783 if (!weston_surface_is_mapped(es) &&
5784 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005785 remove_popup_grab(shsurf);
5786 }
5787
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005788 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005789 return;
5790
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005791 if (shsurf->has_next_geometry) {
5792 shsurf->geometry = shsurf->next_geometry;
5793 shsurf->has_next_geometry = false;
5794 shsurf->has_set_geometry = true;
5795 } else if (!shsurf->has_set_geometry) {
5796 surface_subsurfaces_boundingbox(shsurf->surface,
5797 &shsurf->geometry.x,
5798 &shsurf->geometry.y,
5799 &shsurf->geometry.width,
5800 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005801 }
5802
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005803 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005804 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005805 type_changed = 1;
5806 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005807
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005808 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005809 map(shell, shsurf, sx, sy);
Armin Krezović4663aca2016-06-30 06:04:29 +02005810 es->is_mapped = true;
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005811 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005812 shsurf->last_width != es->width ||
5813 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005814 float from_x, from_y;
5815 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005816
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005817 if (shsurf->resize_edges) {
5818 sx = 0;
5819 sy = 0;
5820 }
5821
5822 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5823 sx = shsurf->last_width - es->width;
5824 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5825 sy = shsurf->last_height - es->height;
5826
5827 shsurf->last_width = es->width;
5828 shsurf->last_height = es->height;
5829
Jason Ekstranda7af7042013-10-12 22:38:11 -05005830 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5831 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005832 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005833 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005834 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005835 }
5836}
5837
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005838static bool
5839check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5840{
5841 struct timespec now;
5842
5843 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5844 return false;
5845
5846 /*
5847 * If the shell helper client dies before the session has been
5848 * up for roughly 30 seconds, better just make Weston shut down,
5849 * because the user likely has no way to interact with the desktop
5850 * anyway.
5851 */
5852 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5853 weston_log("Error: %s apparently cannot run at all.\n",
5854 shell->client);
5855 weston_log_continue(STAMP_SPACE "Quitting...");
5856 wl_display_terminate(shell->compositor->wl_display);
5857
5858 return true;
5859 }
5860
5861 return false;
5862}
5863
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005864static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005865
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005866static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005867respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005868{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005869 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005870
5871 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5872 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005873 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005874 shell->child.deathstamp = time;
5875 shell->child.deathcount = 0;
5876 }
5877
5878 shell->child.deathcount++;
5879 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005880 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005881 return;
5882 }
5883
Pekka Paalanen826dc142014-08-27 12:11:53 +03005884 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005885 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005886}
5887
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005888static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005889desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5890{
5891 struct desktop_shell *shell;
5892
5893 shell = container_of(listener, struct desktop_shell,
5894 child.client_destroy_listener);
5895
Pekka Paalanen826dc142014-08-27 12:11:53 +03005896 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005897 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005898 /*
5899 * unbind_desktop_shell() will reset shell->child.desktop_shell
5900 * before the respawned process has a chance to create a new
5901 * desktop_shell object, because we are being called from the
5902 * wl_client destructor which destroys all wl_resources before
5903 * returning.
5904 */
5905
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005906 if (!check_desktop_shell_crash_too_early(shell))
5907 respawn_desktop_shell_process(shell);
5908
Pekka Paalanen826dc142014-08-27 12:11:53 +03005909 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005910}
5911
5912static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005913launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005914{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005915 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005916
Pekka Paalanen826dc142014-08-27 12:11:53 +03005917 shell->child.client = weston_client_start(shell->compositor,
5918 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005919
Arnaud Vrac42631192014-08-25 20:56:46 +02005920 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005921 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005922 return;
5923 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005924
5925 shell->child.client_destroy_listener.notify =
5926 desktop_shell_client_destroy;
5927 wl_client_add_destroy_listener(shell->child.client,
5928 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005929}
5930
5931static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005932handle_shell_client_destroy(struct wl_listener *listener, void *data)
5933{
5934 struct shell_client *sc =
5935 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005936 struct wl_resource *shsurf_resource;
5937 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005938
5939 if (sc->ping_timer)
5940 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005941
5942 /* Since we're about to free shell_client, we remove it from the
5943 * head of the surface list so we don't use that freed list node
5944 * during surface clean up later on.
5945 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005946 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5947 shsurf = wl_resource_get_user_data(shsurf_resource);
5948 shsurf->owner = NULL;
5949 }
Derek Foremane42d7542015-05-29 10:46:44 -05005950 wl_list_remove(&sc->surface_list);
5951
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005952 free(sc);
5953}
5954
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005955static struct shell_client *
5956shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5957 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005958{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005959 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005960
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005961 sc = zalloc(sizeof *sc);
5962 if (sc == NULL) {
5963 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005964 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005965 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005966
5967 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005968 if (sc->resource == NULL) {
5969 free(sc);
5970 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005971 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005972 }
5973
5974 sc->client = client;
5975 sc->shell = shell;
5976 sc->destroy_listener.notify = handle_shell_client_destroy;
5977 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005978 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005979
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005980 return sc;
5981}
5982
5983static void
5984bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5985{
5986 struct desktop_shell *shell = data;
5987 struct shell_client *sc;
5988
5989 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5990 if (sc)
5991 wl_resource_set_implementation(sc->resource,
5992 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005993 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005994}
5995
5996static void
5997bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5998{
5999 struct desktop_shell *shell = data;
6000 struct shell_client *sc;
6001
6002 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
6003 if (sc)
6004 wl_resource_set_dispatcher(sc->resource,
6005 xdg_shell_unversioned_dispatch,
6006 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02006007}
6008
6009static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006010unbind_desktop_shell(struct wl_resource *resource)
6011{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05006012 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05006013
6014 if (shell->locked)
6015 resume_desktop(shell);
6016
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006017 shell->child.desktop_shell = NULL;
6018 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006019}
6020
6021static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04006022bind_desktop_shell(struct wl_client *client,
6023 void *data, uint32_t version, uint32_t id)
6024{
Tiago Vignattibe143262012-04-16 17:31:41 +03006025 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006026 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04006027
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006028 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6029 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006030
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006031 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006032 wl_resource_set_implementation(resource,
6033 &desktop_shell_implementation,
6034 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006035 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006036 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006037 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006038
6039 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6040 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04006041}
6042
Kristian Høgsberg07045392012-02-19 18:52:44 -05006043struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006044 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006045 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006046 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006047 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006048 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006049};
6050
6051static void
6052switcher_next(struct switcher *switcher)
6053{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006054 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006055 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006056 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006057 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006058
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006059 /* temporary re-display minimized surfaces */
6060 struct weston_view *tmp;
6061 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006062 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6063 weston_layer_entry_remove(&view->layer_link);
6064 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006065 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6066 *minimized = view;
6067 }
6068
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006069 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006070 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006071 if (shsurf &&
6072 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6073 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006074 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006075 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006076 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006077 next = view;
6078 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006079 view->alpha = 0.25;
6080 weston_view_geometry_dirty(view);
6081 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006082 }
Alex Wu1659daa2012-04-01 20:13:09 +08006083
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02006084 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05006085 view->alpha = 0.25;
6086 weston_view_geometry_dirty(view);
6087 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006088 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006089 }
6090
6091 if (next == NULL)
6092 next = first;
6093
Alex Wu07b26062012-03-12 16:06:01 +08006094 if (next == NULL)
6095 return;
6096
Kristian Høgsberg07045392012-02-19 18:52:44 -05006097 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006098 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006099
6100 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006101 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006102 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006103
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006104 shsurf = get_shell_surface(switcher->current->surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006105 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006106 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006107}
6108
6109static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006110switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006111{
6112 struct switcher *switcher =
6113 container_of(listener, struct switcher, listener);
6114
6115 switcher_next(switcher);
6116}
6117
6118static void
Daniel Stone351eb612012-05-31 15:27:47 -04006119switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006120{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006121 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006122 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006123 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006124
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006125 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006126 if (is_focus_view(view))
6127 continue;
6128
Jason Ekstranda7af7042013-10-12 22:38:11 -05006129 view->alpha = 1.0;
6130 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006131 }
6132
Alex Wu07b26062012-03-12 16:06:01 +08006133 if (switcher->current)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02006134 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006135 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006136 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006137 weston_keyboard_end_grab(keyboard);
6138 if (keyboard->input_method_resource)
6139 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006140
6141 /* re-hide surfaces that were temporary shown during the switch */
6142 struct weston_view **minimized;
6143 wl_array_for_each(minimized, &switcher->minimized_array) {
6144 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006145 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006146 weston_layer_entry_remove(&(*minimized)->layer_link);
6147 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006148 weston_view_damage_below(*minimized);
6149 }
6150 }
6151 wl_array_release(&switcher->minimized_array);
6152
Kristian Høgsberg07045392012-02-19 18:52:44 -05006153 free(switcher);
6154}
6155
6156static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006157switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006158 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006159{
6160 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006161 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006162
Daniel Stonec9785ea2012-05-30 16:31:52 +01006163 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006164 switcher_next(switcher);
6165}
6166
6167static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006168switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006169 uint32_t mods_depressed, uint32_t mods_latched,
6170 uint32_t mods_locked, uint32_t group)
6171{
6172 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006173 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006174
Daniel Stone351eb612012-05-31 15:27:47 -04006175 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6176 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006177}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006178
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006179static void
6180switcher_cancel(struct weston_keyboard_grab *grab)
6181{
6182 struct switcher *switcher = container_of(grab, struct switcher, grab);
6183
6184 switcher_destroy(switcher);
6185}
6186
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006187static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006188 switcher_key,
6189 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006190 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006191};
6192
6193static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006194switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6195 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006196{
Tiago Vignattibe143262012-04-16 17:31:41 +03006197 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006198 struct switcher *switcher;
6199
6200 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006201 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006202 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006203 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006204 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006205 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006206
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006207 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006208 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006209 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006210 weston_keyboard_start_grab(keyboard, &switcher->grab);
6211 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006212 switcher_next(switcher);
6213}
6214
Pekka Paalanen3c647232011-12-22 13:43:43 +02006215static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006216backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6217 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006218{
6219 struct weston_compositor *compositor = data;
6220 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006221 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006222
6223 /* TODO: we're limiting to simple use cases, where we assume just
6224 * control on the primary display. We'd have to extend later if we
6225 * ever get support for setting backlights on random desktop LCD
6226 * panels though */
6227 output = get_default_output(compositor);
6228 if (!output)
6229 return;
6230
6231 if (!output->set_backlight)
6232 return;
6233
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006234 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6235 backlight_new = output->backlight_current - 25;
6236 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6237 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006238
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006239 if (backlight_new < 5)
6240 backlight_new = 5;
6241 if (backlight_new > 255)
6242 backlight_new = 255;
6243
6244 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006245 output->set_backlight(output, output->backlight_current);
6246}
6247
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006248static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006249force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6250 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006251{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006252 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006253 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006254 struct desktop_shell *shell = data;
6255 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006256 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006257
Derek Foreman8ae2db52015-07-15 13:00:36 -05006258 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006259 if (!focus_surface)
6260 return;
6261
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006262 wl_signal_emit(&compositor->kill_signal, focus_surface);
6263
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006264 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006265 wl_client_get_credentials(client, &pid, NULL, NULL);
6266
6267 /* Skip clients that we launched ourselves (the credentials of
6268 * the socketpair is ours) */
6269 if (pid == getpid())
6270 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006271
Daniel Stone325fc2d2012-05-30 16:31:58 +01006272 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006273}
6274
6275static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006276workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006277 uint32_t key, void *data)
6278{
6279 struct desktop_shell *shell = data;
6280 unsigned int new_index = shell->workspaces.current;
6281
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006282 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006283 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006284 if (new_index != 0)
6285 new_index--;
6286
6287 change_workspace(shell, new_index);
6288}
6289
6290static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006291workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006292 uint32_t key, void *data)
6293{
6294 struct desktop_shell *shell = data;
6295 unsigned int new_index = shell->workspaces.current;
6296
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006297 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006298 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006299 if (new_index < shell->workspaces.num - 1)
6300 new_index++;
6301
6302 change_workspace(shell, new_index);
6303}
6304
6305static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006306workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006307 uint32_t key, void *data)
6308{
6309 struct desktop_shell *shell = data;
6310 unsigned int new_index;
6311
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006312 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006313 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006314 new_index = key - KEY_F1;
6315 if (new_index >= shell->workspaces.num)
6316 new_index = shell->workspaces.num - 1;
6317
6318 change_workspace(shell, new_index);
6319}
6320
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006321static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006322workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6323 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006324{
6325 struct desktop_shell *shell = data;
6326 unsigned int new_index = shell->workspaces.current;
6327
6328 if (shell->locked)
6329 return;
6330
6331 if (new_index != 0)
6332 new_index--;
6333
Derek Foreman8ae2db52015-07-15 13:00:36 -05006334 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006335}
6336
6337static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006338workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6339 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006340{
6341 struct desktop_shell *shell = data;
6342 unsigned int new_index = shell->workspaces.current;
6343
6344 if (shell->locked)
6345 return;
6346
6347 if (new_index < shell->workspaces.num - 1)
6348 new_index++;
6349
Derek Foreman8ae2db52015-07-15 13:00:36 -05006350 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006351}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006352
6353static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006354shell_reposition_view_on_output_destroy(struct weston_view *view)
6355{
6356 struct weston_output *output, *first_output;
6357 struct weston_compositor *ec = view->surface->compositor;
6358 struct shell_surface *shsurf;
6359 float x, y;
6360 int visible;
6361
6362 x = view->geometry.x;
6363 y = view->geometry.y;
6364
6365 /* At this point the destroyed output is not in the list anymore.
6366 * If the view is still visible somewhere, we leave where it is,
6367 * otherwise, move it to the first output. */
6368 visible = 0;
6369 wl_list_for_each(output, &ec->output_list, link) {
6370 if (pixman_region32_contains_point(&output->region,
6371 x, y, NULL)) {
6372 visible = 1;
6373 break;
6374 }
6375 }
6376
6377 if (!visible) {
6378 first_output = container_of(ec->output_list.next,
6379 struct weston_output, link);
6380
6381 x = first_output->x + first_output->width / 4;
6382 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006383
6384 weston_view_set_position(view, x, y);
6385 } else {
6386 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006387 }
6388
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006389
6390 shsurf = get_shell_surface(view->surface);
6391
6392 if (shsurf) {
6393 shsurf->saved_position_valid = false;
6394 shsurf->next_state.maximized = false;
6395 shsurf->next_state.fullscreen = false;
6396 shsurf->state_changed = true;
6397 }
6398}
6399
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006400void
6401shell_for_each_layer(struct desktop_shell *shell,
6402 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006403{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006404 struct workspace **ws;
6405
6406 func(shell, &shell->fullscreen_layer, data);
6407 func(shell, &shell->panel_layer, data);
6408 func(shell, &shell->background_layer, data);
6409 func(shell, &shell->lock_layer, data);
6410 func(shell, &shell->input_panel_layer, data);
6411
6412 wl_array_for_each(ws, &shell->workspaces.array)
6413 func(shell, &(*ws)->layer, data);
6414}
6415
6416static void
6417shell_output_destroy_move_layer(struct desktop_shell *shell,
6418 struct weston_layer *layer,
6419 void *data)
6420{
6421 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006422 struct weston_view *view;
6423
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006424 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006425 if (view->output != output)
6426 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006427
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006428 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006429 }
6430}
6431
6432static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006433handle_output_destroy(struct wl_listener *listener, void *data)
6434{
6435 struct shell_output *output_listener =
6436 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006437 struct weston_output *output = output_listener->output;
6438 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006439
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006440 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006441
Xiong Zhang6b481422013-10-23 13:58:32 +08006442 wl_list_remove(&output_listener->destroy_listener.link);
6443 wl_list_remove(&output_listener->link);
6444 free(output_listener);
6445}
6446
6447static void
David Fort50d962f2016-06-09 17:55:52 +02006448shell_resize_surface_to_output(struct desktop_shell *shell,
6449 struct weston_surface *surface,
6450 const struct weston_output *output)
6451{
6452 if (!surface)
6453 return;
6454
6455 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6456 surface->resource,
6457 output->width,
6458 output->height);
6459}
6460
6461
6462static void
6463handle_output_resized(struct wl_listener *listener, void *data)
6464{
6465 struct desktop_shell *shell =
6466 container_of(listener, struct desktop_shell, resized_listener);
6467 struct weston_output *output = (struct weston_output *)data;
6468 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6469
6470 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6471 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6472}
6473
6474static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006475create_shell_output(struct desktop_shell *shell,
6476 struct weston_output *output)
6477{
6478 struct shell_output *shell_output;
6479
6480 shell_output = zalloc(sizeof *shell_output);
6481 if (shell_output == NULL)
6482 return;
6483
6484 shell_output->output = output;
6485 shell_output->shell = shell;
6486 shell_output->destroy_listener.notify = handle_output_destroy;
6487 wl_signal_add(&output->destroy_signal,
6488 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006489 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006490}
6491
6492static void
6493handle_output_create(struct wl_listener *listener, void *data)
6494{
6495 struct desktop_shell *shell =
6496 container_of(listener, struct desktop_shell, output_create_listener);
6497 struct weston_output *output = (struct weston_output *)data;
6498
6499 create_shell_output(shell, output);
6500}
6501
6502static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006503handle_output_move_layer(struct desktop_shell *shell,
6504 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006505{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006506 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006507 struct weston_view *view;
6508 float x, y;
6509
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006510 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006511 if (view->output != output)
6512 continue;
6513
6514 x = view->geometry.x + output->move_x;
6515 y = view->geometry.y + output->move_y;
6516 weston_view_set_position(view, x, y);
6517 }
6518}
6519
6520static void
6521handle_output_move(struct wl_listener *listener, void *data)
6522{
6523 struct desktop_shell *shell;
6524
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006525 shell = container_of(listener, struct desktop_shell,
6526 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006527
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006528 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006529}
6530
6531static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006532setup_output_destroy_handler(struct weston_compositor *ec,
6533 struct desktop_shell *shell)
6534{
6535 struct weston_output *output;
6536
6537 wl_list_init(&shell->output_list);
6538 wl_list_for_each(output, &ec->output_list, link)
6539 create_shell_output(shell, output);
6540
6541 shell->output_create_listener.notify = handle_output_create;
6542 wl_signal_add(&ec->output_created_signal,
6543 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006544
6545 shell->output_move_listener.notify = handle_output_move;
6546 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006547}
6548
6549static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006550shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006551{
Tiago Vignattibe143262012-04-16 17:31:41 +03006552 struct desktop_shell *shell =
6553 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006554 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006555 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006556
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006557 /* Force state to unlocked so we don't try to fade */
6558 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006559
6560 if (shell->child.client) {
6561 /* disable respawn */
6562 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006563 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006564 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006565
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006566 wl_list_remove(&shell->idle_listener.link);
6567 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006568 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006569
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006570 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006571 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006572
Xiong Zhang6b481422013-10-23 13:58:32 +08006573 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6574 wl_list_remove(&shell_output->destroy_listener.link);
6575 wl_list_remove(&shell_output->link);
6576 free(shell_output);
6577 }
6578
6579 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006580 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02006581 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006582
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006583 wl_array_for_each(ws, &shell->workspaces.array)
6584 workspace_destroy(*ws);
6585 wl_array_release(&shell->workspaces.array);
6586
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006587 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006588 free(shell);
6589}
6590
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006591static void
6592shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6593{
6594 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006595 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006596
Bob Ham744e6532016-01-12 10:21:48 +00006597 if (shell->allow_zap)
6598 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6599 MODIFIER_CTRL | MODIFIER_ALT,
6600 terminate_binding, ec);
6601
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006602 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006603 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6604 click_to_activate_binding,
6605 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006606 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6607 click_to_activate_binding,
6608 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006609 weston_compositor_add_touch_binding(ec, 0,
6610 touch_to_activate_binding,
6611 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006612 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6613 MODIFIER_SUPER | MODIFIER_ALT,
6614 surface_opacity_binding, NULL);
6615 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6616 MODIFIER_SUPER, zoom_axis_binding,
6617 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006618 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6619 backlight_binding, ec);
6620 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6621 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006622
6623 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006624 if (shell->exposay_modifier)
6625 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6626 exposay_binding, shell);
6627
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006628 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006629 if (!mod)
6630 return;
6631
Daniel Stone325fc2d2012-05-30 16:31:58 +01006632 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6633 zoom_key_binding, NULL);
6634 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6635 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006636 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6637 maximize_binding, NULL);
6638 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6639 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006640 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6641 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006642 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006643 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006644 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006645 weston_compositor_add_button_binding(ec, BTN_LEFT,
6646 mod | MODIFIER_SHIFT,
6647 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006648
6649 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006650 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006651 rotate_binding, NULL);
6652
Daniel Stone325fc2d2012-05-30 16:31:58 +01006653 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6654 shell);
6655 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6656 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006657 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6658 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006659 weston_compositor_add_key_binding(ec, KEY_K, mod,
6660 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006661 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6662 workspace_up_binding, shell);
6663 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6664 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006665 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6666 workspace_move_surface_up_binding,
6667 shell);
6668 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6669 workspace_move_surface_down_binding,
6670 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006671
6672 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6673 if (shell->workspaces.num > 1) {
6674 num_workspace_bindings = shell->workspaces.num;
6675 if (num_workspace_bindings > 6)
6676 num_workspace_bindings = 6;
6677 for (i = 0; i < num_workspace_bindings; i++)
6678 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6679 workspace_f_binding,
6680 shell);
6681 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006682
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006683 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006684}
6685
Jason Ekstrand024177c2014-04-21 19:42:58 -05006686static void
6687handle_seat_created(struct wl_listener *listener, void *data)
6688{
6689 struct weston_seat *seat = data;
6690
6691 create_shell_seat(seat);
6692}
6693
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006694WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006695module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006696 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006697{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006698 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006699 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006700 struct workspace **pws;
6701 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006702 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006703
Peter Huttererf3d62272013-08-08 11:57:05 +10006704 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006705 if (shell == NULL)
6706 return -1;
6707
Kristian Høgsberg75840622011-09-06 13:48:16 -04006708 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006709
6710 shell->destroy_listener.notify = shell_destroy;
6711 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006712 shell->idle_listener.notify = idle_handler;
6713 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6714 shell->wake_listener.notify = wake_handler;
6715 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006716 shell->transform_listener.notify = transform_handler;
6717 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006718
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006719 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006720 ec->shell_interface.create_shell_surface = create_shell_surface;
6721 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006722 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006723 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006724 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006725 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006726 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006727 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006728 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006729 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006730 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006731 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006732
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006733 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6734 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006735 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6736 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006737 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006738
6739 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006740 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006741
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006742 if (input_panel_setup(shell) < 0)
6743 return -1;
6744
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006745 shell->text_backend = text_backend_init(ec);
6746 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006747 return -1;
6748
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006749 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006750
Daniel Stonedf8133b2013-11-19 11:37:14 +01006751 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6752 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6753
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006754 for (i = 0; i < shell->workspaces.num; i++) {
6755 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6756 if (pws == NULL)
6757 return -1;
6758
6759 *pws = workspace_create();
6760 if (*pws == NULL)
6761 return -1;
6762 }
6763 activate_workspace(shell, 0);
6764
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006765 weston_layer_init(&shell->minimized_layer, NULL);
6766
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006767 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006768 wl_list_init(&shell->workspaces.animation.link);
6769 shell->workspaces.animation.frame = animate_workspace_change_frame;
6770
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006771 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006772 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006773 return -1;
6774
Rafael Antognollie2a34552013-12-03 15:35:45 -02006775 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6776 shell, bind_xdg_shell) == NULL)
6777 return -1;
6778
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006779 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006780 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006781 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006782 return -1;
6783
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006784 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006785
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006786 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006787
Xiong Zhang6b481422013-10-23 13:58:32 +08006788 setup_output_destroy_handler(ec, shell);
6789
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006790 loop = wl_display_get_event_loop(ec->wl_display);
6791 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006792
Jason Ekstrand024177c2014-04-21 19:42:58 -05006793 wl_list_for_each(seat, &ec->seat_list, link)
6794 handle_seat_created(NULL, seat);
6795 shell->seat_create_listener.notify = handle_seat_created;
6796 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006797
David Fort50d962f2016-06-09 17:55:52 +02006798 shell->resized_listener.notify = handle_output_resized;
6799 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6800
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006801 screenshooter_create(ec);
6802
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006803 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006804
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006805 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006806
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006807 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6808
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006809 return 0;
6810}