blob: 79cf8dde24f754541fa22798cf6f01a861af8e35 [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"
Giulio Camuffo26f62d42016-06-02 21:48:09 +030039#include "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{
691 return container_of(compositor->output_list.next,
692 struct weston_output, link);
693}
694
Pekka Paalanen8274d902014-08-06 19:36:51 +0300695static int
696focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
697{
698 return snprintf(buf, len, "focus highlight effect for output %s",
699 surface->output->name);
700}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100701
702/* no-op func for checking focus surface */
703static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600704focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705{
706}
707
708static struct focus_surface *
709get_focus_surface(struct weston_surface *surface)
710{
711 if (surface->configure == focus_surface_configure)
712 return surface->configure_private;
713 else
714 return NULL;
715}
716
717static bool
718is_focus_surface (struct weston_surface *es)
719{
720 return (es->configure == focus_surface_configure);
721}
722
723static bool
724is_focus_view (struct weston_view *view)
725{
726 return is_focus_surface (view->surface);
727}
728
729static struct focus_surface *
730create_focus_surface(struct weston_compositor *ec,
731 struct weston_output *output)
732{
733 struct focus_surface *fsurf = NULL;
734 struct weston_surface *surface = NULL;
735
736 fsurf = malloc(sizeof *fsurf);
737 if (!fsurf)
738 return NULL;
739
740 fsurf->surface = weston_surface_create(ec);
741 surface = fsurf->surface;
742 if (surface == NULL) {
743 free(fsurf);
744 return NULL;
745 }
746
747 surface->configure = focus_surface_configure;
748 surface->output = output;
749 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300750 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100751
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800752 fsurf->view = weston_view_create(surface);
753 if (fsurf->view == NULL) {
754 weston_surface_destroy(surface);
755 free(fsurf);
756 return NULL;
757 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100758 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100759
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600760 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600761 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100762 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
763 pixman_region32_fini(&surface->opaque);
764 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
765 output->width, output->height);
766 pixman_region32_fini(&surface->input);
767 pixman_region32_init(&surface->input);
768
769 wl_list_init(&fsurf->workspace_transform.link);
770
771 return fsurf;
772}
773
774static void
775focus_surface_destroy(struct focus_surface *fsurf)
776{
777 weston_surface_destroy(fsurf->surface);
778 free(fsurf);
779}
780
781static void
782focus_animation_done(struct weston_view_animation *animation, void *data)
783{
784 struct workspace *ws = data;
785
786 ws->focus_animation = NULL;
787}
788
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200789static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200790focus_state_destroy(struct focus_state *state)
791{
792 wl_list_remove(&state->seat_destroy_listener.link);
793 wl_list_remove(&state->surface_destroy_listener.link);
794 free(state);
795}
796
797static void
798focus_state_seat_destroy(struct wl_listener *listener, void *data)
799{
800 struct focus_state *state = container_of(listener,
801 struct focus_state,
802 seat_destroy_listener);
803
804 wl_list_remove(&state->link);
805 focus_state_destroy(state);
806}
807
808static void
809focus_state_surface_destroy(struct wl_listener *listener, void *data)
810{
811 struct focus_state *state = container_of(listener,
812 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400813 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400814 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500815 struct weston_surface *main_surface, *next;
816 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200817
Pekka Paalanen01388e22013-04-25 13:57:44 +0300818 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
819
Kristian Høgsberge3778222012-07-31 17:29:30 -0400820 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300821 wl_list_for_each(view,
822 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500823 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400824 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100825 if (is_focus_view(view))
826 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400827
Jason Ekstranda7af7042013-10-12 22:38:11 -0500828 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400829 break;
830 }
831
Pekka Paalanen01388e22013-04-25 13:57:44 +0300832 /* if the focus was a sub-surface, activate its main surface */
833 if (main_surface != state->keyboard_focus)
834 next = main_surface;
835
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400837 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100838 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100839 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400840 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
842 if (state->ws->focus_animation)
843 weston_view_animation_destroy(state->ws->focus_animation);
844
845 state->ws->focus_animation = weston_fade_run(
846 state->ws->fsurf_front->view,
847 state->ws->fsurf_front->view->alpha, 0.0, 300,
848 focus_animation_done, state->ws);
849 }
850
Kristian Høgsberge3778222012-07-31 17:29:30 -0400851 wl_list_remove(&state->link);
852 focus_state_destroy(state);
853 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200854}
855
856static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400857focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200858{
Jonas Ådahl04769742012-06-13 00:01:24 +0200859 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200860
861 state = malloc(sizeof *state);
862 if (state == NULL)
863 return NULL;
864
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400866 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200867 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400868 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200869
870 state->seat_destroy_listener.notify = focus_state_seat_destroy;
871 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400872 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200873 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400874 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200875
876 return state;
877}
878
Jonas Ådahl8538b222012-08-29 22:13:03 +0200879static struct focus_state *
880ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
881{
882 struct workspace *ws = get_current_workspace(shell);
883 struct focus_state *state;
884
885 wl_list_for_each(state, &ws->focus_list, link)
886 if (state->seat == seat)
887 break;
888
889 if (&state->link == &ws->focus_list)
890 state = focus_state_create(seat, ws);
891
892 return state;
893}
894
Jonas Ådahl04769742012-06-13 00:01:24 +0200895static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800896focus_state_set_focus(struct focus_state *state,
897 struct weston_surface *surface)
898{
899 if (state->keyboard_focus) {
900 wl_list_remove(&state->surface_destroy_listener.link);
901 wl_list_init(&state->surface_destroy_listener.link);
902 }
903
904 state->keyboard_focus = surface;
905 if (surface)
906 wl_signal_add(&surface->destroy_signal,
907 &state->surface_destroy_listener);
908}
909
910static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400911restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200912{
913 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400914 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100915 struct wl_list pending_seat_list;
916 struct weston_seat *seat, *next_seat;
917
918 /* Temporarily steal the list of seats so that we can keep
919 * track of the seats we've already processed */
920 wl_list_init(&pending_seat_list);
921 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
922 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200923
924 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500925 struct weston_keyboard *keyboard =
926 weston_seat_get_keyboard(state->seat);
927
Neil Roberts4237f502014-04-09 16:33:31 +0100928 wl_list_remove(&state->seat->link);
929 wl_list_insert(&shell->compositor->seat_list,
930 &state->seat->link);
931
Derek Foreman1281a362015-07-31 16:55:32 -0500932 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800933 continue;
934
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400935 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200936
Derek Foreman1281a362015-07-31 16:55:32 -0500937 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200938 }
Neil Roberts4237f502014-04-09 16:33:31 +0100939
940 /* For any remaining seats that we don't have a focus state
941 * for we'll reset the keyboard focus to NULL */
942 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500943 struct weston_keyboard *keyboard =
944 weston_seat_get_keyboard(seat);
945
Neil Roberts4237f502014-04-09 16:33:31 +0100946 wl_list_insert(&shell->compositor->seat_list, &seat->link);
947
Derek Foreman1281a362015-07-31 16:55:32 -0500948 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100949 continue;
950
Derek Foreman1281a362015-07-31 16:55:32 -0500951 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100952 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200953}
954
955static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
957 struct weston_seat *seat)
958{
Derek Foreman1281a362015-07-31 16:55:32 -0500959 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200960 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200961
962 wl_list_for_each(state, &ws->focus_list, link) {
963 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500964 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200965 return;
966 }
967 }
968}
969
970static void
971drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
972 struct weston_surface *surface)
973{
974 struct focus_state *state;
975
976 wl_list_for_each(state, &ws->focus_list, link)
977 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800978 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200979}
980
981static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100982animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
983 struct weston_view *from, struct weston_view *to)
984{
985 struct weston_output *output;
986 bool focus_surface_created = false;
987
988 /* FIXME: Only support dim animation using two layers */
989 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
990 return;
991
992 output = get_default_output(shell->compositor);
993 if (ws->fsurf_front == NULL && (from || to)) {
994 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800995 if (ws->fsurf_front == NULL)
996 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800998
999 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1000 if (ws->fsurf_back == NULL) {
1001 focus_surface_destroy(ws->fsurf_front);
1002 return;
1003 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001005
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001006 focus_surface_created = true;
1007 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001008 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1009 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001010 }
1011
1012 if (ws->focus_animation) {
1013 weston_view_animation_destroy(ws->focus_animation);
1014 ws->focus_animation = NULL;
1015 }
1016
1017 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001018 weston_layer_entry_insert(&to->layer_link,
1019 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001020 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001021 weston_layer_entry_insert(&ws->layer.view_list,
1022 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001023
1024 if (focus_surface_created) {
1025 ws->focus_animation = weston_fade_run(
1026 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001027 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 focus_animation_done, ws);
1029 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001030 weston_layer_entry_insert(&from->layer_link,
1031 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001032 ws->focus_animation = weston_stable_fade_run(
1033 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001034 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001035 focus_animation_done, ws);
1036 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001037 weston_layer_entry_insert(&ws->layer.view_list,
1038 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 ws->focus_animation = weston_stable_fade_run(
1040 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001041 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001042 focus_animation_done, ws);
1043 }
1044}
1045
1046static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001047workspace_destroy(struct workspace *ws)
1048{
Jonas Ådahl04769742012-06-13 00:01:24 +02001049 struct focus_state *state, *next;
1050
1051 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1052 focus_state_destroy(state);
1053
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001054 if (ws->fsurf_front)
1055 focus_surface_destroy(ws->fsurf_front);
1056 if (ws->fsurf_back)
1057 focus_surface_destroy(ws->fsurf_back);
1058
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001059 free(ws);
1060}
1061
Jonas Ådahl04769742012-06-13 00:01:24 +02001062static void
1063seat_destroyed(struct wl_listener *listener, void *data)
1064{
1065 struct weston_seat *seat = data;
1066 struct focus_state *state, *next;
1067 struct workspace *ws = container_of(listener,
1068 struct workspace,
1069 seat_destroyed_listener);
1070
1071 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1072 if (state->seat == seat)
1073 wl_list_remove(&state->link);
1074}
1075
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001076static struct workspace *
1077workspace_create(void)
1078{
1079 struct workspace *ws = malloc(sizeof *ws);
1080 if (ws == NULL)
1081 return NULL;
1082
1083 weston_layer_init(&ws->layer, NULL);
1084
Jonas Ådahl04769742012-06-13 00:01:24 +02001085 wl_list_init(&ws->focus_list);
1086 wl_list_init(&ws->seat_destroyed_listener.link);
1087 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001088 ws->fsurf_front = NULL;
1089 ws->fsurf_back = NULL;
1090 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001091
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001092 return ws;
1093}
1094
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095static int
1096workspace_is_empty(struct workspace *ws)
1097{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001098 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001099}
1100
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001101static struct workspace *
1102get_workspace(struct desktop_shell *shell, unsigned int index)
1103{
1104 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001105 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001106 pws += index;
1107 return *pws;
1108}
1109
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001110struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001111get_current_workspace(struct desktop_shell *shell)
1112{
1113 return get_workspace(shell, shell->workspaces.current);
1114}
1115
1116static void
1117activate_workspace(struct desktop_shell *shell, unsigned int index)
1118{
1119 struct workspace *ws;
1120
1121 ws = get_workspace(shell, index);
1122 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1123
1124 shell->workspaces.current = index;
1125}
1126
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127static unsigned int
1128get_output_height(struct weston_output *output)
1129{
1130 return abs(output->region.extents.y1 - output->region.extents.y2);
1131}
1132
1133static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001134view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136 struct weston_transform *transform;
1137
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001138 if (is_focus_view(view)) {
1139 struct focus_surface *fsurf = get_focus_surface(view->surface);
1140 transform = &fsurf->workspace_transform;
1141 } else {
1142 struct shell_surface *shsurf = get_shell_surface(view->surface);
1143 transform = &shsurf->workspace_transform;
1144 }
1145
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001148 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001150 weston_matrix_init(&transform->matrix);
1151 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154}
1155
1156static void
1157workspace_translate_out(struct workspace *ws, double fraction)
1158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 unsigned int height;
1161 double d;
1162
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001163 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001165 d = height * fraction;
1166
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001167 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168 }
1169}
1170
1171static void
1172workspace_translate_in(struct workspace *ws, double fraction)
1173{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001175 unsigned int height;
1176 double d;
1177
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001178 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001180
1181 if (fraction > 0)
1182 d = -(height - height * fraction);
1183 else
1184 d = height + height * fraction;
1185
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001186 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187 }
1188}
1189
1190static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001191reverse_workspace_change_animation(struct desktop_shell *shell,
1192 unsigned int index,
1193 struct workspace *from,
1194 struct workspace *to)
1195{
1196 shell->workspaces.current = index;
1197
1198 shell->workspaces.anim_to = to;
1199 shell->workspaces.anim_from = from;
1200 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1201 shell->workspaces.anim_timestamp = 0;
1202
Scott Moreau4272e632012-08-13 09:58:41 -06001203 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001204}
1205
1206static void
1207workspace_deactivate_transforms(struct workspace *ws)
1208{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001210 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001211
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001212 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001213 if (is_focus_view(view)) {
1214 struct focus_surface *fsurf = get_focus_surface(view->surface);
1215 transform = &fsurf->workspace_transform;
1216 } else {
1217 struct shell_surface *shsurf = get_shell_surface(view->surface);
1218 transform = &shsurf->workspace_transform;
1219 }
1220
1221 if (!wl_list_empty(&transform->link)) {
1222 wl_list_remove(&transform->link);
1223 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001226 }
1227}
1228
1229static void
1230finish_workspace_change_animation(struct desktop_shell *shell,
1231 struct workspace *from,
1232 struct workspace *to)
1233{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001234 struct weston_view *view;
1235
Scott Moreau4272e632012-08-13 09:58:41 -06001236 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001237
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001238 /* Views that extend past the bottom of the output are still
1239 * visible after the workspace animation ends but before its layer
1240 * is hidden. In that case, we need to damage below those views so
1241 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001242 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001243 weston_view_damage_below(view);
1244
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001245 wl_list_remove(&shell->workspaces.animation.link);
1246 workspace_deactivate_transforms(from);
1247 workspace_deactivate_transforms(to);
1248 shell->workspaces.anim_to = NULL;
1249
1250 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1251}
1252
1253static void
1254animate_workspace_change_frame(struct weston_animation *animation,
1255 struct weston_output *output, uint32_t msecs)
1256{
1257 struct desktop_shell *shell =
1258 container_of(animation, struct desktop_shell,
1259 workspaces.animation);
1260 struct workspace *from = shell->workspaces.anim_from;
1261 struct workspace *to = shell->workspaces.anim_to;
1262 uint32_t t;
1263 double x, y;
1264
1265 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1266 finish_workspace_change_animation(shell, from, to);
1267 return;
1268 }
1269
1270 if (shell->workspaces.anim_timestamp == 0) {
1271 if (shell->workspaces.anim_current == 0.0)
1272 shell->workspaces.anim_timestamp = msecs;
1273 else
1274 shell->workspaces.anim_timestamp =
1275 msecs -
1276 /* Invers of movement function 'y' below. */
1277 (asin(1.0 - shell->workspaces.anim_current) *
1278 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1279 M_2_PI);
1280 }
1281
1282 t = msecs - shell->workspaces.anim_timestamp;
1283
1284 /*
1285 * x = [0, π/2]
1286 * y(x) = sin(x)
1287 */
1288 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1289 y = sin(x);
1290
1291 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001292 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001293
1294 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1295 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1296 shell->workspaces.anim_current = y;
1297
Scott Moreau4272e632012-08-13 09:58:41 -06001298 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001299 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001300 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001301 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001302}
1303
1304static void
1305animate_workspace_change(struct desktop_shell *shell,
1306 unsigned int index,
1307 struct workspace *from,
1308 struct workspace *to)
1309{
1310 struct weston_output *output;
1311
1312 int dir;
1313
1314 if (index > shell->workspaces.current)
1315 dir = -1;
1316 else
1317 dir = 1;
1318
1319 shell->workspaces.current = index;
1320
1321 shell->workspaces.anim_dir = dir;
1322 shell->workspaces.anim_from = from;
1323 shell->workspaces.anim_to = to;
1324 shell->workspaces.anim_current = 0.0;
1325 shell->workspaces.anim_timestamp = 0;
1326
1327 output = container_of(shell->compositor->output_list.next,
1328 struct weston_output, link);
1329 wl_list_insert(&output->animation_list,
1330 &shell->workspaces.animation.link);
1331
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001332 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001333
1334 workspace_translate_in(to, 0);
1335
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001336 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001337
Scott Moreau4272e632012-08-13 09:58:41 -06001338 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001339}
1340
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001341static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001342update_workspace(struct desktop_shell *shell, unsigned int index,
1343 struct workspace *from, struct workspace *to)
1344{
1345 shell->workspaces.current = index;
1346 wl_list_insert(&from->layer.link, &to->layer.link);
1347 wl_list_remove(&from->layer.link);
1348}
1349
1350static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001351change_workspace(struct desktop_shell *shell, unsigned int index)
1352{
1353 struct workspace *from;
1354 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001355 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001356
1357 if (index == shell->workspaces.current)
1358 return;
1359
1360 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001361 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001362 return;
1363
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001364 from = get_current_workspace(shell);
1365 to = get_workspace(shell, index);
1366
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001367 if (shell->workspaces.anim_from == to &&
1368 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001369 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001370 reverse_workspace_change_animation(shell, index, from, to);
1371 return;
1372 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001373
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001374 if (shell->workspaces.anim_to != NULL)
1375 finish_workspace_change_animation(shell,
1376 shell->workspaces.anim_from,
1377 shell->workspaces.anim_to);
1378
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001379 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001380
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001381 if (shell->focus_animation_type != ANIMATION_NONE) {
1382 wl_list_for_each(state, &from->focus_list, link)
1383 if (state->keyboard_focus)
1384 animate_focus_change(shell, from,
1385 get_default_view(state->keyboard_focus), NULL);
1386
1387 wl_list_for_each(state, &to->focus_list, link)
1388 if (state->keyboard_focus)
1389 animate_focus_change(shell, to,
1390 NULL, get_default_view(state->keyboard_focus));
1391 }
1392
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001393 if (workspace_is_empty(to) && workspace_is_empty(from))
1394 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001395 else
1396 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001397}
1398
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001399static bool
1400workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1401{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001402 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001403 struct wl_list *e;
1404
1405 if (wl_list_empty(list))
1406 return false;
1407
1408 e = list->next;
1409
1410 if (e->next != list)
1411 return false;
1412
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001413 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001414}
1415
1416static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001417surface_keyboard_focus_lost(struct weston_surface *surface)
1418{
1419 struct weston_compositor *compositor = surface->compositor;
1420 struct weston_seat *seat;
1421 struct weston_surface *focus;
1422
1423 wl_list_for_each(seat, &compositor->seat_list, link) {
1424 struct weston_keyboard *keyboard =
1425 weston_seat_get_keyboard(seat);
1426
1427 if (!keyboard)
1428 continue;
1429
1430 focus = weston_surface_get_main_surface(keyboard->focus);
1431 if (focus == surface)
1432 weston_keyboard_set_focus(keyboard, NULL);
1433 }
1434}
1435
1436static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001437take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001438 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001439 unsigned int index)
1440{
Derek Foreman1281a362015-07-31 16:55:32 -05001441 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001442 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001444 struct shell_surface *shsurf;
1445 struct workspace *from;
1446 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001447 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001448
Derek Foreman1281a362015-07-31 16:55:32 -05001449 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 view = get_default_view(surface);
1451 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001452 index == shell->workspaces.current ||
1453 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001454 return;
1455
1456 from = get_current_workspace(shell);
1457 to = get_workspace(shell, index);
1458
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001459 weston_layer_entry_remove(&view->layer_link);
1460 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001461
Philip Withnall659163d2013-11-25 18:01:36 +00001462 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001463 if (shsurf != NULL)
1464 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001465
Jonas Ådahle9d22502012-08-29 22:13:01 +02001466 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001467 drop_focus_state(shell, from, surface);
1468
1469 if (shell->workspaces.anim_from == to &&
1470 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001471 wl_list_remove(&to->layer.link);
1472 wl_list_insert(from->layer.link.prev, &to->layer.link);
1473
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001474 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001475
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001476 return;
1477 }
1478
1479 if (shell->workspaces.anim_to != NULL)
1480 finish_workspace_change_animation(shell,
1481 shell->workspaces.anim_from,
1482 shell->workspaces.anim_to);
1483
1484 if (workspace_is_empty(from) &&
1485 workspace_has_only(to, surface))
1486 update_workspace(shell, index, from, to);
1487 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001488 if (shsurf != NULL &&
1489 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001490 wl_list_insert(&shell->workspaces.anim_sticky_list,
1491 &shsurf->workspace_transform.link);
1492
1493 animate_workspace_change(shell, index, from, to);
1494 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001495
Jonas Ådahl8538b222012-08-29 22:13:03 +02001496 state = ensure_focus_state(shell, seat);
1497 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001498 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001499}
1500
1501static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001502touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001503 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001504{
1505}
1506
1507static void
1508touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1509{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001510 struct weston_touch_move_grab *move =
1511 (struct weston_touch_move_grab *) container_of(
1512 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001513
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001514 if (touch_id == 0)
1515 move->active = 0;
1516
Jonas Ådahl9484b692013-12-02 22:05:03 +01001517 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001518 shell_touch_grab_end(&move->base);
1519 free(move);
1520 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001521}
1522
1523static void
1524touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001525 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001526{
1527 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1528 struct shell_surface *shsurf = move->base.shsurf;
1529 struct weston_surface *es;
1530 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1531 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1532
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001533 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001534 return;
1535
1536 es = shsurf->surface;
1537
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001538 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001539
1540 weston_compositor_schedule_repaint(es->compositor);
1541}
1542
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001543static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001544touch_move_grab_frame(struct weston_touch_grab *grab)
1545{
1546}
1547
1548static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001549touch_move_grab_cancel(struct weston_touch_grab *grab)
1550{
1551 struct weston_touch_move_grab *move =
1552 (struct weston_touch_move_grab *) container_of(
1553 grab, struct shell_touch_grab, grab);
1554
1555 shell_touch_grab_end(&move->base);
1556 free(move);
1557}
1558
Rusty Lynch1084da52013-08-15 09:10:08 -07001559static const struct weston_touch_grab_interface touch_move_grab_interface = {
1560 touch_move_grab_down,
1561 touch_move_grab_up,
1562 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001563 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001564 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001565};
1566
1567static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001568surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001569{
1570 struct weston_touch_move_grab *move;
1571
1572 if (!shsurf)
1573 return -1;
1574
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001575 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001576 return 0;
1577
1578 move = malloc(sizeof *move);
1579 if (!move)
1580 return -1;
1581
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001582 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001584 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001586 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001587
1588 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001589 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001590
1591 return 0;
1592}
1593
1594static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001595noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001596{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001597}
1598
1599static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001600noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001601 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001602{
1603}
1604
1605static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001606noop_grab_axis_source(struct weston_pointer_grab *grab,
1607 uint32_t source)
1608{
1609}
1610
1611static void
1612noop_grab_frame(struct weston_pointer_grab *grab)
1613{
1614}
1615
1616static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001617constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1618{
1619 struct shell_surface *shsurf = move->base.shsurf;
1620 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001621 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001622 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001623 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001624
1625 x = wl_fixed_to_int(pointer->x + move->dx);
1626 y = wl_fixed_to_int(pointer->y + move->dy);
1627
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001628 if (shsurf->shell->panel_position ==
1629 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001630 get_output_work_area(shsurf->shell,
1631 shsurf->surface->output,
1632 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001633
Derek Foreman6feb0f92015-04-15 12:23:56 -05001634 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1635 if (bottom - safety < area.y)
1636 y = area.y + safety - shsurf->geometry.height
1637 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001638
1639 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001640 y + shsurf->geometry.y < area.y)
1641 y = area.y - shsurf->geometry.y;
1642
Jonny Lambd73c6942014-08-20 15:53:20 +02001643 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001644
1645 *cx = x;
1646 *cy = y;
1647}
1648
1649static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001650move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001651 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001652{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001653 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001655 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001656 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001657
Jonas Ådahld2510102014-10-05 21:39:14 +02001658 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001659 if (!shsurf)
1660 return;
1661
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001662 constrain_position(move, &cx, &cy);
1663
1664 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001667}
1668
1669static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001670move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001671 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001672{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001673 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1674 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001675 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001676 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001677
Daniel Stone4dbadb12012-05-30 16:31:51 +01001678 if (pointer->button_count == 0 &&
1679 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001680 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001681 free(grab);
1682 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001683}
1684
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001685static void
1686move_grab_cancel(struct weston_pointer_grab *grab)
1687{
1688 struct shell_grab *shell_grab =
1689 container_of(grab, struct shell_grab, grab);
1690
1691 shell_grab_end(shell_grab);
1692 free(grab);
1693}
1694
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001695static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001696 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001697 move_grab_motion,
1698 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001699 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001700 noop_grab_axis_source,
1701 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001702 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001703};
1704
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001706surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001707 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708{
1709 struct weston_move_grab *move;
1710
1711 if (!shsurf)
1712 return -1;
1713
Derek Foreman45a7c272015-09-11 14:27:40 -05001714 shsurf = find_toplevel_surface(shsurf);
1715
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001716 if (shsurf->grabbed ||
1717 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001718 return 0;
1719
1720 move = malloc(sizeof *move);
1721 if (!move)
1722 return -1;
1723
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001725 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001726 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001727 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001728 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001729
1730 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001731 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732
1733 return 0;
1734}
1735
1736static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001737common_surface_move(struct wl_resource *resource,
1738 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001740 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001741 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1742 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001743 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001744 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745
Derek Foreman1281a362015-07-31 16:55:32 -05001746 if (pointer &&
1747 pointer->focus &&
1748 pointer->button_count > 0 &&
1749 pointer->grab_serial == serial) {
1750 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001751 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001752 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001753 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001754 } else if (touch &&
1755 touch->focus &&
1756 touch->grab_serial == serial) {
1757 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001758 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001759 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001760 wl_resource_post_no_memory(resource);
1761 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762}
1763
Rafael Antognollie2a34552013-12-03 15:35:45 -02001764static void
1765shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1766 struct wl_resource *seat_resource, uint32_t serial)
1767{
1768 common_surface_move(resource, seat_resource, serial);
1769}
1770
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771struct weston_resize_grab {
1772 struct shell_grab base;
1773 uint32_t edges;
1774 int32_t width, height;
1775};
1776
1777static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001778resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001779 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001780{
1781 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001782 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001783 struct shell_surface *shsurf = resize->base.shsurf;
1784 int32_t width, height;
1785 wl_fixed_t from_x, from_y;
1786 wl_fixed_t to_x, to_y;
1787
Jonas Ådahld2510102014-10-05 21:39:14 +02001788 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001789
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001790 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001791 return;
1792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 weston_view_from_global_fixed(shsurf->view,
1794 pointer->grab_x, pointer->grab_y,
1795 &from_x, &from_y);
1796 weston_view_from_global_fixed(shsurf->view,
1797 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001798
1799 width = resize->width;
1800 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1801 width += wl_fixed_to_int(from_x - to_x);
1802 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1803 width += wl_fixed_to_int(to_x - from_x);
1804 }
1805
1806 height = resize->height;
1807 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1808 height += wl_fixed_to_int(from_y - to_y);
1809 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1810 height += wl_fixed_to_int(to_y - from_y);
1811 }
1812
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001813 if (width < 1)
1814 width = 1;
1815 if (height < 1)
1816 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001817 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001818}
1819
1820static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001821send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001822{
1823 struct shell_surface *shsurf = get_shell_surface(surface);
1824
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001825 assert(shsurf);
1826
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001827 if (shsurf->resource)
1828 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001829 shsurf->resize_edges,
1830 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001831}
1832
1833static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001834 send_configure,
1835 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001836};
1837
1838static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001839resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001840 uint32_t time, uint32_t button, uint32_t state_w)
1841{
1842 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001843 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001844 enum wl_pointer_button_state state = state_w;
1845
1846 if (pointer->button_count == 0 &&
1847 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1848 shell_grab_end(&resize->base);
1849 free(grab);
1850 }
1851}
1852
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001853static void
1854resize_grab_cancel(struct weston_pointer_grab *grab)
1855{
1856 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1857
1858 shell_grab_end(&resize->base);
1859 free(grab);
1860}
1861
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001862static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001863 noop_grab_focus,
1864 resize_grab_motion,
1865 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001866 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001867 noop_grab_axis_source,
1868 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001869 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001870};
1871
Giulio Camuffob8366642013-04-25 13:57:46 +03001872/*
1873 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001874 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001875static void
1876surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1877 int32_t *y, int32_t *w, int32_t *h) {
1878 pixman_region32_t region;
1879 pixman_box32_t *box;
1880 struct weston_subsurface *subsurface;
1881
1882 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 surface->width,
1884 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001885
1886 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1887 pixman_region32_union_rect(&region, &region,
1888 subsurface->position.x,
1889 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890 subsurface->surface->width,
1891 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001892 }
1893
1894 box = pixman_region32_extents(&region);
1895 if (x)
1896 *x = box->x1;
1897 if (y)
1898 *y = box->y1;
1899 if (w)
1900 *w = box->x2 - box->x1;
1901 if (h)
1902 *h = box->y2 - box->y1;
1903
1904 pixman_region32_fini(&region);
1905}
1906
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001907static int
1908surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001909 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001910{
1911 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001912 const unsigned resize_topbottom =
1913 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1914 const unsigned resize_leftright =
1915 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1916 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001917
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001918 if (shsurf->grabbed ||
1919 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001920 return 0;
1921
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001922 /* Check for invalid edge combinations. */
1923 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1924 (edges & resize_topbottom) == resize_topbottom ||
1925 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001926 return 0;
1927
1928 resize = malloc(sizeof *resize);
1929 if (!resize)
1930 return -1;
1931
1932 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001933
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001934 resize->width = shsurf->geometry.width;
1935 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001936
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001937 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001938 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001939 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001940 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001941
1942 return 0;
1943}
1944
1945static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001946common_surface_resize(struct wl_resource *resource,
1947 struct wl_resource *seat_resource, uint32_t serial,
1948 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001949{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001950 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001951 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001952 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001953 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001954
Derek Foreman1281a362015-07-31 16:55:32 -05001955 if (!pointer ||
1956 pointer->button_count == 0 ||
1957 pointer->grab_serial != serial ||
1958 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001959 return;
1960
Derek Foreman1281a362015-07-31 16:55:32 -05001961 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001962 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001963 return;
1964
Derek Foreman1281a362015-07-31 16:55:32 -05001965 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001966 wl_resource_post_no_memory(resource);
1967}
1968
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001969static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001970shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1971 struct wl_resource *seat_resource, uint32_t serial,
1972 uint32_t edges)
1973{
1974 common_surface_resize(resource, seat_resource, serial, edges);
1975}
1976
1977static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001978busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001979{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001980 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001981 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001983 wl_fixed_t sx, sy;
1984
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985 view = weston_compositor_pick_view(pointer->seat->compositor,
1986 pointer->x, pointer->y,
1987 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001988
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001989 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1990 shell_grab_end(grab);
1991 free(grab);
1992 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001993}
1994
1995static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001996busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001997 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001998{
Jonas Ådahld2510102014-10-05 21:39:14 +02001999 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002000}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002001
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002002static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002003busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002004 uint32_t time, uint32_t button, uint32_t state)
2005{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002006 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002007 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002008 struct weston_pointer *pointer = grab->grab.pointer;
2009 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002010
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002011 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002012 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002013 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002014 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002015 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002016 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002017 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002018}
2019
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002020static void
2021busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2022{
2023 struct shell_grab *grab = (struct shell_grab *) base;
2024
2025 shell_grab_end(grab);
2026 free(grab);
2027}
2028
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002029static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030 busy_cursor_grab_focus,
2031 busy_cursor_grab_motion,
2032 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002033 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002034 noop_grab_axis_source,
2035 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002036 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002037};
2038
2039static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002040set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041{
2042 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002044 if (pointer->grab->interface == &busy_cursor_grab_interface)
2045 return;
2046
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047 grab = malloc(sizeof *grab);
2048 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002049 return;
2050
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002051 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002052 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002053 /* Mark the shsurf as ungrabbed so that button binding is able
2054 * to move it. */
2055 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002057
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002058static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002059end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002060{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002061 struct shell_grab *grab;
2062 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002063
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002064 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002065 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2066
2067 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002068 continue;
2069
Derek Foreman1281a362015-07-31 16:55:32 -05002070 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002071 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002072 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002073 wl_resource_get_client(grab->shsurf->resource) == client) {
2074 shell_grab_end(grab);
2075 free(grab);
2076 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002077 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002078}
2079
Scott Moreau9521d5e2012-04-19 13:06:17 -06002080static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002081handle_shell_client_destroy(struct wl_listener *listener, void *data);
2082
2083static int
2084xdg_ping_timeout_handler(void *data)
2085{
2086 struct shell_client *sc = data;
2087 struct weston_seat *seat;
2088 struct shell_surface *shsurf;
2089
2090 /* Client is not responding */
2091 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002092 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002093 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2094
2095 if (!pointer ||
2096 !pointer->focus ||
2097 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002098 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002099
Derek Foreman1281a362015-07-31 16:55:32 -05002100 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002101 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002102 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002103 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002104 }
2105
2106 return 1;
2107}
2108
2109static void
2110handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2111{
2112 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002113 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002114 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002115 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002116
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002117 if (sc->unresponsive) {
2118 xdg_ping_timeout_handler(sc);
2119 return;
2120 }
2121
2122 sc->ping_serial = serial;
2123 loop = wl_display_get_event_loop(compositor->wl_display);
2124 if (sc->ping_timer == NULL)
2125 sc->ping_timer =
2126 wl_event_loop_add_timer(loop,
2127 xdg_ping_timeout_handler, sc);
2128 if (sc->ping_timer == NULL)
2129 return;
2130
2131 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2132
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002133 if (shell_surface_is_xdg_surface(shsurf) ||
2134 shell_surface_is_xdg_popup(shsurf))
2135 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002136 else if (shell_surface_is_wl_shell_surface(shsurf)
2137 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002138 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002139}
2140
Scott Moreauff1db4a2012-04-17 19:06:18 -06002141static void
2142ping_handler(struct weston_surface *surface, uint32_t serial)
2143{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002144 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002145
2146 if (!shsurf)
2147 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002148 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002149 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002150 if (shsurf->surface == shsurf->shell->grab_surface)
2151 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002152 if (!shsurf->owner)
2153 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002154
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002155 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002156}
2157
2158static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002159handle_pointer_focus(struct wl_listener *listener, void *data)
2160{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002161 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002162 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002163 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002164 uint32_t serial;
2165
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002167 return;
2168
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002170 serial = wl_display_next_serial(compositor->wl_display);
2171 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002172}
2173
2174static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002175shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2176{
2177 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002178 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002179}
2180
2181static void
2182shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2183{
2184 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002185 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002186}
2187
2188static void
2189handle_keyboard_focus(struct wl_listener *listener, void *data)
2190{
2191 struct weston_keyboard *keyboard = data;
2192 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2193
2194 if (seat->focused_surface) {
2195 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2196 if (shsurf)
2197 shell_surface_lose_keyboard_focus(shsurf);
2198 }
2199
2200 seat->focused_surface = keyboard->focus;
2201
2202 if (seat->focused_surface) {
2203 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2204 if (shsurf)
2205 shell_surface_gain_keyboard_focus(shsurf);
2206 }
2207}
2208
2209static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002210shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002211{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002212 if (sc->ping_serial != serial)
2213 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002214
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002215 sc->unresponsive = 0;
2216 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002217
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002218 if (sc->ping_timer) {
2219 wl_event_source_remove(sc->ping_timer);
2220 sc->ping_timer = NULL;
2221 }
2222
2223}
2224
2225static void
2226shell_surface_pong(struct wl_client *client,
2227 struct wl_resource *resource, uint32_t serial)
2228{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002229 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2230 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002231
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002232 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002233}
2234
2235static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002236set_title(struct shell_surface *shsurf, const char *title)
2237{
2238 free(shsurf->title);
2239 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002240 shsurf->surface->timeline.force_refresh = 1;
2241}
2242
2243static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002244set_pid(struct shell_surface *shsurf, pid_t pid)
2245{
2246 /* We have no use for it */
2247}
2248
2249static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002250set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2251{
2252 shsurf->type = t;
2253 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002254}
2255
2256static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002257set_window_geometry(struct shell_surface *shsurf,
2258 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002259{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002260 shsurf->next_geometry.x = x;
2261 shsurf->next_geometry.y = y;
2262 shsurf->next_geometry.width = width;
2263 shsurf->next_geometry.height = height;
2264 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002265}
2266
2267static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002268shell_surface_set_title(struct wl_client *client,
2269 struct wl_resource *resource, const char *title)
2270{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002271 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002272
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002273 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002274}
2275
2276static void
2277shell_surface_set_class(struct wl_client *client,
2278 struct wl_resource *resource, const char *class)
2279{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002280 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002281
2282 free(shsurf->class);
2283 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002284 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002285}
2286
Alex Wu4539b082012-03-01 12:57:46 +08002287static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002288restore_output_mode(struct weston_output *output)
2289{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002290 if (output->original_mode)
2291 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002292}
2293
2294static void
2295restore_all_output_modes(struct weston_compositor *compositor)
2296{
2297 struct weston_output *output;
2298
2299 wl_list_for_each(output, &compositor->output_list, link)
2300 restore_output_mode(output);
2301}
2302
Philip Withnall07926d92013-11-25 18:01:40 +00002303/* The surface will be inserted into the list immediately after the link
2304 * returned by this function (i.e. will be stacked immediately above the
2305 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002306static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002307shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2308{
2309 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002310 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002311
2312 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002313 case SHELL_SURFACE_XWAYLAND:
2314 return &shsurf->shell->fullscreen_layer.view_list;
2315
2316 case SHELL_SURFACE_NONE:
2317 return NULL;
2318
Kristian Høgsbergead52422014-01-01 13:51:52 -08002319 case SHELL_SURFACE_POPUP:
2320 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002321 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002322 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002323 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002324 /* Move the surface to its parent layer so
2325 * that surfaces which are transient for
2326 * fullscreen surfaces don't get hidden by the
2327 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002328
2329 /* TODO: Handle a parent with multiple views */
2330 parent = get_default_view(shsurf->parent);
2331 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002332 return container_of(parent->layer_link.link.prev,
2333 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002334 }
Philip Withnall07926d92013-11-25 18:01:40 +00002335
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002336 /* Move the surface to a normal workspace layer so that surfaces
2337 * which were previously fullscreen or transient are no longer
2338 * rendered on top. */
2339 ws = get_current_workspace(shsurf->shell);
2340 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002341 }
2342
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002343 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002344}
2345
Philip Withnall648a4dd2013-11-25 18:01:44 +00002346static void
2347shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2348{
2349 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002350 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002351
2352 /* Move the child layers to the same workspace as shsurf. They will be
2353 * stacked above shsurf. */
2354 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002355 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002356 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002357 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002358 prev = container_of(shsurf->view->layer_link.link.prev,
2359 struct weston_layer_entry, link);
2360 weston_layer_entry_remove(&child->view->layer_link);
2361 weston_layer_entry_insert(prev,
2362 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002363 weston_view_geometry_dirty(child->view);
2364 weston_surface_damage(child->surface);
2365
2366 /* Recurse. We don’t expect this to recurse very far (if
2367 * at all) because that would imply we have transient
2368 * (or popup) children of transient surfaces, which
2369 * would be unusual. */
2370 shell_surface_update_child_surface_layers(child);
2371 }
2372 }
2373}
2374
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002375/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002376 * geometry to ensure the changes are redrawn.
2377 *
2378 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2379 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002380static void
2381shell_surface_update_layer(struct shell_surface *shsurf)
2382{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002383 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002384
2385 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2386
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002387 if (new_layer_link == NULL)
2388 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002389 if (new_layer_link == &shsurf->view->layer_link)
2390 return;
2391
2392 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002393 weston_layer_entry_remove(&shsurf->view->layer_link);
2394 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002395 weston_view_geometry_dirty(shsurf->view);
2396 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002397
2398 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002399}
2400
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002401static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002402shell_surface_set_parent(struct shell_surface *shsurf,
2403 struct weston_surface *parent)
2404{
2405 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002406
2407 wl_list_remove(&shsurf->children_link);
2408 wl_list_init(&shsurf->children_link);
2409
2410 /* Insert into the parent surface’s child list. */
2411 if (parent != NULL) {
2412 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2413 if (parent_shsurf != NULL)
2414 wl_list_insert(&parent_shsurf->children_list,
2415 &shsurf->children_link);
2416 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002417}
2418
2419static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002420shell_surface_set_output(struct shell_surface *shsurf,
2421 struct weston_output *output)
2422{
2423 struct weston_surface *es = shsurf->surface;
2424
2425 /* get the default output, if the client set it as NULL
2426 check whether the ouput is available */
2427 if (output)
2428 shsurf->output = output;
2429 else if (es->output)
2430 shsurf->output = es->output;
2431 else
2432 shsurf->output = get_default_output(es->compositor);
2433}
2434
2435static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002436surface_clear_next_states(struct shell_surface *shsurf)
2437{
2438 shsurf->next_state.maximized = false;
2439 shsurf->next_state.fullscreen = false;
2440
2441 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2442 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2443 shsurf->state_changed = true;
2444}
2445
2446static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002447set_toplevel(struct shell_surface *shsurf)
2448{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002449 shell_surface_set_parent(shsurf, NULL);
2450 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002451 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002452
2453 /* The layer_link is updated in set_surface_type(),
2454 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002455}
2456
2457static void
2458shell_surface_set_toplevel(struct wl_client *client,
2459 struct wl_resource *resource)
2460{
2461 struct shell_surface *surface = wl_resource_get_user_data(resource);
2462
2463 set_toplevel(surface);
2464}
2465
2466static void
2467set_transient(struct shell_surface *shsurf,
2468 struct weston_surface *parent, int x, int y, uint32_t flags)
2469{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002470 assert(parent != NULL);
2471
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002472 shell_surface_set_parent(shsurf, parent);
2473
2474 surface_clear_next_states(shsurf);
2475
Philip Withnallbecb77e2013-11-25 18:01:30 +00002476 shsurf->transient.x = x;
2477 shsurf->transient.y = y;
2478 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002479
Rafael Antognollied207b42013-12-03 15:35:43 -02002480 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002481 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002482 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002483
2484 /* The layer_link is updated in set_surface_type(),
2485 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002486}
2487
2488static void
2489shell_surface_set_transient(struct wl_client *client,
2490 struct wl_resource *resource,
2491 struct wl_resource *parent_resource,
2492 int x, int y, uint32_t flags)
2493{
2494 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2495 struct weston_surface *parent =
2496 wl_resource_get_user_data(parent_resource);
2497
2498 set_transient(shsurf, parent, x, y, flags);
2499}
2500
2501static void
2502set_fullscreen(struct shell_surface *shsurf,
2503 uint32_t method,
2504 uint32_t framerate,
2505 struct weston_output *output)
2506{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002507 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002508 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002509
2510 shsurf->fullscreen_output = shsurf->output;
2511 shsurf->fullscreen.type = method;
2512 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002513
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002514 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002515}
2516
2517static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002518weston_view_set_initial_position(struct weston_view *view,
2519 struct desktop_shell *shell);
2520
2521static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002522unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002523{
Philip Withnallf85fe842013-11-25 18:01:37 +00002524 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002525 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2526 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002527 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002528 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002529
Alex Wu4539b082012-03-01 12:57:46 +08002530 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2531 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002532
Alex Wu4539b082012-03-01 12:57:46 +08002533 wl_list_remove(&shsurf->fullscreen.transform.link);
2534 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002535
Jason Ekstranda7af7042013-10-12 22:38:11 -05002536 if (shsurf->fullscreen.black_view)
2537 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2538 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002539
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002540 if (shsurf->saved_position_valid)
2541 weston_view_set_position(shsurf->view,
2542 shsurf->saved_x, shsurf->saved_y);
2543 else
2544 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2545
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002546 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002548 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002549 shsurf->saved_rotation_valid = false;
2550 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002551
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002552 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002553}
2554
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002555static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002556shell_surface_set_fullscreen(struct wl_client *client,
2557 struct wl_resource *resource,
2558 uint32_t method,
2559 uint32_t framerate,
2560 struct wl_resource *output_resource)
2561{
2562 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2563 struct weston_output *output;
2564
Emmanuel Gil Peyrot28834b62016-05-10 03:22:43 +02002565 if (shsurf->fullscreen_output == shsurf->output &&
2566 shsurf->fullscreen.type == method &&
2567 shsurf->fullscreen.framerate == framerate) {
2568 send_configure_for_surface(shsurf);
2569 return;
2570 }
2571
Philip Withnallbecb77e2013-11-25 18:01:30 +00002572 if (output_resource)
2573 output = wl_resource_get_user_data(output_resource);
2574 else
2575 output = NULL;
2576
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002577 shell_surface_set_parent(shsurf, NULL);
2578
Rafael Antognolli03b16592013-12-03 15:35:42 -02002579 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002580 shsurf->next_state.fullscreen = true;
2581 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002582 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002583}
2584
2585static void
2586set_popup(struct shell_surface *shsurf,
2587 struct weston_surface *parent,
2588 struct weston_seat *seat,
2589 uint32_t serial,
2590 int32_t x,
2591 int32_t y)
2592{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002593 assert(parent != NULL);
2594
Philip Withnallbecb77e2013-11-25 18:01:30 +00002595 shsurf->popup.shseat = get_shell_seat(seat);
2596 shsurf->popup.serial = serial;
2597 shsurf->popup.x = x;
2598 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002599
Pekka Paalanenb5026542014-11-12 15:09:24 +02002600 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002601}
2602
2603static void
2604shell_surface_set_popup(struct wl_client *client,
2605 struct wl_resource *resource,
2606 struct wl_resource *seat_resource,
2607 uint32_t serial,
2608 struct wl_resource *parent_resource,
2609 int32_t x, int32_t y, uint32_t flags)
2610{
2611 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002612 struct weston_surface *parent =
2613 wl_resource_get_user_data(parent_resource);
2614
2615 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002616
Rafael Antognolli03b16592013-12-03 15:35:42 -02002617 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002618 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002619 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002620 wl_resource_get_user_data(seat_resource),
2621 serial, x, y);
2622}
2623
2624static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002625unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002626{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002627 /* undo all maximized things here */
2628 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002629
2630 if (shsurf->saved_position_valid)
2631 weston_view_set_position(shsurf->view,
2632 shsurf->saved_x, shsurf->saved_y);
2633 else
2634 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002635
2636 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002637 wl_list_insert(&shsurf->view->geometry.transformation_list,
2638 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002639 shsurf->saved_rotation_valid = false;
2640 }
2641
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002642 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002643}
2644
Philip Withnallbecb77e2013-11-25 18:01:30 +00002645static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002646set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002647{
2648 struct shell_surface *shsurf;
2649 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002650 struct weston_view *view;
2651
2652 view = get_default_view(surface);
2653 if (!view)
2654 return;
2655
2656 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2657
2658 shsurf = get_shell_surface(surface);
2659 current_ws = get_current_workspace(shsurf->shell);
2660
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002661 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002662 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002663
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002664 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002665 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002666
2667 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002668 weston_view_damage_below(view);
2669}
2670
2671static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002672shell_surface_set_maximized(struct wl_client *client,
2673 struct wl_resource *resource,
2674 struct wl_resource *output_resource)
2675{
2676 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2677 struct weston_output *output;
2678
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002679 surface_clear_next_states(shsurf);
2680 shsurf->next_state.maximized = true;
2681 shsurf->state_changed = true;
2682
Pekka Paalanenb5026542014-11-12 15:09:24 +02002683 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002684 shell_surface_set_parent(shsurf, NULL);
2685
Philip Withnallbecb77e2013-11-25 18:01:30 +00002686 if (output_resource)
2687 output = wl_resource_get_user_data(output_resource);
2688 else
2689 output = NULL;
2690
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002691 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002692
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002693 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002694}
2695
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002696/* This is only ever called from set_surface_type(), so there’s no need to
2697 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002698static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002699reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002700{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002701 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002702 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002703 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002704 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002705
Pekka Paalanen98262232011-12-01 10:42:22 +02002706 return 0;
2707}
2708
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002709static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002710set_full_output(struct shell_surface *shsurf)
2711{
2712 shsurf->saved_x = shsurf->view->geometry.x;
2713 shsurf->saved_y = shsurf->view->geometry.y;
2714 shsurf->saved_position_valid = true;
2715
2716 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2717 wl_list_remove(&shsurf->rotation.transform.link);
2718 wl_list_init(&shsurf->rotation.transform.link);
2719 weston_view_geometry_dirty(shsurf->view);
2720 shsurf->saved_rotation_valid = true;
2721 }
2722}
2723
2724static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002725set_surface_type(struct shell_surface *shsurf)
2726{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002727 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002729
Philip Withnallbecb77e2013-11-25 18:01:30 +00002730 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002731
Rafael Antognolli03b16592013-12-03 15:35:42 -02002732 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002733 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002734
2735 switch (shsurf->type) {
2736 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002737 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002738 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002739 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 weston_view_set_position(shsurf->view,
2741 pev->geometry.x + shsurf->transient.x,
2742 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002743 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002744 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002745
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002746 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 weston_view_set_position(shsurf->view, shsurf->transient.x,
2748 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002749 break;
2750
Philip Withnall0f640e22013-11-25 18:01:31 +00002751 case SHELL_SURFACE_POPUP:
2752 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002753 default:
2754 break;
2755 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002756
2757 /* Update the surface’s layer. */
2758 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002759}
2760
Tiago Vignattibe143262012-04-16 17:31:41 +03002761static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002762shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002763{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002764 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002765}
2766
Pekka Paalanen8274d902014-08-06 19:36:51 +03002767static int
2768black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2769{
2770 struct weston_surface *fs_surface = surface->configure_private;
2771 int n;
2772 int rem;
2773 int ret;
2774
2775 n = snprintf(buf, len, "black background surface for ");
2776 if (n < 0)
2777 return n;
2778
2779 rem = (int)len - n;
2780 if (rem < 0)
2781 rem = 0;
2782
2783 if (fs_surface->get_label)
2784 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2785 else
2786 ret = snprintf(buf + n, rem, "<unknown>");
2787
2788 if (ret < 0)
2789 return n;
2790
2791 return n + ret;
2792}
2793
Alex Wu21858432012-04-01 20:13:08 +08002794static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002795black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002796
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002798create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002799 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002800 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002801{
2802 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002804
2805 surface = weston_surface_create(ec);
2806 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002807 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002808 return NULL;
2809 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 view = weston_view_create(surface);
2811 if (surface == NULL) {
2812 weston_log("no memory\n");
2813 weston_surface_destroy(surface);
2814 return NULL;
2815 }
Alex Wu4539b082012-03-01 12:57:46 +08002816
Alex Wu21858432012-04-01 20:13:08 +08002817 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002818 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002819 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002820 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002821 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002822 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002823 pixman_region32_fini(&surface->input);
2824 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002825
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002826 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002827 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828
2829 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002830}
2831
Philip Withnalled8f1a92013-11-25 18:01:43 +00002832static void
2833shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2834{
2835 struct weston_output *output = shsurf->fullscreen_output;
2836
Rafael Antognolli03b16592013-12-03 15:35:42 -02002837 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002838
2839 if (!shsurf->fullscreen.black_view)
2840 shsurf->fullscreen.black_view =
2841 create_black_surface(shsurf->surface->compositor,
2842 shsurf->surface,
2843 output->x, output->y,
2844 output->width,
2845 output->height);
2846
2847 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002848 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2849 weston_layer_entry_insert(&shsurf->view->layer_link,
2850 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002851 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2852 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002853
2854 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002855}
2856
Alex Wu4539b082012-03-01 12:57:46 +08002857/* Create black surface and append it to the associated fullscreen surface.
2858 * Handle size dismatch and positioning according to the method. */
2859static void
2860shell_configure_fullscreen(struct shell_surface *shsurf)
2861{
2862 struct weston_output *output = shsurf->fullscreen_output;
2863 struct weston_surface *surface = shsurf->surface;
2864 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002865 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002866 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002867
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002868 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2869 restore_output_mode(output);
2870
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002871 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002872 weston_layer_entry_remove(&shsurf->view->layer_link);
2873 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002874
Philip Withnalled8f1a92013-11-25 18:01:43 +00002875 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002876
Jason Ekstranda7af7042013-10-12 22:38:11 -05002877 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002878 &surf_width, &surf_height);
2879
Alex Wu4539b082012-03-01 12:57:46 +08002880 switch (shsurf->fullscreen.type) {
2881 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002882 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002883 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002884 break;
2885 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002886 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002887 if (output->width == surf_width &&
2888 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 weston_view_set_position(shsurf->view,
2890 output->x - surf_x,
2891 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002892 break;
2893 }
2894
Alex Wu4539b082012-03-01 12:57:46 +08002895 matrix = &shsurf->fullscreen.transform.matrix;
2896 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002897
Scott Moreau1bad5db2012-08-18 01:04:05 -06002898 output_aspect = (float) output->width /
2899 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002900 /* XXX: Use surf_width and surf_height here? */
2901 surface_aspect = (float) surface->width /
2902 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002903 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002904 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002905 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002906 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002907 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002908 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002909
Alex Wu4539b082012-03-01 12:57:46 +08002910 weston_matrix_scale(matrix, scale, scale, 1);
2911 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002913 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002914 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2915 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002916 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002917
Alex Wu4539b082012-03-01 12:57:46 +08002918 break;
2919 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002920 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002921 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002922 surf_width * surface->buffer_viewport.buffer.scale,
2923 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002924 shsurf->fullscreen.framerate};
2925
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002926 if (weston_output_mode_switch_to_temporary(output, &mode,
2927 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 weston_view_set_position(shsurf->view,
2929 output->x - surf_x,
2930 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002931 shsurf->fullscreen.black_view->surface->width = output->width;
2932 shsurf->fullscreen.black_view->surface->height = output->height;
2933 weston_view_set_position(shsurf->fullscreen.black_view,
2934 output->x - surf_x,
2935 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002936 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002937 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002938 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002939 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002941 }
Alex Wubd3354b2012-04-17 17:20:49 +08002942 }
Alex Wu4539b082012-03-01 12:57:46 +08002943 break;
2944 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002945 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002946 break;
2947 default:
2948 break;
2949 }
2950}
2951
Alex Wu4539b082012-03-01 12:57:46 +08002952static void
2953shell_map_fullscreen(struct shell_surface *shsurf)
2954{
Alex Wubd3354b2012-04-17 17:20:49 +08002955 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002956}
2957
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002958static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002959set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2960{
2961 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002962 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002963 shsurf->transient.x = x;
2964 shsurf->transient.y = y;
2965 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002966
2967 shell_surface_set_parent(shsurf, NULL);
2968
Pekka Paalanenb5026542014-11-12 15:09:24 +02002969 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002970 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002971}
2972
Kristian Høgsberg47792412014-05-04 13:47:06 -07002973static void
2974shell_interface_set_fullscreen(struct shell_surface *shsurf,
2975 uint32_t method,
2976 uint32_t framerate,
2977 struct weston_output *output)
2978{
2979 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002980 shsurf->next_state.fullscreen = true;
2981 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002982
2983 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002984}
2985
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002986static struct weston_output *
2987get_focused_output(struct weston_compositor *compositor)
2988{
2989 struct weston_seat *seat;
2990 struct weston_output *output = NULL;
2991
2992 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002993 struct weston_touch *touch = weston_seat_get_touch(seat);
2994 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2995 struct weston_keyboard *keyboard =
2996 weston_seat_get_keyboard(seat);
2997
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002998 /* Priority has touch focus, then pointer and
2999 * then keyboard focus. We should probably have
3000 * three for loops and check frist for touch,
3001 * then for pointer, etc. but unless somebody has some
3002 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003003 if (touch && touch->focus)
3004 output = touch->focus->output;
3005 else if (pointer && pointer->focus)
3006 output = pointer->focus->output;
3007 else if (keyboard && keyboard->focus)
3008 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003009
3010 if (output)
3011 break;
3012 }
3013
3014 return output;
3015}
3016
3017static void
3018shell_interface_set_maximized(struct shell_surface *shsurf)
3019{
3020 struct weston_output *output;
3021
3022 surface_clear_next_states(shsurf);
3023 shsurf->next_state.maximized = true;
3024 shsurf->state_changed = true;
3025 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3026
3027 if (!weston_surface_is_mapped(shsurf->surface))
3028 output = get_focused_output(shsurf->surface->compositor);
3029 else
3030 output = shsurf->surface->output;
3031
3032 shell_surface_set_output(shsurf, output);
3033 send_configure_for_surface(shsurf);
3034}
3035
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003036static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003037shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003038{
Derek Foremane4d6c832015-08-05 14:48:11 -05003039 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003040}
3041
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003042static int
3043shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003044 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003045 uint32_t edges)
3046{
Derek Foremane4d6c832015-08-05 14:48:11 -05003047 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003048}
3049
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003050static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003051
3052static void
3053destroy_shell_seat(struct wl_listener *listener, void *data)
3054{
3055 struct shell_seat *shseat =
3056 container_of(listener,
3057 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003058 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003059
3060 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003061 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003062 shseat->popup_grab.client = NULL;
3063
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003064 wl_list_for_each_safe(shsurf, next,
3065 &shseat->popup_grab.surfaces_list,
3066 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003067 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003068 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003070 }
3071
3072 wl_list_remove(&shseat->seat_destroy_listener.link);
3073 free(shseat);
3074}
3075
Jason Ekstrand024177c2014-04-21 19:42:58 -05003076static void
3077shell_seat_caps_changed(struct wl_listener *listener, void *data)
3078{
Derek Foreman1281a362015-07-31 16:55:32 -05003079 struct weston_keyboard *keyboard;
3080 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003081 struct shell_seat *seat;
3082
3083 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003084 keyboard = weston_seat_get_keyboard(seat->seat);
3085 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003086
Derek Foreman1281a362015-07-31 16:55:32 -05003087 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003088 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003089 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003090 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003091 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003092 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003093 wl_list_init(&seat->keyboard_focus_listener.link);
3094 }
3095
Derek Foreman1281a362015-07-31 16:55:32 -05003096 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003097 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003098 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003099 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003100 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003101 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003102 wl_list_init(&seat->pointer_focus_listener.link);
3103 }
3104}
3105
Giulio Camuffo5085a752013-03-25 21:42:45 +01003106static struct shell_seat *
3107create_shell_seat(struct weston_seat *seat)
3108{
3109 struct shell_seat *shseat;
3110
3111 shseat = calloc(1, sizeof *shseat);
3112 if (!shseat) {
3113 weston_log("no memory to allocate shell seat\n");
3114 return NULL;
3115 }
3116
3117 shseat->seat = seat;
3118 wl_list_init(&shseat->popup_grab.surfaces_list);
3119
3120 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3121 wl_signal_add(&seat->destroy_signal,
3122 &shseat->seat_destroy_listener);
3123
Jason Ekstrand024177c2014-04-21 19:42:58 -05003124 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3125 wl_list_init(&shseat->keyboard_focus_listener.link);
3126
3127 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3128 wl_list_init(&shseat->pointer_focus_listener.link);
3129
3130 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3131 wl_signal_add(&seat->updated_caps_signal,
3132 &shseat->caps_changed_listener);
3133 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3134
Giulio Camuffo5085a752013-03-25 21:42:45 +01003135 return shseat;
3136}
3137
3138static struct shell_seat *
3139get_shell_seat(struct weston_seat *seat)
3140{
3141 struct wl_listener *listener;
3142
3143 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003144 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003145
3146 return container_of(listener,
3147 struct shell_seat, seat_destroy_listener);
3148}
3149
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003150static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003151popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003152{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003153 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003155 struct shell_seat *shseat =
3156 container_of(grab, struct shell_seat, popup_grab.grab);
3157 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003158 wl_fixed_t sx, sy;
3159
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 view = weston_compositor_pick_view(pointer->seat->compositor,
3161 pointer->x, pointer->y,
3162 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003163
Jason Ekstranda7af7042013-10-12 22:38:11 -05003164 if (view && view->surface->resource &&
3165 wl_resource_get_client(view->surface->resource) == client) {
3166 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003167 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003168 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003169 }
3170}
3171
3172static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003173popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003174 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003176 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003177 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003178 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003179 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003180 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181
Jonas Ådahl61917c82014-08-11 22:44:02 +02003182 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003183 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003184 weston_view_from_global_fixed(pointer->focus, x, y,
3185 &pointer->sx, &pointer->sy);
3186 }
3187
Jonas Ådahld2510102014-10-05 21:39:14 +02003188 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003189
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003190 if (!pointer->focus_client)
3191 return;
3192
3193 resource_list = &pointer->focus_client->pointer_resources;
3194 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003195 weston_view_from_global_fixed(pointer->focus,
3196 pointer->x, pointer->y,
3197 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003198 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003199 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003200}
3201
3202static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003203popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003204 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003205{
3206 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003207 struct shell_seat *shseat =
3208 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003209 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003210 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003211 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003212 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003213
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003214 if (grab->pointer->focus_client)
3215 resource_list = &grab->pointer->focus_client->pointer_resources;
3216 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003217 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003218 wl_resource_for_each(resource, resource_list) {
3219 wl_pointer_send_button(resource, serial,
3220 time, button, state);
3221 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003222 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003223 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003224 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003225 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003226 }
3227
Daniel Stone4dbadb12012-05-30 16:31:51 +01003228 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003229 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003230}
3231
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003232static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003233popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003234 uint32_t time,
3235 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003236{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003237 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003238}
3239
3240static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003241popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3242{
3243 weston_pointer_send_axis_source(grab->pointer, source);
3244}
3245
3246static void
3247popup_grab_frame(struct weston_pointer_grab *grab)
3248{
3249 weston_pointer_send_frame(grab->pointer);
3250}
3251
3252static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003253popup_grab_cancel(struct weston_pointer_grab *grab)
3254{
3255 popup_grab_end(grab->pointer);
3256}
3257
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003258static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003259 popup_grab_focus,
3260 popup_grab_motion,
3261 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003262 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003263 popup_grab_axis_source,
3264 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003265 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003266};
3267
3268static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003269touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003270 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003271{
3272 struct wl_resource *resource;
3273 struct shell_seat *shseat =
3274 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3275 struct wl_display *display = shseat->seat->compositor->wl_display;
3276 uint32_t serial;
3277 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003278 wl_fixed_t sx, sy;
3279
3280 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003281
3282 resource_list = &grab->touch->focus_resource_list;
3283 if (!wl_list_empty(resource_list)) {
3284 serial = wl_display_get_serial(display);
3285 wl_resource_for_each(resource, resource_list) {
3286 wl_touch_send_down(resource, serial, time,
3287 grab->touch->focus->surface->resource,
3288 touch_id, sx, sy);
3289 }
3290 }
3291}
3292
3293static void
3294touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3295{
3296 struct wl_resource *resource;
3297 struct shell_seat *shseat =
3298 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3299 struct wl_display *display = shseat->seat->compositor->wl_display;
3300 uint32_t serial;
3301 struct wl_list *resource_list;
3302
3303 resource_list = &grab->touch->focus_resource_list;
3304 if (!wl_list_empty(resource_list)) {
3305 serial = wl_display_get_serial(display);
3306 wl_resource_for_each(resource, resource_list) {
3307 wl_touch_send_up(resource, serial, time, touch_id);
3308 }
3309 }
3310}
3311
3312static void
3313touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003314 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003315{
3316 struct wl_resource *resource;
3317 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003318 wl_fixed_t sx, sy;
3319
3320 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003321
3322 resource_list = &grab->touch->focus_resource_list;
3323 if (!wl_list_empty(resource_list)) {
3324 wl_resource_for_each(resource, resource_list) {
3325 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3326 }
3327 }
3328}
3329
3330static void
3331touch_popup_grab_frame(struct weston_touch_grab *grab)
3332{
3333}
3334
3335static void
3336touch_popup_grab_cancel(struct weston_touch_grab *grab)
3337{
3338 touch_popup_grab_end(grab->touch);
3339}
3340
3341static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3342 touch_popup_grab_down,
3343 touch_popup_grab_up,
3344 touch_popup_grab_motion,
3345 touch_popup_grab_frame,
3346 touch_popup_grab_cancel,
3347};
3348
3349static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003350shell_surface_send_popup_done(struct shell_surface *shsurf)
3351{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003352 if (shsurf->resource == NULL)
3353 return;
3354 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003355 wl_shell_surface_send_popup_done(shsurf->resource);
3356 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003357 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003358}
3359
3360static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003361popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003362{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003363 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003364 struct shell_seat *shseat =
3365 container_of(grab, struct shell_seat, popup_grab.grab);
3366 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003367 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003368
3369 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003370 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003371 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003372 shseat->popup_grab.grab.interface = NULL;
3373 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003374 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003375 wl_list_for_each_safe(shsurf, next,
3376 &shseat->popup_grab.surfaces_list,
3377 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003378 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003379 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003380 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003381 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003382 wl_list_init(&shseat->popup_grab.surfaces_list);
3383 }
3384}
3385
3386static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003387touch_popup_grab_end(struct weston_touch *touch)
3388{
3389 struct weston_touch_grab *grab = touch->grab;
3390 struct shell_seat *shseat =
3391 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3392 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003393 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003394
3395 if (touch->grab->interface == &touch_popup_grab_interface) {
3396 weston_touch_end_grab(grab->touch);
3397 shseat->popup_grab.client = NULL;
3398 shseat->popup_grab.touch_grab.interface = NULL;
3399 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3400 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003401 wl_list_for_each_safe(shsurf, next,
3402 &shseat->popup_grab.surfaces_list,
3403 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003404 shell_surface_send_popup_done(shsurf);
3405 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003406 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003407 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003408 wl_list_init(&shseat->popup_grab.surfaces_list);
3409 }
3410}
3411
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003412static struct shell_surface *
3413get_top_popup(struct shell_seat *shseat)
3414{
3415 struct shell_surface *shsurf;
3416
3417 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3418 return NULL;
3419 } else {
3420 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3421 struct shell_surface,
3422 popup.grab_link);
3423 return shsurf;
3424 }
3425}
3426
3427static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003428add_popup_grab(struct shell_surface *shsurf,
3429 struct shell_seat *shseat,
3430 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003431{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003432 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003433 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003434 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3435 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003436
3437 parent = get_shell_surface(shsurf->parent);
3438 top_surface = get_top_popup(shseat);
3439 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003440 (!parent ||
3441 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003442 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003443 wl_resource_post_error(shsurf->owner_resource,
3444 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003445 "xdg_popup was not created on the "
3446 "topmost popup");
3447 return -1;
3448 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003449
3450 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003451 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003452 shseat->popup_grab.client =
3453 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003454
3455 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003456 shseat->popup_grab.grab.interface =
3457 &popup_grab_interface;
3458
3459 /* We must make sure here that this popup was opened
3460 * after a mouse press, and not just by moving around
3461 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003462 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003463 shseat->popup_grab.initial_up = 0;
3464 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003465 shseat->popup_grab.touch_grab.interface =
3466 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003467 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003468
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003469 wl_list_insert(&shseat->popup_grab.surfaces_list,
3470 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003471
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003472 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003473 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003474 &shseat->popup_grab.grab);
3475 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003476 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003477 &shseat->popup_grab.touch_grab);
3478 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003479 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003480 wl_list_insert(&shseat->popup_grab.surfaces_list,
3481 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003482 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003483
3484 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003485}
3486
3487static void
3488remove_popup_grab(struct shell_surface *shsurf)
3489{
3490 struct shell_seat *shseat = shsurf->popup.shseat;
3491
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003492 if (shell_surface_is_xdg_popup(shsurf) &&
3493 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003494 wl_resource_post_error(shsurf->owner_resource,
3495 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003496 "xdg_popup was destroyed while it was "
3497 "not the topmost popup.");
3498 return;
3499 }
3500
Giulio Camuffo5085a752013-03-25 21:42:45 +01003501 wl_list_remove(&shsurf->popup.grab_link);
3502 wl_list_init(&shsurf->popup.grab_link);
3503 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003504 if (shseat->popup_grab.type == POINTER) {
3505 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3506 shseat->popup_grab.grab.interface = NULL;
3507 } else if (shseat->popup_grab.type == TOUCH) {
3508 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3509 shseat->popup_grab.touch_grab.interface = NULL;
3510 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003511 }
3512}
3513
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003514static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003515shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003516{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003517 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003518 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003519 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3520 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003521
Jason Ekstranda7af7042013-10-12 22:38:11 -05003522 shsurf->surface->output = parent_view->output;
3523 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003524
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 weston_view_set_transform_parent(shsurf->view, parent_view);
3526 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3527 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003528
Derek Foreman1281a362015-07-31 16:55:32 -05003529 if (pointer &&
3530 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003531 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3532 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003533 } else if (touch &&
3534 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003535 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3536 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003537 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003538 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003539 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003540 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003541
3542 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003543}
3544
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003545static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003546 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003547 shell_surface_move,
3548 shell_surface_resize,
3549 shell_surface_set_toplevel,
3550 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003551 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003552 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003553 shell_surface_set_maximized,
3554 shell_surface_set_title,
3555 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003556};
3557
3558static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003559destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003560{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003561 struct shell_surface *child, *next;
3562
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003563 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3564
Giulio Camuffo5085a752013-03-25 21:42:45 +01003565 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3566 remove_popup_grab(shsurf);
3567 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003568
Alex Wubd3354b2012-04-17 17:20:49 +08003569 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003570 shell_surface_is_top_fullscreen(shsurf))
3571 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003572
Jason Ekstranda7af7042013-10-12 22:38:11 -05003573 if (shsurf->fullscreen.black_view)
3574 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003575
Alex Wubd3354b2012-04-17 17:20:49 +08003576 /* As destroy_resource() use wl_list_for_each_safe(),
3577 * we can always remove the listener.
3578 */
3579 wl_list_remove(&shsurf->surface_destroy_listener.link);
3580 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003581 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003582 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003583
Jason Ekstranda7af7042013-10-12 22:38:11 -05003584 weston_view_destroy(shsurf->view);
3585
Philip Withnall648a4dd2013-11-25 18:01:44 +00003586 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003587 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3588 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003589
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003590 wl_list_remove(&shsurf->link);
3591 free(shsurf);
3592}
3593
3594static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003595shell_destroy_shell_surface(struct wl_resource *resource)
3596{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003597 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003598
Bryan Caina46b9462014-04-04 17:41:24 -05003599 if (!wl_list_empty(&shsurf->popup.grab_link))
3600 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003601 if (shsurf->resource)
3602 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003603 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003604}
3605
3606static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003607shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003608{
3609 struct shell_surface *shsurf = container_of(listener,
3610 struct shell_surface,
3611 surface_destroy_listener);
3612
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003613 if (shsurf->resource)
3614 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003615
3616 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003617}
3618
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003619static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003620fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003621{
3622 struct shell_surface *shsurf = data;
3623
3624 weston_surface_destroy(shsurf->surface);
3625}
3626
3627static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003628fade_out_done(struct weston_view_animation *animation, void *data)
3629{
3630 struct shell_surface *shsurf = data;
3631 struct wl_event_loop *loop;
3632
3633 loop = wl_display_get_event_loop(
3634 shsurf->surface->compositor->wl_display);
3635
3636 if (!shsurf->destroying) {
3637 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3638 shsurf->destroying = true;
3639 }
3640}
3641
3642static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003643handle_resource_destroy(struct wl_listener *listener, void *data)
3644{
3645 struct shell_surface *shsurf =
3646 container_of(listener, struct shell_surface,
3647 resource_destroy_listener);
3648
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003649 if (!weston_surface_is_mapped(shsurf->surface))
3650 return;
3651
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003652 shsurf->surface->ref_count++;
3653
3654 pixman_region32_fini(&shsurf->surface->pending.input);
3655 pixman_region32_init(&shsurf->surface->pending.input);
3656 pixman_region32_fini(&shsurf->surface->input);
3657 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003658 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3659 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3660 fade_out_done, shsurf);
3661 } else {
3662 weston_surface_destroy(shsurf->surface);
3663 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003664}
3665
3666static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003667shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003668
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003669struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003670get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003671{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003672 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003673 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003674 else
3675 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003676}
3677
Rafael Antognollie2a34552013-12-03 15:35:45 -02003678static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003679create_common_surface(struct shell_client *owner, void *shell,
3680 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003681 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003682{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003683 struct shell_surface *shsurf;
3684
Pekka Paalanen50b67472014-10-01 15:02:41 +03003685 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003686
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003687 shsurf = calloc(1, sizeof *shsurf);
3688 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003689 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003690 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003691 }
3692
Jason Ekstranda7af7042013-10-12 22:38:11 -05003693 shsurf->view = weston_view_create(surface);
3694 if (!shsurf->view) {
3695 weston_log("no memory to allocate shell surface\n");
3696 free(shsurf);
3697 return NULL;
3698 }
3699
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003700 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003701 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003702 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003703
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003704 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3705 wl_resource_add_destroy_listener(surface->resource,
3706 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003707 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003708
Tiago Vignattibc052c92012-04-19 16:18:18 +03003709 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003710 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003711 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003712 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003713 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003714 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3715 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003716 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003717 wl_list_init(&shsurf->fullscreen.transform.link);
3718
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003719 shsurf->output = get_default_output(shsurf->shell->compositor);
3720
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003721 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003722 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003723 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003724 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003725
3726 /* init link so its safe to always remove it in destroy_shell_surface */
3727 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003728 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003729
Pekka Paalanen460099f2012-01-20 16:48:25 +02003730 /* empty when not in use */
3731 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003732 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003733
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003734 wl_list_init(&shsurf->workspace_transform.link);
3735
Philip Withnall648a4dd2013-11-25 18:01:44 +00003736 wl_list_init(&shsurf->children_link);
3737 wl_list_init(&shsurf->children_list);
3738 shsurf->parent = NULL;
3739
Pekka Paalanenb5026542014-11-12 15:09:24 +02003740 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003741
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003742 shsurf->client = client;
3743
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003744 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003745}
3746
Rafael Antognollie2a34552013-12-03 15:35:45 -02003747static struct shell_surface *
3748create_shell_surface(void *shell, struct weston_surface *surface,
3749 const struct weston_shell_client *client)
3750{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003751 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003752}
3753
Tiago Vignattibc052c92012-04-19 16:18:18 +03003754static void
3755shell_get_shell_surface(struct wl_client *client,
3756 struct wl_resource *resource,
3757 uint32_t id,
3758 struct wl_resource *surface_resource)
3759{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003760 struct weston_surface *surface =
3761 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003762 struct shell_client *sc = wl_resource_get_user_data(resource);
3763 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003764 struct shell_surface *shsurf;
3765
Pekka Paalanen50b67472014-10-01 15:02:41 +03003766 if (weston_surface_set_role(surface, "wl_shell_surface",
3767 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003768 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003769
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003770 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003771 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003772 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003773 return;
3774 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003775
Jason Ekstranda85118c2013-06-27 20:17:02 -05003776 shsurf->resource =
3777 wl_resource_create(client,
3778 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003779 if (!shsurf->resource) {
3780 wl_resource_post_no_memory(surface_resource);
3781 return;
3782 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003783 wl_resource_set_implementation(shsurf->resource,
3784 &shell_surface_implementation,
3785 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003786 wl_list_insert(&sc->surface_list,
3787 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003788}
3789
Rafael Antognollie2a34552013-12-03 15:35:45 -02003790static bool
3791shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3792{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003793 /* A shell surface without a resource is created from xwayland
3794 * and is considered a wl_shell surface for now. */
3795
3796 return shsurf->resource == NULL ||
3797 wl_resource_instance_of(shsurf->resource,
3798 &wl_shell_surface_interface,
3799 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003800}
3801
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003802static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003803 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003804};
3805
Rafael Antognollie2a34552013-12-03 15:35:45 -02003806/****************************
3807 * xdg-shell implementation */
3808
3809static void
3810xdg_surface_destroy(struct wl_client *client,
3811 struct wl_resource *resource)
3812{
3813 wl_resource_destroy(resource);
3814}
3815
3816static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003817xdg_surface_set_parent(struct wl_client *client,
3818 struct wl_resource *resource,
3819 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003820{
3821 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003822 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003823
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003824 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003825 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003826 shell_surface_set_parent(shsurf, parent->surface);
3827 } else {
3828 shell_surface_set_parent(shsurf, NULL);
3829 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003830}
3831
3832static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003833xdg_surface_set_app_id(struct wl_client *client,
3834 struct wl_resource *resource,
3835 const char *app_id)
3836{
3837 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3838
3839 free(shsurf->class);
3840 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003841 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003842}
3843
3844static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003845xdg_surface_show_window_menu(struct wl_client *client,
3846 struct wl_resource *surface_resource,
3847 struct wl_resource *seat_resource,
3848 uint32_t serial,
3849 int32_t x,
3850 int32_t y)
3851{
3852 /* TODO */
3853}
3854
3855static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003856xdg_surface_set_title(struct wl_client *client,
3857 struct wl_resource *resource, const char *title)
3858{
3859 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3860
3861 set_title(shsurf, title);
3862}
3863
3864static void
3865xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3866 struct wl_resource *seat_resource, uint32_t serial)
3867{
3868 common_surface_move(resource, seat_resource, serial);
3869}
3870
3871static void
3872xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3873 struct wl_resource *seat_resource, uint32_t serial,
3874 uint32_t edges)
3875{
3876 common_surface_resize(resource, seat_resource, serial, edges);
3877}
3878
3879static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003880xdg_surface_ack_configure(struct wl_client *client,
3881 struct wl_resource *resource,
3882 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003883{
3884 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3885
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003886 if (shsurf->state_requested) {
3887 shsurf->next_state = shsurf->requested_state;
3888 shsurf->state_changed = true;
3889 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003890 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003891}
3892
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003893static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003894xdg_surface_set_window_geometry(struct wl_client *client,
3895 struct wl_resource *resource,
3896 int32_t x,
3897 int32_t y,
3898 int32_t width,
3899 int32_t height)
3900{
3901 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3902
3903 set_window_geometry(shsurf, x, y, width, height);
3904}
3905
3906static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003907xdg_surface_set_maximized(struct wl_client *client,
3908 struct wl_resource *resource)
3909{
3910 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003911 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003912
3913 shsurf->state_requested = true;
3914 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003915
3916 if (!weston_surface_is_mapped(shsurf->surface))
3917 output = get_focused_output(shsurf->surface->compositor);
3918 else
3919 output = shsurf->surface->output;
3920
3921 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003922 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003923}
3924
3925static void
3926xdg_surface_unset_maximized(struct wl_client *client,
3927 struct wl_resource *resource)
3928{
3929 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3930
3931 shsurf->state_requested = true;
3932 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003933 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003934}
3935
3936static void
3937xdg_surface_set_fullscreen(struct wl_client *client,
3938 struct wl_resource *resource,
3939 struct wl_resource *output_resource)
3940{
3941 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3942 struct weston_output *output;
3943
3944 shsurf->state_requested = true;
3945 shsurf->requested_state.fullscreen = true;
3946
3947 if (output_resource)
3948 output = wl_resource_get_user_data(output_resource);
3949 else
3950 output = NULL;
3951
Marek Chalupa052917a2014-09-02 11:35:12 +02003952 /* handle clients launching in fullscreen */
3953 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3954 /* Set the output to the one that has focus currently. */
3955 assert(shsurf->surface);
3956 output = get_focused_output(shsurf->surface->compositor);
3957 }
3958
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003959 shell_surface_set_output(shsurf, output);
3960 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003961
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003962 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003963}
3964
3965static void
3966xdg_surface_unset_fullscreen(struct wl_client *client,
3967 struct wl_resource *resource)
3968{
3969 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3970
3971 shsurf->state_requested = true;
3972 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003973 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003974}
3975
3976static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003977xdg_surface_set_minimized(struct wl_client *client,
3978 struct wl_resource *resource)
3979{
3980 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3981
3982 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3983 return;
3984
3985 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003986 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003987}
3988
Rafael Antognollie2a34552013-12-03 15:35:45 -02003989static const struct xdg_surface_interface xdg_surface_implementation = {
3990 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003991 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003992 xdg_surface_set_title,
3993 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003994 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003995 xdg_surface_move,
3996 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003997 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003998 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003999 xdg_surface_set_maximized,
4000 xdg_surface_unset_maximized,
4001 xdg_surface_set_fullscreen,
4002 xdg_surface_unset_fullscreen,
4003 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004004};
4005
4006static void
4007xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004008 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004009{
4010 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004011 uint32_t *s;
4012 struct wl_array states;
4013 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004014
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004015 assert(shsurf);
4016
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004017 if (!shsurf->resource)
4018 return;
4019
4020 wl_array_init(&states);
4021 if (shsurf->requested_state.fullscreen) {
4022 s = wl_array_add(&states, sizeof *s);
4023 *s = XDG_SURFACE_STATE_FULLSCREEN;
4024 } else if (shsurf->requested_state.maximized) {
4025 s = wl_array_add(&states, sizeof *s);
4026 *s = XDG_SURFACE_STATE_MAXIMIZED;
4027 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004028 if (shsurf->resize_edges != 0) {
4029 s = wl_array_add(&states, sizeof *s);
4030 *s = XDG_SURFACE_STATE_RESIZING;
4031 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004032 if (shsurf->focus_count > 0) {
4033 s = wl_array_add(&states, sizeof *s);
4034 *s = XDG_SURFACE_STATE_ACTIVATED;
4035 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004036
4037 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4038 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4039
4040 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004041}
4042
4043static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004044 xdg_send_configure,
4045 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004046};
4047
4048static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004049xdg_shell_destroy(struct wl_client *client,
4050 struct wl_resource *resource)
4051{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004052 struct shell_client *sc = wl_resource_get_user_data(resource);
4053 struct wl_resource *shsurf_resource;
4054 struct shell_surface *shsurf;
4055
4056 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4057 shsurf = wl_resource_get_user_data(shsurf_resource);
4058 if (shsurf->owner_resource == resource) {
4059 wl_resource_post_error(
4060 resource,
4061 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4062 "not all child surface objects destroyed");
4063 return;
4064 }
4065 }
4066
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004067 wl_resource_destroy(resource);
4068}
4069
4070static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004071xdg_use_unstable_version(struct wl_client *client,
4072 struct wl_resource *resource,
4073 int32_t version)
4074{
4075 if (version > 1) {
4076 wl_resource_post_error(resource,
4077 1,
4078 "xdg-shell:: version not implemented yet.");
4079 return;
4080 }
4081}
4082
4083static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004084create_xdg_surface(struct shell_client *owner, void *shell,
4085 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004086 const struct weston_shell_client *client)
4087{
4088 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004089
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004090 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004091 if (!shsurf)
4092 return NULL;
4093
Pekka Paalanenb5026542014-11-12 15:09:24 +02004094 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004095
4096 return shsurf;
4097}
4098
4099static void
4100xdg_get_xdg_surface(struct wl_client *client,
4101 struct wl_resource *resource,
4102 uint32_t id,
4103 struct wl_resource *surface_resource)
4104{
4105 struct weston_surface *surface =
4106 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004107 struct shell_client *sc = wl_resource_get_user_data(resource);
4108 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004109 struct shell_surface *shsurf;
4110
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004111 shsurf = get_shell_surface(surface);
4112 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4113 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4114 "This wl_surface is already an "
4115 "xdg_surface");
4116 return;
4117 }
4118
Pekka Paalanen50b67472014-10-01 15:02:41 +03004119 if (weston_surface_set_role(surface, "xdg_surface",
4120 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004121 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004122
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004123 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004124 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004125 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004126 return;
4127 }
4128
4129 shsurf->resource =
4130 wl_resource_create(client,
4131 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004132 if (!shsurf->resource) {
4133 wl_resource_post_no_memory(surface_resource);
4134 return;
4135 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004136 wl_resource_set_implementation(shsurf->resource,
4137 &xdg_surface_implementation,
4138 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004139 shsurf->owner_resource = resource;
4140 wl_list_insert(&sc->surface_list,
4141 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004142}
4143
4144static bool
4145shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4146{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004147 return shsurf->resource &&
4148 wl_resource_instance_of(shsurf->resource,
4149 &xdg_surface_interface,
4150 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004151}
4152
4153/* xdg-popup implementation */
4154
4155static void
4156xdg_popup_destroy(struct wl_client *client,
4157 struct wl_resource *resource)
4158{
4159 wl_resource_destroy(resource);
4160}
4161
Rafael Antognollie2a34552013-12-03 15:35:45 -02004162static const struct xdg_popup_interface xdg_popup_implementation = {
4163 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004164};
4165
4166static void
4167xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004168 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004169{
4170}
4171
4172static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004173 xdg_popup_send_configure,
4174 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004175};
4176
4177static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004178create_xdg_popup(struct shell_client *owner, void *shell,
4179 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004180 const struct weston_shell_client *client,
4181 struct weston_surface *parent,
4182 struct shell_seat *seat,
4183 uint32_t serial,
4184 int32_t x, int32_t y)
4185{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004186 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004187
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004188 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004189 if (!shsurf)
4190 return NULL;
4191
Pekka Paalanenb5026542014-11-12 15:09:24 +02004192 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004193 shsurf->popup.shseat = seat;
4194 shsurf->popup.serial = serial;
4195 shsurf->popup.x = x;
4196 shsurf->popup.y = y;
4197 shell_surface_set_parent(shsurf, parent);
4198
4199 return shsurf;
4200}
4201
4202static void
4203xdg_get_xdg_popup(struct wl_client *client,
4204 struct wl_resource *resource,
4205 uint32_t id,
4206 struct wl_resource *surface_resource,
4207 struct wl_resource *parent_resource,
4208 struct wl_resource *seat_resource,
4209 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004210 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004211{
4212 struct weston_surface *surface =
4213 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004214 struct shell_client *sc = wl_resource_get_user_data(resource);
4215 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004216 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004217 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004218 struct weston_surface *parent;
4219 struct shell_seat *seat;
4220
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004221 shsurf = get_shell_surface(surface);
4222 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4223 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4224 "This wl_surface is already an "
4225 "xdg_popup");
4226 return;
4227 }
4228
Pekka Paalanen50b67472014-10-01 15:02:41 +03004229 if (weston_surface_set_role(surface, "xdg_popup",
4230 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004231 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004232
4233 if (!parent_resource) {
4234 wl_resource_post_error(surface_resource,
4235 WL_DISPLAY_ERROR_INVALID_OBJECT,
4236 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004237 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004238 }
4239
4240 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004241 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004242
Jonas Ådahlee45a552015-03-18 16:37:47 +08004243 /* Verify that we are creating the top most popup when mapping,
4244 * as it's not until then we know whether it was mapped as most
4245 * top level or not. */
4246
4247 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004248 if (!parent_shsurf ||
4249 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4250 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004251 wl_resource_post_error(resource,
4252 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4253 "xdg_popup parent was invalid");
4254 return;
4255 }
4256
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004257 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004258 parent, seat, serial, x, y);
4259 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004260 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004261 return;
4262 }
4263
4264 shsurf->resource =
4265 wl_resource_create(client,
4266 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004267 if (!shsurf->resource) {
4268 wl_resource_post_no_memory(surface_resource);
4269 return;
4270 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004271 wl_resource_set_implementation(shsurf->resource,
4272 &xdg_popup_implementation,
4273 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004274 shsurf->owner_resource = resource;
4275 wl_list_insert(&sc->surface_list,
4276 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004277}
4278
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004279static void
4280xdg_pong(struct wl_client *client,
4281 struct wl_resource *resource, uint32_t serial)
4282{
4283 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004284
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004285 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004286}
4287
Rafael Antognollie2a34552013-12-03 15:35:45 -02004288static bool
4289shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4290{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004291 return (shsurf->resource &&
4292 wl_resource_instance_of(shsurf->resource,
4293 &xdg_popup_interface,
4294 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004295}
4296
4297static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004298 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004299 xdg_use_unstable_version,
4300 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004301 xdg_get_xdg_popup,
4302 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004303};
4304
4305static int
4306xdg_shell_unversioned_dispatch(const void *implementation,
4307 void *_target, uint32_t opcode,
4308 const struct wl_message *message,
4309 union wl_argument *args)
4310{
4311 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004312 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004313
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004314 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004315 wl_resource_post_error(resource,
4316 WL_DISPLAY_ERROR_INVALID_OBJECT,
4317 "must call use_unstable_version first");
4318 return 0;
4319 }
4320
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004321#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004322
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004323 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4324 "shell implementation doesn't match protocol version");
4325
Rafael Antognollie2a34552013-12-03 15:35:45 -02004326 if (args[0].i != XDG_SERVER_VERSION) {
4327 wl_resource_post_error(resource,
4328 WL_DISPLAY_ERROR_INVALID_OBJECT,
4329 "incompatible version, server is %d "
4330 "client wants %d",
4331 XDG_SERVER_VERSION, args[0].i);
4332 return 0;
4333 }
4334
4335 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004336 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004337
4338 return 1;
4339}
4340
4341/* end of xdg-shell implementation */
4342/***********************************/
4343
Kristian Høgsberg07937562011-04-12 17:25:42 -04004344static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004345shell_fade(struct desktop_shell *shell, enum fade_type type);
4346
Pekka Paalanen77346a62011-11-30 16:26:35 +02004347static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004348configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004349{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004351
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004352 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 if (v->output == ev->output && v != ev) {
4354 weston_view_unmap(v);
4355 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004356 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004357 }
4358 }
4359
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004360 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004361
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004362 if (wl_list_empty(&ev->layer_link.link)) {
4363 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004364 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004365 }
4366}
4367
David Fort50d962f2016-06-09 17:55:52 +02004368
4369static struct shell_output *
4370find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4371{
4372 struct shell_output *shell_output;
4373
4374 wl_list_for_each(shell_output, &shell->output_list, link) {
4375 if (shell_output->output == output)
4376 return shell_output;
4377 }
4378
4379 return NULL;
4380}
4381
Pekka Paalanen8274d902014-08-06 19:36:51 +03004382static int
4383background_get_label(struct weston_surface *surface, char *buf, size_t len)
4384{
4385 return snprintf(buf, len, "background for output %s",
4386 surface->output->name);
4387}
4388
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004389static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004390background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004391{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004392 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004394
Jason Ekstranda7af7042013-10-12 22:38:11 -05004395 view = container_of(es->views.next, struct weston_view, surface_link);
4396
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004397 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004398}
4399
4400static void
David Fort50d962f2016-06-09 17:55:52 +02004401handle_background_surface_destroy(struct wl_listener *listener, void *data)
4402{
4403 struct shell_output *output =
4404 container_of(listener, struct shell_output, background_surface_listener);
4405
4406 weston_log("background surface gone\n");
4407 output->background_surface = NULL;
4408}
4409
4410static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004411desktop_shell_set_background(struct wl_client *client,
4412 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004413 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004414 struct wl_resource *surface_resource)
4415{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004416 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004417 struct weston_surface *surface =
4418 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02004419 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004421
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004422 if (surface->configure) {
4423 wl_resource_post_error(surface_resource,
4424 WL_DISPLAY_ERROR_INVALID_OBJECT,
4425 "surface role already assigned");
4426 return;
4427 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004428
Jason Ekstranda7af7042013-10-12 22:38:11 -05004429 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4430 weston_view_destroy(view);
4431 view = weston_view_create(surface);
4432
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004433 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004434 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004435 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004436 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004437 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004438 weston_desktop_shell_send_configure(resource, 0,
4439 surface_resource,
4440 surface->output->width,
4441 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004442
4443 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4444 sh_output->background_surface = surface;
4445
4446 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4447 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004448}
4449
Pekka Paalanen8274d902014-08-06 19:36:51 +03004450static int
4451panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4452{
4453 return snprintf(buf, len, "panel for output %s",
4454 surface->output->name);
4455}
4456
Kristian Høgsberg75840622011-09-06 13:48:16 -04004457static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004458panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004459{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004460 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004461 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004462
Jason Ekstranda7af7042013-10-12 22:38:11 -05004463 view = container_of(es->views.next, struct weston_view, surface_link);
4464
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004465 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004466}
4467
4468static void
David Fort50d962f2016-06-09 17:55:52 +02004469handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4470{
4471 struct shell_output *output =
4472 container_of(listener, struct shell_output, panel_surface_listener);
4473
4474 weston_log("panel surface gone\n");
4475 output->panel_surface = NULL;
4476}
4477
4478
4479static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004480desktop_shell_set_panel(struct wl_client *client,
4481 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004482 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004483 struct wl_resource *surface_resource)
4484{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004485 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004486 struct weston_surface *surface =
4487 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004488 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02004489 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004490
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004491 if (surface->configure) {
4492 wl_resource_post_error(surface_resource,
4493 WL_DISPLAY_ERROR_INVALID_OBJECT,
4494 "surface role already assigned");
4495 return;
4496 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004497
Jason Ekstranda7af7042013-10-12 22:38:11 -05004498 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4499 weston_view_destroy(view);
4500 view = weston_view_create(surface);
4501
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004502 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004503 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004504 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004505 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004506 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004507 weston_desktop_shell_send_configure(resource, 0,
4508 surface_resource,
4509 surface->output->width,
4510 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004511
4512 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4513 sh_output->panel_surface = surface;
4514
4515 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4516 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004517}
4518
Pekka Paalanen8274d902014-08-06 19:36:51 +03004519static int
4520lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4521{
4522 return snprintf(buf, len, "lock window");
4523}
4524
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004525static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004526lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004527{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004528 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004529 struct weston_view *view;
4530
4531 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004532
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004533 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004534 return;
4535
Jason Ekstranda7af7042013-10-12 22:38:11 -05004536 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004537
4538 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004539 weston_layer_entry_insert(&shell->lock_layer.view_list,
4540 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004541 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004542 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004543 }
4544}
4545
4546static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004547handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004548{
Tiago Vignattibe143262012-04-16 17:31:41 +03004549 struct desktop_shell *shell =
4550 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004551
Martin Minarik6d118362012-06-07 18:01:59 +02004552 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004553 shell->lock_surface = NULL;
4554}
4555
4556static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004557desktop_shell_set_lock_surface(struct wl_client *client,
4558 struct wl_resource *resource,
4559 struct wl_resource *surface_resource)
4560{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004561 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004562 struct weston_surface *surface =
4563 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004564
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004565 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004566
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004567 if (!shell->locked)
4568 return;
4569
Pekka Paalanen98262232011-12-01 10:42:22 +02004570 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004571
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004572 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004573 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004574 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004575
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004576 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004577 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004578 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004579 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004580}
4581
4582static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004583resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004584{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004585 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004586
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004587 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004588 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004589 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004590 &shell->input_panel_layer.link);
4591 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004592 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004593 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004594 wl_list_insert(&shell->compositor->cursor_layer.link,
4595 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004596 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004597 wl_list_insert(&shell->fullscreen_layer.link,
4598 &shell->panel_layer.link);
4599 wl_list_insert(&shell->panel_layer.link,
4600 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004601
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004602 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004603
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004604 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004605 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004606 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004607}
4608
4609static void
4610desktop_shell_unlock(struct wl_client *client,
4611 struct wl_resource *resource)
4612{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004613 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004614
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004615 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004616
4617 if (shell->locked)
4618 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004619}
4620
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004621static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004622desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004623 struct wl_resource *resource,
4624 struct wl_resource *surface_resource)
4625{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004626 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004627
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004628 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004629 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004630}
4631
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004632static void
4633desktop_shell_desktop_ready(struct wl_client *client,
4634 struct wl_resource *resource)
4635{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004636 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004637
4638 shell_fade_startup(shell);
4639}
4640
Jonny Lamb765760d2014-08-20 15:53:19 +02004641static void
4642desktop_shell_set_panel_position(struct wl_client *client,
4643 struct wl_resource *resource,
4644 uint32_t position)
4645{
4646 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4647
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004648 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4649 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4650 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4651 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004652 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004653 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004654 "bad position argument");
4655 return;
4656 }
4657
4658 shell->panel_position = position;
4659}
4660
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004661static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004662 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004663 desktop_shell_set_panel,
4664 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004665 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004666 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004667 desktop_shell_desktop_ready,
4668 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004669};
4670
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004671static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004672get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004673{
4674 struct shell_surface *shsurf;
4675
4676 shsurf = get_shell_surface(surface);
4677 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004678 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004679 return shsurf->type;
4680}
4681
Kristian Høgsberg75840622011-09-06 13:48:16 -04004682static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004683move_binding(struct weston_pointer *pointer, uint32_t time,
4684 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004685{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004686 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004687 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004688 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004689
Derek Foreman8ae2db52015-07-15 13:00:36 -05004690 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004691 return;
4692
Derek Foreman8ae2db52015-07-15 13:00:36 -05004693 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004694
Pekka Paalanen01388e22013-04-25 13:57:44 +03004695 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004696 if (surface == NULL)
4697 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004698
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004699 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004700 if (shsurf == NULL || shsurf->state.fullscreen ||
4701 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004702 return;
4703
Derek Foreman794fa0e2015-07-15 13:00:38 -05004704 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004705}
4706
4707static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004708maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4709 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004710{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004711 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004712 struct weston_surface *surface;
4713 struct shell_surface *shsurf;
4714
4715 surface = weston_surface_get_main_surface(focus);
4716 if (surface == NULL)
4717 return;
4718
4719 shsurf = get_shell_surface(surface);
4720 if (shsurf == NULL)
4721 return;
4722
4723 if (!shell_surface_is_xdg_surface(shsurf))
4724 return;
4725
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004726 shsurf->state_requested = true;
4727 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004728 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004729}
4730
4731static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004732fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4733 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004734{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004735 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004736 struct weston_surface *surface;
4737 struct shell_surface *shsurf;
4738
4739 surface = weston_surface_get_main_surface(focus);
4740 if (surface == NULL)
4741 return;
4742
4743 shsurf = get_shell_surface(surface);
4744 if (shsurf == NULL)
4745 return;
4746
4747 if (!shell_surface_is_xdg_surface(shsurf))
4748 return;
4749
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004750 shsurf->state_requested = true;
4751 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004752 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004753 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004754}
4755
4756static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004757touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004758{
Derek Foreman362656b2014-09-04 10:23:05 -05004759 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004760 struct weston_surface *surface;
4761 struct shell_surface *shsurf;
4762
Derek Foreman8ae2db52015-07-15 13:00:36 -05004763 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004764 return;
4765
Derek Foreman8ae2db52015-07-15 13:00:36 -05004766 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004767 surface = weston_surface_get_main_surface(focus);
4768 if (surface == NULL)
4769 return;
4770
4771 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004772 if (shsurf == NULL || shsurf->state.fullscreen ||
4773 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004774 return;
4775
Derek Foremanb7674ae2015-07-15 13:00:37 -05004776 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004777}
4778
4779static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004780resize_binding(struct weston_pointer *pointer, uint32_t time,
4781 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004782{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004783 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004784 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004785 uint32_t edges = 0;
4786 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004787 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004788
Derek Foreman8ae2db52015-07-15 13:00:36 -05004789 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004790 return;
4791
Derek Foreman8ae2db52015-07-15 13:00:36 -05004792 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004793
Pekka Paalanen01388e22013-04-25 13:57:44 +03004794 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004795 if (surface == NULL)
4796 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004797
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004798 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004799 if (shsurf == NULL || shsurf->state.fullscreen ||
4800 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004801 return;
4802
Jason Ekstranda7af7042013-10-12 22:38:11 -05004803 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004804 wl_fixed_to_int(pointer->grab_x),
4805 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004806 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004807
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004808 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004809 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004810 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004811 edges |= 0;
4812 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004813 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004814
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004815 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004816 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004817 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004818 edges |= 0;
4819 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004820 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004821
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004822 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004823}
4824
4825static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004826surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004827 struct weston_pointer_axis_event *event,
4828 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004829{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004830 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004831 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004832 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004833 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004834
Pekka Paalanen01388e22013-04-25 13:57:44 +03004835 /* XXX: broken for windows containing sub-surfaces */
4836 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004837 if (surface == NULL)
4838 return;
4839
4840 shsurf = get_shell_surface(surface);
4841 if (!shsurf)
4842 return;
4843
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004844 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004845
Jason Ekstranda7af7042013-10-12 22:38:11 -05004846 if (shsurf->view->alpha > 1.0)
4847 shsurf->view->alpha = 1.0;
4848 if (shsurf->view->alpha < step)
4849 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004850
Jason Ekstranda7af7042013-10-12 22:38:11 -05004851 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004852 weston_surface_damage(surface);
4853}
4854
4855static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004856do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004857 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004858{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004859 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004860 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004861 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004862 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004863
Derek Foreman1281a362015-07-31 16:55:32 -05004864 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004865 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4866 return;
4867 }
4868
Scott Moreauccbf29d2012-02-22 14:21:41 -07004869 wl_list_for_each(output, &compositor->output_list, link) {
4870 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004871 wl_fixed_to_double(pointer->x),
4872 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004873 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004874 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004875 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004876 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004877 increment = -output->zoom.increment;
4878 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004879 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004880 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004881 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004882 else
4883 increment = 0;
4884
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004885 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004886
Scott Moreaue6603982012-06-11 13:07:51 -06004887 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004888 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004889 else if (output->zoom.level > output->zoom.max_level)
4890 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004891
4892 if (!output->zoom.active) {
4893 if (output->zoom.level <= 0.0)
4894 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004895 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004896 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004897
Scott Moreaue6603982012-06-11 13:07:51 -06004898 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004899
Jason Ekstranda7af7042013-10-12 22:38:11 -05004900 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004901 }
4902 }
4903}
4904
Scott Moreauccbf29d2012-02-22 14:21:41 -07004905static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004906zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004907 struct weston_pointer_axis_event *event,
4908 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004909{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004910 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004911}
4912
4913static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004914zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4915 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004916{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004917 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004918}
4919
4920static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004921terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4922 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004923{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004924 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004925
Daniel Stone325fc2d2012-05-30 16:31:58 +01004926 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004927}
4928
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004929static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004930rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004931 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004932{
4933 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004934 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004935 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004936 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004937 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004938
Jonas Ådahld2510102014-10-05 21:39:14 +02004939 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004940
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004941 if (!shsurf)
4942 return;
4943
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004944 cx = 0.5f * shsurf->surface->width;
4945 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004946
Daniel Stone37816df2012-05-16 18:45:18 +01004947 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4948 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004949 r = sqrtf(dx * dx + dy * dy);
4950
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004951 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004952 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004953
4954 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004955 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004956 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004957
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004958 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004959 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004960
4961 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004962 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004963 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004964 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004965 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004966
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004967 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004968 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004969 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004970 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004971 wl_list_init(&shsurf->rotation.transform.link);
4972 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004973 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004974 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004975
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004976 /* We need to adjust the position of the surface
4977 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004978 cposx = shsurf->view->geometry.x + cx;
4979 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004980 dposx = rotate->center.x - cposx;
4981 dposy = rotate->center.y - cposy;
4982 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004983 weston_view_set_position(shsurf->view,
4984 shsurf->view->geometry.x + dposx,
4985 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004986 }
4987
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004988 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004989 * lazily applies the damage due to rotation update.
4990 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004991 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004992}
4993
4994static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004995rotate_grab_button(struct weston_pointer_grab *grab,
4996 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004997{
4998 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004999 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005000 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005001 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01005002 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005003
Daniel Stone4dbadb12012-05-30 16:31:51 +01005004 if (pointer->button_count == 0 &&
5005 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005006 if (shsurf)
5007 weston_matrix_multiply(&shsurf->rotation.rotation,
5008 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005009 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005010 free(rotate);
5011 }
5012}
5013
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005014static void
5015rotate_grab_cancel(struct weston_pointer_grab *grab)
5016{
5017 struct rotate_grab *rotate =
5018 container_of(grab, struct rotate_grab, base.grab);
5019
5020 shell_grab_end(&rotate->base);
5021 free(rotate);
5022}
5023
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005024static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005025 noop_grab_focus,
5026 rotate_grab_motion,
5027 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005028 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10005029 noop_grab_axis_source,
5030 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005031 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005032};
5033
5034static void
Derek Foreman74de4692015-07-15 13:00:39 -05005035surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005036{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005037 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005038 float dx, dy;
5039 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005040
Derek Foreman45a7c272015-09-11 14:27:40 -05005041 surface = find_toplevel_surface(surface);
5042
Pekka Paalanen460099f2012-01-20 16:48:25 +02005043 rotate = malloc(sizeof *rotate);
5044 if (!rotate)
5045 return;
5046
Jason Ekstranda7af7042013-10-12 22:38:11 -05005047 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005048 surface->surface->width * 0.5f,
5049 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005050 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005051
Derek Foreman74de4692015-07-15 13:00:39 -05005052 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5053 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005054 r = sqrtf(dx * dx + dy * dy);
5055 if (r > 20.0f) {
5056 struct weston_matrix inverse;
5057
5058 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005059 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005060 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005061
5062 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005063 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005064 } else {
5065 weston_matrix_init(&surface->rotation.rotation);
5066 weston_matrix_init(&rotate->rotation);
5067 }
5068
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005069 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005070 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005071}
5072
5073static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005074rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005075 void *data)
5076{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005077 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005078 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005079 struct shell_surface *surface;
5080
Derek Foreman8ae2db52015-07-15 13:00:36 -05005081 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005082 return;
5083
Derek Foreman8ae2db52015-07-15 13:00:36 -05005084 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005085
Pekka Paalanen01388e22013-04-25 13:57:44 +03005086 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005087 if (base_surface == NULL)
5088 return;
5089
5090 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005091 if (surface == NULL || surface->state.fullscreen ||
5092 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005093 return;
5094
Derek Foreman74de4692015-07-15 13:00:39 -05005095 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005096}
5097
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005098/* Move all fullscreen layers down to the current workspace and hide their
5099 * black views. The surfaces' state is set to both fullscreen and lowered,
5100 * and this is reversed when such a surface is re-configured, see
5101 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5102 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005103 * lowering_output = NULL - Lower on all outputs, else only lower on the
5104 * specified output.
5105 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005106 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005107 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005108void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005109lower_fullscreen_layer(struct desktop_shell *shell,
5110 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005111{
5112 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005113 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005114
5115 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005116 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005117 &shell->fullscreen_layer.view_list.link,
5118 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005119 struct shell_surface *shsurf = get_shell_surface(view->surface);
5120
5121 if (!shsurf)
5122 continue;
5123
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005124 /* Only lower surfaces which have lowering_output as their fullscreen
5125 * output, unless a NULL output asks for lowering on all outputs.
5126 */
5127 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5128 continue;
5129
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005130 /* We can have a non-fullscreen popup for a fullscreen surface
5131 * in the fullscreen layer. */
5132 if (shsurf->state.fullscreen) {
5133 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005134 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5135 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005136 weston_view_damage_below(shsurf->fullscreen.black_view);
5137
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005138 }
5139
5140 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005141 weston_layer_entry_remove(&view->layer_link);
5142 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005143 weston_view_damage_below(view);
5144 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005145
5146 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005147 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005148}
5149
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005150void
Tiago Vignattibe143262012-04-16 17:31:41 +03005151activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005152 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005153{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005154 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005155 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005156 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005157 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005158 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005159
Pekka Paalanen01388e22013-04-25 13:57:44 +03005160 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005161 shsurf = get_shell_surface(main_surface);
5162 assert(shsurf);
5163
5164 /* Only demote fullscreen surfaces on the output of activated shsurf.
5165 * Leave fullscreen surfaces on unrelated outputs alone. */
5166 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005167
Daniel Stone37816df2012-05-16 18:45:18 +01005168 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005169
Jonas Ådahl8538b222012-08-29 22:13:03 +02005170 state = ensure_focus_state(shell, seat);
5171 if (state == NULL)
5172 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005173
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005174 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005175 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005176
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005177 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005178 shell_configure_fullscreen(shsurf);
5179 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005180 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005181
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005182 /* Update the surface’s layer. This brings it to the top of the stacking
5183 * order as appropriate. */
5184 shell_surface_update_layer(shsurf);
5185
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005186 if (shell->focus_animation_type != ANIMATION_NONE) {
5187 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005188 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005189 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005190}
5191
Alex Wu21858432012-04-01 20:13:08 +08005192/* no-op func for checking black surface */
5193static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005194black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005195{
5196}
5197
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005198static bool
Alex Wu21858432012-04-01 20:13:08 +08005199is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5200{
5201 if (es->configure == black_surface_configure) {
5202 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005203 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005204 return true;
5205 }
5206 return false;
5207}
5208
Kristian Høgsberg75840622011-09-06 13:48:16 -04005209static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005210activate_binding(struct weston_seat *seat,
5211 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005212 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005213{
Derek Foremanbbde6432015-12-10 13:08:01 -06005214 struct focus_state *state;
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005215 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005216 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005217
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005218 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005219
Pekka Paalanen01388e22013-04-25 13:57:44 +03005220 if (is_black_surface(focus, &main_surface))
5221 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005222
Pekka Paalanen01388e22013-04-25 13:57:44 +03005223 main_surface = weston_surface_get_main_surface(focus);
5224 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005225 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005226
Derek Foremanbbde6432015-12-10 13:08:01 -06005227 state = ensure_focus_state(shell, seat);
5228 if (state == NULL)
5229 return;
5230
5231 if (state->keyboard_focus == focus)
5232 return;
5233
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005234 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005235}
5236
5237static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005238click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5239 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005240{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005241 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005242 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005243 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005244 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005245
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005246 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005247}
5248
5249static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005250touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5251 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005252{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005253 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005254 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005255 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005256 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005257
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005258 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005259}
5260
5261static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005262unfocus_all_seats(struct desktop_shell *shell)
5263{
5264 struct weston_seat *seat, *next;
5265
5266 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005267 struct weston_keyboard *keyboard =
5268 weston_seat_get_keyboard(seat);
5269
5270 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005271 continue;
5272
Derek Foreman1281a362015-07-31 16:55:32 -05005273 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005274 }
5275}
5276
5277static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005278lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005279{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005280 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005281
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005282 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005283 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005284 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005285 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005286
5287 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005288
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005289 /* Hide all surfaces by removing the fullscreen, panel and
5290 * toplevel layers. This way nothing else can show or receive
5291 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005292
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005293 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005294 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005295 if (shell->showing_input_panels)
5296 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005297 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005298 wl_list_insert(&shell->compositor->cursor_layer.link,
5299 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005300
Derek Foreman004b4a12015-07-20 16:28:13 -05005301 weston_compositor_sleep(shell->compositor);
5302
Neil Robertsb4a91702014-04-09 16:33:32 +01005303 /* Remove the keyboard focus on all seats. This will be
5304 * restored to the workspace's saved state via
5305 * restore_focus_state when the compositor is unlocked */
5306 unfocus_all_seats(shell);
5307
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005308 /* TODO: disable bindings that should not work while locked. */
5309
5310 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005311}
5312
5313static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005314unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005315{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005316 struct wl_resource *shell_resource;
5317
Pekka Paalanend81c2162011-11-16 13:47:34 +02005318 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005319 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005320 return;
5321 }
5322
5323 /* If desktop-shell client has gone away, unlock immediately. */
5324 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005325 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005326 return;
5327 }
5328
5329 if (shell->prepare_event_sent)
5330 return;
5331
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005332 shell_resource = shell->child.desktop_shell;
5333 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005334 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005335}
5336
5337static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005338shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005339{
5340 struct desktop_shell *shell = data;
5341
5342 shell->fade.animation = NULL;
5343
5344 switch (shell->fade.type) {
5345 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005346 weston_surface_destroy(shell->fade.view->surface);
5347 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005348 break;
5349 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005350 lock(shell);
5351 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005352 default:
5353 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005354 }
5355}
5356
Jason Ekstranda7af7042013-10-12 22:38:11 -05005357static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005358shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005359{
5360 struct weston_compositor *compositor = shell->compositor;
5361 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005362 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005363
5364 surface = weston_surface_create(compositor);
5365 if (!surface)
5366 return NULL;
5367
Jason Ekstranda7af7042013-10-12 22:38:11 -05005368 view = weston_view_create(surface);
5369 if (!view) {
5370 weston_surface_destroy(surface);
5371 return NULL;
5372 }
5373
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005374 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005375 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005376 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005377 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5378 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005379 pixman_region32_init(&surface->input);
5380
Jason Ekstranda7af7042013-10-12 22:38:11 -05005381 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005382}
5383
5384static void
5385shell_fade(struct desktop_shell *shell, enum fade_type type)
5386{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005387 float tint;
5388
5389 switch (type) {
5390 case FADE_IN:
5391 tint = 0.0;
5392 break;
5393 case FADE_OUT:
5394 tint = 1.0;
5395 break;
5396 default:
5397 weston_log("shell: invalid fade type\n");
5398 return;
5399 }
5400
5401 shell->fade.type = type;
5402
Jason Ekstranda7af7042013-10-12 22:38:11 -05005403 if (shell->fade.view == NULL) {
5404 shell->fade.view = shell_fade_create_surface(shell);
5405 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005406 return;
5407
Jason Ekstranda7af7042013-10-12 22:38:11 -05005408 shell->fade.view->alpha = 1.0 - tint;
5409 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005410 }
5411
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005412 if (shell->fade.view->output == NULL) {
5413 /* If the black view gets a NULL output, we lost the
5414 * last output and we'll just cancel the fade. This
5415 * happens when you close the last window under the
5416 * X11 or Wayland backends. */
5417 shell->locked = false;
5418 weston_surface_destroy(shell->fade.view->surface);
5419 shell->fade.view = NULL;
5420 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005421 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005422 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005423 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005424 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005425 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005426 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005427 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005428}
5429
5430static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005431do_shell_fade_startup(void *data)
5432{
5433 struct desktop_shell *shell = data;
5434
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005435 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005436 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005437 } else {
5438 weston_log("desktop shell: "
5439 "unexpected fade-in animation type %d\n",
5440 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005441 weston_surface_destroy(shell->fade.view->surface);
5442 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005443 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005444}
5445
5446static void
5447shell_fade_startup(struct desktop_shell *shell)
5448{
5449 struct wl_event_loop *loop;
5450
5451 if (!shell->fade.startup_timer)
5452 return;
5453
5454 wl_event_source_remove(shell->fade.startup_timer);
5455 shell->fade.startup_timer = NULL;
5456
5457 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5458 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5459}
5460
5461static int
5462fade_startup_timeout(void *data)
5463{
5464 struct desktop_shell *shell = data;
5465
5466 shell_fade_startup(shell);
5467 return 0;
5468}
5469
5470static void
5471shell_fade_init(struct desktop_shell *shell)
5472{
5473 /* Make compositor output all black, and wait for the desktop-shell
5474 * client to signal it is ready, then fade in. The timer triggers a
5475 * fade-in, in case the desktop-shell client takes too long.
5476 */
5477
5478 struct wl_event_loop *loop;
5479
Jason Ekstranda7af7042013-10-12 22:38:11 -05005480 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005481 weston_log("%s: warning: fade surface already exists\n",
5482 __func__);
5483 return;
5484 }
5485
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005486 if (shell->startup_animation_type == ANIMATION_NONE)
5487 return;
5488
Jason Ekstranda7af7042013-10-12 22:38:11 -05005489 shell->fade.view = shell_fade_create_surface(shell);
5490 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005491 return;
5492
Jason Ekstranda7af7042013-10-12 22:38:11 -05005493 weston_view_update_transform(shell->fade.view);
5494 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005495
5496 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5497 shell->fade.startup_timer =
5498 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5499 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5500}
5501
5502static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005503idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005504{
5505 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005506 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005507 struct weston_seat *seat;
5508
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005509 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005510 struct weston_touch *touch = weston_seat_get_touch(seat);
5511 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5512
5513 if (pointer)
5514 popup_grab_end(pointer);
5515 if (touch)
5516 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005517 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005518
5519 shell_fade(shell, FADE_OUT);
5520 /* lock() is called from shell_fade_done() */
5521}
5522
5523static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005524wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005525{
5526 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005527 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005528
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005529 unlock(shell);
5530}
5531
5532static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005533transform_handler(struct wl_listener *listener, void *data)
5534{
5535 struct weston_surface *surface = data;
5536 struct shell_surface *shsurf = get_shell_surface(surface);
5537 struct weston_view *view;;
5538 int x, y;
5539
5540 if (!shsurf || !shsurf->client->send_position)
5541 return;
5542
5543 view = shsurf->view;
5544 if (!view || !weston_view_is_mapped(view))
5545 return;
5546
5547 x = view->geometry.x;
5548 y = view->geometry.y;
5549
5550 shsurf->client->send_position(surface, x, y);
5551}
5552
5553static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005554center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005555{
Giulio Camuffob8366642013-04-25 13:57:46 +03005556 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005557 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005558
Jason Ekstranda7af7042013-10-12 22:38:11 -05005559 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005560
5561 x = output->x + (output->width - width) / 2 - surf_x / 2;
5562 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005563
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005564 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005565}
5566
5567static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005568weston_view_set_initial_position(struct weston_view *view,
5569 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005570{
5571 struct weston_compositor *compositor = shell->compositor;
5572 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005573 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005574 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005575 struct weston_output *output, *target_output = NULL;
5576 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005577 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005578
5579 /* As a heuristic place the new window on the same output as the
5580 * pointer. Falling back to the output containing 0, 0.
5581 *
5582 * TODO: Do something clever for touch too?
5583 */
5584 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005585 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5586
5587 if (pointer) {
5588 ix = wl_fixed_to_int(pointer->x);
5589 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005590 break;
5591 }
5592 }
5593
5594 wl_list_for_each(output, &compositor->output_list, link) {
5595 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5596 target_output = output;
5597 break;
5598 }
5599 }
5600
5601 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005602 weston_view_set_position(view, 10 + random() % 400,
5603 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005604 return;
5605 }
5606
5607 /* Valid range within output where the surface will still be onscreen.
5608 * If this is negative it means that the surface is bigger than
5609 * output.
5610 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005611 get_output_work_area(shell, target_output, &area);
5612
Derek Foremanf814c5d2015-04-15 12:23:54 -05005613 x = area.x;
5614 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005615 range_x = area.width - view->surface->width;
5616 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005617
Rob Bradford4cb88c72012-08-13 15:18:44 +01005618 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005619 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005620
5621 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005622 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005623
Jason Ekstranda7af7042013-10-12 22:38:11 -05005624 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005625}
5626
5627static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005628set_maximized_position(struct desktop_shell *shell,
5629 struct shell_surface *shsurf)
5630{
5631 int32_t surf_x, surf_y;
5632 pixman_rectangle32_t area;
5633
Jonny Lambd73c6942014-08-20 15:53:20 +02005634 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005635 if (shsurf->has_set_geometry) {
5636 surf_x = shsurf->geometry.x;
5637 surf_y = shsurf->geometry.y;
5638 } else {
5639 surface_subsurfaces_boundingbox(shsurf->surface,
5640 &surf_x, &surf_y, NULL, NULL);
5641 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005642
5643 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005644 area.x - surf_x,
5645 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005646}
5647
5648static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005649map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005650 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005651{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005652 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005653 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005654
Pekka Paalanen77346a62011-11-30 16:26:35 +02005655 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005656 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005657 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005658 if (shsurf->state.fullscreen) {
5659 center_on_output(shsurf->view, shsurf->fullscreen_output);
5660 shell_map_fullscreen(shsurf);
5661 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005662 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005663 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005664 weston_view_set_initial_position(shsurf->view, shell);
5665 }
Juan Zhao96879df2012-02-07 08:45:41 +08005666 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005667 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005668 if (shell_map_popup(shsurf) != 0)
5669 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005670 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005671 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005672 weston_view_set_position(shsurf->view,
5673 shsurf->view->geometry.x + sx,
5674 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005675 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005676 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005677 default:
5678 ;
5679 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005680
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005681 /* Surface stacking order, see also activate(). */
5682 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005683
Jason Ekstranda7af7042013-10-12 22:38:11 -05005684 if (shsurf->type != SHELL_SURFACE_NONE) {
5685 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005686 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005687 shsurf->surface->output = shsurf->output;
5688 shsurf->view->output = shsurf->output;
5689 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005690 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005691
Jason Ekstranda7af7042013-10-12 22:38:11 -05005692 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005693 /* XXX: xwayland's using the same fields for transient type */
5694 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005695 if (shsurf->transient.flags ==
5696 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5697 break;
5698 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005699 if (shsurf->state.relative &&
5700 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5701 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005702 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005703 break;
5704 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005705 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005706 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005707 case SHELL_SURFACE_POPUP:
5708 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005709 default:
5710 break;
5711 }
5712
Rafael Antognolli03b16592013-12-03 15:35:42 -02005713 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5714 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005715 {
5716 switch (shell->win_animation_type) {
5717 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005718 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005719 break;
5720 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005721 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005722 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005723 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005724 default:
5725 break;
5726 }
5727 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005728}
5729
5730static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005731configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005732 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005733{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005734 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005735 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005736
Pekka Paalanen77346a62011-11-30 16:26:35 +02005737 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005738
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005739 assert(shsurf);
5740
Rafael Antognolli03b16592013-12-03 15:35:42 -02005741 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005742 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005743 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005744 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005745 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005746 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005747 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005748
Alex Wu4539b082012-03-01 12:57:46 +08005749 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005750 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005751 wl_list_for_each(view, &surface->views, surface_link)
5752 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005753
Rafael Antognolli03b16592013-12-03 15:35:42 -02005754 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005755 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005756 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005757}
5758
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005759static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005760shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005761{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005762 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005763 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005764 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005765
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005766 assert(shsurf);
5767
5768 shell = shsurf->shell;
5769
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005770 if (!weston_surface_is_mapped(es) &&
5771 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005772 remove_popup_grab(shsurf);
5773 }
5774
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005775 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005776 return;
5777
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005778 if (shsurf->has_next_geometry) {
5779 shsurf->geometry = shsurf->next_geometry;
5780 shsurf->has_next_geometry = false;
5781 shsurf->has_set_geometry = true;
5782 } else if (!shsurf->has_set_geometry) {
5783 surface_subsurfaces_boundingbox(shsurf->surface,
5784 &shsurf->geometry.x,
5785 &shsurf->geometry.y,
5786 &shsurf->geometry.width,
5787 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005788 }
5789
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005790 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005791 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005792 type_changed = 1;
5793 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005794
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005795 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005796 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005797 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005798 shsurf->last_width != es->width ||
5799 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005800 float from_x, from_y;
5801 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005802
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005803 if (shsurf->resize_edges) {
5804 sx = 0;
5805 sy = 0;
5806 }
5807
5808 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5809 sx = shsurf->last_width - es->width;
5810 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5811 sy = shsurf->last_height - es->height;
5812
5813 shsurf->last_width = es->width;
5814 shsurf->last_height = es->height;
5815
Jason Ekstranda7af7042013-10-12 22:38:11 -05005816 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5817 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005818 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005819 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005820 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005821 }
5822}
5823
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005824static bool
5825check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5826{
5827 struct timespec now;
5828
5829 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5830 return false;
5831
5832 /*
5833 * If the shell helper client dies before the session has been
5834 * up for roughly 30 seconds, better just make Weston shut down,
5835 * because the user likely has no way to interact with the desktop
5836 * anyway.
5837 */
5838 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5839 weston_log("Error: %s apparently cannot run at all.\n",
5840 shell->client);
5841 weston_log_continue(STAMP_SPACE "Quitting...");
5842 wl_display_terminate(shell->compositor->wl_display);
5843
5844 return true;
5845 }
5846
5847 return false;
5848}
5849
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005850static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005851
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005852static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005853respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005854{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005855 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005856
5857 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5858 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005859 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005860 shell->child.deathstamp = time;
5861 shell->child.deathcount = 0;
5862 }
5863
5864 shell->child.deathcount++;
5865 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005866 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005867 return;
5868 }
5869
Pekka Paalanen826dc142014-08-27 12:11:53 +03005870 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005871 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005872}
5873
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005874static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005875desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5876{
5877 struct desktop_shell *shell;
5878
5879 shell = container_of(listener, struct desktop_shell,
5880 child.client_destroy_listener);
5881
Pekka Paalanen826dc142014-08-27 12:11:53 +03005882 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005883 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005884 /*
5885 * unbind_desktop_shell() will reset shell->child.desktop_shell
5886 * before the respawned process has a chance to create a new
5887 * desktop_shell object, because we are being called from the
5888 * wl_client destructor which destroys all wl_resources before
5889 * returning.
5890 */
5891
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005892 if (!check_desktop_shell_crash_too_early(shell))
5893 respawn_desktop_shell_process(shell);
5894
Pekka Paalanen826dc142014-08-27 12:11:53 +03005895 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005896}
5897
5898static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005899launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005900{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005901 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005902
Pekka Paalanen826dc142014-08-27 12:11:53 +03005903 shell->child.client = weston_client_start(shell->compositor,
5904 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005905
Arnaud Vrac42631192014-08-25 20:56:46 +02005906 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005907 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005908 return;
5909 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005910
5911 shell->child.client_destroy_listener.notify =
5912 desktop_shell_client_destroy;
5913 wl_client_add_destroy_listener(shell->child.client,
5914 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005915}
5916
5917static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005918handle_shell_client_destroy(struct wl_listener *listener, void *data)
5919{
5920 struct shell_client *sc =
5921 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005922 struct wl_resource *shsurf_resource;
5923 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005924
5925 if (sc->ping_timer)
5926 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005927
5928 /* Since we're about to free shell_client, we remove it from the
5929 * head of the surface list so we don't use that freed list node
5930 * during surface clean up later on.
5931 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005932 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5933 shsurf = wl_resource_get_user_data(shsurf_resource);
5934 shsurf->owner = NULL;
5935 }
Derek Foremane42d7542015-05-29 10:46:44 -05005936 wl_list_remove(&sc->surface_list);
5937
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005938 free(sc);
5939}
5940
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005941static struct shell_client *
5942shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5943 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005944{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005945 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005946
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005947 sc = zalloc(sizeof *sc);
5948 if (sc == NULL) {
5949 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005950 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005951 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005952
5953 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005954 if (sc->resource == NULL) {
5955 free(sc);
5956 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005957 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005958 }
5959
5960 sc->client = client;
5961 sc->shell = shell;
5962 sc->destroy_listener.notify = handle_shell_client_destroy;
5963 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005964 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005965
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005966 return sc;
5967}
5968
5969static void
5970bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5971{
5972 struct desktop_shell *shell = data;
5973 struct shell_client *sc;
5974
5975 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5976 if (sc)
5977 wl_resource_set_implementation(sc->resource,
5978 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005979 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005980}
5981
5982static void
5983bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5984{
5985 struct desktop_shell *shell = data;
5986 struct shell_client *sc;
5987
5988 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5989 if (sc)
5990 wl_resource_set_dispatcher(sc->resource,
5991 xdg_shell_unversioned_dispatch,
5992 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005993}
5994
5995static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005996unbind_desktop_shell(struct wl_resource *resource)
5997{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005998 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005999
6000 if (shell->locked)
6001 resume_desktop(shell);
6002
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006003 shell->child.desktop_shell = NULL;
6004 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006005}
6006
6007static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04006008bind_desktop_shell(struct wl_client *client,
6009 void *data, uint32_t version, uint32_t id)
6010{
Tiago Vignattibe143262012-04-16 17:31:41 +03006011 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006012 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04006013
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006014 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6015 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006016
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006017 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006018 wl_resource_set_implementation(resource,
6019 &desktop_shell_implementation,
6020 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006021 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006022 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006023 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006024
6025 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6026 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04006027}
6028
Kristian Høgsberg07045392012-02-19 18:52:44 -05006029struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006030 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006031 struct weston_surface *current;
6032 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006033 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006034 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006035};
6036
6037static void
6038switcher_next(struct switcher *switcher)
6039{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006040 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006041 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006042 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006043 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006045 /* temporary re-display minimized surfaces */
6046 struct weston_view *tmp;
6047 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006048 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6049 weston_layer_entry_remove(&view->layer_link);
6050 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006051 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6052 *minimized = view;
6053 }
6054
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006055 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006056 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006057 if (shsurf &&
6058 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6059 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006060 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006061 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006062 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006063 next = view->surface;
6064 prev = view->surface;
6065 view->alpha = 0.25;
6066 weston_view_geometry_dirty(view);
6067 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006068 }
Alex Wu1659daa2012-04-01 20:13:09 +08006069
Jason Ekstranda7af7042013-10-12 22:38:11 -05006070 if (is_black_surface(view->surface, NULL)) {
6071 view->alpha = 0.25;
6072 weston_view_geometry_dirty(view);
6073 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006074 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006075 }
6076
6077 if (next == NULL)
6078 next = first;
6079
Alex Wu07b26062012-03-12 16:06:01 +08006080 if (next == NULL)
6081 return;
6082
Kristian Høgsberg07045392012-02-19 18:52:44 -05006083 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006084 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006085
6086 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006087 wl_list_for_each(view, &next->views, surface_link)
6088 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006089
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006090 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006091 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006092 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006093}
6094
6095static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006096switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006097{
6098 struct switcher *switcher =
6099 container_of(listener, struct switcher, listener);
6100
6101 switcher_next(switcher);
6102}
6103
6104static void
Daniel Stone351eb612012-05-31 15:27:47 -04006105switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006106{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006107 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006108 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006109 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006110
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006111 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006112 if (is_focus_view(view))
6113 continue;
6114
Jason Ekstranda7af7042013-10-12 22:38:11 -05006115 view->alpha = 1.0;
6116 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006117 }
6118
Alex Wu07b26062012-03-12 16:06:01 +08006119 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006120 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006121 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006122 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006123 weston_keyboard_end_grab(keyboard);
6124 if (keyboard->input_method_resource)
6125 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006126
6127 /* re-hide surfaces that were temporary shown during the switch */
6128 struct weston_view **minimized;
6129 wl_array_for_each(minimized, &switcher->minimized_array) {
6130 /* with the exception of the current selected */
6131 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006132 weston_layer_entry_remove(&(*minimized)->layer_link);
6133 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006134 weston_view_damage_below(*minimized);
6135 }
6136 }
6137 wl_array_release(&switcher->minimized_array);
6138
Kristian Høgsberg07045392012-02-19 18:52:44 -05006139 free(switcher);
6140}
6141
6142static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006143switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006144 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006145{
6146 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006147 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006148
Daniel Stonec9785ea2012-05-30 16:31:52 +01006149 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006150 switcher_next(switcher);
6151}
6152
6153static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006154switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006155 uint32_t mods_depressed, uint32_t mods_latched,
6156 uint32_t mods_locked, uint32_t group)
6157{
6158 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006159 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006160
Daniel Stone351eb612012-05-31 15:27:47 -04006161 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6162 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006163}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006164
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006165static void
6166switcher_cancel(struct weston_keyboard_grab *grab)
6167{
6168 struct switcher *switcher = container_of(grab, struct switcher, grab);
6169
6170 switcher_destroy(switcher);
6171}
6172
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006173static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006174 switcher_key,
6175 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006176 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006177};
6178
6179static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006180switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6181 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006182{
Tiago Vignattibe143262012-04-16 17:31:41 +03006183 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006184 struct switcher *switcher;
6185
6186 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006187 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006188 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006189 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006190 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006191 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006192
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006193 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006194 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006195 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006196 weston_keyboard_start_grab(keyboard, &switcher->grab);
6197 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006198 switcher_next(switcher);
6199}
6200
Pekka Paalanen3c647232011-12-22 13:43:43 +02006201static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006202backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6203 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006204{
6205 struct weston_compositor *compositor = data;
6206 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006207 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006208
6209 /* TODO: we're limiting to simple use cases, where we assume just
6210 * control on the primary display. We'd have to extend later if we
6211 * ever get support for setting backlights on random desktop LCD
6212 * panels though */
6213 output = get_default_output(compositor);
6214 if (!output)
6215 return;
6216
6217 if (!output->set_backlight)
6218 return;
6219
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006220 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6221 backlight_new = output->backlight_current - 25;
6222 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6223 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006224
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006225 if (backlight_new < 5)
6226 backlight_new = 5;
6227 if (backlight_new > 255)
6228 backlight_new = 255;
6229
6230 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006231 output->set_backlight(output, output->backlight_current);
6232}
6233
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006234static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006235force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6236 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006237{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006238 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006239 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006240 struct desktop_shell *shell = data;
6241 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006242 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006243
Derek Foreman8ae2db52015-07-15 13:00:36 -05006244 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006245 if (!focus_surface)
6246 return;
6247
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006248 wl_signal_emit(&compositor->kill_signal, focus_surface);
6249
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006250 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006251 wl_client_get_credentials(client, &pid, NULL, NULL);
6252
6253 /* Skip clients that we launched ourselves (the credentials of
6254 * the socketpair is ours) */
6255 if (pid == getpid())
6256 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006257
Daniel Stone325fc2d2012-05-30 16:31:58 +01006258 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006259}
6260
6261static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006262workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006263 uint32_t key, void *data)
6264{
6265 struct desktop_shell *shell = data;
6266 unsigned int new_index = shell->workspaces.current;
6267
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006268 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006269 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006270 if (new_index != 0)
6271 new_index--;
6272
6273 change_workspace(shell, new_index);
6274}
6275
6276static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006277workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006278 uint32_t key, void *data)
6279{
6280 struct desktop_shell *shell = data;
6281 unsigned int new_index = shell->workspaces.current;
6282
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006283 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006284 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006285 if (new_index < shell->workspaces.num - 1)
6286 new_index++;
6287
6288 change_workspace(shell, new_index);
6289}
6290
6291static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006292workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006293 uint32_t key, void *data)
6294{
6295 struct desktop_shell *shell = data;
6296 unsigned int new_index;
6297
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006298 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006299 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006300 new_index = key - KEY_F1;
6301 if (new_index >= shell->workspaces.num)
6302 new_index = shell->workspaces.num - 1;
6303
6304 change_workspace(shell, new_index);
6305}
6306
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006307static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006308workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6309 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006310{
6311 struct desktop_shell *shell = data;
6312 unsigned int new_index = shell->workspaces.current;
6313
6314 if (shell->locked)
6315 return;
6316
6317 if (new_index != 0)
6318 new_index--;
6319
Derek Foreman8ae2db52015-07-15 13:00:36 -05006320 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006321}
6322
6323static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006324workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6325 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006326{
6327 struct desktop_shell *shell = data;
6328 unsigned int new_index = shell->workspaces.current;
6329
6330 if (shell->locked)
6331 return;
6332
6333 if (new_index < shell->workspaces.num - 1)
6334 new_index++;
6335
Derek Foreman8ae2db52015-07-15 13:00:36 -05006336 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006337}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006338
6339static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006340shell_reposition_view_on_output_destroy(struct weston_view *view)
6341{
6342 struct weston_output *output, *first_output;
6343 struct weston_compositor *ec = view->surface->compositor;
6344 struct shell_surface *shsurf;
6345 float x, y;
6346 int visible;
6347
6348 x = view->geometry.x;
6349 y = view->geometry.y;
6350
6351 /* At this point the destroyed output is not in the list anymore.
6352 * If the view is still visible somewhere, we leave where it is,
6353 * otherwise, move it to the first output. */
6354 visible = 0;
6355 wl_list_for_each(output, &ec->output_list, link) {
6356 if (pixman_region32_contains_point(&output->region,
6357 x, y, NULL)) {
6358 visible = 1;
6359 break;
6360 }
6361 }
6362
6363 if (!visible) {
6364 first_output = container_of(ec->output_list.next,
6365 struct weston_output, link);
6366
6367 x = first_output->x + first_output->width / 4;
6368 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006369
6370 weston_view_set_position(view, x, y);
6371 } else {
6372 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006373 }
6374
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006375
6376 shsurf = get_shell_surface(view->surface);
6377
6378 if (shsurf) {
6379 shsurf->saved_position_valid = false;
6380 shsurf->next_state.maximized = false;
6381 shsurf->next_state.fullscreen = false;
6382 shsurf->state_changed = true;
6383 }
6384}
6385
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006386void
6387shell_for_each_layer(struct desktop_shell *shell,
6388 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006389{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006390 struct workspace **ws;
6391
6392 func(shell, &shell->fullscreen_layer, data);
6393 func(shell, &shell->panel_layer, data);
6394 func(shell, &shell->background_layer, data);
6395 func(shell, &shell->lock_layer, data);
6396 func(shell, &shell->input_panel_layer, data);
6397
6398 wl_array_for_each(ws, &shell->workspaces.array)
6399 func(shell, &(*ws)->layer, data);
6400}
6401
6402static void
6403shell_output_destroy_move_layer(struct desktop_shell *shell,
6404 struct weston_layer *layer,
6405 void *data)
6406{
6407 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006408 struct weston_view *view;
6409
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006410 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006411 if (view->output != output)
6412 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006413
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006414 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006415 }
6416}
6417
6418static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006419handle_output_destroy(struct wl_listener *listener, void *data)
6420{
6421 struct shell_output *output_listener =
6422 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006423 struct weston_output *output = output_listener->output;
6424 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006425
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006426 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006427
Xiong Zhang6b481422013-10-23 13:58:32 +08006428 wl_list_remove(&output_listener->destroy_listener.link);
6429 wl_list_remove(&output_listener->link);
6430 free(output_listener);
6431}
6432
6433static void
David Fort50d962f2016-06-09 17:55:52 +02006434shell_resize_surface_to_output(struct desktop_shell *shell,
6435 struct weston_surface *surface,
6436 const struct weston_output *output)
6437{
6438 if (!surface)
6439 return;
6440
6441 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6442 surface->resource,
6443 output->width,
6444 output->height);
6445}
6446
6447
6448static void
6449handle_output_resized(struct wl_listener *listener, void *data)
6450{
6451 struct desktop_shell *shell =
6452 container_of(listener, struct desktop_shell, resized_listener);
6453 struct weston_output *output = (struct weston_output *)data;
6454 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6455
6456 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6457 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6458}
6459
6460static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006461create_shell_output(struct desktop_shell *shell,
6462 struct weston_output *output)
6463{
6464 struct shell_output *shell_output;
6465
6466 shell_output = zalloc(sizeof *shell_output);
6467 if (shell_output == NULL)
6468 return;
6469
6470 shell_output->output = output;
6471 shell_output->shell = shell;
6472 shell_output->destroy_listener.notify = handle_output_destroy;
6473 wl_signal_add(&output->destroy_signal,
6474 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006475 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006476}
6477
6478static void
6479handle_output_create(struct wl_listener *listener, void *data)
6480{
6481 struct desktop_shell *shell =
6482 container_of(listener, struct desktop_shell, output_create_listener);
6483 struct weston_output *output = (struct weston_output *)data;
6484
6485 create_shell_output(shell, output);
6486}
6487
6488static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006489handle_output_move_layer(struct desktop_shell *shell,
6490 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006491{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006492 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006493 struct weston_view *view;
6494 float x, y;
6495
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006496 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006497 if (view->output != output)
6498 continue;
6499
6500 x = view->geometry.x + output->move_x;
6501 y = view->geometry.y + output->move_y;
6502 weston_view_set_position(view, x, y);
6503 }
6504}
6505
6506static void
6507handle_output_move(struct wl_listener *listener, void *data)
6508{
6509 struct desktop_shell *shell;
6510
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006511 shell = container_of(listener, struct desktop_shell,
6512 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006513
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006514 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006515}
6516
6517static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006518setup_output_destroy_handler(struct weston_compositor *ec,
6519 struct desktop_shell *shell)
6520{
6521 struct weston_output *output;
6522
6523 wl_list_init(&shell->output_list);
6524 wl_list_for_each(output, &ec->output_list, link)
6525 create_shell_output(shell, output);
6526
6527 shell->output_create_listener.notify = handle_output_create;
6528 wl_signal_add(&ec->output_created_signal,
6529 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006530
6531 shell->output_move_listener.notify = handle_output_move;
6532 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006533}
6534
6535static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006536shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006537{
Tiago Vignattibe143262012-04-16 17:31:41 +03006538 struct desktop_shell *shell =
6539 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006540 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006541 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006542
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006543 /* Force state to unlocked so we don't try to fade */
6544 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006545
6546 if (shell->child.client) {
6547 /* disable respawn */
6548 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006549 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006550 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006551
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006552 wl_list_remove(&shell->idle_listener.link);
6553 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006554 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006555
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006556 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006557 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006558
Xiong Zhang6b481422013-10-23 13:58:32 +08006559 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6560 wl_list_remove(&shell_output->destroy_listener.link);
6561 wl_list_remove(&shell_output->link);
6562 free(shell_output);
6563 }
6564
6565 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006566 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02006567 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006568
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006569 wl_array_for_each(ws, &shell->workspaces.array)
6570 workspace_destroy(*ws);
6571 wl_array_release(&shell->workspaces.array);
6572
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006573 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006574 free(shell);
6575}
6576
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006577static void
6578shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6579{
6580 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006581 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006582
Bob Ham744e6532016-01-12 10:21:48 +00006583 if (shell->allow_zap)
6584 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6585 MODIFIER_CTRL | MODIFIER_ALT,
6586 terminate_binding, ec);
6587
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006588 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006589 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6590 click_to_activate_binding,
6591 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006592 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6593 click_to_activate_binding,
6594 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006595 weston_compositor_add_touch_binding(ec, 0,
6596 touch_to_activate_binding,
6597 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006598 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6599 MODIFIER_SUPER | MODIFIER_ALT,
6600 surface_opacity_binding, NULL);
6601 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6602 MODIFIER_SUPER, zoom_axis_binding,
6603 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006604 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6605 backlight_binding, ec);
6606 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6607 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006608
6609 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006610 if (shell->exposay_modifier)
6611 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6612 exposay_binding, shell);
6613
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006614 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006615 if (!mod)
6616 return;
6617
Daniel Stone325fc2d2012-05-30 16:31:58 +01006618 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6619 zoom_key_binding, NULL);
6620 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6621 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006622 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6623 maximize_binding, NULL);
6624 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6625 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006626 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6627 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006628 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006629 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006630 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006631 weston_compositor_add_button_binding(ec, BTN_LEFT,
6632 mod | MODIFIER_SHIFT,
6633 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006634
6635 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006636 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006637 rotate_binding, NULL);
6638
Daniel Stone325fc2d2012-05-30 16:31:58 +01006639 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6640 shell);
6641 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6642 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006643 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6644 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006645 weston_compositor_add_key_binding(ec, KEY_K, mod,
6646 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006647 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6648 workspace_up_binding, shell);
6649 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6650 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006651 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6652 workspace_move_surface_up_binding,
6653 shell);
6654 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6655 workspace_move_surface_down_binding,
6656 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006657
6658 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6659 if (shell->workspaces.num > 1) {
6660 num_workspace_bindings = shell->workspaces.num;
6661 if (num_workspace_bindings > 6)
6662 num_workspace_bindings = 6;
6663 for (i = 0; i < num_workspace_bindings; i++)
6664 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6665 workspace_f_binding,
6666 shell);
6667 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006668
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006669 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006670}
6671
Jason Ekstrand024177c2014-04-21 19:42:58 -05006672static void
6673handle_seat_created(struct wl_listener *listener, void *data)
6674{
6675 struct weston_seat *seat = data;
6676
6677 create_shell_seat(seat);
6678}
6679
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006680WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006681module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006682 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006683{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006684 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006685 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006686 struct workspace **pws;
6687 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006688 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006689
Peter Huttererf3d62272013-08-08 11:57:05 +10006690 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006691 if (shell == NULL)
6692 return -1;
6693
Kristian Høgsberg75840622011-09-06 13:48:16 -04006694 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006695
6696 shell->destroy_listener.notify = shell_destroy;
6697 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006698 shell->idle_listener.notify = idle_handler;
6699 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6700 shell->wake_listener.notify = wake_handler;
6701 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006702 shell->transform_listener.notify = transform_handler;
6703 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006704
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006705 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006706 ec->shell_interface.create_shell_surface = create_shell_surface;
6707 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006708 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006709 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006710 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006711 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006712 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006713 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006714 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006715 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006716 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006717 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006718
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006719 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6720 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006721 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6722 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006723 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006724
6725 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006726 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006727
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006728 if (input_panel_setup(shell) < 0)
6729 return -1;
6730
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006731 shell->text_backend = text_backend_init(ec);
6732 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006733 return -1;
6734
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006735 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006736
Daniel Stonedf8133b2013-11-19 11:37:14 +01006737 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6738 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6739
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006740 for (i = 0; i < shell->workspaces.num; i++) {
6741 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6742 if (pws == NULL)
6743 return -1;
6744
6745 *pws = workspace_create();
6746 if (*pws == NULL)
6747 return -1;
6748 }
6749 activate_workspace(shell, 0);
6750
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006751 weston_layer_init(&shell->minimized_layer, NULL);
6752
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006753 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006754 wl_list_init(&shell->workspaces.animation.link);
6755 shell->workspaces.animation.frame = animate_workspace_change_frame;
6756
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006757 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006758 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006759 return -1;
6760
Rafael Antognollie2a34552013-12-03 15:35:45 -02006761 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6762 shell, bind_xdg_shell) == NULL)
6763 return -1;
6764
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006765 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006766 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006767 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006768 return -1;
6769
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006770 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006771
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006772 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006773
Xiong Zhang6b481422013-10-23 13:58:32 +08006774 setup_output_destroy_handler(ec, shell);
6775
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006776 loop = wl_display_get_event_loop(ec->wl_display);
6777 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006778
Jason Ekstrand024177c2014-04-21 19:42:58 -05006779 wl_list_for_each(seat, &ec->seat_list, link)
6780 handle_seat_created(NULL, seat);
6781 shell->seat_create_listener.notify = handle_seat_created;
6782 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006783
David Fort50d962f2016-06-09 17:55:52 +02006784 shell->resized_listener.notify = handle_output_resized;
6785 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6786
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006787 screenshooter_create(ec);
6788
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006789 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006790
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006791 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006792
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006793 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6794
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006795 return 0;
6796}