blob: 6f1c1672154dffbf8ad249f50a0e5f6e356ed79d [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"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080039#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070040#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070041#include "shared/helpers.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080042#include "xdg-shell-unstable-v5-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010047#ifndef static_assert
48#define static_assert(cond, msg)
49#endif
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 struct weston_surface *keyboard_focus;
55 struct wl_list link;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
58};
59
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050060enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020061 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050062 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030063 SHELL_SURFACE_POPUP,
64 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020065};
66
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050067struct shell_client;
68
Philip Withnall648a4dd2013-11-25 18:01:44 +000069/*
70 * Surface stacking and ordering.
71 *
72 * This is handled using several linked lists of surfaces, organised into
73 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
74 * above all of the surfaces in the layer below. The set of layers is static and
75 * in the following order (top-most first):
76 * • Lock layer (only ever displayed on its own)
77 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010078 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000079 * • Fullscreen layer
80 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000081 * • Workspace layers
82 * • Background layer
83 *
84 * The list of layers may be manipulated to remove whole layers of surfaces from
85 * display. For example, when locking the screen, all layers except the lock
86 * layer are removed.
87 *
88 * A surface’s layer is modified on configuring the surface, in
89 * set_surface_type() (which is only called when the surface’s type change is
90 * _committed_). If a surface’s type changes (e.g. when making a window
91 * fullscreen) its layer changes too.
92 *
93 * In order to allow popup and transient surfaces to be correctly stacked above
94 * their parent surfaces, each surface tracks both its parent surface, and a
95 * linked list of its children. When a surface’s layer is updated, so are the
96 * layers of its children. Note that child surfaces are *not* the same as
97 * subsurfaces — child/parent surfaces are purely for maintaining stacking
98 * order.
99 *
100 * The children_link list of siblings of a surface (i.e. those surfaces which
101 * have the same parent) only contains weston_surfaces which have a
102 * shell_surface. Stacking is not implemented for non-shell_surface
103 * weston_surfaces. This means that the following implication does *not* hold:
104 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
105 */
106
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200107struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500108 struct wl_resource *resource;
109 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500110 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800111 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200112
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500114 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600115 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200116 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700117 struct wl_listener resource_destroy_listener;
118
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400119 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000120 struct wl_list children_list; /* child surfaces of this one */
121 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300122 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200123
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800124 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400125 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500126 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800128 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200129 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800130 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700131 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800132 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100133
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500136 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200137 } rotation;
138
139 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100142 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400143 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500144 } popup;
145
Alex Wu4539b082012-03-01 12:57:46 +0800146 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400148 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300149 } transient;
150
151 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800152 enum wl_shell_surface_fullscreen_method type;
153 struct weston_transform transform; /* matrix from x, y */
154 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500155 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800156 } fullscreen;
157
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200158 struct weston_transform workspace_transform;
159
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500160 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500161 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100162 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400163
164 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200165
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700166 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200167 bool maximized;
168 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200169 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100170 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200172 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500173 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500174
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700175 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400176 int32_t x, y, width, height;
177 } geometry, next_geometry;
178 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700179
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500180 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500181
182 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200183};
184
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300185struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400186 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300187 struct shell_surface *shsurf;
188 struct wl_listener shsurf_destroy_listener;
189};
190
Rusty Lynch1084da52013-08-15 09:10:08 -0700191struct shell_touch_grab {
192 struct weston_touch_grab grab;
193 struct shell_surface *shsurf;
194 struct wl_listener shsurf_destroy_listener;
195 struct weston_touch *touch;
196};
197
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198struct weston_move_grab {
199 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100200 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500201 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500202};
203
Rusty Lynch1084da52013-08-15 09:10:08 -0700204struct weston_touch_move_grab {
205 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800206 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700207 wl_fixed_t dx, dy;
208};
209
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300211 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500212 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200214 float x;
215 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200216 } center;
217};
218
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219struct shell_seat {
220 struct weston_seat *seat;
221 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500222 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100223
Jason Ekstrand024177c2014-04-21 19:42:58 -0500224 struct wl_listener caps_changed_listener;
225 struct wl_listener pointer_focus_listener;
226 struct wl_listener keyboard_focus_listener;
227
Giulio Camuffo5085a752013-03-25 21:42:45 +0100228 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400229 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200230 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100231 struct wl_list surfaces_list;
232 struct wl_client *client;
233 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200234 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100235 } popup_grab;
236};
237
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800238struct shell_client {
239 struct wl_resource *resource;
240 struct wl_client *client;
241 struct desktop_shell *shell;
242 struct wl_listener destroy_listener;
243 struct wl_event_source *ping_timer;
244 uint32_t ping_serial;
245 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800246 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800247};
248
Alex Wubd3354b2012-04-17 17:20:49 +0800249static struct desktop_shell *
250shell_surface_get_shell(struct shell_surface *shsurf);
251
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500252static void
Derek Foreman74de4692015-07-15 13:00:39 -0500253surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500254
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300255static void
256shell_fade_startup(struct desktop_shell *shell);
257
Philip Withnallbecb77e2013-11-25 18:01:30 +0000258static struct shell_seat *
259get_shell_seat(struct weston_seat *seat);
260
Jonny Lambd73c6942014-08-20 15:53:20 +0200261static void
262get_output_panel_size(struct desktop_shell *shell,
263 struct weston_output *output,
264 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700265
Philip Withnall648a4dd2013-11-25 18:01:44 +0000266static void
267shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
268
Alex Wubd3354b2012-04-17 17:20:49 +0800269static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200270shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
271
272static bool
273shell_surface_is_xdg_surface(struct shell_surface *shsurf);
274
275static bool
276shell_surface_is_xdg_popup(struct shell_surface *shsurf);
277
278static void
279shell_surface_set_parent(struct shell_surface *shsurf,
280 struct weston_surface *parent);
281
Pekka Paalanen8274d902014-08-06 19:36:51 +0300282static int
283shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
284{
285 struct shell_surface *shsurf;
286 const char *typestr[] = {
287 [SHELL_SURFACE_NONE] = "unidentified",
288 [SHELL_SURFACE_TOPLEVEL] = "top-level",
289 [SHELL_SURFACE_POPUP] = "popup",
290 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
291 };
292 const char *t, *c;
293
294 shsurf = get_shell_surface(surface);
295 if (!shsurf)
296 return snprintf(buf, len, "unidentified window");
297
298 t = shsurf->title;
299 c = shsurf->class;
300
301 return snprintf(buf, len, "%s window%s%s%s%s%s",
302 typestr[shsurf->type],
303 t ? " '" : "", t ?: "", t ? "'" : "",
304 c ? " of " : "", c ?: "");
305}
306
Rafael Antognollie2a34552013-12-03 15:35:45 -0200307static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800308shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
309{
310 struct desktop_shell *shell;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200311 struct weston_view *view;
312 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800313
314 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100315
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300316 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800317 return false;
318
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200319 /* Find topmost shsurf on the same fullscreen output on which shsurf
320 * is displaying. We don't care about other outputs.
321 */
322 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
323 layer_link.link) {
324 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
325
326 if (cand_shsurf &&
327 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
328 top_fs_shsurf = cand_shsurf;
329 break;
330 }
331 }
332
333 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800334}
335
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500336static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400337destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338{
339 struct shell_grab *grab;
340
341 grab = container_of(listener, struct shell_grab,
342 shsurf_destroy_listener);
343
344 grab->shsurf = NULL;
345}
346
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800347struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348get_default_view(struct weston_surface *surface)
349{
350 struct shell_surface *shsurf;
351 struct weston_view *view;
352
353 if (!surface || wl_list_empty(&surface->views))
354 return NULL;
355
356 shsurf = get_shell_surface(surface);
357 if (shsurf)
358 return shsurf->view;
359
360 wl_list_for_each(view, &surface->views, surface_link)
361 if (weston_view_is_mapped(view))
362 return view;
363
364 return container_of(surface->views.next, struct weston_view, surface_link);
365}
366
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400368popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200369static void
370touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400371
372static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300373shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400374 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300375 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400376 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800377 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300379 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500380 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300381
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400382 popup_grab_end(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -0500383 if (touch)
384 touch_popup_grab_end(touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400385
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300386 grab->grab.interface = interface;
387 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400388 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500389 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400390 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300391
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700392 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400393 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400394 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800395 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400396 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397 weston_pointer_set_focus(pointer,
398 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400399 wl_fixed_from_int(0),
400 wl_fixed_from_int(0));
401 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300402}
403
Jonny Lambd73c6942014-08-20 15:53:20 +0200404static void
405get_output_panel_size(struct desktop_shell *shell,
406 struct weston_output *output,
407 int *width,
408 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700409{
410 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200411
412 *width = 0;
413 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700414
415 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200416 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700417
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300418 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200419 float x, y;
420
421 if (view->surface->output != output)
422 continue;
423
424 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800425 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
426 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200427 weston_view_to_global_float(view,
428 view->surface->width, 0,
429 &x, &y);
430
Derek Foreman612341f2015-04-15 12:23:55 -0500431 *width = (int)x - output->x;
432 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200433 return;
434
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800435 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
436 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200437 weston_view_to_global_float(view,
438 0, view->surface->height,
439 &x, &y);
440
Derek Foreman612341f2015-04-15 12:23:55 -0500441 *width = view->surface->width + (int)x - output->x;
442 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200443 return;
444
445 default:
446 /* we've already set width and height to
447 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700448 break;
449 }
450 }
451
Jonny Lambd73c6942014-08-20 15:53:20 +0200452 /* the correct view wasn't found */
453}
454
455static void
456get_output_work_area(struct desktop_shell *shell,
457 struct weston_output *output,
458 pixman_rectangle32_t *area)
459{
460 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
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400643 section = weston_config_get_section(shell->compositor->config,
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,
1601 uint32_t time, uint32_t axis, wl_fixed_t value)
1602{
1603}
1604
1605static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001606constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1607{
1608 struct shell_surface *shsurf = move->base.shsurf;
1609 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001610 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001611 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001612 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001613
1614 x = wl_fixed_to_int(pointer->x + move->dx);
1615 y = wl_fixed_to_int(pointer->y + move->dy);
1616
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001617 if (shsurf->shell->panel_position ==
1618 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001619 get_output_work_area(shsurf->shell,
1620 shsurf->surface->output,
1621 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001622
Derek Foreman6feb0f92015-04-15 12:23:56 -05001623 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1624 if (bottom - safety < area.y)
1625 y = area.y + safety - shsurf->geometry.height
1626 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001627
1628 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001629 y + shsurf->geometry.y < area.y)
1630 y = area.y - shsurf->geometry.y;
1631
Jonny Lambd73c6942014-08-20 15:53:20 +02001632 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001633
1634 *cx = x;
1635 *cy = y;
1636}
1637
1638static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001639move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001640 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001641{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001642 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001643 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001644 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001645 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001646
Jonas Ådahld2510102014-10-05 21:39:14 +02001647 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001648 if (!shsurf)
1649 return;
1650
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001651 constrain_position(move, &cx, &cy);
1652
1653 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001654
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001656}
1657
1658static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001659move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001660 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001661{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001662 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1663 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001664 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001665 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001666
Daniel Stone4dbadb12012-05-30 16:31:51 +01001667 if (pointer->button_count == 0 &&
1668 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001669 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001670 free(grab);
1671 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001672}
1673
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001674static void
1675move_grab_cancel(struct weston_pointer_grab *grab)
1676{
1677 struct shell_grab *shell_grab =
1678 container_of(grab, struct shell_grab, grab);
1679
1680 shell_grab_end(shell_grab);
1681 free(grab);
1682}
1683
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001684static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001685 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001686 move_grab_motion,
1687 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001688 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001689 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001690};
1691
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001693surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001694 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695{
1696 struct weston_move_grab *move;
1697
1698 if (!shsurf)
1699 return -1;
1700
Derek Foreman45a7c272015-09-11 14:27:40 -05001701 shsurf = find_toplevel_surface(shsurf);
1702
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001703 if (shsurf->grabbed ||
1704 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705 return 0;
1706
1707 move = malloc(sizeof *move);
1708 if (!move)
1709 return -1;
1710
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001712 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001714 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001715 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716
1717 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001718 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719
1720 return 0;
1721}
1722
1723static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001724common_surface_move(struct wl_resource *resource,
1725 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001727 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001728 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1729 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001730 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001731 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732
Derek Foreman1281a362015-07-31 16:55:32 -05001733 if (pointer &&
1734 pointer->focus &&
1735 pointer->button_count > 0 &&
1736 pointer->grab_serial == serial) {
1737 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001738 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001739 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001740 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001741 } else if (touch &&
1742 touch->focus &&
1743 touch->grab_serial == serial) {
1744 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001745 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001746 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001747 wl_resource_post_no_memory(resource);
1748 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749}
1750
Rafael Antognollie2a34552013-12-03 15:35:45 -02001751static void
1752shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1753 struct wl_resource *seat_resource, uint32_t serial)
1754{
1755 common_surface_move(resource, seat_resource, serial);
1756}
1757
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001758struct weston_resize_grab {
1759 struct shell_grab base;
1760 uint32_t edges;
1761 int32_t width, height;
1762};
1763
1764static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001765resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001766 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767{
1768 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001769 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770 struct shell_surface *shsurf = resize->base.shsurf;
1771 int32_t width, height;
1772 wl_fixed_t from_x, from_y;
1773 wl_fixed_t to_x, to_y;
1774
Jonas Ådahld2510102014-10-05 21:39:14 +02001775 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001776
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001777 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778 return;
1779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 weston_view_from_global_fixed(shsurf->view,
1781 pointer->grab_x, pointer->grab_y,
1782 &from_x, &from_y);
1783 weston_view_from_global_fixed(shsurf->view,
1784 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001785
1786 width = resize->width;
1787 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1788 width += wl_fixed_to_int(from_x - to_x);
1789 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1790 width += wl_fixed_to_int(to_x - from_x);
1791 }
1792
1793 height = resize->height;
1794 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1795 height += wl_fixed_to_int(from_y - to_y);
1796 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1797 height += wl_fixed_to_int(to_y - from_y);
1798 }
1799
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001800 if (width < 1)
1801 width = 1;
1802 if (height < 1)
1803 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001804 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001805}
1806
1807static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001808send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001809{
1810 struct shell_surface *shsurf = get_shell_surface(surface);
1811
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001812 assert(shsurf);
1813
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001814 if (shsurf->resource)
1815 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001816 shsurf->resize_edges,
1817 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001818}
1819
1820static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001821 send_configure,
1822 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001823};
1824
1825static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001826resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001827 uint32_t time, uint32_t button, uint32_t state_w)
1828{
1829 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001830 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001831 enum wl_pointer_button_state state = state_w;
1832
1833 if (pointer->button_count == 0 &&
1834 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1835 shell_grab_end(&resize->base);
1836 free(grab);
1837 }
1838}
1839
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001840static void
1841resize_grab_cancel(struct weston_pointer_grab *grab)
1842{
1843 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1844
1845 shell_grab_end(&resize->base);
1846 free(grab);
1847}
1848
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001849static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001850 noop_grab_focus,
1851 resize_grab_motion,
1852 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001853 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001854 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001855};
1856
Giulio Camuffob8366642013-04-25 13:57:46 +03001857/*
1858 * Returns the bounding box of a surface and all its sub-surfaces,
1859 * in the surface coordinates system. */
1860static void
1861surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1862 int32_t *y, int32_t *w, int32_t *h) {
1863 pixman_region32_t region;
1864 pixman_box32_t *box;
1865 struct weston_subsurface *subsurface;
1866
1867 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 surface->width,
1869 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001870
1871 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1872 pixman_region32_union_rect(&region, &region,
1873 subsurface->position.x,
1874 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875 subsurface->surface->width,
1876 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001877 }
1878
1879 box = pixman_region32_extents(&region);
1880 if (x)
1881 *x = box->x1;
1882 if (y)
1883 *y = box->y1;
1884 if (w)
1885 *w = box->x2 - box->x1;
1886 if (h)
1887 *h = box->y2 - box->y1;
1888
1889 pixman_region32_fini(&region);
1890}
1891
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001892static int
1893surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001894 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001895{
1896 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001897 const unsigned resize_topbottom =
1898 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1899 const unsigned resize_leftright =
1900 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1901 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001902
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001903 if (shsurf->grabbed ||
1904 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001905 return 0;
1906
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001907 /* Check for invalid edge combinations. */
1908 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1909 (edges & resize_topbottom) == resize_topbottom ||
1910 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001911 return 0;
1912
1913 resize = malloc(sizeof *resize);
1914 if (!resize)
1915 return -1;
1916
1917 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001918
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001919 resize->width = shsurf->geometry.width;
1920 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001921
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001922 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001923 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001924 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001925 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001926
1927 return 0;
1928}
1929
1930static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001931common_surface_resize(struct wl_resource *resource,
1932 struct wl_resource *seat_resource, uint32_t serial,
1933 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001934{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001935 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001936 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001937 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001938 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001939
Derek Foreman1281a362015-07-31 16:55:32 -05001940 if (!pointer ||
1941 pointer->button_count == 0 ||
1942 pointer->grab_serial != serial ||
1943 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001944 return;
1945
Derek Foreman1281a362015-07-31 16:55:32 -05001946 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001947 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001948 return;
1949
Derek Foreman1281a362015-07-31 16:55:32 -05001950 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001951 wl_resource_post_no_memory(resource);
1952}
1953
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001954static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001955shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1956 struct wl_resource *seat_resource, uint32_t serial,
1957 uint32_t edges)
1958{
1959 common_surface_resize(resource, seat_resource, serial, edges);
1960}
1961
1962static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001963busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001964{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001965 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001966 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001967 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001968 wl_fixed_t sx, sy;
1969
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970 view = weston_compositor_pick_view(pointer->seat->compositor,
1971 pointer->x, pointer->y,
1972 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001973
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001974 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1975 shell_grab_end(grab);
1976 free(grab);
1977 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001978}
1979
1980static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001981busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001982 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001983{
Jonas Ådahld2510102014-10-05 21:39:14 +02001984 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001985}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001986
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001987static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001988busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001989 uint32_t time, uint32_t button, uint32_t state)
1990{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001991 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001992 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001993 struct weston_pointer *pointer = grab->grab.pointer;
1994 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001995
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001996 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001997 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001998 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001999 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002000 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002001 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002002 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002003}
2004
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002005static void
2006busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2007{
2008 struct shell_grab *grab = (struct shell_grab *) base;
2009
2010 shell_grab_end(grab);
2011 free(grab);
2012}
2013
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002014static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002015 busy_cursor_grab_focus,
2016 busy_cursor_grab_motion,
2017 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002018 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002019 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002020};
2021
2022static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002023set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024{
2025 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002026
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002027 if (pointer->grab->interface == &busy_cursor_grab_interface)
2028 return;
2029
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030 grab = malloc(sizeof *grab);
2031 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002032 return;
2033
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002034 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002035 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002036 /* Mark the shsurf as ungrabbed so that button binding is able
2037 * to move it. */
2038 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002039}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002040
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002042end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002044 struct shell_grab *grab;
2045 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002046
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002047 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002048 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2049
2050 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002051 continue;
2052
Derek Foreman1281a362015-07-31 16:55:32 -05002053 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002054 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002055 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002056 wl_resource_get_client(grab->shsurf->resource) == client) {
2057 shell_grab_end(grab);
2058 free(grab);
2059 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002060 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002061}
2062
Scott Moreau9521d5e2012-04-19 13:06:17 -06002063static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002064handle_shell_client_destroy(struct wl_listener *listener, void *data);
2065
2066static int
2067xdg_ping_timeout_handler(void *data)
2068{
2069 struct shell_client *sc = data;
2070 struct weston_seat *seat;
2071 struct shell_surface *shsurf;
2072
2073 /* Client is not responding */
2074 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002075 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002076 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2077
2078 if (!pointer ||
2079 !pointer->focus ||
2080 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002081 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002082
Derek Foreman1281a362015-07-31 16:55:32 -05002083 shsurf = get_shell_surface(pointer->focus->surface);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002084 if (shsurf &&
2085 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002086 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002087 }
2088
2089 return 1;
2090}
2091
2092static void
2093handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2094{
2095 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002096 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002097 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002098 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002099
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002100 if (sc->unresponsive) {
2101 xdg_ping_timeout_handler(sc);
2102 return;
2103 }
2104
2105 sc->ping_serial = serial;
2106 loop = wl_display_get_event_loop(compositor->wl_display);
2107 if (sc->ping_timer == NULL)
2108 sc->ping_timer =
2109 wl_event_loop_add_timer(loop,
2110 xdg_ping_timeout_handler, sc);
2111 if (sc->ping_timer == NULL)
2112 return;
2113
2114 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2115
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002116 if (shell_surface_is_xdg_surface(shsurf) ||
2117 shell_surface_is_xdg_popup(shsurf))
2118 xdg_shell_send_ping(sc->resource, serial);
2119 else if (shell_surface_is_wl_shell_surface(shsurf))
2120 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002121}
2122
Scott Moreauff1db4a2012-04-17 19:06:18 -06002123static void
2124ping_handler(struct weston_surface *surface, uint32_t serial)
2125{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002126 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002127
2128 if (!shsurf)
2129 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002130 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002131 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002132 if (shsurf->surface == shsurf->shell->grab_surface)
2133 return;
2134
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002135 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002136}
2137
2138static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002139handle_pointer_focus(struct wl_listener *listener, void *data)
2140{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002141 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002143 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002144 uint32_t serial;
2145
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002147 return;
2148
Jason Ekstranda7af7042013-10-12 22:38:11 -05002149 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002150 serial = wl_display_next_serial(compositor->wl_display);
2151 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002152}
2153
2154static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002155shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2156{
2157 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002158 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002159}
2160
2161static void
2162shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2163{
2164 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002165 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002166}
2167
2168static void
2169handle_keyboard_focus(struct wl_listener *listener, void *data)
2170{
2171 struct weston_keyboard *keyboard = data;
2172 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2173
2174 if (seat->focused_surface) {
2175 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2176 if (shsurf)
2177 shell_surface_lose_keyboard_focus(shsurf);
2178 }
2179
2180 seat->focused_surface = keyboard->focus;
2181
2182 if (seat->focused_surface) {
2183 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2184 if (shsurf)
2185 shell_surface_gain_keyboard_focus(shsurf);
2186 }
2187}
2188
2189static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002190shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002191{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002192 if (sc->ping_serial != serial)
2193 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002194
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002195 sc->unresponsive = 0;
2196 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002197
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002198 if (sc->ping_timer) {
2199 wl_event_source_remove(sc->ping_timer);
2200 sc->ping_timer = NULL;
2201 }
2202
2203}
2204
2205static void
2206shell_surface_pong(struct wl_client *client,
2207 struct wl_resource *resource, uint32_t serial)
2208{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002209 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2210 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002211
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002212 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002213}
2214
2215static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002216set_title(struct shell_surface *shsurf, const char *title)
2217{
2218 free(shsurf->title);
2219 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002220 shsurf->surface->timeline.force_refresh = 1;
2221}
2222
2223static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002224set_pid(struct shell_surface *shsurf, pid_t pid)
2225{
2226 /* We have no use for it */
2227}
2228
2229static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002230set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2231{
2232 shsurf->type = t;
2233 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002234}
2235
2236static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002237set_window_geometry(struct shell_surface *shsurf,
2238 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002239{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002240 shsurf->next_geometry.x = x;
2241 shsurf->next_geometry.y = y;
2242 shsurf->next_geometry.width = width;
2243 shsurf->next_geometry.height = height;
2244 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002245}
2246
2247static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002248shell_surface_set_title(struct wl_client *client,
2249 struct wl_resource *resource, const char *title)
2250{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002251 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002252
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002253 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002254}
2255
2256static void
2257shell_surface_set_class(struct wl_client *client,
2258 struct wl_resource *resource, const char *class)
2259{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002260 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002261
2262 free(shsurf->class);
2263 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002264 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002265}
2266
Alex Wu4539b082012-03-01 12:57:46 +08002267static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002268restore_output_mode(struct weston_output *output)
2269{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002270 if (output->original_mode)
2271 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002272}
2273
2274static void
2275restore_all_output_modes(struct weston_compositor *compositor)
2276{
2277 struct weston_output *output;
2278
2279 wl_list_for_each(output, &compositor->output_list, link)
2280 restore_output_mode(output);
2281}
2282
Philip Withnall07926d92013-11-25 18:01:40 +00002283/* The surface will be inserted into the list immediately after the link
2284 * returned by this function (i.e. will be stacked immediately above the
2285 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002286static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002287shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2288{
2289 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002290 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002291
2292 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002293 case SHELL_SURFACE_XWAYLAND:
2294 return &shsurf->shell->fullscreen_layer.view_list;
2295
2296 case SHELL_SURFACE_NONE:
2297 return NULL;
2298
Kristian Høgsbergead52422014-01-01 13:51:52 -08002299 case SHELL_SURFACE_POPUP:
2300 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002301 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002302 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002303 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002304 /* Move the surface to its parent layer so
2305 * that surfaces which are transient for
2306 * fullscreen surfaces don't get hidden by the
2307 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002308
2309 /* TODO: Handle a parent with multiple views */
2310 parent = get_default_view(shsurf->parent);
2311 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002312 return container_of(parent->layer_link.link.prev,
2313 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002314 }
Philip Withnall07926d92013-11-25 18:01:40 +00002315
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002316 /* Move the surface to a normal workspace layer so that surfaces
2317 * which were previously fullscreen or transient are no longer
2318 * rendered on top. */
2319 ws = get_current_workspace(shsurf->shell);
2320 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002321 }
2322
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002323 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002324}
2325
Philip Withnall648a4dd2013-11-25 18:01:44 +00002326static void
2327shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2328{
2329 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002330 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002331
2332 /* Move the child layers to the same workspace as shsurf. They will be
2333 * stacked above shsurf. */
2334 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002335 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002336 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002337 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002338 prev = container_of(shsurf->view->layer_link.link.prev,
2339 struct weston_layer_entry, link);
2340 weston_layer_entry_remove(&child->view->layer_link);
2341 weston_layer_entry_insert(prev,
2342 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002343 weston_view_geometry_dirty(child->view);
2344 weston_surface_damage(child->surface);
2345
2346 /* Recurse. We don’t expect this to recurse very far (if
2347 * at all) because that would imply we have transient
2348 * (or popup) children of transient surfaces, which
2349 * would be unusual. */
2350 shell_surface_update_child_surface_layers(child);
2351 }
2352 }
2353}
2354
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002355/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002356 * geometry to ensure the changes are redrawn.
2357 *
2358 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2359 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002360static void
2361shell_surface_update_layer(struct shell_surface *shsurf)
2362{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002363 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002364
2365 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2366
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002367 if (new_layer_link == NULL)
2368 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002369 if (new_layer_link == &shsurf->view->layer_link)
2370 return;
2371
2372 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002373 weston_layer_entry_remove(&shsurf->view->layer_link);
2374 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002375 weston_view_geometry_dirty(shsurf->view);
2376 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002377
2378 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002379}
2380
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002381static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002382shell_surface_set_parent(struct shell_surface *shsurf,
2383 struct weston_surface *parent)
2384{
2385 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002386
2387 wl_list_remove(&shsurf->children_link);
2388 wl_list_init(&shsurf->children_link);
2389
2390 /* Insert into the parent surface’s child list. */
2391 if (parent != NULL) {
2392 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2393 if (parent_shsurf != NULL)
2394 wl_list_insert(&parent_shsurf->children_list,
2395 &shsurf->children_link);
2396 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002397}
2398
2399static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002400shell_surface_set_output(struct shell_surface *shsurf,
2401 struct weston_output *output)
2402{
2403 struct weston_surface *es = shsurf->surface;
2404
2405 /* get the default output, if the client set it as NULL
2406 check whether the ouput is available */
2407 if (output)
2408 shsurf->output = output;
2409 else if (es->output)
2410 shsurf->output = es->output;
2411 else
2412 shsurf->output = get_default_output(es->compositor);
2413}
2414
2415static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002416surface_clear_next_states(struct shell_surface *shsurf)
2417{
2418 shsurf->next_state.maximized = false;
2419 shsurf->next_state.fullscreen = false;
2420
2421 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2422 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2423 shsurf->state_changed = true;
2424}
2425
2426static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002427set_toplevel(struct shell_surface *shsurf)
2428{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002429 shell_surface_set_parent(shsurf, NULL);
2430 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002431 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002432
2433 /* The layer_link is updated in set_surface_type(),
2434 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002435}
2436
2437static void
2438shell_surface_set_toplevel(struct wl_client *client,
2439 struct wl_resource *resource)
2440{
2441 struct shell_surface *surface = wl_resource_get_user_data(resource);
2442
2443 set_toplevel(surface);
2444}
2445
2446static void
2447set_transient(struct shell_surface *shsurf,
2448 struct weston_surface *parent, int x, int y, uint32_t flags)
2449{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002450 assert(parent != NULL);
2451
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002452 shell_surface_set_parent(shsurf, parent);
2453
2454 surface_clear_next_states(shsurf);
2455
Philip Withnallbecb77e2013-11-25 18:01:30 +00002456 shsurf->transient.x = x;
2457 shsurf->transient.y = y;
2458 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002459
Rafael Antognollied207b42013-12-03 15:35:43 -02002460 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002461 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002462 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002463
2464 /* The layer_link is updated in set_surface_type(),
2465 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466}
2467
2468static void
2469shell_surface_set_transient(struct wl_client *client,
2470 struct wl_resource *resource,
2471 struct wl_resource *parent_resource,
2472 int x, int y, uint32_t flags)
2473{
2474 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2475 struct weston_surface *parent =
2476 wl_resource_get_user_data(parent_resource);
2477
2478 set_transient(shsurf, parent, x, y, flags);
2479}
2480
2481static void
2482set_fullscreen(struct shell_surface *shsurf,
2483 uint32_t method,
2484 uint32_t framerate,
2485 struct weston_output *output)
2486{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002487 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002488 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002489
2490 shsurf->fullscreen_output = shsurf->output;
2491 shsurf->fullscreen.type = method;
2492 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002493
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002494 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495}
2496
2497static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002498weston_view_set_initial_position(struct weston_view *view,
2499 struct desktop_shell *shell);
2500
2501static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002502unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002503{
Philip Withnallf85fe842013-11-25 18:01:37 +00002504 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002505 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2506 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002507 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002508 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002509
Alex Wu4539b082012-03-01 12:57:46 +08002510 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2511 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002512
Alex Wu4539b082012-03-01 12:57:46 +08002513 wl_list_remove(&shsurf->fullscreen.transform.link);
2514 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002515
Jason Ekstranda7af7042013-10-12 22:38:11 -05002516 if (shsurf->fullscreen.black_view)
2517 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2518 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002519
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002520 if (shsurf->saved_position_valid)
2521 weston_view_set_position(shsurf->view,
2522 shsurf->saved_x, shsurf->saved_y);
2523 else
2524 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2525
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002526 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002528 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002529 shsurf->saved_rotation_valid = false;
2530 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002531
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002532 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002533}
2534
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002535static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002536shell_surface_set_fullscreen(struct wl_client *client,
2537 struct wl_resource *resource,
2538 uint32_t method,
2539 uint32_t framerate,
2540 struct wl_resource *output_resource)
2541{
2542 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2543 struct weston_output *output;
2544
2545 if (output_resource)
2546 output = wl_resource_get_user_data(output_resource);
2547 else
2548 output = NULL;
2549
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002550 shell_surface_set_parent(shsurf, NULL);
2551
Rafael Antognolli03b16592013-12-03 15:35:42 -02002552 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002553 shsurf->next_state.fullscreen = true;
2554 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002555 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002556}
2557
2558static void
2559set_popup(struct shell_surface *shsurf,
2560 struct weston_surface *parent,
2561 struct weston_seat *seat,
2562 uint32_t serial,
2563 int32_t x,
2564 int32_t y)
2565{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002566 assert(parent != NULL);
2567
Philip Withnallbecb77e2013-11-25 18:01:30 +00002568 shsurf->popup.shseat = get_shell_seat(seat);
2569 shsurf->popup.serial = serial;
2570 shsurf->popup.x = x;
2571 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002572
Pekka Paalanenb5026542014-11-12 15:09:24 +02002573 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574}
2575
2576static void
2577shell_surface_set_popup(struct wl_client *client,
2578 struct wl_resource *resource,
2579 struct wl_resource *seat_resource,
2580 uint32_t serial,
2581 struct wl_resource *parent_resource,
2582 int32_t x, int32_t y, uint32_t flags)
2583{
2584 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002585 struct weston_surface *parent =
2586 wl_resource_get_user_data(parent_resource);
2587
2588 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002589
Rafael Antognolli03b16592013-12-03 15:35:42 -02002590 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002591 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002592 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002593 wl_resource_get_user_data(seat_resource),
2594 serial, x, y);
2595}
2596
2597static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002598unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002599{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002600 /* undo all maximized things here */
2601 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002602
2603 if (shsurf->saved_position_valid)
2604 weston_view_set_position(shsurf->view,
2605 shsurf->saved_x, shsurf->saved_y);
2606 else
2607 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002608
2609 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610 wl_list_insert(&shsurf->view->geometry.transformation_list,
2611 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002612 shsurf->saved_rotation_valid = false;
2613 }
2614
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002615 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002616}
2617
Philip Withnallbecb77e2013-11-25 18:01:30 +00002618static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002619set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002620{
2621 struct shell_surface *shsurf;
2622 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002623 struct weston_view *view;
2624
2625 view = get_default_view(surface);
2626 if (!view)
2627 return;
2628
2629 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2630
2631 shsurf = get_shell_surface(surface);
2632 current_ws = get_current_workspace(shsurf->shell);
2633
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002634 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002635 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002636
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002637 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002638 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002639
2640 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002641 weston_view_damage_below(view);
2642}
2643
2644static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002645shell_surface_set_maximized(struct wl_client *client,
2646 struct wl_resource *resource,
2647 struct wl_resource *output_resource)
2648{
2649 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2650 struct weston_output *output;
2651
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002652 surface_clear_next_states(shsurf);
2653 shsurf->next_state.maximized = true;
2654 shsurf->state_changed = true;
2655
Pekka Paalanenb5026542014-11-12 15:09:24 +02002656 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002657 shell_surface_set_parent(shsurf, NULL);
2658
Philip Withnallbecb77e2013-11-25 18:01:30 +00002659 if (output_resource)
2660 output = wl_resource_get_user_data(output_resource);
2661 else
2662 output = NULL;
2663
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002664 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002665
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002666 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002667}
2668
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002669/* This is only ever called from set_surface_type(), so there’s no need to
2670 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002671static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002672reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002673{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002674 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002675 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002676 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002678
Pekka Paalanen98262232011-12-01 10:42:22 +02002679 return 0;
2680}
2681
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002682static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002683set_full_output(struct shell_surface *shsurf)
2684{
2685 shsurf->saved_x = shsurf->view->geometry.x;
2686 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002687 shsurf->saved_width = shsurf->surface->width;
2688 shsurf->saved_height = shsurf->surface->height;
2689 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002690 shsurf->saved_position_valid = true;
2691
2692 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2693 wl_list_remove(&shsurf->rotation.transform.link);
2694 wl_list_init(&shsurf->rotation.transform.link);
2695 weston_view_geometry_dirty(shsurf->view);
2696 shsurf->saved_rotation_valid = true;
2697 }
2698}
2699
2700static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002701set_surface_type(struct shell_surface *shsurf)
2702{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002703 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002705
Philip Withnallbecb77e2013-11-25 18:01:30 +00002706 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002707
Rafael Antognolli03b16592013-12-03 15:35:42 -02002708 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002709 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002710
2711 switch (shsurf->type) {
2712 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002713 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002714 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002715 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002716 weston_view_set_position(shsurf->view,
2717 pev->geometry.x + shsurf->transient.x,
2718 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002719 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002720 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002721
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002722 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002723 weston_view_set_position(shsurf->view, shsurf->transient.x,
2724 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002725 break;
2726
Philip Withnall0f640e22013-11-25 18:01:31 +00002727 case SHELL_SURFACE_POPUP:
2728 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002729 default:
2730 break;
2731 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002732
2733 /* Update the surface’s layer. */
2734 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002735}
2736
Tiago Vignattibe143262012-04-16 17:31:41 +03002737static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002738shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002739{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002740 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002741}
2742
Pekka Paalanen8274d902014-08-06 19:36:51 +03002743static int
2744black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2745{
2746 struct weston_surface *fs_surface = surface->configure_private;
2747 int n;
2748 int rem;
2749 int ret;
2750
2751 n = snprintf(buf, len, "black background surface for ");
2752 if (n < 0)
2753 return n;
2754
2755 rem = (int)len - n;
2756 if (rem < 0)
2757 rem = 0;
2758
2759 if (fs_surface->get_label)
2760 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2761 else
2762 ret = snprintf(buf + n, rem, "<unknown>");
2763
2764 if (ret < 0)
2765 return n;
2766
2767 return n + ret;
2768}
2769
Alex Wu21858432012-04-01 20:13:08 +08002770static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002771black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002772
Jason Ekstranda7af7042013-10-12 22:38:11 -05002773static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002774create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002775 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002776 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002777{
2778 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002779 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002780
2781 surface = weston_surface_create(ec);
2782 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002783 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002784 return NULL;
2785 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 view = weston_view_create(surface);
2787 if (surface == NULL) {
2788 weston_log("no memory\n");
2789 weston_surface_destroy(surface);
2790 return NULL;
2791 }
Alex Wu4539b082012-03-01 12:57:46 +08002792
Alex Wu21858432012-04-01 20:13:08 +08002793 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002794 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002795 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002796 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002797 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002798 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002799 pixman_region32_fini(&surface->input);
2800 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002801
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002802 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002803 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002804
2805 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002806}
2807
Philip Withnalled8f1a92013-11-25 18:01:43 +00002808static void
2809shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2810{
2811 struct weston_output *output = shsurf->fullscreen_output;
2812
Rafael Antognolli03b16592013-12-03 15:35:42 -02002813 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002814
2815 if (!shsurf->fullscreen.black_view)
2816 shsurf->fullscreen.black_view =
2817 create_black_surface(shsurf->surface->compositor,
2818 shsurf->surface,
2819 output->x, output->y,
2820 output->width,
2821 output->height);
2822
2823 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002824 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2825 weston_layer_entry_insert(&shsurf->view->layer_link,
2826 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002827 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2828 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002829
2830 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002831}
2832
Alex Wu4539b082012-03-01 12:57:46 +08002833/* Create black surface and append it to the associated fullscreen surface.
2834 * Handle size dismatch and positioning according to the method. */
2835static void
2836shell_configure_fullscreen(struct shell_surface *shsurf)
2837{
2838 struct weston_output *output = shsurf->fullscreen_output;
2839 struct weston_surface *surface = shsurf->surface;
2840 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002841 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002842 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002843
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002844 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2845 restore_output_mode(output);
2846
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002847 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002848 weston_layer_entry_remove(&shsurf->view->layer_link);
2849 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002850
Philip Withnalled8f1a92013-11-25 18:01:43 +00002851 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002852
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002854 &surf_width, &surf_height);
2855
Alex Wu4539b082012-03-01 12:57:46 +08002856 switch (shsurf->fullscreen.type) {
2857 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002858 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002859 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002860 break;
2861 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002862 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002863 if (output->width == surf_width &&
2864 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002865 weston_view_set_position(shsurf->view,
2866 output->x - surf_x,
2867 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002868 break;
2869 }
2870
Alex Wu4539b082012-03-01 12:57:46 +08002871 matrix = &shsurf->fullscreen.transform.matrix;
2872 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002873
Scott Moreau1bad5db2012-08-18 01:04:05 -06002874 output_aspect = (float) output->width /
2875 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876 /* XXX: Use surf_width and surf_height here? */
2877 surface_aspect = (float) surface->width /
2878 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002879 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002880 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002881 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002882 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002883 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002884 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002885
Alex Wu4539b082012-03-01 12:57:46 +08002886 weston_matrix_scale(matrix, scale, scale, 1);
2887 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002888 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002889 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002890 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2891 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002893
Alex Wu4539b082012-03-01 12:57:46 +08002894 break;
2895 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002896 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002897 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002898 surf_width * surface->buffer_viewport.buffer.scale,
2899 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002900 shsurf->fullscreen.framerate};
2901
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002902 if (weston_output_mode_switch_to_temporary(output, &mode,
2903 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002904 weston_view_set_position(shsurf->view,
2905 output->x - surf_x,
2906 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002907 shsurf->fullscreen.black_view->surface->width = output->width;
2908 shsurf->fullscreen.black_view->surface->height = output->height;
2909 weston_view_set_position(shsurf->fullscreen.black_view,
2910 output->x - surf_x,
2911 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002912 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002913 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002914 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002915 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002916 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002917 }
Alex Wubd3354b2012-04-17 17:20:49 +08002918 }
Alex Wu4539b082012-03-01 12:57:46 +08002919 break;
2920 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002921 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002922 break;
2923 default:
2924 break;
2925 }
2926}
2927
Alex Wu4539b082012-03-01 12:57:46 +08002928static void
2929shell_map_fullscreen(struct shell_surface *shsurf)
2930{
Alex Wubd3354b2012-04-17 17:20:49 +08002931 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002932}
2933
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002934static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002935set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2936{
2937 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002938 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002939 shsurf->transient.x = x;
2940 shsurf->transient.y = y;
2941 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002942
2943 shell_surface_set_parent(shsurf, NULL);
2944
Pekka Paalanenb5026542014-11-12 15:09:24 +02002945 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002946 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002947}
2948
Kristian Høgsberg47792412014-05-04 13:47:06 -07002949static void
2950shell_interface_set_fullscreen(struct shell_surface *shsurf,
2951 uint32_t method,
2952 uint32_t framerate,
2953 struct weston_output *output)
2954{
2955 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002956 shsurf->next_state.fullscreen = true;
2957 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002958
2959 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002960}
2961
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002962static struct weston_output *
2963get_focused_output(struct weston_compositor *compositor)
2964{
2965 struct weston_seat *seat;
2966 struct weston_output *output = NULL;
2967
2968 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002969 struct weston_touch *touch = weston_seat_get_touch(seat);
2970 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2971 struct weston_keyboard *keyboard =
2972 weston_seat_get_keyboard(seat);
2973
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002974 /* Priority has touch focus, then pointer and
2975 * then keyboard focus. We should probably have
2976 * three for loops and check frist for touch,
2977 * then for pointer, etc. but unless somebody has some
2978 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002979 if (touch && touch->focus)
2980 output = touch->focus->output;
2981 else if (pointer && pointer->focus)
2982 output = pointer->focus->output;
2983 else if (keyboard && keyboard->focus)
2984 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002985
2986 if (output)
2987 break;
2988 }
2989
2990 return output;
2991}
2992
2993static void
2994shell_interface_set_maximized(struct shell_surface *shsurf)
2995{
2996 struct weston_output *output;
2997
2998 surface_clear_next_states(shsurf);
2999 shsurf->next_state.maximized = true;
3000 shsurf->state_changed = true;
3001 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3002
3003 if (!weston_surface_is_mapped(shsurf->surface))
3004 output = get_focused_output(shsurf->surface->compositor);
3005 else
3006 output = shsurf->surface->output;
3007
3008 shell_surface_set_output(shsurf, output);
3009 send_configure_for_surface(shsurf);
3010}
3011
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003012static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003013shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003014{
Derek Foremane4d6c832015-08-05 14:48:11 -05003015 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003016}
3017
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003018static int
3019shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003020 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003021 uint32_t edges)
3022{
Derek Foremane4d6c832015-08-05 14:48:11 -05003023 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003024}
3025
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003026static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003027
3028static void
3029destroy_shell_seat(struct wl_listener *listener, void *data)
3030{
3031 struct shell_seat *shseat =
3032 container_of(listener,
3033 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003034 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003035
3036 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003037 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003038 shseat->popup_grab.client = NULL;
3039
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003040 wl_list_for_each_safe(shsurf, next,
3041 &shseat->popup_grab.surfaces_list,
3042 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003043 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003044 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003045 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003046 }
3047
3048 wl_list_remove(&shseat->seat_destroy_listener.link);
3049 free(shseat);
3050}
3051
Jason Ekstrand024177c2014-04-21 19:42:58 -05003052static void
3053shell_seat_caps_changed(struct wl_listener *listener, void *data)
3054{
Derek Foreman1281a362015-07-31 16:55:32 -05003055 struct weston_keyboard *keyboard;
3056 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003057 struct shell_seat *seat;
3058
3059 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003060 keyboard = weston_seat_get_keyboard(seat->seat);
3061 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003062
Derek Foreman1281a362015-07-31 16:55:32 -05003063 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003064 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003065 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003066 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003067 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003068 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003069 wl_list_init(&seat->keyboard_focus_listener.link);
3070 }
3071
Derek Foreman1281a362015-07-31 16:55:32 -05003072 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003073 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003074 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003075 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003076 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003077 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003078 wl_list_init(&seat->pointer_focus_listener.link);
3079 }
3080}
3081
Giulio Camuffo5085a752013-03-25 21:42:45 +01003082static struct shell_seat *
3083create_shell_seat(struct weston_seat *seat)
3084{
3085 struct shell_seat *shseat;
3086
3087 shseat = calloc(1, sizeof *shseat);
3088 if (!shseat) {
3089 weston_log("no memory to allocate shell seat\n");
3090 return NULL;
3091 }
3092
3093 shseat->seat = seat;
3094 wl_list_init(&shseat->popup_grab.surfaces_list);
3095
3096 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3097 wl_signal_add(&seat->destroy_signal,
3098 &shseat->seat_destroy_listener);
3099
Jason Ekstrand024177c2014-04-21 19:42:58 -05003100 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3101 wl_list_init(&shseat->keyboard_focus_listener.link);
3102
3103 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3104 wl_list_init(&shseat->pointer_focus_listener.link);
3105
3106 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3107 wl_signal_add(&seat->updated_caps_signal,
3108 &shseat->caps_changed_listener);
3109 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3110
Giulio Camuffo5085a752013-03-25 21:42:45 +01003111 return shseat;
3112}
3113
3114static struct shell_seat *
3115get_shell_seat(struct weston_seat *seat)
3116{
3117 struct wl_listener *listener;
3118
3119 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003120 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003121
3122 return container_of(listener,
3123 struct shell_seat, seat_destroy_listener);
3124}
3125
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003126static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003127popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003128{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003129 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003130 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003131 struct shell_seat *shseat =
3132 container_of(grab, struct shell_seat, popup_grab.grab);
3133 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003134 wl_fixed_t sx, sy;
3135
Jason Ekstranda7af7042013-10-12 22:38:11 -05003136 view = weston_compositor_pick_view(pointer->seat->compositor,
3137 pointer->x, pointer->y,
3138 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003139
Jason Ekstranda7af7042013-10-12 22:38:11 -05003140 if (view && view->surface->resource &&
3141 wl_resource_get_client(view->surface->resource) == client) {
3142 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003143 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003144 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003145 }
3146}
3147
3148static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003149popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003150 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003151{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003152 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003153 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003154 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003155 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003156 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003157
Jonas Ådahl61917c82014-08-11 22:44:02 +02003158 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003159 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003160 weston_view_from_global_fixed(pointer->focus, x, y,
3161 &pointer->sx, &pointer->sy);
3162 }
3163
Jonas Ådahld2510102014-10-05 21:39:14 +02003164 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003165
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003166 if (!pointer->focus_client)
3167 return;
3168
3169 resource_list = &pointer->focus_client->pointer_resources;
3170 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003171 weston_view_from_global_fixed(pointer->focus,
3172 pointer->x, pointer->y,
3173 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003174 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003175 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003176}
3177
3178static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003179popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003180 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181{
3182 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003183 struct shell_seat *shseat =
3184 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003185 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003186 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003187 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003188 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003189
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003190 if (grab->pointer->focus_client)
3191 resource_list = &grab->pointer->focus_client->pointer_resources;
3192 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003193 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003194 wl_resource_for_each(resource, resource_list) {
3195 wl_pointer_send_button(resource, serial,
3196 time, button, state);
3197 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003198 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003199 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003200 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003201 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003202 }
3203
Daniel Stone4dbadb12012-05-30 16:31:51 +01003204 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003205 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003206}
3207
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003208static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003209popup_grab_axis(struct weston_pointer_grab *grab,
3210 uint32_t time, uint32_t axis, wl_fixed_t value)
3211{
Peter Hutterer11189522015-11-16 12:35:57 +10003212 weston_pointer_send_axis(grab->pointer, time, axis, value);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003213}
3214
3215static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003216popup_grab_cancel(struct weston_pointer_grab *grab)
3217{
3218 popup_grab_end(grab->pointer);
3219}
3220
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003221static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003222 popup_grab_focus,
3223 popup_grab_motion,
3224 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003225 popup_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003226 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003227};
3228
3229static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003230touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003231 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003232{
3233 struct wl_resource *resource;
3234 struct shell_seat *shseat =
3235 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3236 struct wl_display *display = shseat->seat->compositor->wl_display;
3237 uint32_t serial;
3238 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003239 wl_fixed_t sx, sy;
3240
3241 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003242
3243 resource_list = &grab->touch->focus_resource_list;
3244 if (!wl_list_empty(resource_list)) {
3245 serial = wl_display_get_serial(display);
3246 wl_resource_for_each(resource, resource_list) {
3247 wl_touch_send_down(resource, serial, time,
3248 grab->touch->focus->surface->resource,
3249 touch_id, sx, sy);
3250 }
3251 }
3252}
3253
3254static void
3255touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3256{
3257 struct wl_resource *resource;
3258 struct shell_seat *shseat =
3259 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3260 struct wl_display *display = shseat->seat->compositor->wl_display;
3261 uint32_t serial;
3262 struct wl_list *resource_list;
3263
3264 resource_list = &grab->touch->focus_resource_list;
3265 if (!wl_list_empty(resource_list)) {
3266 serial = wl_display_get_serial(display);
3267 wl_resource_for_each(resource, resource_list) {
3268 wl_touch_send_up(resource, serial, time, touch_id);
3269 }
3270 }
3271}
3272
3273static void
3274touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003275 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003276{
3277 struct wl_resource *resource;
3278 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003279 wl_fixed_t sx, sy;
3280
3281 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003282
3283 resource_list = &grab->touch->focus_resource_list;
3284 if (!wl_list_empty(resource_list)) {
3285 wl_resource_for_each(resource, resource_list) {
3286 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3287 }
3288 }
3289}
3290
3291static void
3292touch_popup_grab_frame(struct weston_touch_grab *grab)
3293{
3294}
3295
3296static void
3297touch_popup_grab_cancel(struct weston_touch_grab *grab)
3298{
3299 touch_popup_grab_end(grab->touch);
3300}
3301
3302static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3303 touch_popup_grab_down,
3304 touch_popup_grab_up,
3305 touch_popup_grab_motion,
3306 touch_popup_grab_frame,
3307 touch_popup_grab_cancel,
3308};
3309
3310static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003311shell_surface_send_popup_done(struct shell_surface *shsurf)
3312{
3313 if (shell_surface_is_wl_shell_surface(shsurf))
3314 wl_shell_surface_send_popup_done(shsurf->resource);
3315 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003316 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003317}
3318
3319static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003320popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003321{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003322 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003323 struct shell_seat *shseat =
3324 container_of(grab, struct shell_seat, popup_grab.grab);
3325 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003326 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003327
3328 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003329 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003330 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003331 shseat->popup_grab.grab.interface = NULL;
3332 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003333 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003334 wl_list_for_each_safe(shsurf, next,
3335 &shseat->popup_grab.surfaces_list,
3336 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003337 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003338 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003339 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003340 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003341 wl_list_init(&shseat->popup_grab.surfaces_list);
3342 }
3343}
3344
3345static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003346touch_popup_grab_end(struct weston_touch *touch)
3347{
3348 struct weston_touch_grab *grab = touch->grab;
3349 struct shell_seat *shseat =
3350 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3351 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003352 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003353
3354 if (touch->grab->interface == &touch_popup_grab_interface) {
3355 weston_touch_end_grab(grab->touch);
3356 shseat->popup_grab.client = NULL;
3357 shseat->popup_grab.touch_grab.interface = NULL;
3358 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3359 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003360 wl_list_for_each_safe(shsurf, next,
3361 &shseat->popup_grab.surfaces_list,
3362 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003363 shell_surface_send_popup_done(shsurf);
3364 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003365 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003366 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003367 wl_list_init(&shseat->popup_grab.surfaces_list);
3368 }
3369}
3370
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003371static struct shell_surface *
3372get_top_popup(struct shell_seat *shseat)
3373{
3374 struct shell_surface *shsurf;
3375
3376 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3377 return NULL;
3378 } else {
3379 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3380 struct shell_surface,
3381 popup.grab_link);
3382 return shsurf;
3383 }
3384}
3385
3386static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003387add_popup_grab(struct shell_surface *shsurf,
3388 struct shell_seat *shseat,
3389 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003390{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003391 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003392 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003393 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3394 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003395
3396 parent = get_shell_surface(shsurf->parent);
3397 top_surface = get_top_popup(shseat);
3398 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003399 (!parent ||
3400 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003401 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003402 wl_resource_post_error(shsurf->owner_resource,
3403 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003404 "xdg_popup was not created on the "
3405 "topmost popup");
3406 return -1;
3407 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003408
3409 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003410 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003411 shseat->popup_grab.client =
3412 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003413
3414 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003415 shseat->popup_grab.grab.interface =
3416 &popup_grab_interface;
3417
3418 /* We must make sure here that this popup was opened
3419 * after a mouse press, and not just by moving around
3420 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003421 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003422 shseat->popup_grab.initial_up = 0;
3423 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003424 shseat->popup_grab.touch_grab.interface =
3425 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003426 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003427
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003428 wl_list_insert(&shseat->popup_grab.surfaces_list,
3429 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003430
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003431 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003432 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003433 &shseat->popup_grab.grab);
3434 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003435 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003436 &shseat->popup_grab.touch_grab);
3437 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003438 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003439 wl_list_insert(&shseat->popup_grab.surfaces_list,
3440 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003441 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003442
3443 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003444}
3445
3446static void
3447remove_popup_grab(struct shell_surface *shsurf)
3448{
3449 struct shell_seat *shseat = shsurf->popup.shseat;
3450
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003451 if (shell_surface_is_xdg_popup(shsurf) &&
3452 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003453 wl_resource_post_error(shsurf->owner_resource,
3454 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003455 "xdg_popup was destroyed while it was "
3456 "not the topmost popup.");
3457 return;
3458 }
3459
Giulio Camuffo5085a752013-03-25 21:42:45 +01003460 wl_list_remove(&shsurf->popup.grab_link);
3461 wl_list_init(&shsurf->popup.grab_link);
3462 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003463 if (shseat->popup_grab.type == POINTER) {
3464 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3465 shseat->popup_grab.grab.interface = NULL;
3466 } else if (shseat->popup_grab.type == TOUCH) {
3467 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3468 shseat->popup_grab.touch_grab.interface = NULL;
3469 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003470 }
3471}
3472
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003473static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003474shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003475{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003476 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003477 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003478 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3479 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003480
Jason Ekstranda7af7042013-10-12 22:38:11 -05003481 shsurf->surface->output = parent_view->output;
3482 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003483
Jason Ekstranda7af7042013-10-12 22:38:11 -05003484 weston_view_set_transform_parent(shsurf->view, parent_view);
3485 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3486 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003487
Derek Foreman1281a362015-07-31 16:55:32 -05003488 if (pointer &&
3489 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003490 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3491 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003492 } else if (touch &&
3493 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003494 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3495 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003496 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003497 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003498 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003499 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003500
3501 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003502}
3503
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003504static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003505 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003506 shell_surface_move,
3507 shell_surface_resize,
3508 shell_surface_set_toplevel,
3509 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003510 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003511 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003512 shell_surface_set_maximized,
3513 shell_surface_set_title,
3514 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003515};
3516
3517static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003518destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003519{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003520 struct shell_surface *child, *next;
3521
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003522 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3523
Giulio Camuffo5085a752013-03-25 21:42:45 +01003524 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3525 remove_popup_grab(shsurf);
3526 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003527
Alex Wubd3354b2012-04-17 17:20:49 +08003528 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003529 shell_surface_is_top_fullscreen(shsurf))
3530 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003531
Jason Ekstranda7af7042013-10-12 22:38:11 -05003532 if (shsurf->fullscreen.black_view)
3533 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003534
Alex Wubd3354b2012-04-17 17:20:49 +08003535 /* As destroy_resource() use wl_list_for_each_safe(),
3536 * we can always remove the listener.
3537 */
3538 wl_list_remove(&shsurf->surface_destroy_listener.link);
3539 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003540 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003541 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003542
Jason Ekstranda7af7042013-10-12 22:38:11 -05003543 weston_view_destroy(shsurf->view);
3544
Philip Withnall648a4dd2013-11-25 18:01:44 +00003545 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003546 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3547 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003548
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003549 wl_list_remove(&shsurf->link);
3550 free(shsurf);
3551}
3552
3553static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003554shell_destroy_shell_surface(struct wl_resource *resource)
3555{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003556 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003557
Bryan Caina46b9462014-04-04 17:41:24 -05003558 if (!wl_list_empty(&shsurf->popup.grab_link))
3559 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003560 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003561 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003562}
3563
3564static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003565shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003566{
3567 struct shell_surface *shsurf = container_of(listener,
3568 struct shell_surface,
3569 surface_destroy_listener);
3570
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003571 if (shsurf->resource)
3572 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003573
3574 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003575}
3576
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003577static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003578fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003579{
3580 struct shell_surface *shsurf = data;
3581
3582 weston_surface_destroy(shsurf->surface);
3583}
3584
3585static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003586fade_out_done(struct weston_view_animation *animation, void *data)
3587{
3588 struct shell_surface *shsurf = data;
3589 struct wl_event_loop *loop;
3590
3591 loop = wl_display_get_event_loop(
3592 shsurf->surface->compositor->wl_display);
3593
3594 if (!shsurf->destroying) {
3595 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3596 shsurf->destroying = true;
3597 }
3598}
3599
3600static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003601handle_resource_destroy(struct wl_listener *listener, void *data)
3602{
3603 struct shell_surface *shsurf =
3604 container_of(listener, struct shell_surface,
3605 resource_destroy_listener);
3606
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003607 if (!weston_surface_is_mapped(shsurf->surface))
3608 return;
3609
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003610 shsurf->surface->ref_count++;
3611
3612 pixman_region32_fini(&shsurf->surface->pending.input);
3613 pixman_region32_init(&shsurf->surface->pending.input);
3614 pixman_region32_fini(&shsurf->surface->input);
3615 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003616 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3617 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3618 fade_out_done, shsurf);
3619 } else {
3620 weston_surface_destroy(shsurf->surface);
3621 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003622}
3623
3624static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003625shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003626
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003627struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003628get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003629{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003630 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003631 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003632 else
3633 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003634}
3635
Rafael Antognollie2a34552013-12-03 15:35:45 -02003636static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003637create_common_surface(struct shell_client *owner, void *shell,
3638 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003639 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003640{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003641 struct shell_surface *shsurf;
3642
Pekka Paalanen50b67472014-10-01 15:02:41 +03003643 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003644
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003645 shsurf = calloc(1, sizeof *shsurf);
3646 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003647 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003648 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003649 }
3650
Jason Ekstranda7af7042013-10-12 22:38:11 -05003651 shsurf->view = weston_view_create(surface);
3652 if (!shsurf->view) {
3653 weston_log("no memory to allocate shell surface\n");
3654 free(shsurf);
3655 return NULL;
3656 }
3657
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003658 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003659 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003660 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003661
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003662 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3663 wl_resource_add_destroy_listener(surface->resource,
3664 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003665 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003666
Tiago Vignattibc052c92012-04-19 16:18:18 +03003667 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003668 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003669 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003670 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003671 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003672 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003673 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3674 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003675 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003676 wl_list_init(&shsurf->fullscreen.transform.link);
3677
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003678 shsurf->output = get_default_output(shsurf->shell->compositor);
3679
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003680 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003681 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003682 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003683 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003684
3685 /* init link so its safe to always remove it in destroy_shell_surface */
3686 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003687 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003688
Pekka Paalanen460099f2012-01-20 16:48:25 +02003689 /* empty when not in use */
3690 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003691 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003692
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003693 wl_list_init(&shsurf->workspace_transform.link);
3694
Philip Withnall648a4dd2013-11-25 18:01:44 +00003695 wl_list_init(&shsurf->children_link);
3696 wl_list_init(&shsurf->children_list);
3697 shsurf->parent = NULL;
3698
Pekka Paalanenb5026542014-11-12 15:09:24 +02003699 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003700
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003701 shsurf->client = client;
3702
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003703 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003704}
3705
Rafael Antognollie2a34552013-12-03 15:35:45 -02003706static struct shell_surface *
3707create_shell_surface(void *shell, struct weston_surface *surface,
3708 const struct weston_shell_client *client)
3709{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003710 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003711}
3712
Tiago Vignattibc052c92012-04-19 16:18:18 +03003713static void
3714shell_get_shell_surface(struct wl_client *client,
3715 struct wl_resource *resource,
3716 uint32_t id,
3717 struct wl_resource *surface_resource)
3718{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003719 struct weston_surface *surface =
3720 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003721 struct shell_client *sc = wl_resource_get_user_data(resource);
3722 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003723 struct shell_surface *shsurf;
3724
Pekka Paalanen50b67472014-10-01 15:02:41 +03003725 if (weston_surface_set_role(surface, "wl_shell_surface",
3726 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003727 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003728
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003729 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003730 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003731 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003732 return;
3733 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003734
Jason Ekstranda85118c2013-06-27 20:17:02 -05003735 shsurf->resource =
3736 wl_resource_create(client,
3737 &wl_shell_surface_interface, 1, id);
3738 wl_resource_set_implementation(shsurf->resource,
3739 &shell_surface_implementation,
3740 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003741 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003742}
3743
Rafael Antognollie2a34552013-12-03 15:35:45 -02003744static bool
3745shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3746{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003747 /* A shell surface without a resource is created from xwayland
3748 * and is considered a wl_shell surface for now. */
3749
3750 return shsurf->resource == NULL ||
3751 wl_resource_instance_of(shsurf->resource,
3752 &wl_shell_surface_interface,
3753 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003754}
3755
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003756static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003757 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003758};
3759
Rafael Antognollie2a34552013-12-03 15:35:45 -02003760/****************************
3761 * xdg-shell implementation */
3762
3763static void
3764xdg_surface_destroy(struct wl_client *client,
3765 struct wl_resource *resource)
3766{
3767 wl_resource_destroy(resource);
3768}
3769
3770static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003771xdg_surface_set_parent(struct wl_client *client,
3772 struct wl_resource *resource,
3773 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003774{
3775 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003776 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003777
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003778 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003779 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003780 shell_surface_set_parent(shsurf, parent->surface);
3781 } else {
3782 shell_surface_set_parent(shsurf, NULL);
3783 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003784}
3785
3786static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003787xdg_surface_set_app_id(struct wl_client *client,
3788 struct wl_resource *resource,
3789 const char *app_id)
3790{
3791 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3792
3793 free(shsurf->class);
3794 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003795 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003796}
3797
3798static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003799xdg_surface_show_window_menu(struct wl_client *client,
3800 struct wl_resource *surface_resource,
3801 struct wl_resource *seat_resource,
3802 uint32_t serial,
3803 int32_t x,
3804 int32_t y)
3805{
3806 /* TODO */
3807}
3808
3809static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003810xdg_surface_set_title(struct wl_client *client,
3811 struct wl_resource *resource, const char *title)
3812{
3813 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3814
3815 set_title(shsurf, title);
3816}
3817
3818static void
3819xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3820 struct wl_resource *seat_resource, uint32_t serial)
3821{
3822 common_surface_move(resource, seat_resource, serial);
3823}
3824
3825static void
3826xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3827 struct wl_resource *seat_resource, uint32_t serial,
3828 uint32_t edges)
3829{
3830 common_surface_resize(resource, seat_resource, serial, edges);
3831}
3832
3833static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003834xdg_surface_ack_configure(struct wl_client *client,
3835 struct wl_resource *resource,
3836 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003837{
3838 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3839
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003840 if (shsurf->state_requested) {
3841 shsurf->next_state = shsurf->requested_state;
3842 shsurf->state_changed = true;
3843 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003844 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003845}
3846
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003847static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003848xdg_surface_set_window_geometry(struct wl_client *client,
3849 struct wl_resource *resource,
3850 int32_t x,
3851 int32_t y,
3852 int32_t width,
3853 int32_t height)
3854{
3855 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3856
3857 set_window_geometry(shsurf, x, y, width, height);
3858}
3859
3860static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003861xdg_surface_set_maximized(struct wl_client *client,
3862 struct wl_resource *resource)
3863{
3864 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003865 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003866
3867 shsurf->state_requested = true;
3868 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003869
3870 if (!weston_surface_is_mapped(shsurf->surface))
3871 output = get_focused_output(shsurf->surface->compositor);
3872 else
3873 output = shsurf->surface->output;
3874
3875 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003876 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003877}
3878
3879static void
3880xdg_surface_unset_maximized(struct wl_client *client,
3881 struct wl_resource *resource)
3882{
3883 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3884
3885 shsurf->state_requested = true;
3886 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003887 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003888}
3889
3890static void
3891xdg_surface_set_fullscreen(struct wl_client *client,
3892 struct wl_resource *resource,
3893 struct wl_resource *output_resource)
3894{
3895 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3896 struct weston_output *output;
3897
3898 shsurf->state_requested = true;
3899 shsurf->requested_state.fullscreen = true;
3900
3901 if (output_resource)
3902 output = wl_resource_get_user_data(output_resource);
3903 else
3904 output = NULL;
3905
Marek Chalupa052917a2014-09-02 11:35:12 +02003906 /* handle clients launching in fullscreen */
3907 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3908 /* Set the output to the one that has focus currently. */
3909 assert(shsurf->surface);
3910 output = get_focused_output(shsurf->surface->compositor);
3911 }
3912
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003913 shell_surface_set_output(shsurf, output);
3914 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003915
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003916 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003917}
3918
3919static void
3920xdg_surface_unset_fullscreen(struct wl_client *client,
3921 struct wl_resource *resource)
3922{
3923 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3924
3925 shsurf->state_requested = true;
3926 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003927 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003928}
3929
3930static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003931xdg_surface_set_minimized(struct wl_client *client,
3932 struct wl_resource *resource)
3933{
3934 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3935
3936 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3937 return;
3938
3939 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003940 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003941}
3942
Rafael Antognollie2a34552013-12-03 15:35:45 -02003943static const struct xdg_surface_interface xdg_surface_implementation = {
3944 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003945 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003946 xdg_surface_set_title,
3947 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003948 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003949 xdg_surface_move,
3950 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003951 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003952 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003953 xdg_surface_set_maximized,
3954 xdg_surface_unset_maximized,
3955 xdg_surface_set_fullscreen,
3956 xdg_surface_unset_fullscreen,
3957 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003958};
3959
3960static void
3961xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003962 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003963{
3964 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003965 uint32_t *s;
3966 struct wl_array states;
3967 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003968
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003969 assert(shsurf);
3970
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003971 if (!shsurf->resource)
3972 return;
3973
3974 wl_array_init(&states);
3975 if (shsurf->requested_state.fullscreen) {
3976 s = wl_array_add(&states, sizeof *s);
3977 *s = XDG_SURFACE_STATE_FULLSCREEN;
3978 } else if (shsurf->requested_state.maximized) {
3979 s = wl_array_add(&states, sizeof *s);
3980 *s = XDG_SURFACE_STATE_MAXIMIZED;
3981 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003982 if (shsurf->resize_edges != 0) {
3983 s = wl_array_add(&states, sizeof *s);
3984 *s = XDG_SURFACE_STATE_RESIZING;
3985 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003986 if (shsurf->focus_count > 0) {
3987 s = wl_array_add(&states, sizeof *s);
3988 *s = XDG_SURFACE_STATE_ACTIVATED;
3989 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003990
3991 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3992 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3993
3994 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003995}
3996
3997static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003998 xdg_send_configure,
3999 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004000};
4001
4002static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004003xdg_shell_destroy(struct wl_client *client,
4004 struct wl_resource *resource)
4005{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004006 struct shell_client *sc = wl_resource_get_user_data(resource);
4007 struct wl_resource *shsurf_resource;
4008 struct shell_surface *shsurf;
4009
4010 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4011 shsurf = wl_resource_get_user_data(shsurf_resource);
4012 if (shsurf->owner_resource == resource) {
4013 wl_resource_post_error(
4014 resource,
4015 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4016 "not all child surface objects destroyed");
4017 return;
4018 }
4019 }
4020
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004021 wl_resource_destroy(resource);
4022}
4023
4024static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004025xdg_use_unstable_version(struct wl_client *client,
4026 struct wl_resource *resource,
4027 int32_t version)
4028{
4029 if (version > 1) {
4030 wl_resource_post_error(resource,
4031 1,
4032 "xdg-shell:: version not implemented yet.");
4033 return;
4034 }
4035}
4036
4037static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004038create_xdg_surface(struct shell_client *owner, void *shell,
4039 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004040 const struct weston_shell_client *client)
4041{
4042 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004043
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004044 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004045 if (!shsurf)
4046 return NULL;
4047
Pekka Paalanenb5026542014-11-12 15:09:24 +02004048 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004049
4050 return shsurf;
4051}
4052
4053static void
4054xdg_get_xdg_surface(struct wl_client *client,
4055 struct wl_resource *resource,
4056 uint32_t id,
4057 struct wl_resource *surface_resource)
4058{
4059 struct weston_surface *surface =
4060 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004061 struct shell_client *sc = wl_resource_get_user_data(resource);
4062 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004063 struct shell_surface *shsurf;
4064
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004065 shsurf = get_shell_surface(surface);
4066 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4067 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4068 "This wl_surface is already an "
4069 "xdg_surface");
4070 return;
4071 }
4072
Pekka Paalanen50b67472014-10-01 15:02:41 +03004073 if (weston_surface_set_role(surface, "xdg_surface",
4074 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004075 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004076
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004077 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004078 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004079 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004080 return;
4081 }
4082
4083 shsurf->resource =
4084 wl_resource_create(client,
4085 &xdg_surface_interface, 1, id);
4086 wl_resource_set_implementation(shsurf->resource,
4087 &xdg_surface_implementation,
4088 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004089 shsurf->owner_resource = resource;
4090 wl_list_insert(&sc->surface_list,
4091 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004092}
4093
4094static bool
4095shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4096{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004097 return shsurf->resource &&
4098 wl_resource_instance_of(shsurf->resource,
4099 &xdg_surface_interface,
4100 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004101}
4102
4103/* xdg-popup implementation */
4104
4105static void
4106xdg_popup_destroy(struct wl_client *client,
4107 struct wl_resource *resource)
4108{
4109 wl_resource_destroy(resource);
4110}
4111
Rafael Antognollie2a34552013-12-03 15:35:45 -02004112static const struct xdg_popup_interface xdg_popup_implementation = {
4113 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004114};
4115
4116static void
4117xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004118 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004119{
4120}
4121
4122static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004123 xdg_popup_send_configure,
4124 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004125};
4126
4127static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004128create_xdg_popup(struct shell_client *owner, void *shell,
4129 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004130 const struct weston_shell_client *client,
4131 struct weston_surface *parent,
4132 struct shell_seat *seat,
4133 uint32_t serial,
4134 int32_t x, int32_t y)
4135{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004136 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004137
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004138 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004139 if (!shsurf)
4140 return NULL;
4141
Pekka Paalanenb5026542014-11-12 15:09:24 +02004142 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004143 shsurf->popup.shseat = seat;
4144 shsurf->popup.serial = serial;
4145 shsurf->popup.x = x;
4146 shsurf->popup.y = y;
4147 shell_surface_set_parent(shsurf, parent);
4148
4149 return shsurf;
4150}
4151
4152static void
4153xdg_get_xdg_popup(struct wl_client *client,
4154 struct wl_resource *resource,
4155 uint32_t id,
4156 struct wl_resource *surface_resource,
4157 struct wl_resource *parent_resource,
4158 struct wl_resource *seat_resource,
4159 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004160 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004161{
4162 struct weston_surface *surface =
4163 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004164 struct shell_client *sc = wl_resource_get_user_data(resource);
4165 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004166 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004167 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004168 struct weston_surface *parent;
4169 struct shell_seat *seat;
4170
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004171 shsurf = get_shell_surface(surface);
4172 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4173 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4174 "This wl_surface is already an "
4175 "xdg_popup");
4176 return;
4177 }
4178
Pekka Paalanen50b67472014-10-01 15:02:41 +03004179 if (weston_surface_set_role(surface, "xdg_popup",
4180 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004181 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004182
4183 if (!parent_resource) {
4184 wl_resource_post_error(surface_resource,
4185 WL_DISPLAY_ERROR_INVALID_OBJECT,
4186 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004187 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004188 }
4189
4190 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004191 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004192
Jonas Ådahlee45a552015-03-18 16:37:47 +08004193 /* Verify that we are creating the top most popup when mapping,
4194 * as it's not until then we know whether it was mapped as most
4195 * top level or not. */
4196
4197 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004198 if (!parent_shsurf ||
4199 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4200 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004201 wl_resource_post_error(resource,
4202 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4203 "xdg_popup parent was invalid");
4204 return;
4205 }
4206
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004207 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004208 parent, seat, serial, x, y);
4209 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004210 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004211 return;
4212 }
4213
4214 shsurf->resource =
4215 wl_resource_create(client,
4216 &xdg_popup_interface, 1, id);
4217 wl_resource_set_implementation(shsurf->resource,
4218 &xdg_popup_implementation,
4219 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004220 shsurf->owner_resource = resource;
4221 wl_list_insert(&sc->surface_list,
4222 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004223}
4224
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004225static void
4226xdg_pong(struct wl_client *client,
4227 struct wl_resource *resource, uint32_t serial)
4228{
4229 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004230
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004231 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004232}
4233
Rafael Antognollie2a34552013-12-03 15:35:45 -02004234static bool
4235shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4236{
4237 return wl_resource_instance_of(shsurf->resource,
4238 &xdg_popup_interface,
4239 &xdg_popup_implementation);
4240}
4241
4242static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004243 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004244 xdg_use_unstable_version,
4245 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004246 xdg_get_xdg_popup,
4247 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004248};
4249
4250static int
4251xdg_shell_unversioned_dispatch(const void *implementation,
4252 void *_target, uint32_t opcode,
4253 const struct wl_message *message,
4254 union wl_argument *args)
4255{
4256 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004257 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004258
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004259 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004260 wl_resource_post_error(resource,
4261 WL_DISPLAY_ERROR_INVALID_OBJECT,
4262 "must call use_unstable_version first");
4263 return 0;
4264 }
4265
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004266#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004267
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004268 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4269 "shell implementation doesn't match protocol version");
4270
Rafael Antognollie2a34552013-12-03 15:35:45 -02004271 if (args[0].i != XDG_SERVER_VERSION) {
4272 wl_resource_post_error(resource,
4273 WL_DISPLAY_ERROR_INVALID_OBJECT,
4274 "incompatible version, server is %d "
4275 "client wants %d",
4276 XDG_SERVER_VERSION, args[0].i);
4277 return 0;
4278 }
4279
4280 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004281 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004282
4283 return 1;
4284}
4285
4286/* end of xdg-shell implementation */
4287/***********************************/
4288
Kristian Høgsberg07937562011-04-12 17:25:42 -04004289static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004290shell_fade(struct desktop_shell *shell, enum fade_type type);
4291
Pekka Paalanen77346a62011-11-30 16:26:35 +02004292static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004293configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004296
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004297 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004298 if (v->output == ev->output && v != ev) {
4299 weston_view_unmap(v);
4300 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004301 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004302 }
4303 }
4304
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004305 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004306
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004307 if (wl_list_empty(&ev->layer_link.link)) {
4308 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004310 }
4311}
4312
Pekka Paalanen8274d902014-08-06 19:36:51 +03004313static int
4314background_get_label(struct weston_surface *surface, char *buf, size_t len)
4315{
4316 return snprintf(buf, len, "background for output %s",
4317 surface->output->name);
4318}
4319
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004320static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004321background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004322{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004323 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004324 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004325
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 view = container_of(es->views.next, struct weston_view, surface_link);
4327
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004328 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004329}
4330
4331static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004332desktop_shell_set_background(struct wl_client *client,
4333 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004334 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004335 struct wl_resource *surface_resource)
4336{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004337 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004338 struct weston_surface *surface =
4339 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004340 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004341
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004342 if (surface->configure) {
4343 wl_resource_post_error(surface_resource,
4344 WL_DISPLAY_ERROR_INVALID_OBJECT,
4345 "surface role already assigned");
4346 return;
4347 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004348
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4350 weston_view_destroy(view);
4351 view = weston_view_create(surface);
4352
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004353 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004354 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004355 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004356 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004358 weston_desktop_shell_send_configure(resource, 0,
4359 surface_resource,
4360 surface->output->width,
4361 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004362}
4363
Pekka Paalanen8274d902014-08-06 19:36:51 +03004364static int
4365panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4366{
4367 return snprintf(buf, len, "panel for output %s",
4368 surface->output->name);
4369}
4370
Kristian Høgsberg75840622011-09-06 13:48:16 -04004371static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004372panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004373{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004374 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004376
Jason Ekstranda7af7042013-10-12 22:38:11 -05004377 view = container_of(es->views.next, struct weston_view, surface_link);
4378
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004379 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004380}
4381
4382static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004383desktop_shell_set_panel(struct wl_client *client,
4384 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004385 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004386 struct wl_resource *surface_resource)
4387{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004388 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004389 struct weston_surface *surface =
4390 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004392
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004393 if (surface->configure) {
4394 wl_resource_post_error(surface_resource,
4395 WL_DISPLAY_ERROR_INVALID_OBJECT,
4396 "surface role already assigned");
4397 return;
4398 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004399
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4401 weston_view_destroy(view);
4402 view = weston_view_create(surface);
4403
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004404 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004405 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004406 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004407 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004409 weston_desktop_shell_send_configure(resource, 0,
4410 surface_resource,
4411 surface->output->width,
4412 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004413}
4414
Pekka Paalanen8274d902014-08-06 19:36:51 +03004415static int
4416lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4417{
4418 return snprintf(buf, len, "lock window");
4419}
4420
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004421static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004422lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004423{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004424 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004425 struct weston_view *view;
4426
4427 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004428
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004429 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004430 return;
4431
Jason Ekstranda7af7042013-10-12 22:38:11 -05004432 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004433
4434 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004435 weston_layer_entry_insert(&shell->lock_layer.view_list,
4436 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004437 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004438 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004439 }
4440}
4441
4442static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004443handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004444{
Tiago Vignattibe143262012-04-16 17:31:41 +03004445 struct desktop_shell *shell =
4446 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004447
Martin Minarik6d118362012-06-07 18:01:59 +02004448 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004449 shell->lock_surface = NULL;
4450}
4451
4452static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004453desktop_shell_set_lock_surface(struct wl_client *client,
4454 struct wl_resource *resource,
4455 struct wl_resource *surface_resource)
4456{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004457 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004458 struct weston_surface *surface =
4459 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004460
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004461 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004462
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004463 if (!shell->locked)
4464 return;
4465
Pekka Paalanen98262232011-12-01 10:42:22 +02004466 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004467
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004468 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004469 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004470 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004471
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004472 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004473 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004474 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004475 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004476}
4477
4478static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004479resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004480{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004481 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004482
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004483 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004484 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004485 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004486 &shell->input_panel_layer.link);
4487 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004488 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004489 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004490 wl_list_insert(&shell->compositor->cursor_layer.link,
4491 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004492 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004493 wl_list_insert(&shell->fullscreen_layer.link,
4494 &shell->panel_layer.link);
4495 wl_list_insert(&shell->panel_layer.link,
4496 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004497
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004498 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004499
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004500 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004501 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004502 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004503}
4504
4505static void
4506desktop_shell_unlock(struct wl_client *client,
4507 struct wl_resource *resource)
4508{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004509 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004510
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004511 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004512
4513 if (shell->locked)
4514 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004515}
4516
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004517static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004518desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004519 struct wl_resource *resource,
4520 struct wl_resource *surface_resource)
4521{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004522 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004523
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004524 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004525 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004526}
4527
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004528static void
4529desktop_shell_desktop_ready(struct wl_client *client,
4530 struct wl_resource *resource)
4531{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004532 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004533
4534 shell_fade_startup(shell);
4535}
4536
Jonny Lamb765760d2014-08-20 15:53:19 +02004537static void
4538desktop_shell_set_panel_position(struct wl_client *client,
4539 struct wl_resource *resource,
4540 uint32_t position)
4541{
4542 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4543
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004544 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4545 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4546 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4547 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004548 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004549 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004550 "bad position argument");
4551 return;
4552 }
4553
4554 shell->panel_position = position;
4555}
4556
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004557static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004558 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004559 desktop_shell_set_panel,
4560 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004561 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004562 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004563 desktop_shell_desktop_ready,
4564 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004565};
4566
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004567static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004568get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004569{
4570 struct shell_surface *shsurf;
4571
4572 shsurf = get_shell_surface(surface);
4573 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004574 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004575 return shsurf->type;
4576}
4577
Kristian Høgsberg75840622011-09-06 13:48:16 -04004578static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004579move_binding(struct weston_pointer *pointer, uint32_t time,
4580 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004581{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004582 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004583 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004584 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004585
Derek Foreman8ae2db52015-07-15 13:00:36 -05004586 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004587 return;
4588
Derek Foreman8ae2db52015-07-15 13:00:36 -05004589 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004590
Pekka Paalanen01388e22013-04-25 13:57:44 +03004591 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004592 if (surface == NULL)
4593 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004594
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004595 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004596 if (shsurf == NULL || shsurf->state.fullscreen ||
4597 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004598 return;
4599
Derek Foreman794fa0e2015-07-15 13:00:38 -05004600 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004601}
4602
4603static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004604maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4605 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004606{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004607 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004608 struct weston_surface *surface;
4609 struct shell_surface *shsurf;
4610
4611 surface = weston_surface_get_main_surface(focus);
4612 if (surface == NULL)
4613 return;
4614
4615 shsurf = get_shell_surface(surface);
4616 if (shsurf == NULL)
4617 return;
4618
4619 if (!shell_surface_is_xdg_surface(shsurf))
4620 return;
4621
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004622 shsurf->state_requested = true;
4623 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004624 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004625}
4626
4627static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004628fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4629 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004630{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004631 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004632 struct weston_surface *surface;
4633 struct shell_surface *shsurf;
4634
4635 surface = weston_surface_get_main_surface(focus);
4636 if (surface == NULL)
4637 return;
4638
4639 shsurf = get_shell_surface(surface);
4640 if (shsurf == NULL)
4641 return;
4642
4643 if (!shell_surface_is_xdg_surface(shsurf))
4644 return;
4645
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004646 shsurf->state_requested = true;
4647 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004648 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004649 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004650}
4651
4652static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004653touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004654{
Derek Foreman362656b2014-09-04 10:23:05 -05004655 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004656 struct weston_surface *surface;
4657 struct shell_surface *shsurf;
4658
Derek Foreman8ae2db52015-07-15 13:00:36 -05004659 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004660 return;
4661
Derek Foreman8ae2db52015-07-15 13:00:36 -05004662 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004663 surface = weston_surface_get_main_surface(focus);
4664 if (surface == NULL)
4665 return;
4666
4667 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004668 if (shsurf == NULL || shsurf->state.fullscreen ||
4669 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004670 return;
4671
Derek Foremanb7674ae2015-07-15 13:00:37 -05004672 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004673}
4674
4675static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004676resize_binding(struct weston_pointer *pointer, uint32_t time,
4677 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004678{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004679 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004680 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004681 uint32_t edges = 0;
4682 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004683 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004684
Derek Foreman8ae2db52015-07-15 13:00:36 -05004685 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004686 return;
4687
Derek Foreman8ae2db52015-07-15 13:00:36 -05004688 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004689
Pekka Paalanen01388e22013-04-25 13:57:44 +03004690 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004691 if (surface == NULL)
4692 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004693
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004694 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004695 if (shsurf == NULL || shsurf->state.fullscreen ||
4696 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004697 return;
4698
Jason Ekstranda7af7042013-10-12 22:38:11 -05004699 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004700 wl_fixed_to_int(pointer->grab_x),
4701 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004702 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004703
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004704 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004705 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004706 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004707 edges |= 0;
4708 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004709 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004710
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004711 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004712 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004713 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004714 edges |= 0;
4715 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004716 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004717
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004718 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004719}
4720
4721static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004722surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4723 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004724{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004725 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004726 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004727 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004728 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004729
Pekka Paalanen01388e22013-04-25 13:57:44 +03004730 /* XXX: broken for windows containing sub-surfaces */
4731 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004732 if (surface == NULL)
4733 return;
4734
4735 shsurf = get_shell_surface(surface);
4736 if (!shsurf)
4737 return;
4738
Jason Ekstranda7af7042013-10-12 22:38:11 -05004739 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004740
Jason Ekstranda7af7042013-10-12 22:38:11 -05004741 if (shsurf->view->alpha > 1.0)
4742 shsurf->view->alpha = 1.0;
4743 if (shsurf->view->alpha < step)
4744 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004745
Jason Ekstranda7af7042013-10-12 22:38:11 -05004746 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004747 weston_surface_damage(surface);
4748}
4749
4750static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004751do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004752 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004753{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004754 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004755 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004756 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004757 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004758
Derek Foreman1281a362015-07-31 16:55:32 -05004759 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004760 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4761 return;
4762 }
4763
Scott Moreauccbf29d2012-02-22 14:21:41 -07004764 wl_list_for_each(output, &compositor->output_list, link) {
4765 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004766 wl_fixed_to_double(pointer->x),
4767 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004768 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004769 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004770 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004771 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004772 increment = -output->zoom.increment;
4773 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004774 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004775 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004776 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004777 else
4778 increment = 0;
4779
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004780 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004781
Scott Moreaue6603982012-06-11 13:07:51 -06004782 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004783 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004784 else if (output->zoom.level > output->zoom.max_level)
4785 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004786
4787 if (!output->zoom.active) {
4788 if (output->zoom.level <= 0.0)
4789 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004790 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004791 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004792
Scott Moreaue6603982012-06-11 13:07:51 -06004793 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004794
Jason Ekstranda7af7042013-10-12 22:38:11 -05004795 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004796 }
4797 }
4798}
4799
Scott Moreauccbf29d2012-02-22 14:21:41 -07004800static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004801zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4802 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004803{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004804 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004805}
4806
4807static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004808zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4809 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004810{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004811 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004812}
4813
4814static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004815terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4816 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004817{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004818 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004819
Daniel Stone325fc2d2012-05-30 16:31:58 +01004820 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004821}
4822
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004823static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004824rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004825 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004826{
4827 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004828 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004829 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004830 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004831 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004832
Jonas Ådahld2510102014-10-05 21:39:14 +02004833 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004834
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004835 if (!shsurf)
4836 return;
4837
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004838 cx = 0.5f * shsurf->surface->width;
4839 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004840
Daniel Stone37816df2012-05-16 18:45:18 +01004841 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4842 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004843 r = sqrtf(dx * dx + dy * dy);
4844
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004845 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004846 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004847
4848 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004849 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004850 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004851
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004852 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004853 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004854
4855 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004856 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004857 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004858 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004859 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004860
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004861 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004862 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004863 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004864 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004865 wl_list_init(&shsurf->rotation.transform.link);
4866 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004867 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004868 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004869
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004870 /* We need to adjust the position of the surface
4871 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004872 cposx = shsurf->view->geometry.x + cx;
4873 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004874 dposx = rotate->center.x - cposx;
4875 dposy = rotate->center.y - cposy;
4876 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004877 weston_view_set_position(shsurf->view,
4878 shsurf->view->geometry.x + dposx,
4879 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004880 }
4881
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004882 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004883 * lazily applies the damage due to rotation update.
4884 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004885 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004886}
4887
4888static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004889rotate_grab_button(struct weston_pointer_grab *grab,
4890 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004891{
4892 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004893 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004894 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004895 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004896 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004897
Daniel Stone4dbadb12012-05-30 16:31:51 +01004898 if (pointer->button_count == 0 &&
4899 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004900 if (shsurf)
4901 weston_matrix_multiply(&shsurf->rotation.rotation,
4902 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004903 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004904 free(rotate);
4905 }
4906}
4907
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004908static void
4909rotate_grab_cancel(struct weston_pointer_grab *grab)
4910{
4911 struct rotate_grab *rotate =
4912 container_of(grab, struct rotate_grab, base.grab);
4913
4914 shell_grab_end(&rotate->base);
4915 free(rotate);
4916}
4917
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004918static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004919 noop_grab_focus,
4920 rotate_grab_motion,
4921 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02004922 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004923 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004924};
4925
4926static void
Derek Foreman74de4692015-07-15 13:00:39 -05004927surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004928{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004929 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004930 float dx, dy;
4931 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004932
Derek Foreman45a7c272015-09-11 14:27:40 -05004933 surface = find_toplevel_surface(surface);
4934
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935 rotate = malloc(sizeof *rotate);
4936 if (!rotate)
4937 return;
4938
Jason Ekstranda7af7042013-10-12 22:38:11 -05004939 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004940 surface->surface->width * 0.5f,
4941 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004942 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004943
Derek Foreman74de4692015-07-15 13:00:39 -05004944 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4945 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004946 r = sqrtf(dx * dx + dy * dy);
4947 if (r > 20.0f) {
4948 struct weston_matrix inverse;
4949
4950 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004951 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004952 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004953
4954 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004955 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004956 } else {
4957 weston_matrix_init(&surface->rotation.rotation);
4958 weston_matrix_init(&rotate->rotation);
4959 }
4960
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004961 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004962 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963}
4964
4965static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004966rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004967 void *data)
4968{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004969 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004970 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004971 struct shell_surface *surface;
4972
Derek Foreman8ae2db52015-07-15 13:00:36 -05004973 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004974 return;
4975
Derek Foreman8ae2db52015-07-15 13:00:36 -05004976 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004977
Pekka Paalanen01388e22013-04-25 13:57:44 +03004978 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004979 if (base_surface == NULL)
4980 return;
4981
4982 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004983 if (surface == NULL || surface->state.fullscreen ||
4984 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004985 return;
4986
Derek Foreman74de4692015-07-15 13:00:39 -05004987 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004988}
4989
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004990/* Move all fullscreen layers down to the current workspace and hide their
4991 * black views. The surfaces' state is set to both fullscreen and lowered,
4992 * and this is reversed when such a surface is re-configured, see
4993 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4994 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004995 * lowering_output = NULL - Lower on all outputs, else only lower on the
4996 * specified output.
4997 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004998 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004999 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005000void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005001lower_fullscreen_layer(struct desktop_shell *shell,
5002 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005003{
5004 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005005 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005006
5007 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005008 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005009 &shell->fullscreen_layer.view_list.link,
5010 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005011 struct shell_surface *shsurf = get_shell_surface(view->surface);
5012
5013 if (!shsurf)
5014 continue;
5015
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005016 /* Only lower surfaces which have lowering_output as their fullscreen
5017 * output, unless a NULL output asks for lowering on all outputs.
5018 */
5019 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5020 continue;
5021
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005022 /* We can have a non-fullscreen popup for a fullscreen surface
5023 * in the fullscreen layer. */
5024 if (shsurf->state.fullscreen) {
5025 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005026 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5027 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005028 weston_view_damage_below(shsurf->fullscreen.black_view);
5029
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005030 }
5031
5032 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005033 weston_layer_entry_remove(&view->layer_link);
5034 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005035 weston_view_damage_below(view);
5036 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005037
5038 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005039 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005040}
5041
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005042void
Tiago Vignattibe143262012-04-16 17:31:41 +03005043activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005044 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005045{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005046 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005047 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005048 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005049 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005050 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005051
Pekka Paalanen01388e22013-04-25 13:57:44 +03005052 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005053 shsurf = get_shell_surface(main_surface);
5054 assert(shsurf);
5055
5056 /* Only demote fullscreen surfaces on the output of activated shsurf.
5057 * Leave fullscreen surfaces on unrelated outputs alone. */
5058 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005059
Daniel Stone37816df2012-05-16 18:45:18 +01005060 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005061
Jonas Ådahl8538b222012-08-29 22:13:03 +02005062 state = ensure_focus_state(shell, seat);
5063 if (state == NULL)
5064 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005065
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005066 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005067 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005068
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005069 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005070 shell_configure_fullscreen(shsurf);
5071 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005072 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005073
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005074 /* Update the surface’s layer. This brings it to the top of the stacking
5075 * order as appropriate. */
5076 shell_surface_update_layer(shsurf);
5077
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005078 if (shell->focus_animation_type != ANIMATION_NONE) {
5079 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005080 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005081 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005082}
5083
Alex Wu21858432012-04-01 20:13:08 +08005084/* no-op func for checking black surface */
5085static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005086black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005087{
5088}
5089
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005090static bool
Alex Wu21858432012-04-01 20:13:08 +08005091is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5092{
5093 if (es->configure == black_surface_configure) {
5094 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005095 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005096 return true;
5097 }
5098 return false;
5099}
5100
Kristian Høgsberg75840622011-09-06 13:48:16 -04005101static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005102activate_binding(struct weston_seat *seat,
5103 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005104 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005105{
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005106 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005107 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005108
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005109 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005110
Pekka Paalanen01388e22013-04-25 13:57:44 +03005111 if (is_black_surface(focus, &main_surface))
5112 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005113
Pekka Paalanen01388e22013-04-25 13:57:44 +03005114 main_surface = weston_surface_get_main_surface(focus);
5115 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005116 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005117
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005118 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005119}
5120
5121static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005122click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5123 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005124{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005125 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005126 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005127 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005128 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005129
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005130 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005131}
5132
5133static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005134touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5135 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005136{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005137 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005138 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005139 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005140 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005141
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005142 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005143}
5144
5145static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005146unfocus_all_seats(struct desktop_shell *shell)
5147{
5148 struct weston_seat *seat, *next;
5149
5150 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005151 struct weston_keyboard *keyboard =
5152 weston_seat_get_keyboard(seat);
5153
5154 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005155 continue;
5156
Derek Foreman1281a362015-07-31 16:55:32 -05005157 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005158 }
5159}
5160
5161static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005162lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005163{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005164 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005165
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005166 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005167 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005168 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005169 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005170
5171 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005172
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005173 /* Hide all surfaces by removing the fullscreen, panel and
5174 * toplevel layers. This way nothing else can show or receive
5175 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005176
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005177 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005178 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005179 if (shell->showing_input_panels)
5180 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005181 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005182 wl_list_insert(&shell->compositor->cursor_layer.link,
5183 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005184
Derek Foreman004b4a12015-07-20 16:28:13 -05005185 weston_compositor_sleep(shell->compositor);
5186
Neil Robertsb4a91702014-04-09 16:33:32 +01005187 /* Remove the keyboard focus on all seats. This will be
5188 * restored to the workspace's saved state via
5189 * restore_focus_state when the compositor is unlocked */
5190 unfocus_all_seats(shell);
5191
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005192 /* TODO: disable bindings that should not work while locked. */
5193
5194 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005195}
5196
5197static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005198unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005199{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005200 struct wl_resource *shell_resource;
5201
Pekka Paalanend81c2162011-11-16 13:47:34 +02005202 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005203 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005204 return;
5205 }
5206
5207 /* If desktop-shell client has gone away, unlock immediately. */
5208 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005209 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005210 return;
5211 }
5212
5213 if (shell->prepare_event_sent)
5214 return;
5215
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005216 shell_resource = shell->child.desktop_shell;
5217 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005218 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005219}
5220
5221static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005222shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005223{
5224 struct desktop_shell *shell = data;
5225
5226 shell->fade.animation = NULL;
5227
5228 switch (shell->fade.type) {
5229 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005230 weston_surface_destroy(shell->fade.view->surface);
5231 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005232 break;
5233 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005234 lock(shell);
5235 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005236 default:
5237 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005238 }
5239}
5240
Jason Ekstranda7af7042013-10-12 22:38:11 -05005241static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005242shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005243{
5244 struct weston_compositor *compositor = shell->compositor;
5245 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005246 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005247
5248 surface = weston_surface_create(compositor);
5249 if (!surface)
5250 return NULL;
5251
Jason Ekstranda7af7042013-10-12 22:38:11 -05005252 view = weston_view_create(surface);
5253 if (!view) {
5254 weston_surface_destroy(surface);
5255 return NULL;
5256 }
5257
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005258 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005259 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005260 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005261 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5262 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005263 pixman_region32_init(&surface->input);
5264
Jason Ekstranda7af7042013-10-12 22:38:11 -05005265 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005266}
5267
5268static void
5269shell_fade(struct desktop_shell *shell, enum fade_type type)
5270{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005271 float tint;
5272
5273 switch (type) {
5274 case FADE_IN:
5275 tint = 0.0;
5276 break;
5277 case FADE_OUT:
5278 tint = 1.0;
5279 break;
5280 default:
5281 weston_log("shell: invalid fade type\n");
5282 return;
5283 }
5284
5285 shell->fade.type = type;
5286
Jason Ekstranda7af7042013-10-12 22:38:11 -05005287 if (shell->fade.view == NULL) {
5288 shell->fade.view = shell_fade_create_surface(shell);
5289 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005290 return;
5291
Jason Ekstranda7af7042013-10-12 22:38:11 -05005292 shell->fade.view->alpha = 1.0 - tint;
5293 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005294 }
5295
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005296 if (shell->fade.view->output == NULL) {
5297 /* If the black view gets a NULL output, we lost the
5298 * last output and we'll just cancel the fade. This
5299 * happens when you close the last window under the
5300 * X11 or Wayland backends. */
5301 shell->locked = false;
5302 weston_surface_destroy(shell->fade.view->surface);
5303 shell->fade.view = NULL;
5304 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005305 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005306 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005307 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005308 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005309 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005310 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005311 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005312}
5313
5314static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005315do_shell_fade_startup(void *data)
5316{
5317 struct desktop_shell *shell = data;
5318
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005319 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005320 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005321 } else {
5322 weston_log("desktop shell: "
5323 "unexpected fade-in animation type %d\n",
5324 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005325 weston_surface_destroy(shell->fade.view->surface);
5326 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005327 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005328}
5329
5330static void
5331shell_fade_startup(struct desktop_shell *shell)
5332{
5333 struct wl_event_loop *loop;
5334
5335 if (!shell->fade.startup_timer)
5336 return;
5337
5338 wl_event_source_remove(shell->fade.startup_timer);
5339 shell->fade.startup_timer = NULL;
5340
5341 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5342 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5343}
5344
5345static int
5346fade_startup_timeout(void *data)
5347{
5348 struct desktop_shell *shell = data;
5349
5350 shell_fade_startup(shell);
5351 return 0;
5352}
5353
5354static void
5355shell_fade_init(struct desktop_shell *shell)
5356{
5357 /* Make compositor output all black, and wait for the desktop-shell
5358 * client to signal it is ready, then fade in. The timer triggers a
5359 * fade-in, in case the desktop-shell client takes too long.
5360 */
5361
5362 struct wl_event_loop *loop;
5363
Jason Ekstranda7af7042013-10-12 22:38:11 -05005364 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005365 weston_log("%s: warning: fade surface already exists\n",
5366 __func__);
5367 return;
5368 }
5369
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005370 if (shell->startup_animation_type == ANIMATION_NONE)
5371 return;
5372
Jason Ekstranda7af7042013-10-12 22:38:11 -05005373 shell->fade.view = shell_fade_create_surface(shell);
5374 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005375 return;
5376
Jason Ekstranda7af7042013-10-12 22:38:11 -05005377 weston_view_update_transform(shell->fade.view);
5378 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005379
5380 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5381 shell->fade.startup_timer =
5382 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5383 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5384}
5385
5386static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005387idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005388{
5389 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005390 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005391 struct weston_seat *seat;
5392
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005393 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005394 struct weston_touch *touch = weston_seat_get_touch(seat);
5395 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5396
5397 if (pointer)
5398 popup_grab_end(pointer);
5399 if (touch)
5400 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005401 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005402
5403 shell_fade(shell, FADE_OUT);
5404 /* lock() is called from shell_fade_done() */
5405}
5406
5407static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005408wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005409{
5410 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005411 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005412
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005413 unlock(shell);
5414}
5415
5416static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005417transform_handler(struct wl_listener *listener, void *data)
5418{
5419 struct weston_surface *surface = data;
5420 struct shell_surface *shsurf = get_shell_surface(surface);
5421 struct weston_view *view;;
5422 int x, y;
5423
5424 if (!shsurf || !shsurf->client->send_position)
5425 return;
5426
5427 view = shsurf->view;
5428 if (!view || !weston_view_is_mapped(view))
5429 return;
5430
5431 x = view->geometry.x;
5432 y = view->geometry.y;
5433
5434 shsurf->client->send_position(surface, x, y);
5435}
5436
5437static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005438center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005439{
Giulio Camuffob8366642013-04-25 13:57:46 +03005440 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005441 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005442
Jason Ekstranda7af7042013-10-12 22:38:11 -05005443 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005444
5445 x = output->x + (output->width - width) / 2 - surf_x / 2;
5446 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005447
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005448 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005449}
5450
5451static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005452weston_view_set_initial_position(struct weston_view *view,
5453 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005454{
5455 struct weston_compositor *compositor = shell->compositor;
5456 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005457 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005458 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005459 struct weston_output *output, *target_output = NULL;
5460 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005461 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005462
5463 /* As a heuristic place the new window on the same output as the
5464 * pointer. Falling back to the output containing 0, 0.
5465 *
5466 * TODO: Do something clever for touch too?
5467 */
5468 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005469 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5470
5471 if (pointer) {
5472 ix = wl_fixed_to_int(pointer->x);
5473 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005474 break;
5475 }
5476 }
5477
5478 wl_list_for_each(output, &compositor->output_list, link) {
5479 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5480 target_output = output;
5481 break;
5482 }
5483 }
5484
5485 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005486 weston_view_set_position(view, 10 + random() % 400,
5487 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005488 return;
5489 }
5490
5491 /* Valid range within output where the surface will still be onscreen.
5492 * If this is negative it means that the surface is bigger than
5493 * output.
5494 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005495 get_output_work_area(shell, target_output, &area);
5496
Derek Foremanf814c5d2015-04-15 12:23:54 -05005497 x = area.x;
5498 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005499 range_x = area.width - view->surface->width;
5500 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005501
Rob Bradford4cb88c72012-08-13 15:18:44 +01005502 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005503 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005504
5505 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005506 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005507
Jason Ekstranda7af7042013-10-12 22:38:11 -05005508 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005509}
5510
5511static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005512set_maximized_position(struct desktop_shell *shell,
5513 struct shell_surface *shsurf)
5514{
5515 int32_t surf_x, surf_y;
5516 pixman_rectangle32_t area;
5517
Jonny Lambd73c6942014-08-20 15:53:20 +02005518 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005519 if (shsurf->has_set_geometry) {
5520 surf_x = shsurf->geometry.x;
5521 surf_y = shsurf->geometry.y;
5522 } else {
5523 surface_subsurfaces_boundingbox(shsurf->surface,
5524 &surf_x, &surf_y, NULL, NULL);
5525 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005526
5527 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005528 area.x - surf_x,
5529 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005530}
5531
5532static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005533map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005534 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005535{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005536 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005537 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005538
Pekka Paalanen77346a62011-11-30 16:26:35 +02005539 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005540 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005541 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005542 if (shsurf->state.fullscreen) {
5543 center_on_output(shsurf->view, shsurf->fullscreen_output);
5544 shell_map_fullscreen(shsurf);
5545 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005546 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005547 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005548 weston_view_set_initial_position(shsurf->view, shell);
5549 }
Juan Zhao96879df2012-02-07 08:45:41 +08005550 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005551 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005552 if (shell_map_popup(shsurf) != 0)
5553 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005554 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005555 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005556 weston_view_set_position(shsurf->view,
5557 shsurf->view->geometry.x + sx,
5558 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005559 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005560 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005561 default:
5562 ;
5563 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005564
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005565 /* Surface stacking order, see also activate(). */
5566 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005567
Jason Ekstranda7af7042013-10-12 22:38:11 -05005568 if (shsurf->type != SHELL_SURFACE_NONE) {
5569 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005570 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005571 shsurf->surface->output = shsurf->output;
5572 shsurf->view->output = shsurf->output;
5573 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005574 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005575
Jason Ekstranda7af7042013-10-12 22:38:11 -05005576 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005577 /* XXX: xwayland's using the same fields for transient type */
5578 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005579 if (shsurf->transient.flags ==
5580 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5581 break;
5582 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005583 if (shsurf->state.relative &&
5584 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5585 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005586 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005587 break;
5588 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005589 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005590 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005591 case SHELL_SURFACE_POPUP:
5592 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005593 default:
5594 break;
5595 }
5596
Rafael Antognolli03b16592013-12-03 15:35:42 -02005597 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5598 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005599 {
5600 switch (shell->win_animation_type) {
5601 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005602 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005603 break;
5604 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005605 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005606 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005607 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005608 default:
5609 break;
5610 }
5611 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005612}
5613
5614static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005615configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005616 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005617{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005618 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005619 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005620
Pekka Paalanen77346a62011-11-30 16:26:35 +02005621 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005622
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005623 assert(shsurf);
5624
Rafael Antognolli03b16592013-12-03 15:35:42 -02005625 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005626 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005627 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005628 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005629 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005630 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005631 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005632
Alex Wu4539b082012-03-01 12:57:46 +08005633 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005634 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005635 wl_list_for_each(view, &surface->views, surface_link)
5636 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005637
Rafael Antognolli03b16592013-12-03 15:35:42 -02005638 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005639 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005640 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005641}
5642
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005643static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005644shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005645{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005646 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005647 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005648 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005649
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005650 assert(shsurf);
5651
5652 shell = shsurf->shell;
5653
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005654 if (!weston_surface_is_mapped(es) &&
5655 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005656 remove_popup_grab(shsurf);
5657 }
5658
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005659 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005660 return;
5661
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005662 if (shsurf->has_next_geometry) {
5663 shsurf->geometry = shsurf->next_geometry;
5664 shsurf->has_next_geometry = false;
5665 shsurf->has_set_geometry = true;
5666 } else if (!shsurf->has_set_geometry) {
5667 surface_subsurfaces_boundingbox(shsurf->surface,
5668 &shsurf->geometry.x,
5669 &shsurf->geometry.y,
5670 &shsurf->geometry.width,
5671 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005672 }
5673
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005674 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005675 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005676 type_changed = 1;
5677 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005678
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005679 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005680 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005681 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005682 shsurf->last_width != es->width ||
5683 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005684 float from_x, from_y;
5685 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005686
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005687 if (shsurf->resize_edges) {
5688 sx = 0;
5689 sy = 0;
5690 }
5691
5692 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5693 sx = shsurf->last_width - es->width;
5694 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5695 sy = shsurf->last_height - es->height;
5696
5697 shsurf->last_width = es->width;
5698 shsurf->last_height = es->height;
5699
Jason Ekstranda7af7042013-10-12 22:38:11 -05005700 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5701 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005702 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005703 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005704 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005705 }
5706}
5707
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005708static bool
5709check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5710{
5711 struct timespec now;
5712
5713 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5714 return false;
5715
5716 /*
5717 * If the shell helper client dies before the session has been
5718 * up for roughly 30 seconds, better just make Weston shut down,
5719 * because the user likely has no way to interact with the desktop
5720 * anyway.
5721 */
5722 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5723 weston_log("Error: %s apparently cannot run at all.\n",
5724 shell->client);
5725 weston_log_continue(STAMP_SPACE "Quitting...");
5726 wl_display_terminate(shell->compositor->wl_display);
5727
5728 return true;
5729 }
5730
5731 return false;
5732}
5733
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005734static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005735
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005736static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005737respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005738{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005739 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005740
5741 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5742 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005743 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005744 shell->child.deathstamp = time;
5745 shell->child.deathcount = 0;
5746 }
5747
5748 shell->child.deathcount++;
5749 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005750 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005751 return;
5752 }
5753
Pekka Paalanen826dc142014-08-27 12:11:53 +03005754 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005755 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005756}
5757
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005758static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005759desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5760{
5761 struct desktop_shell *shell;
5762
5763 shell = container_of(listener, struct desktop_shell,
5764 child.client_destroy_listener);
5765
Pekka Paalanen826dc142014-08-27 12:11:53 +03005766 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005767 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005768 /*
5769 * unbind_desktop_shell() will reset shell->child.desktop_shell
5770 * before the respawned process has a chance to create a new
5771 * desktop_shell object, because we are being called from the
5772 * wl_client destructor which destroys all wl_resources before
5773 * returning.
5774 */
5775
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005776 if (!check_desktop_shell_crash_too_early(shell))
5777 respawn_desktop_shell_process(shell);
5778
Pekka Paalanen826dc142014-08-27 12:11:53 +03005779 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005780}
5781
5782static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005783launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005784{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005785 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005786
Pekka Paalanen826dc142014-08-27 12:11:53 +03005787 shell->child.client = weston_client_start(shell->compositor,
5788 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005789
Arnaud Vrac42631192014-08-25 20:56:46 +02005790 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005791 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005792 return;
5793 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005794
5795 shell->child.client_destroy_listener.notify =
5796 desktop_shell_client_destroy;
5797 wl_client_add_destroy_listener(shell->child.client,
5798 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005799}
5800
5801static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005802handle_shell_client_destroy(struct wl_listener *listener, void *data)
5803{
5804 struct shell_client *sc =
5805 container_of(listener, struct shell_client, destroy_listener);
5806
5807 if (sc->ping_timer)
5808 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005809
5810 /* Since we're about to free shell_client, we remove it from the
5811 * head of the surface list so we don't use that freed list node
5812 * during surface clean up later on.
5813 */
5814 wl_list_remove(&sc->surface_list);
5815
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005816 free(sc);
5817}
5818
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005819static struct shell_client *
5820shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5821 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005822{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005823 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005824
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005825 sc = zalloc(sizeof *sc);
5826 if (sc == NULL) {
5827 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005828 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005829 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005830
5831 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005832 if (sc->resource == NULL) {
5833 free(sc);
5834 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005835 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005836 }
5837
5838 sc->client = client;
5839 sc->shell = shell;
5840 sc->destroy_listener.notify = handle_shell_client_destroy;
5841 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005842 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005843
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005844 return sc;
5845}
5846
5847static void
5848bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5849{
5850 struct desktop_shell *shell = data;
5851 struct shell_client *sc;
5852
5853 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5854 if (sc)
5855 wl_resource_set_implementation(sc->resource,
5856 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005857 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005858}
5859
5860static void
5861bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5862{
5863 struct desktop_shell *shell = data;
5864 struct shell_client *sc;
5865
5866 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5867 if (sc)
5868 wl_resource_set_dispatcher(sc->resource,
5869 xdg_shell_unversioned_dispatch,
5870 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005871}
5872
5873static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005874unbind_desktop_shell(struct wl_resource *resource)
5875{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005876 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005877
5878 if (shell->locked)
5879 resume_desktop(shell);
5880
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005881 shell->child.desktop_shell = NULL;
5882 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005883}
5884
5885static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005886bind_desktop_shell(struct wl_client *client,
5887 void *data, uint32_t version, uint32_t id)
5888{
Tiago Vignattibe143262012-04-16 17:31:41 +03005889 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005890 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005891
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005892 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5893 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005894
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005895 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005896 wl_resource_set_implementation(resource,
5897 &desktop_shell_implementation,
5898 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005899 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005900 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005901 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005902
5903 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5904 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005905}
5906
Kristian Høgsberg07045392012-02-19 18:52:44 -05005907struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005908 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005909 struct weston_surface *current;
5910 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005911 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005912 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005913};
5914
5915static void
5916switcher_next(struct switcher *switcher)
5917{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005918 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005919 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005920 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005921 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005922
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005923 /* temporary re-display minimized surfaces */
5924 struct weston_view *tmp;
5925 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005926 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5927 weston_layer_entry_remove(&view->layer_link);
5928 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005929 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5930 *minimized = view;
5931 }
5932
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005933 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005934 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005935 if (shsurf &&
5936 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5937 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005938 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005939 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005940 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005941 next = view->surface;
5942 prev = view->surface;
5943 view->alpha = 0.25;
5944 weston_view_geometry_dirty(view);
5945 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005946 }
Alex Wu1659daa2012-04-01 20:13:09 +08005947
Jason Ekstranda7af7042013-10-12 22:38:11 -05005948 if (is_black_surface(view->surface, NULL)) {
5949 view->alpha = 0.25;
5950 weston_view_geometry_dirty(view);
5951 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005952 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005953 }
5954
5955 if (next == NULL)
5956 next = first;
5957
Alex Wu07b26062012-03-12 16:06:01 +08005958 if (next == NULL)
5959 return;
5960
Kristian Høgsberg07045392012-02-19 18:52:44 -05005961 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005962 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005963
5964 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005965 wl_list_for_each(view, &next->views, surface_link)
5966 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005967
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005968 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005969 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005970 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005971}
5972
5973static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005974switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005975{
5976 struct switcher *switcher =
5977 container_of(listener, struct switcher, listener);
5978
5979 switcher_next(switcher);
5980}
5981
5982static void
Daniel Stone351eb612012-05-31 15:27:47 -04005983switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005984{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005985 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005986 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005987 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005988
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005989 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005990 if (is_focus_view(view))
5991 continue;
5992
Jason Ekstranda7af7042013-10-12 22:38:11 -05005993 view->alpha = 1.0;
5994 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005995 }
5996
Alex Wu07b26062012-03-12 16:06:01 +08005997 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005998 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005999 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006000 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006001 weston_keyboard_end_grab(keyboard);
6002 if (keyboard->input_method_resource)
6003 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006004
6005 /* re-hide surfaces that were temporary shown during the switch */
6006 struct weston_view **minimized;
6007 wl_array_for_each(minimized, &switcher->minimized_array) {
6008 /* with the exception of the current selected */
6009 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006010 weston_layer_entry_remove(&(*minimized)->layer_link);
6011 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006012 weston_view_damage_below(*minimized);
6013 }
6014 }
6015 wl_array_release(&switcher->minimized_array);
6016
Kristian Høgsberg07045392012-02-19 18:52:44 -05006017 free(switcher);
6018}
6019
6020static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006021switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006022 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006023{
6024 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006025 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006026
Daniel Stonec9785ea2012-05-30 16:31:52 +01006027 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006028 switcher_next(switcher);
6029}
6030
6031static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006032switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006033 uint32_t mods_depressed, uint32_t mods_latched,
6034 uint32_t mods_locked, uint32_t group)
6035{
6036 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006037 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006038
Daniel Stone351eb612012-05-31 15:27:47 -04006039 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6040 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006041}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006042
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006043static void
6044switcher_cancel(struct weston_keyboard_grab *grab)
6045{
6046 struct switcher *switcher = container_of(grab, struct switcher, grab);
6047
6048 switcher_destroy(switcher);
6049}
6050
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006051static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006052 switcher_key,
6053 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006054 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006055};
6056
6057static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006058switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6059 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006060{
Tiago Vignattibe143262012-04-16 17:31:41 +03006061 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006062 struct switcher *switcher;
6063
6064 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006065 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006066 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006067 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006068 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006069 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006070
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006071 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006072 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006073 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006074 weston_keyboard_start_grab(keyboard, &switcher->grab);
6075 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006076 switcher_next(switcher);
6077}
6078
Pekka Paalanen3c647232011-12-22 13:43:43 +02006079static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006080backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6081 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006082{
6083 struct weston_compositor *compositor = data;
6084 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006085 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006086
6087 /* TODO: we're limiting to simple use cases, where we assume just
6088 * control on the primary display. We'd have to extend later if we
6089 * ever get support for setting backlights on random desktop LCD
6090 * panels though */
6091 output = get_default_output(compositor);
6092 if (!output)
6093 return;
6094
6095 if (!output->set_backlight)
6096 return;
6097
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006098 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6099 backlight_new = output->backlight_current - 25;
6100 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6101 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006102
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006103 if (backlight_new < 5)
6104 backlight_new = 5;
6105 if (backlight_new > 255)
6106 backlight_new = 255;
6107
6108 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006109 output->set_backlight(output, output->backlight_current);
6110}
6111
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006112static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006113force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6114 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006115{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006116 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006117 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006118 struct desktop_shell *shell = data;
6119 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006120 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006121
Derek Foreman8ae2db52015-07-15 13:00:36 -05006122 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006123 if (!focus_surface)
6124 return;
6125
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006126 wl_signal_emit(&compositor->kill_signal, focus_surface);
6127
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006128 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006129 wl_client_get_credentials(client, &pid, NULL, NULL);
6130
6131 /* Skip clients that we launched ourselves (the credentials of
6132 * the socketpair is ours) */
6133 if (pid == getpid())
6134 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006135
Daniel Stone325fc2d2012-05-30 16:31:58 +01006136 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006137}
6138
6139static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006140workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006141 uint32_t key, void *data)
6142{
6143 struct desktop_shell *shell = data;
6144 unsigned int new_index = shell->workspaces.current;
6145
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006146 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006147 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006148 if (new_index != 0)
6149 new_index--;
6150
6151 change_workspace(shell, new_index);
6152}
6153
6154static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006155workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006156 uint32_t key, void *data)
6157{
6158 struct desktop_shell *shell = data;
6159 unsigned int new_index = shell->workspaces.current;
6160
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006161 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006162 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006163 if (new_index < shell->workspaces.num - 1)
6164 new_index++;
6165
6166 change_workspace(shell, new_index);
6167}
6168
6169static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006170workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006171 uint32_t key, void *data)
6172{
6173 struct desktop_shell *shell = data;
6174 unsigned int new_index;
6175
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006176 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006177 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006178 new_index = key - KEY_F1;
6179 if (new_index >= shell->workspaces.num)
6180 new_index = shell->workspaces.num - 1;
6181
6182 change_workspace(shell, new_index);
6183}
6184
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006185static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006186workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6187 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006188{
6189 struct desktop_shell *shell = data;
6190 unsigned int new_index = shell->workspaces.current;
6191
6192 if (shell->locked)
6193 return;
6194
6195 if (new_index != 0)
6196 new_index--;
6197
Derek Foreman8ae2db52015-07-15 13:00:36 -05006198 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006199}
6200
6201static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006202workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6203 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006204{
6205 struct desktop_shell *shell = data;
6206 unsigned int new_index = shell->workspaces.current;
6207
6208 if (shell->locked)
6209 return;
6210
6211 if (new_index < shell->workspaces.num - 1)
6212 new_index++;
6213
Derek Foreman8ae2db52015-07-15 13:00:36 -05006214 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006215}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006216
6217static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006218shell_reposition_view_on_output_destroy(struct weston_view *view)
6219{
6220 struct weston_output *output, *first_output;
6221 struct weston_compositor *ec = view->surface->compositor;
6222 struct shell_surface *shsurf;
6223 float x, y;
6224 int visible;
6225
6226 x = view->geometry.x;
6227 y = view->geometry.y;
6228
6229 /* At this point the destroyed output is not in the list anymore.
6230 * If the view is still visible somewhere, we leave where it is,
6231 * otherwise, move it to the first output. */
6232 visible = 0;
6233 wl_list_for_each(output, &ec->output_list, link) {
6234 if (pixman_region32_contains_point(&output->region,
6235 x, y, NULL)) {
6236 visible = 1;
6237 break;
6238 }
6239 }
6240
6241 if (!visible) {
6242 first_output = container_of(ec->output_list.next,
6243 struct weston_output, link);
6244
6245 x = first_output->x + first_output->width / 4;
6246 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006247
6248 weston_view_set_position(view, x, y);
6249 } else {
6250 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006251 }
6252
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006253
6254 shsurf = get_shell_surface(view->surface);
6255
6256 if (shsurf) {
6257 shsurf->saved_position_valid = false;
6258 shsurf->next_state.maximized = false;
6259 shsurf->next_state.fullscreen = false;
6260 shsurf->state_changed = true;
6261 }
6262}
6263
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006264void
6265shell_for_each_layer(struct desktop_shell *shell,
6266 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006267{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006268 struct workspace **ws;
6269
6270 func(shell, &shell->fullscreen_layer, data);
6271 func(shell, &shell->panel_layer, data);
6272 func(shell, &shell->background_layer, data);
6273 func(shell, &shell->lock_layer, data);
6274 func(shell, &shell->input_panel_layer, data);
6275
6276 wl_array_for_each(ws, &shell->workspaces.array)
6277 func(shell, &(*ws)->layer, data);
6278}
6279
6280static void
6281shell_output_destroy_move_layer(struct desktop_shell *shell,
6282 struct weston_layer *layer,
6283 void *data)
6284{
6285 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006286 struct weston_view *view;
6287
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006288 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006289 if (view->output != output)
6290 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006291
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006292 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006293 }
6294}
6295
6296static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006297handle_output_destroy(struct wl_listener *listener, void *data)
6298{
6299 struct shell_output *output_listener =
6300 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006301 struct weston_output *output = output_listener->output;
6302 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006303
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006304 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006305
Xiong Zhang6b481422013-10-23 13:58:32 +08006306 wl_list_remove(&output_listener->destroy_listener.link);
6307 wl_list_remove(&output_listener->link);
6308 free(output_listener);
6309}
6310
6311static void
6312create_shell_output(struct desktop_shell *shell,
6313 struct weston_output *output)
6314{
6315 struct shell_output *shell_output;
6316
6317 shell_output = zalloc(sizeof *shell_output);
6318 if (shell_output == NULL)
6319 return;
6320
6321 shell_output->output = output;
6322 shell_output->shell = shell;
6323 shell_output->destroy_listener.notify = handle_output_destroy;
6324 wl_signal_add(&output->destroy_signal,
6325 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006326 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006327}
6328
6329static void
6330handle_output_create(struct wl_listener *listener, void *data)
6331{
6332 struct desktop_shell *shell =
6333 container_of(listener, struct desktop_shell, output_create_listener);
6334 struct weston_output *output = (struct weston_output *)data;
6335
6336 create_shell_output(shell, output);
6337}
6338
6339static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006340handle_output_move_layer(struct desktop_shell *shell,
6341 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006342{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006343 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006344 struct weston_view *view;
6345 float x, y;
6346
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006347 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006348 if (view->output != output)
6349 continue;
6350
6351 x = view->geometry.x + output->move_x;
6352 y = view->geometry.y + output->move_y;
6353 weston_view_set_position(view, x, y);
6354 }
6355}
6356
6357static void
6358handle_output_move(struct wl_listener *listener, void *data)
6359{
6360 struct desktop_shell *shell;
6361
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006362 shell = container_of(listener, struct desktop_shell,
6363 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006364
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006365 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006366}
6367
6368static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006369setup_output_destroy_handler(struct weston_compositor *ec,
6370 struct desktop_shell *shell)
6371{
6372 struct weston_output *output;
6373
6374 wl_list_init(&shell->output_list);
6375 wl_list_for_each(output, &ec->output_list, link)
6376 create_shell_output(shell, output);
6377
6378 shell->output_create_listener.notify = handle_output_create;
6379 wl_signal_add(&ec->output_created_signal,
6380 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006381
6382 shell->output_move_listener.notify = handle_output_move;
6383 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006384}
6385
6386static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006387shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006388{
Tiago Vignattibe143262012-04-16 17:31:41 +03006389 struct desktop_shell *shell =
6390 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006391 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006392 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006393
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006394 /* Force state to unlocked so we don't try to fade */
6395 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006396
6397 if (shell->child.client) {
6398 /* disable respawn */
6399 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006400 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006401 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006402
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006403 wl_list_remove(&shell->idle_listener.link);
6404 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006405 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006406
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006407 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006408 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006409
Xiong Zhang6b481422013-10-23 13:58:32 +08006410 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6411 wl_list_remove(&shell_output->destroy_listener.link);
6412 wl_list_remove(&shell_output->link);
6413 free(shell_output);
6414 }
6415
6416 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006417 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006418
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006419 wl_array_for_each(ws, &shell->workspaces.array)
6420 workspace_destroy(*ws);
6421 wl_array_release(&shell->workspaces.array);
6422
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006423 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006424 free(shell);
6425}
6426
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006427static void
6428shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6429{
6430 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006431 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006432
Bob Ham744e6532016-01-12 10:21:48 +00006433 if (shell->allow_zap)
6434 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6435 MODIFIER_CTRL | MODIFIER_ALT,
6436 terminate_binding, ec);
6437
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006438 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006439 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6440 click_to_activate_binding,
6441 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006442 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6443 click_to_activate_binding,
6444 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006445 weston_compositor_add_touch_binding(ec, 0,
6446 touch_to_activate_binding,
6447 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006448 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6449 MODIFIER_SUPER | MODIFIER_ALT,
6450 surface_opacity_binding, NULL);
6451 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6452 MODIFIER_SUPER, zoom_axis_binding,
6453 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006454 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6455 backlight_binding, ec);
6456 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6457 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006458
6459 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006460 if (shell->exposay_modifier)
6461 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6462 exposay_binding, shell);
6463
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006464 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006465 if (!mod)
6466 return;
6467
Daniel Stone325fc2d2012-05-30 16:31:58 +01006468 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6469 zoom_key_binding, NULL);
6470 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6471 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006472 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6473 maximize_binding, NULL);
6474 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6475 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006476 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6477 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006478 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006479 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006480 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006481 weston_compositor_add_button_binding(ec, BTN_LEFT,
6482 mod | MODIFIER_SHIFT,
6483 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006484
6485 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006486 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006487 rotate_binding, NULL);
6488
Daniel Stone325fc2d2012-05-30 16:31:58 +01006489 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6490 shell);
6491 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6492 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006493 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6494 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006495 weston_compositor_add_key_binding(ec, KEY_K, mod,
6496 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006497 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6498 workspace_up_binding, shell);
6499 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6500 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006501 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6502 workspace_move_surface_up_binding,
6503 shell);
6504 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6505 workspace_move_surface_down_binding,
6506 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006507
6508 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6509 if (shell->workspaces.num > 1) {
6510 num_workspace_bindings = shell->workspaces.num;
6511 if (num_workspace_bindings > 6)
6512 num_workspace_bindings = 6;
6513 for (i = 0; i < num_workspace_bindings; i++)
6514 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6515 workspace_f_binding,
6516 shell);
6517 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006518
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006519 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006520}
6521
Jason Ekstrand024177c2014-04-21 19:42:58 -05006522static void
6523handle_seat_created(struct wl_listener *listener, void *data)
6524{
6525 struct weston_seat *seat = data;
6526
6527 create_shell_seat(seat);
6528}
6529
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006530WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006531module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006532 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006533{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006534 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006535 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006536 struct workspace **pws;
6537 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006538 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006539
Peter Huttererf3d62272013-08-08 11:57:05 +10006540 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006541 if (shell == NULL)
6542 return -1;
6543
Kristian Høgsberg75840622011-09-06 13:48:16 -04006544 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006545
6546 shell->destroy_listener.notify = shell_destroy;
6547 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006548 shell->idle_listener.notify = idle_handler;
6549 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6550 shell->wake_listener.notify = wake_handler;
6551 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006552 shell->transform_listener.notify = transform_handler;
6553 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006554
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006555 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006556 ec->shell_interface.create_shell_surface = create_shell_surface;
6557 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006558 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006559 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006560 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006561 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006562 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006563 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006564 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006565 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006566 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006567
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006568 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6569 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006570 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6571 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006572 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006573
6574 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006575 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006576
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006577 if (input_panel_setup(shell) < 0)
6578 return -1;
6579
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006580 shell->text_backend = text_backend_init(ec);
6581 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006582 return -1;
6583
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006584 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006585
Daniel Stonedf8133b2013-11-19 11:37:14 +01006586 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6587 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6588
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006589 for (i = 0; i < shell->workspaces.num; i++) {
6590 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6591 if (pws == NULL)
6592 return -1;
6593
6594 *pws = workspace_create();
6595 if (*pws == NULL)
6596 return -1;
6597 }
6598 activate_workspace(shell, 0);
6599
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006600 weston_layer_init(&shell->minimized_layer, NULL);
6601
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006602 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006603 wl_list_init(&shell->workspaces.animation.link);
6604 shell->workspaces.animation.frame = animate_workspace_change_frame;
6605
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006606 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006607 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006608 return -1;
6609
Rafael Antognollie2a34552013-12-03 15:35:45 -02006610 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6611 shell, bind_xdg_shell) == NULL)
6612 return -1;
6613
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006614 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006615 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006616 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006617 return -1;
6618
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006619 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006620
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006621 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006622
Xiong Zhang6b481422013-10-23 13:58:32 +08006623 setup_output_destroy_handler(ec, shell);
6624
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006625 loop = wl_display_get_event_loop(ec->wl_display);
6626 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006627
Jason Ekstrand024177c2014-04-21 19:42:58 -05006628 wl_list_for_each(seat, &ec->seat_list, link)
6629 handle_seat_created(NULL, seat);
6630 shell->seat_create_listener.notify = handle_seat_created;
6631 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006632
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006633 screenshooter_create(ec);
6634
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006635 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006636
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006637 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006638
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006639 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6640
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006641 return 0;
6642}