blob: 24437d8de84e164585c8f61cfeb465773e0b6a01 [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,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001601 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001602{
1603}
1604
1605static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001606noop_grab_axis_source(struct weston_pointer_grab *grab,
1607 uint32_t source)
1608{
1609}
1610
1611static void
1612noop_grab_frame(struct weston_pointer_grab *grab)
1613{
1614}
1615
1616static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001617constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1618{
1619 struct shell_surface *shsurf = move->base.shsurf;
1620 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001621 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001622 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001623 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001624
1625 x = wl_fixed_to_int(pointer->x + move->dx);
1626 y = wl_fixed_to_int(pointer->y + move->dy);
1627
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001628 if (shsurf->shell->panel_position ==
1629 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001630 get_output_work_area(shsurf->shell,
1631 shsurf->surface->output,
1632 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001633
Derek Foreman6feb0f92015-04-15 12:23:56 -05001634 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1635 if (bottom - safety < area.y)
1636 y = area.y + safety - shsurf->geometry.height
1637 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001638
1639 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001640 y + shsurf->geometry.y < area.y)
1641 y = area.y - shsurf->geometry.y;
1642
Jonny Lambd73c6942014-08-20 15:53:20 +02001643 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001644
1645 *cx = x;
1646 *cy = y;
1647}
1648
1649static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001650move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001651 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001652{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001653 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001655 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001656 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001657
Jonas Ådahld2510102014-10-05 21:39:14 +02001658 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001659 if (!shsurf)
1660 return;
1661
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001662 constrain_position(move, &cx, &cy);
1663
1664 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001667}
1668
1669static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001670move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001671 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001672{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001673 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1674 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001675 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001676 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001677
Daniel Stone4dbadb12012-05-30 16:31:51 +01001678 if (pointer->button_count == 0 &&
1679 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001680 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001681 free(grab);
1682 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001683}
1684
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001685static void
1686move_grab_cancel(struct weston_pointer_grab *grab)
1687{
1688 struct shell_grab *shell_grab =
1689 container_of(grab, struct shell_grab, grab);
1690
1691 shell_grab_end(shell_grab);
1692 free(grab);
1693}
1694
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001695static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001696 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001697 move_grab_motion,
1698 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001699 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001700 noop_grab_axis_source,
1701 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001702 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001703};
1704
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001706surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001707 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708{
1709 struct weston_move_grab *move;
1710
1711 if (!shsurf)
1712 return -1;
1713
Derek Foreman45a7c272015-09-11 14:27:40 -05001714 shsurf = find_toplevel_surface(shsurf);
1715
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001716 if (shsurf->grabbed ||
1717 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001718 return 0;
1719
1720 move = malloc(sizeof *move);
1721 if (!move)
1722 return -1;
1723
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001725 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001726 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001727 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001728 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001729
1730 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001731 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732
1733 return 0;
1734}
1735
1736static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001737common_surface_move(struct wl_resource *resource,
1738 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001740 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001741 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1742 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001743 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001744 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745
Derek Foreman1281a362015-07-31 16:55:32 -05001746 if (pointer &&
1747 pointer->focus &&
1748 pointer->button_count > 0 &&
1749 pointer->grab_serial == serial) {
1750 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001751 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001752 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001753 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001754 } else if (touch &&
1755 touch->focus &&
1756 touch->grab_serial == serial) {
1757 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001758 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001759 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001760 wl_resource_post_no_memory(resource);
1761 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762}
1763
Rafael Antognollie2a34552013-12-03 15:35:45 -02001764static void
1765shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1766 struct wl_resource *seat_resource, uint32_t serial)
1767{
1768 common_surface_move(resource, seat_resource, serial);
1769}
1770
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771struct weston_resize_grab {
1772 struct shell_grab base;
1773 uint32_t edges;
1774 int32_t width, height;
1775};
1776
1777static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001778resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001779 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001780{
1781 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001782 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001783 struct shell_surface *shsurf = resize->base.shsurf;
1784 int32_t width, height;
1785 wl_fixed_t from_x, from_y;
1786 wl_fixed_t to_x, to_y;
1787
Jonas Ådahld2510102014-10-05 21:39:14 +02001788 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001789
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001790 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001791 return;
1792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 weston_view_from_global_fixed(shsurf->view,
1794 pointer->grab_x, pointer->grab_y,
1795 &from_x, &from_y);
1796 weston_view_from_global_fixed(shsurf->view,
1797 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001798
1799 width = resize->width;
1800 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1801 width += wl_fixed_to_int(from_x - to_x);
1802 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1803 width += wl_fixed_to_int(to_x - from_x);
1804 }
1805
1806 height = resize->height;
1807 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1808 height += wl_fixed_to_int(from_y - to_y);
1809 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1810 height += wl_fixed_to_int(to_y - from_y);
1811 }
1812
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001813 if (width < 1)
1814 width = 1;
1815 if (height < 1)
1816 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001817 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001818}
1819
1820static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001821send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001822{
1823 struct shell_surface *shsurf = get_shell_surface(surface);
1824
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001825 assert(shsurf);
1826
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001827 if (shsurf->resource)
1828 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001829 shsurf->resize_edges,
1830 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001831}
1832
1833static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001834 send_configure,
1835 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001836};
1837
1838static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001839resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001840 uint32_t time, uint32_t button, uint32_t state_w)
1841{
1842 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001843 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001844 enum wl_pointer_button_state state = state_w;
1845
1846 if (pointer->button_count == 0 &&
1847 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1848 shell_grab_end(&resize->base);
1849 free(grab);
1850 }
1851}
1852
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001853static void
1854resize_grab_cancel(struct weston_pointer_grab *grab)
1855{
1856 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1857
1858 shell_grab_end(&resize->base);
1859 free(grab);
1860}
1861
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001862static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001863 noop_grab_focus,
1864 resize_grab_motion,
1865 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001866 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001867 noop_grab_axis_source,
1868 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001869 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001870};
1871
Giulio Camuffob8366642013-04-25 13:57:46 +03001872/*
1873 * Returns the bounding box of a surface and all its sub-surfaces,
1874 * in the surface coordinates system. */
1875static void
1876surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1877 int32_t *y, int32_t *w, int32_t *h) {
1878 pixman_region32_t region;
1879 pixman_box32_t *box;
1880 struct weston_subsurface *subsurface;
1881
1882 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 surface->width,
1884 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001885
1886 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1887 pixman_region32_union_rect(&region, &region,
1888 subsurface->position.x,
1889 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890 subsurface->surface->width,
1891 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001892 }
1893
1894 box = pixman_region32_extents(&region);
1895 if (x)
1896 *x = box->x1;
1897 if (y)
1898 *y = box->y1;
1899 if (w)
1900 *w = box->x2 - box->x1;
1901 if (h)
1902 *h = box->y2 - box->y1;
1903
1904 pixman_region32_fini(&region);
1905}
1906
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001907static int
1908surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001909 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001910{
1911 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001912 const unsigned resize_topbottom =
1913 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1914 const unsigned resize_leftright =
1915 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1916 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001917
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001918 if (shsurf->grabbed ||
1919 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001920 return 0;
1921
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001922 /* Check for invalid edge combinations. */
1923 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1924 (edges & resize_topbottom) == resize_topbottom ||
1925 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001926 return 0;
1927
1928 resize = malloc(sizeof *resize);
1929 if (!resize)
1930 return -1;
1931
1932 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001933
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001934 resize->width = shsurf->geometry.width;
1935 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001936
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001937 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001938 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001939 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001940 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001941
1942 return 0;
1943}
1944
1945static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001946common_surface_resize(struct wl_resource *resource,
1947 struct wl_resource *seat_resource, uint32_t serial,
1948 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001949{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001950 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001951 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001952 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001953 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001954
Derek Foreman1281a362015-07-31 16:55:32 -05001955 if (!pointer ||
1956 pointer->button_count == 0 ||
1957 pointer->grab_serial != serial ||
1958 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001959 return;
1960
Derek Foreman1281a362015-07-31 16:55:32 -05001961 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001962 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001963 return;
1964
Derek Foreman1281a362015-07-31 16:55:32 -05001965 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001966 wl_resource_post_no_memory(resource);
1967}
1968
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001969static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001970shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1971 struct wl_resource *seat_resource, uint32_t serial,
1972 uint32_t edges)
1973{
1974 common_surface_resize(resource, seat_resource, serial, edges);
1975}
1976
1977static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001978busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001979{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001980 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001981 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001983 wl_fixed_t sx, sy;
1984
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985 view = weston_compositor_pick_view(pointer->seat->compositor,
1986 pointer->x, pointer->y,
1987 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001988
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001989 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1990 shell_grab_end(grab);
1991 free(grab);
1992 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001993}
1994
1995static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001996busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001997 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001998{
Jonas Ådahld2510102014-10-05 21:39:14 +02001999 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002000}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002001
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002002static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002003busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002004 uint32_t time, uint32_t button, uint32_t state)
2005{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002006 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002007 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002008 struct weston_pointer *pointer = grab->grab.pointer;
2009 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002010
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002011 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002012 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002013 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002014 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002015 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002016 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002017 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002018}
2019
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002020static void
2021busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2022{
2023 struct shell_grab *grab = (struct shell_grab *) base;
2024
2025 shell_grab_end(grab);
2026 free(grab);
2027}
2028
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002029static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002030 busy_cursor_grab_focus,
2031 busy_cursor_grab_motion,
2032 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002033 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002034 noop_grab_axis_source,
2035 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002036 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002037};
2038
2039static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002040set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041{
2042 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002044 if (pointer->grab->interface == &busy_cursor_grab_interface)
2045 return;
2046
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047 grab = malloc(sizeof *grab);
2048 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002049 return;
2050
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002051 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002052 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002053 /* Mark the shsurf as ungrabbed so that button binding is able
2054 * to move it. */
2055 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002057
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002058static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002059end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002060{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002061 struct shell_grab *grab;
2062 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002063
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002064 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002065 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2066
2067 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002068 continue;
2069
Derek Foreman1281a362015-07-31 16:55:32 -05002070 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002071 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002072 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002073 wl_resource_get_client(grab->shsurf->resource) == client) {
2074 shell_grab_end(grab);
2075 free(grab);
2076 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002077 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002078}
2079
Scott Moreau9521d5e2012-04-19 13:06:17 -06002080static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002081handle_shell_client_destroy(struct wl_listener *listener, void *data);
2082
2083static int
2084xdg_ping_timeout_handler(void *data)
2085{
2086 struct shell_client *sc = data;
2087 struct weston_seat *seat;
2088 struct shell_surface *shsurf;
2089
2090 /* Client is not responding */
2091 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002092 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002093 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2094
2095 if (!pointer ||
2096 !pointer->focus ||
2097 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002098 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002099
Derek Foreman1281a362015-07-31 16:55:32 -05002100 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002101 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002102 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002103 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002104 }
2105
2106 return 1;
2107}
2108
2109static void
2110handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2111{
2112 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002113 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002114 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002115 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002116
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002117 if (sc->unresponsive) {
2118 xdg_ping_timeout_handler(sc);
2119 return;
2120 }
2121
2122 sc->ping_serial = serial;
2123 loop = wl_display_get_event_loop(compositor->wl_display);
2124 if (sc->ping_timer == NULL)
2125 sc->ping_timer =
2126 wl_event_loop_add_timer(loop,
2127 xdg_ping_timeout_handler, sc);
2128 if (sc->ping_timer == NULL)
2129 return;
2130
2131 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2132
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002133 if (shell_surface_is_xdg_surface(shsurf) ||
2134 shell_surface_is_xdg_popup(shsurf))
2135 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002136 else if (shell_surface_is_wl_shell_surface(shsurf)
2137 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002138 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002139}
2140
Scott Moreauff1db4a2012-04-17 19:06:18 -06002141static void
2142ping_handler(struct weston_surface *surface, uint32_t serial)
2143{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002144 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002145
2146 if (!shsurf)
2147 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002148 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002149 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002150 if (shsurf->surface == shsurf->shell->grab_surface)
2151 return;
2152
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002153 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002154}
2155
2156static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002157handle_pointer_focus(struct wl_listener *listener, void *data)
2158{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002159 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002161 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002162 uint32_t serial;
2163
Jason Ekstranda7af7042013-10-12 22:38:11 -05002164 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002165 return;
2166
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002168 serial = wl_display_next_serial(compositor->wl_display);
2169 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002170}
2171
2172static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002173shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2174{
2175 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002176 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002177}
2178
2179static void
2180shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2181{
2182 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002183 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002184}
2185
2186static void
2187handle_keyboard_focus(struct wl_listener *listener, void *data)
2188{
2189 struct weston_keyboard *keyboard = data;
2190 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2191
2192 if (seat->focused_surface) {
2193 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2194 if (shsurf)
2195 shell_surface_lose_keyboard_focus(shsurf);
2196 }
2197
2198 seat->focused_surface = keyboard->focus;
2199
2200 if (seat->focused_surface) {
2201 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2202 if (shsurf)
2203 shell_surface_gain_keyboard_focus(shsurf);
2204 }
2205}
2206
2207static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002208shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002209{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002210 if (sc->ping_serial != serial)
2211 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002212
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002213 sc->unresponsive = 0;
2214 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002215
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002216 if (sc->ping_timer) {
2217 wl_event_source_remove(sc->ping_timer);
2218 sc->ping_timer = NULL;
2219 }
2220
2221}
2222
2223static void
2224shell_surface_pong(struct wl_client *client,
2225 struct wl_resource *resource, uint32_t serial)
2226{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002227 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2228 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002229
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002230 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002231}
2232
2233static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002234set_title(struct shell_surface *shsurf, const char *title)
2235{
2236 free(shsurf->title);
2237 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002238 shsurf->surface->timeline.force_refresh = 1;
2239}
2240
2241static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002242set_pid(struct shell_surface *shsurf, pid_t pid)
2243{
2244 /* We have no use for it */
2245}
2246
2247static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002248set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2249{
2250 shsurf->type = t;
2251 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002252}
2253
2254static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002255set_window_geometry(struct shell_surface *shsurf,
2256 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002257{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002258 shsurf->next_geometry.x = x;
2259 shsurf->next_geometry.y = y;
2260 shsurf->next_geometry.width = width;
2261 shsurf->next_geometry.height = height;
2262 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002263}
2264
2265static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002266shell_surface_set_title(struct wl_client *client,
2267 struct wl_resource *resource, const char *title)
2268{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002269 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002270
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002271 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002272}
2273
2274static void
2275shell_surface_set_class(struct wl_client *client,
2276 struct wl_resource *resource, const char *class)
2277{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002278 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002279
2280 free(shsurf->class);
2281 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002282 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002283}
2284
Alex Wu4539b082012-03-01 12:57:46 +08002285static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002286restore_output_mode(struct weston_output *output)
2287{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002288 if (output->original_mode)
2289 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002290}
2291
2292static void
2293restore_all_output_modes(struct weston_compositor *compositor)
2294{
2295 struct weston_output *output;
2296
2297 wl_list_for_each(output, &compositor->output_list, link)
2298 restore_output_mode(output);
2299}
2300
Philip Withnall07926d92013-11-25 18:01:40 +00002301/* The surface will be inserted into the list immediately after the link
2302 * returned by this function (i.e. will be stacked immediately above the
2303 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002304static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002305shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2306{
2307 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002308 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002309
2310 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002311 case SHELL_SURFACE_XWAYLAND:
2312 return &shsurf->shell->fullscreen_layer.view_list;
2313
2314 case SHELL_SURFACE_NONE:
2315 return NULL;
2316
Kristian Høgsbergead52422014-01-01 13:51:52 -08002317 case SHELL_SURFACE_POPUP:
2318 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002319 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002320 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002321 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002322 /* Move the surface to its parent layer so
2323 * that surfaces which are transient for
2324 * fullscreen surfaces don't get hidden by the
2325 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002326
2327 /* TODO: Handle a parent with multiple views */
2328 parent = get_default_view(shsurf->parent);
2329 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002330 return container_of(parent->layer_link.link.prev,
2331 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002332 }
Philip Withnall07926d92013-11-25 18:01:40 +00002333
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002334 /* Move the surface to a normal workspace layer so that surfaces
2335 * which were previously fullscreen or transient are no longer
2336 * rendered on top. */
2337 ws = get_current_workspace(shsurf->shell);
2338 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002339 }
2340
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002341 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002342}
2343
Philip Withnall648a4dd2013-11-25 18:01:44 +00002344static void
2345shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2346{
2347 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002348 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002349
2350 /* Move the child layers to the same workspace as shsurf. They will be
2351 * stacked above shsurf. */
2352 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002353 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002354 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002355 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002356 prev = container_of(shsurf->view->layer_link.link.prev,
2357 struct weston_layer_entry, link);
2358 weston_layer_entry_remove(&child->view->layer_link);
2359 weston_layer_entry_insert(prev,
2360 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002361 weston_view_geometry_dirty(child->view);
2362 weston_surface_damage(child->surface);
2363
2364 /* Recurse. We don’t expect this to recurse very far (if
2365 * at all) because that would imply we have transient
2366 * (or popup) children of transient surfaces, which
2367 * would be unusual. */
2368 shell_surface_update_child_surface_layers(child);
2369 }
2370 }
2371}
2372
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002373/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002374 * geometry to ensure the changes are redrawn.
2375 *
2376 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2377 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002378static void
2379shell_surface_update_layer(struct shell_surface *shsurf)
2380{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002381 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002382
2383 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2384
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002385 if (new_layer_link == NULL)
2386 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002387 if (new_layer_link == &shsurf->view->layer_link)
2388 return;
2389
2390 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002391 weston_layer_entry_remove(&shsurf->view->layer_link);
2392 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002393 weston_view_geometry_dirty(shsurf->view);
2394 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002395
2396 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002397}
2398
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002399static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002400shell_surface_set_parent(struct shell_surface *shsurf,
2401 struct weston_surface *parent)
2402{
2403 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002404
2405 wl_list_remove(&shsurf->children_link);
2406 wl_list_init(&shsurf->children_link);
2407
2408 /* Insert into the parent surface’s child list. */
2409 if (parent != NULL) {
2410 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2411 if (parent_shsurf != NULL)
2412 wl_list_insert(&parent_shsurf->children_list,
2413 &shsurf->children_link);
2414 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002415}
2416
2417static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002418shell_surface_set_output(struct shell_surface *shsurf,
2419 struct weston_output *output)
2420{
2421 struct weston_surface *es = shsurf->surface;
2422
2423 /* get the default output, if the client set it as NULL
2424 check whether the ouput is available */
2425 if (output)
2426 shsurf->output = output;
2427 else if (es->output)
2428 shsurf->output = es->output;
2429 else
2430 shsurf->output = get_default_output(es->compositor);
2431}
2432
2433static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002434surface_clear_next_states(struct shell_surface *shsurf)
2435{
2436 shsurf->next_state.maximized = false;
2437 shsurf->next_state.fullscreen = false;
2438
2439 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2440 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2441 shsurf->state_changed = true;
2442}
2443
2444static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002445set_toplevel(struct shell_surface *shsurf)
2446{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002447 shell_surface_set_parent(shsurf, NULL);
2448 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002449 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002450
2451 /* The layer_link is updated in set_surface_type(),
2452 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002453}
2454
2455static void
2456shell_surface_set_toplevel(struct wl_client *client,
2457 struct wl_resource *resource)
2458{
2459 struct shell_surface *surface = wl_resource_get_user_data(resource);
2460
2461 set_toplevel(surface);
2462}
2463
2464static void
2465set_transient(struct shell_surface *shsurf,
2466 struct weston_surface *parent, int x, int y, uint32_t flags)
2467{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002468 assert(parent != NULL);
2469
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002470 shell_surface_set_parent(shsurf, parent);
2471
2472 surface_clear_next_states(shsurf);
2473
Philip Withnallbecb77e2013-11-25 18:01:30 +00002474 shsurf->transient.x = x;
2475 shsurf->transient.y = y;
2476 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002477
Rafael Antognollied207b42013-12-03 15:35:43 -02002478 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002479 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002480 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002481
2482 /* The layer_link is updated in set_surface_type(),
2483 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002484}
2485
2486static void
2487shell_surface_set_transient(struct wl_client *client,
2488 struct wl_resource *resource,
2489 struct wl_resource *parent_resource,
2490 int x, int y, uint32_t flags)
2491{
2492 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2493 struct weston_surface *parent =
2494 wl_resource_get_user_data(parent_resource);
2495
2496 set_transient(shsurf, parent, x, y, flags);
2497}
2498
2499static void
2500set_fullscreen(struct shell_surface *shsurf,
2501 uint32_t method,
2502 uint32_t framerate,
2503 struct weston_output *output)
2504{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002505 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002506 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002507
2508 shsurf->fullscreen_output = shsurf->output;
2509 shsurf->fullscreen.type = method;
2510 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002511
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002512 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002513}
2514
2515static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002516weston_view_set_initial_position(struct weston_view *view,
2517 struct desktop_shell *shell);
2518
2519static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002520unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002521{
Philip Withnallf85fe842013-11-25 18:01:37 +00002522 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002523 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2524 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002525 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002526 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002527
Alex Wu4539b082012-03-01 12:57:46 +08002528 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2529 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002530
Alex Wu4539b082012-03-01 12:57:46 +08002531 wl_list_remove(&shsurf->fullscreen.transform.link);
2532 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002533
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534 if (shsurf->fullscreen.black_view)
2535 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2536 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002537
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002538 if (shsurf->saved_position_valid)
2539 weston_view_set_position(shsurf->view,
2540 shsurf->saved_x, shsurf->saved_y);
2541 else
2542 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2543
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002544 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002546 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002547 shsurf->saved_rotation_valid = false;
2548 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002549
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002550 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002551}
2552
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002553static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002554shell_surface_set_fullscreen(struct wl_client *client,
2555 struct wl_resource *resource,
2556 uint32_t method,
2557 uint32_t framerate,
2558 struct wl_resource *output_resource)
2559{
2560 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2561 struct weston_output *output;
2562
2563 if (output_resource)
2564 output = wl_resource_get_user_data(output_resource);
2565 else
2566 output = NULL;
2567
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002568 shell_surface_set_parent(shsurf, NULL);
2569
Rafael Antognolli03b16592013-12-03 15:35:42 -02002570 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002571 shsurf->next_state.fullscreen = true;
2572 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002573 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574}
2575
2576static void
2577set_popup(struct shell_surface *shsurf,
2578 struct weston_surface *parent,
2579 struct weston_seat *seat,
2580 uint32_t serial,
2581 int32_t x,
2582 int32_t y)
2583{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002584 assert(parent != NULL);
2585
Philip Withnallbecb77e2013-11-25 18:01:30 +00002586 shsurf->popup.shseat = get_shell_seat(seat);
2587 shsurf->popup.serial = serial;
2588 shsurf->popup.x = x;
2589 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002590
Pekka Paalanenb5026542014-11-12 15:09:24 +02002591 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592}
2593
2594static void
2595shell_surface_set_popup(struct wl_client *client,
2596 struct wl_resource *resource,
2597 struct wl_resource *seat_resource,
2598 uint32_t serial,
2599 struct wl_resource *parent_resource,
2600 int32_t x, int32_t y, uint32_t flags)
2601{
2602 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002603 struct weston_surface *parent =
2604 wl_resource_get_user_data(parent_resource);
2605
2606 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002607
Rafael Antognolli03b16592013-12-03 15:35:42 -02002608 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002609 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002610 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002611 wl_resource_get_user_data(seat_resource),
2612 serial, x, y);
2613}
2614
2615static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002616unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002617{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002618 /* undo all maximized things here */
2619 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002620
2621 if (shsurf->saved_position_valid)
2622 weston_view_set_position(shsurf->view,
2623 shsurf->saved_x, shsurf->saved_y);
2624 else
2625 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002626
2627 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 wl_list_insert(&shsurf->view->geometry.transformation_list,
2629 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002630 shsurf->saved_rotation_valid = false;
2631 }
2632
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002633 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002634}
2635
Philip Withnallbecb77e2013-11-25 18:01:30 +00002636static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002637set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002638{
2639 struct shell_surface *shsurf;
2640 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002641 struct weston_view *view;
2642
2643 view = get_default_view(surface);
2644 if (!view)
2645 return;
2646
2647 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2648
2649 shsurf = get_shell_surface(surface);
2650 current_ws = get_current_workspace(shsurf->shell);
2651
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002652 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002653 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002654
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002655 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002656 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002657
2658 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002659 weston_view_damage_below(view);
2660}
2661
2662static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002663shell_surface_set_maximized(struct wl_client *client,
2664 struct wl_resource *resource,
2665 struct wl_resource *output_resource)
2666{
2667 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2668 struct weston_output *output;
2669
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002670 surface_clear_next_states(shsurf);
2671 shsurf->next_state.maximized = true;
2672 shsurf->state_changed = true;
2673
Pekka Paalanenb5026542014-11-12 15:09:24 +02002674 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002675 shell_surface_set_parent(shsurf, NULL);
2676
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677 if (output_resource)
2678 output = wl_resource_get_user_data(output_resource);
2679 else
2680 output = NULL;
2681
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002682 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002683
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002684 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002685}
2686
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002687/* This is only ever called from set_surface_type(), so there’s no need to
2688 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002689static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002690reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002691{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002692 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002693 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002694 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002695 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002696
Pekka Paalanen98262232011-12-01 10:42:22 +02002697 return 0;
2698}
2699
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002700static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002701set_full_output(struct shell_surface *shsurf)
2702{
2703 shsurf->saved_x = shsurf->view->geometry.x;
2704 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002705 shsurf->saved_width = shsurf->surface->width;
2706 shsurf->saved_height = shsurf->surface->height;
2707 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002708 shsurf->saved_position_valid = true;
2709
2710 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2711 wl_list_remove(&shsurf->rotation.transform.link);
2712 wl_list_init(&shsurf->rotation.transform.link);
2713 weston_view_geometry_dirty(shsurf->view);
2714 shsurf->saved_rotation_valid = true;
2715 }
2716}
2717
2718static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002719set_surface_type(struct shell_surface *shsurf)
2720{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002721 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002723
Philip Withnallbecb77e2013-11-25 18:01:30 +00002724 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002725
Rafael Antognolli03b16592013-12-03 15:35:42 -02002726 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002727 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002728
2729 switch (shsurf->type) {
2730 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002731 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002732 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002733 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734 weston_view_set_position(shsurf->view,
2735 pev->geometry.x + shsurf->transient.x,
2736 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002737 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002738 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002739
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002740 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741 weston_view_set_position(shsurf->view, shsurf->transient.x,
2742 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002743 break;
2744
Philip Withnall0f640e22013-11-25 18:01:31 +00002745 case SHELL_SURFACE_POPUP:
2746 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002747 default:
2748 break;
2749 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002750
2751 /* Update the surface’s layer. */
2752 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002753}
2754
Tiago Vignattibe143262012-04-16 17:31:41 +03002755static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002756shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002757{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002758 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002759}
2760
Pekka Paalanen8274d902014-08-06 19:36:51 +03002761static int
2762black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2763{
2764 struct weston_surface *fs_surface = surface->configure_private;
2765 int n;
2766 int rem;
2767 int ret;
2768
2769 n = snprintf(buf, len, "black background surface for ");
2770 if (n < 0)
2771 return n;
2772
2773 rem = (int)len - n;
2774 if (rem < 0)
2775 rem = 0;
2776
2777 if (fs_surface->get_label)
2778 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2779 else
2780 ret = snprintf(buf + n, rem, "<unknown>");
2781
2782 if (ret < 0)
2783 return n;
2784
2785 return n + ret;
2786}
2787
Alex Wu21858432012-04-01 20:13:08 +08002788static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002789black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002790
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002792create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002793 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002794 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002795{
2796 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002798
2799 surface = weston_surface_create(ec);
2800 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002801 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002802 return NULL;
2803 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002804 view = weston_view_create(surface);
2805 if (surface == NULL) {
2806 weston_log("no memory\n");
2807 weston_surface_destroy(surface);
2808 return NULL;
2809 }
Alex Wu4539b082012-03-01 12:57:46 +08002810
Alex Wu21858432012-04-01 20:13:08 +08002811 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002812 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002813 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002814 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002815 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002816 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002817 pixman_region32_fini(&surface->input);
2818 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002819
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002820 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002821 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822
2823 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002824}
2825
Philip Withnalled8f1a92013-11-25 18:01:43 +00002826static void
2827shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2828{
2829 struct weston_output *output = shsurf->fullscreen_output;
2830
Rafael Antognolli03b16592013-12-03 15:35:42 -02002831 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002832
2833 if (!shsurf->fullscreen.black_view)
2834 shsurf->fullscreen.black_view =
2835 create_black_surface(shsurf->surface->compositor,
2836 shsurf->surface,
2837 output->x, output->y,
2838 output->width,
2839 output->height);
2840
2841 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002842 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2843 weston_layer_entry_insert(&shsurf->view->layer_link,
2844 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002845 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2846 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002847
2848 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002849}
2850
Alex Wu4539b082012-03-01 12:57:46 +08002851/* Create black surface and append it to the associated fullscreen surface.
2852 * Handle size dismatch and positioning according to the method. */
2853static void
2854shell_configure_fullscreen(struct shell_surface *shsurf)
2855{
2856 struct weston_output *output = shsurf->fullscreen_output;
2857 struct weston_surface *surface = shsurf->surface;
2858 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002859 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002860 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002861
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002862 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2863 restore_output_mode(output);
2864
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002865 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002866 weston_layer_entry_remove(&shsurf->view->layer_link);
2867 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002868
Philip Withnalled8f1a92013-11-25 18:01:43 +00002869 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002870
Jason Ekstranda7af7042013-10-12 22:38:11 -05002871 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002872 &surf_width, &surf_height);
2873
Alex Wu4539b082012-03-01 12:57:46 +08002874 switch (shsurf->fullscreen.type) {
2875 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002876 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002877 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002878 break;
2879 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002880 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002881 if (output->width == surf_width &&
2882 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002883 weston_view_set_position(shsurf->view,
2884 output->x - surf_x,
2885 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002886 break;
2887 }
2888
Alex Wu4539b082012-03-01 12:57:46 +08002889 matrix = &shsurf->fullscreen.transform.matrix;
2890 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002891
Scott Moreau1bad5db2012-08-18 01:04:05 -06002892 output_aspect = (float) output->width /
2893 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002894 /* XXX: Use surf_width and surf_height here? */
2895 surface_aspect = (float) surface->width /
2896 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002897 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002898 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002899 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002900 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002901 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002902 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002903
Alex Wu4539b082012-03-01 12:57:46 +08002904 weston_matrix_scale(matrix, scale, scale, 1);
2905 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002906 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002907 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002908 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2909 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002910 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002911
Alex Wu4539b082012-03-01 12:57:46 +08002912 break;
2913 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002914 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002915 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002916 surf_width * surface->buffer_viewport.buffer.scale,
2917 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002918 shsurf->fullscreen.framerate};
2919
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002920 if (weston_output_mode_switch_to_temporary(output, &mode,
2921 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002922 weston_view_set_position(shsurf->view,
2923 output->x - surf_x,
2924 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002925 shsurf->fullscreen.black_view->surface->width = output->width;
2926 shsurf->fullscreen.black_view->surface->height = output->height;
2927 weston_view_set_position(shsurf->fullscreen.black_view,
2928 output->x - surf_x,
2929 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002930 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002931 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002932 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002933 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002935 }
Alex Wubd3354b2012-04-17 17:20:49 +08002936 }
Alex Wu4539b082012-03-01 12:57:46 +08002937 break;
2938 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002940 break;
2941 default:
2942 break;
2943 }
2944}
2945
Alex Wu4539b082012-03-01 12:57:46 +08002946static void
2947shell_map_fullscreen(struct shell_surface *shsurf)
2948{
Alex Wubd3354b2012-04-17 17:20:49 +08002949 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002950}
2951
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002952static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002953set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2954{
2955 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002956 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002957 shsurf->transient.x = x;
2958 shsurf->transient.y = y;
2959 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002960
2961 shell_surface_set_parent(shsurf, NULL);
2962
Pekka Paalanenb5026542014-11-12 15:09:24 +02002963 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002964 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002965}
2966
Kristian Høgsberg47792412014-05-04 13:47:06 -07002967static void
2968shell_interface_set_fullscreen(struct shell_surface *shsurf,
2969 uint32_t method,
2970 uint32_t framerate,
2971 struct weston_output *output)
2972{
2973 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002974 shsurf->next_state.fullscreen = true;
2975 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002976
2977 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002978}
2979
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002980static struct weston_output *
2981get_focused_output(struct weston_compositor *compositor)
2982{
2983 struct weston_seat *seat;
2984 struct weston_output *output = NULL;
2985
2986 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002987 struct weston_touch *touch = weston_seat_get_touch(seat);
2988 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2989 struct weston_keyboard *keyboard =
2990 weston_seat_get_keyboard(seat);
2991
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002992 /* Priority has touch focus, then pointer and
2993 * then keyboard focus. We should probably have
2994 * three for loops and check frist for touch,
2995 * then for pointer, etc. but unless somebody has some
2996 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002997 if (touch && touch->focus)
2998 output = touch->focus->output;
2999 else if (pointer && pointer->focus)
3000 output = pointer->focus->output;
3001 else if (keyboard && keyboard->focus)
3002 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003003
3004 if (output)
3005 break;
3006 }
3007
3008 return output;
3009}
3010
3011static void
3012shell_interface_set_maximized(struct shell_surface *shsurf)
3013{
3014 struct weston_output *output;
3015
3016 surface_clear_next_states(shsurf);
3017 shsurf->next_state.maximized = true;
3018 shsurf->state_changed = true;
3019 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3020
3021 if (!weston_surface_is_mapped(shsurf->surface))
3022 output = get_focused_output(shsurf->surface->compositor);
3023 else
3024 output = shsurf->surface->output;
3025
3026 shell_surface_set_output(shsurf, output);
3027 send_configure_for_surface(shsurf);
3028}
3029
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003030static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003031shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003032{
Derek Foremane4d6c832015-08-05 14:48:11 -05003033 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003034}
3035
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003036static int
3037shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003038 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003039 uint32_t edges)
3040{
Derek Foremane4d6c832015-08-05 14:48:11 -05003041 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003042}
3043
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003044static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003045
3046static void
3047destroy_shell_seat(struct wl_listener *listener, void *data)
3048{
3049 struct shell_seat *shseat =
3050 container_of(listener,
3051 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003052 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003053
3054 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003055 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003056 shseat->popup_grab.client = NULL;
3057
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003058 wl_list_for_each_safe(shsurf, next,
3059 &shseat->popup_grab.surfaces_list,
3060 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003062 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003063 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003064 }
3065
3066 wl_list_remove(&shseat->seat_destroy_listener.link);
3067 free(shseat);
3068}
3069
Jason Ekstrand024177c2014-04-21 19:42:58 -05003070static void
3071shell_seat_caps_changed(struct wl_listener *listener, void *data)
3072{
Derek Foreman1281a362015-07-31 16:55:32 -05003073 struct weston_keyboard *keyboard;
3074 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003075 struct shell_seat *seat;
3076
3077 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003078 keyboard = weston_seat_get_keyboard(seat->seat);
3079 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003080
Derek Foreman1281a362015-07-31 16:55:32 -05003081 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003082 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003083 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003084 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003085 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003086 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003087 wl_list_init(&seat->keyboard_focus_listener.link);
3088 }
3089
Derek Foreman1281a362015-07-31 16:55:32 -05003090 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003091 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003092 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003093 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003094 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003095 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003096 wl_list_init(&seat->pointer_focus_listener.link);
3097 }
3098}
3099
Giulio Camuffo5085a752013-03-25 21:42:45 +01003100static struct shell_seat *
3101create_shell_seat(struct weston_seat *seat)
3102{
3103 struct shell_seat *shseat;
3104
3105 shseat = calloc(1, sizeof *shseat);
3106 if (!shseat) {
3107 weston_log("no memory to allocate shell seat\n");
3108 return NULL;
3109 }
3110
3111 shseat->seat = seat;
3112 wl_list_init(&shseat->popup_grab.surfaces_list);
3113
3114 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3115 wl_signal_add(&seat->destroy_signal,
3116 &shseat->seat_destroy_listener);
3117
Jason Ekstrand024177c2014-04-21 19:42:58 -05003118 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3119 wl_list_init(&shseat->keyboard_focus_listener.link);
3120
3121 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3122 wl_list_init(&shseat->pointer_focus_listener.link);
3123
3124 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3125 wl_signal_add(&seat->updated_caps_signal,
3126 &shseat->caps_changed_listener);
3127 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3128
Giulio Camuffo5085a752013-03-25 21:42:45 +01003129 return shseat;
3130}
3131
3132static struct shell_seat *
3133get_shell_seat(struct weston_seat *seat)
3134{
3135 struct wl_listener *listener;
3136
3137 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003138 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003139
3140 return container_of(listener,
3141 struct shell_seat, seat_destroy_listener);
3142}
3143
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003144static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003145popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003146{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003147 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003148 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003149 struct shell_seat *shseat =
3150 container_of(grab, struct shell_seat, popup_grab.grab);
3151 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003152 wl_fixed_t sx, sy;
3153
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 view = weston_compositor_pick_view(pointer->seat->compositor,
3155 pointer->x, pointer->y,
3156 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003157
Jason Ekstranda7af7042013-10-12 22:38:11 -05003158 if (view && view->surface->resource &&
3159 wl_resource_get_client(view->surface->resource) == client) {
3160 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003161 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003162 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003163 }
3164}
3165
3166static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003167popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003168 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003169{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003170 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003171 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003172 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003173 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003174 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175
Jonas Ådahl61917c82014-08-11 22:44:02 +02003176 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003177 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003178 weston_view_from_global_fixed(pointer->focus, x, y,
3179 &pointer->sx, &pointer->sy);
3180 }
3181
Jonas Ådahld2510102014-10-05 21:39:14 +02003182 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003183
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003184 if (!pointer->focus_client)
3185 return;
3186
3187 resource_list = &pointer->focus_client->pointer_resources;
3188 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003189 weston_view_from_global_fixed(pointer->focus,
3190 pointer->x, pointer->y,
3191 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003192 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003193 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003194}
3195
3196static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003197popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003198 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003199{
3200 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003201 struct shell_seat *shseat =
3202 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003203 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003204 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003205 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003206 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003207
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003208 if (grab->pointer->focus_client)
3209 resource_list = &grab->pointer->focus_client->pointer_resources;
3210 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003211 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003212 wl_resource_for_each(resource, resource_list) {
3213 wl_pointer_send_button(resource, serial,
3214 time, button, state);
3215 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003216 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003217 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003218 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003219 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003220 }
3221
Daniel Stone4dbadb12012-05-30 16:31:51 +01003222 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003223 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003224}
3225
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003226static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003227popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003228 uint32_t time,
3229 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003230{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003231 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003232}
3233
3234static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003235popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3236{
3237 weston_pointer_send_axis_source(grab->pointer, source);
3238}
3239
3240static void
3241popup_grab_frame(struct weston_pointer_grab *grab)
3242{
3243 weston_pointer_send_frame(grab->pointer);
3244}
3245
3246static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003247popup_grab_cancel(struct weston_pointer_grab *grab)
3248{
3249 popup_grab_end(grab->pointer);
3250}
3251
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003252static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003253 popup_grab_focus,
3254 popup_grab_motion,
3255 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003256 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003257 popup_grab_axis_source,
3258 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003259 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003260};
3261
3262static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003263touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003264 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003265{
3266 struct wl_resource *resource;
3267 struct shell_seat *shseat =
3268 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3269 struct wl_display *display = shseat->seat->compositor->wl_display;
3270 uint32_t serial;
3271 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003272 wl_fixed_t sx, sy;
3273
3274 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003275
3276 resource_list = &grab->touch->focus_resource_list;
3277 if (!wl_list_empty(resource_list)) {
3278 serial = wl_display_get_serial(display);
3279 wl_resource_for_each(resource, resource_list) {
3280 wl_touch_send_down(resource, serial, time,
3281 grab->touch->focus->surface->resource,
3282 touch_id, sx, sy);
3283 }
3284 }
3285}
3286
3287static void
3288touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3289{
3290 struct wl_resource *resource;
3291 struct shell_seat *shseat =
3292 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3293 struct wl_display *display = shseat->seat->compositor->wl_display;
3294 uint32_t serial;
3295 struct wl_list *resource_list;
3296
3297 resource_list = &grab->touch->focus_resource_list;
3298 if (!wl_list_empty(resource_list)) {
3299 serial = wl_display_get_serial(display);
3300 wl_resource_for_each(resource, resource_list) {
3301 wl_touch_send_up(resource, serial, time, touch_id);
3302 }
3303 }
3304}
3305
3306static void
3307touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003308 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003309{
3310 struct wl_resource *resource;
3311 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003312 wl_fixed_t sx, sy;
3313
3314 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003315
3316 resource_list = &grab->touch->focus_resource_list;
3317 if (!wl_list_empty(resource_list)) {
3318 wl_resource_for_each(resource, resource_list) {
3319 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3320 }
3321 }
3322}
3323
3324static void
3325touch_popup_grab_frame(struct weston_touch_grab *grab)
3326{
3327}
3328
3329static void
3330touch_popup_grab_cancel(struct weston_touch_grab *grab)
3331{
3332 touch_popup_grab_end(grab->touch);
3333}
3334
3335static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3336 touch_popup_grab_down,
3337 touch_popup_grab_up,
3338 touch_popup_grab_motion,
3339 touch_popup_grab_frame,
3340 touch_popup_grab_cancel,
3341};
3342
3343static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003344shell_surface_send_popup_done(struct shell_surface *shsurf)
3345{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003346 if (shsurf->resource == NULL)
3347 return;
3348 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003349 wl_shell_surface_send_popup_done(shsurf->resource);
3350 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003351 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003352}
3353
3354static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003355popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003356{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003357 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003358 struct shell_seat *shseat =
3359 container_of(grab, struct shell_seat, popup_grab.grab);
3360 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003361 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003362
3363 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003364 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003365 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003366 shseat->popup_grab.grab.interface = NULL;
3367 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003368 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003369 wl_list_for_each_safe(shsurf, next,
3370 &shseat->popup_grab.surfaces_list,
3371 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003372 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003373 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003374 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003375 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003376 wl_list_init(&shseat->popup_grab.surfaces_list);
3377 }
3378}
3379
3380static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003381touch_popup_grab_end(struct weston_touch *touch)
3382{
3383 struct weston_touch_grab *grab = touch->grab;
3384 struct shell_seat *shseat =
3385 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3386 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003387 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003388
3389 if (touch->grab->interface == &touch_popup_grab_interface) {
3390 weston_touch_end_grab(grab->touch);
3391 shseat->popup_grab.client = NULL;
3392 shseat->popup_grab.touch_grab.interface = NULL;
3393 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3394 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003395 wl_list_for_each_safe(shsurf, next,
3396 &shseat->popup_grab.surfaces_list,
3397 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003398 shell_surface_send_popup_done(shsurf);
3399 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003400 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003401 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003402 wl_list_init(&shseat->popup_grab.surfaces_list);
3403 }
3404}
3405
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003406static struct shell_surface *
3407get_top_popup(struct shell_seat *shseat)
3408{
3409 struct shell_surface *shsurf;
3410
3411 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3412 return NULL;
3413 } else {
3414 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3415 struct shell_surface,
3416 popup.grab_link);
3417 return shsurf;
3418 }
3419}
3420
3421static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003422add_popup_grab(struct shell_surface *shsurf,
3423 struct shell_seat *shseat,
3424 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003425{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003426 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003427 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003428 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3429 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003430
3431 parent = get_shell_surface(shsurf->parent);
3432 top_surface = get_top_popup(shseat);
3433 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003434 (!parent ||
3435 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003436 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003437 wl_resource_post_error(shsurf->owner_resource,
3438 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003439 "xdg_popup was not created on the "
3440 "topmost popup");
3441 return -1;
3442 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003443
3444 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003445 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003446 shseat->popup_grab.client =
3447 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003448
3449 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003450 shseat->popup_grab.grab.interface =
3451 &popup_grab_interface;
3452
3453 /* We must make sure here that this popup was opened
3454 * after a mouse press, and not just by moving around
3455 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003456 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003457 shseat->popup_grab.initial_up = 0;
3458 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003459 shseat->popup_grab.touch_grab.interface =
3460 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003461 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003462
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003463 wl_list_insert(&shseat->popup_grab.surfaces_list,
3464 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003465
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003466 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003467 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003468 &shseat->popup_grab.grab);
3469 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003470 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003471 &shseat->popup_grab.touch_grab);
3472 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003473 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003474 wl_list_insert(&shseat->popup_grab.surfaces_list,
3475 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003476 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003477
3478 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003479}
3480
3481static void
3482remove_popup_grab(struct shell_surface *shsurf)
3483{
3484 struct shell_seat *shseat = shsurf->popup.shseat;
3485
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003486 if (shell_surface_is_xdg_popup(shsurf) &&
3487 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003488 wl_resource_post_error(shsurf->owner_resource,
3489 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003490 "xdg_popup was destroyed while it was "
3491 "not the topmost popup.");
3492 return;
3493 }
3494
Giulio Camuffo5085a752013-03-25 21:42:45 +01003495 wl_list_remove(&shsurf->popup.grab_link);
3496 wl_list_init(&shsurf->popup.grab_link);
3497 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003498 if (shseat->popup_grab.type == POINTER) {
3499 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3500 shseat->popup_grab.grab.interface = NULL;
3501 } else if (shseat->popup_grab.type == TOUCH) {
3502 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3503 shseat->popup_grab.touch_grab.interface = NULL;
3504 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003505 }
3506}
3507
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003508static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003509shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003510{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003511 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003512 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003513 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3514 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003515
Jason Ekstranda7af7042013-10-12 22:38:11 -05003516 shsurf->surface->output = parent_view->output;
3517 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003518
Jason Ekstranda7af7042013-10-12 22:38:11 -05003519 weston_view_set_transform_parent(shsurf->view, parent_view);
3520 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3521 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003522
Derek Foreman1281a362015-07-31 16:55:32 -05003523 if (pointer &&
3524 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003525 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3526 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003527 } else if (touch &&
3528 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003529 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3530 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003531 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003532 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003533 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003534 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003535
3536 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003537}
3538
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003539static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003540 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003541 shell_surface_move,
3542 shell_surface_resize,
3543 shell_surface_set_toplevel,
3544 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003545 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003546 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003547 shell_surface_set_maximized,
3548 shell_surface_set_title,
3549 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003550};
3551
3552static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003553destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003554{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003555 struct shell_surface *child, *next;
3556
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003557 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3558
Giulio Camuffo5085a752013-03-25 21:42:45 +01003559 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3560 remove_popup_grab(shsurf);
3561 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003562
Alex Wubd3354b2012-04-17 17:20:49 +08003563 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003564 shell_surface_is_top_fullscreen(shsurf))
3565 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003566
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 if (shsurf->fullscreen.black_view)
3568 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003569
Alex Wubd3354b2012-04-17 17:20:49 +08003570 /* As destroy_resource() use wl_list_for_each_safe(),
3571 * we can always remove the listener.
3572 */
3573 wl_list_remove(&shsurf->surface_destroy_listener.link);
3574 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003575 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003576 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003577
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 weston_view_destroy(shsurf->view);
3579
Philip Withnall648a4dd2013-11-25 18:01:44 +00003580 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003581 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3582 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003583
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003584 wl_list_remove(&shsurf->link);
3585 free(shsurf);
3586}
3587
3588static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003589shell_destroy_shell_surface(struct wl_resource *resource)
3590{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003591 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003592
Bryan Caina46b9462014-04-04 17:41:24 -05003593 if (!wl_list_empty(&shsurf->popup.grab_link))
3594 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003595 if (shsurf->resource)
3596 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003597 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003598}
3599
3600static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003601shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003602{
3603 struct shell_surface *shsurf = container_of(listener,
3604 struct shell_surface,
3605 surface_destroy_listener);
3606
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003607 if (shsurf->resource)
3608 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003609
3610 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003611}
3612
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003613static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003614fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003615{
3616 struct shell_surface *shsurf = data;
3617
3618 weston_surface_destroy(shsurf->surface);
3619}
3620
3621static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003622fade_out_done(struct weston_view_animation *animation, void *data)
3623{
3624 struct shell_surface *shsurf = data;
3625 struct wl_event_loop *loop;
3626
3627 loop = wl_display_get_event_loop(
3628 shsurf->surface->compositor->wl_display);
3629
3630 if (!shsurf->destroying) {
3631 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3632 shsurf->destroying = true;
3633 }
3634}
3635
3636static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003637handle_resource_destroy(struct wl_listener *listener, void *data)
3638{
3639 struct shell_surface *shsurf =
3640 container_of(listener, struct shell_surface,
3641 resource_destroy_listener);
3642
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003643 if (!weston_surface_is_mapped(shsurf->surface))
3644 return;
3645
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003646 shsurf->surface->ref_count++;
3647
3648 pixman_region32_fini(&shsurf->surface->pending.input);
3649 pixman_region32_init(&shsurf->surface->pending.input);
3650 pixman_region32_fini(&shsurf->surface->input);
3651 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003652 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3653 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3654 fade_out_done, shsurf);
3655 } else {
3656 weston_surface_destroy(shsurf->surface);
3657 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003658}
3659
3660static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003661shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003662
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003663struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003664get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003665{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003666 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003667 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003668 else
3669 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003670}
3671
Rafael Antognollie2a34552013-12-03 15:35:45 -02003672static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003673create_common_surface(struct shell_client *owner, void *shell,
3674 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003675 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003676{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003677 struct shell_surface *shsurf;
3678
Pekka Paalanen50b67472014-10-01 15:02:41 +03003679 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003680
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003681 shsurf = calloc(1, sizeof *shsurf);
3682 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003683 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003684 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003685 }
3686
Jason Ekstranda7af7042013-10-12 22:38:11 -05003687 shsurf->view = weston_view_create(surface);
3688 if (!shsurf->view) {
3689 weston_log("no memory to allocate shell surface\n");
3690 free(shsurf);
3691 return NULL;
3692 }
3693
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003694 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003695 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003696 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003697
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003698 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3699 wl_resource_add_destroy_listener(surface->resource,
3700 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003701 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003702
Tiago Vignattibc052c92012-04-19 16:18:18 +03003703 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003704 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003705 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003706 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003707 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003708 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003709 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3710 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003711 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003712 wl_list_init(&shsurf->fullscreen.transform.link);
3713
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003714 shsurf->output = get_default_output(shsurf->shell->compositor);
3715
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003716 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003717 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003718 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003719 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003720
3721 /* init link so its safe to always remove it in destroy_shell_surface */
3722 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003723 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003724
Pekka Paalanen460099f2012-01-20 16:48:25 +02003725 /* empty when not in use */
3726 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003727 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003728
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003729 wl_list_init(&shsurf->workspace_transform.link);
3730
Philip Withnall648a4dd2013-11-25 18:01:44 +00003731 wl_list_init(&shsurf->children_link);
3732 wl_list_init(&shsurf->children_list);
3733 shsurf->parent = NULL;
3734
Pekka Paalanenb5026542014-11-12 15:09:24 +02003735 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003736
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003737 shsurf->client = client;
3738
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003739 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003740}
3741
Rafael Antognollie2a34552013-12-03 15:35:45 -02003742static struct shell_surface *
3743create_shell_surface(void *shell, struct weston_surface *surface,
3744 const struct weston_shell_client *client)
3745{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003746 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003747}
3748
Tiago Vignattibc052c92012-04-19 16:18:18 +03003749static void
3750shell_get_shell_surface(struct wl_client *client,
3751 struct wl_resource *resource,
3752 uint32_t id,
3753 struct wl_resource *surface_resource)
3754{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003755 struct weston_surface *surface =
3756 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003757 struct shell_client *sc = wl_resource_get_user_data(resource);
3758 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003759 struct shell_surface *shsurf;
3760
Pekka Paalanen50b67472014-10-01 15:02:41 +03003761 if (weston_surface_set_role(surface, "wl_shell_surface",
3762 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003763 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003764
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003765 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003766 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003767 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003768 return;
3769 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003770
Jason Ekstranda85118c2013-06-27 20:17:02 -05003771 shsurf->resource =
3772 wl_resource_create(client,
3773 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003774 if (!shsurf->resource) {
3775 wl_resource_post_no_memory(surface_resource);
3776 return;
3777 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003778 wl_resource_set_implementation(shsurf->resource,
3779 &shell_surface_implementation,
3780 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003781 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003782}
3783
Rafael Antognollie2a34552013-12-03 15:35:45 -02003784static bool
3785shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3786{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003787 /* A shell surface without a resource is created from xwayland
3788 * and is considered a wl_shell surface for now. */
3789
3790 return shsurf->resource == NULL ||
3791 wl_resource_instance_of(shsurf->resource,
3792 &wl_shell_surface_interface,
3793 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003794}
3795
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003796static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003797 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003798};
3799
Rafael Antognollie2a34552013-12-03 15:35:45 -02003800/****************************
3801 * xdg-shell implementation */
3802
3803static void
3804xdg_surface_destroy(struct wl_client *client,
3805 struct wl_resource *resource)
3806{
3807 wl_resource_destroy(resource);
3808}
3809
3810static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003811xdg_surface_set_parent(struct wl_client *client,
3812 struct wl_resource *resource,
3813 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003814{
3815 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003816 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003817
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003818 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003819 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003820 shell_surface_set_parent(shsurf, parent->surface);
3821 } else {
3822 shell_surface_set_parent(shsurf, NULL);
3823 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003824}
3825
3826static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003827xdg_surface_set_app_id(struct wl_client *client,
3828 struct wl_resource *resource,
3829 const char *app_id)
3830{
3831 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3832
3833 free(shsurf->class);
3834 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003835 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003836}
3837
3838static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003839xdg_surface_show_window_menu(struct wl_client *client,
3840 struct wl_resource *surface_resource,
3841 struct wl_resource *seat_resource,
3842 uint32_t serial,
3843 int32_t x,
3844 int32_t y)
3845{
3846 /* TODO */
3847}
3848
3849static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003850xdg_surface_set_title(struct wl_client *client,
3851 struct wl_resource *resource, const char *title)
3852{
3853 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3854
3855 set_title(shsurf, title);
3856}
3857
3858static void
3859xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3860 struct wl_resource *seat_resource, uint32_t serial)
3861{
3862 common_surface_move(resource, seat_resource, serial);
3863}
3864
3865static void
3866xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3867 struct wl_resource *seat_resource, uint32_t serial,
3868 uint32_t edges)
3869{
3870 common_surface_resize(resource, seat_resource, serial, edges);
3871}
3872
3873static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003874xdg_surface_ack_configure(struct wl_client *client,
3875 struct wl_resource *resource,
3876 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003877{
3878 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3879
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003880 if (shsurf->state_requested) {
3881 shsurf->next_state = shsurf->requested_state;
3882 shsurf->state_changed = true;
3883 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003884 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003885}
3886
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003887static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003888xdg_surface_set_window_geometry(struct wl_client *client,
3889 struct wl_resource *resource,
3890 int32_t x,
3891 int32_t y,
3892 int32_t width,
3893 int32_t height)
3894{
3895 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3896
3897 set_window_geometry(shsurf, x, y, width, height);
3898}
3899
3900static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003901xdg_surface_set_maximized(struct wl_client *client,
3902 struct wl_resource *resource)
3903{
3904 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003905 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003906
3907 shsurf->state_requested = true;
3908 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003909
3910 if (!weston_surface_is_mapped(shsurf->surface))
3911 output = get_focused_output(shsurf->surface->compositor);
3912 else
3913 output = shsurf->surface->output;
3914
3915 shell_surface_set_output(shsurf, output);
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_maximized(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.maximized = 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
3931xdg_surface_set_fullscreen(struct wl_client *client,
3932 struct wl_resource *resource,
3933 struct wl_resource *output_resource)
3934{
3935 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3936 struct weston_output *output;
3937
3938 shsurf->state_requested = true;
3939 shsurf->requested_state.fullscreen = true;
3940
3941 if (output_resource)
3942 output = wl_resource_get_user_data(output_resource);
3943 else
3944 output = NULL;
3945
Marek Chalupa052917a2014-09-02 11:35:12 +02003946 /* handle clients launching in fullscreen */
3947 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3948 /* Set the output to the one that has focus currently. */
3949 assert(shsurf->surface);
3950 output = get_focused_output(shsurf->surface->compositor);
3951 }
3952
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003953 shell_surface_set_output(shsurf, output);
3954 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003955
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003956 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003957}
3958
3959static void
3960xdg_surface_unset_fullscreen(struct wl_client *client,
3961 struct wl_resource *resource)
3962{
3963 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3964
3965 shsurf->state_requested = true;
3966 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003967 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003968}
3969
3970static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003971xdg_surface_set_minimized(struct wl_client *client,
3972 struct wl_resource *resource)
3973{
3974 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3975
3976 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3977 return;
3978
3979 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003980 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003981}
3982
Rafael Antognollie2a34552013-12-03 15:35:45 -02003983static const struct xdg_surface_interface xdg_surface_implementation = {
3984 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003985 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003986 xdg_surface_set_title,
3987 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003988 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003989 xdg_surface_move,
3990 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003991 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003992 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003993 xdg_surface_set_maximized,
3994 xdg_surface_unset_maximized,
3995 xdg_surface_set_fullscreen,
3996 xdg_surface_unset_fullscreen,
3997 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003998};
3999
4000static void
4001xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004002 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004003{
4004 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004005 uint32_t *s;
4006 struct wl_array states;
4007 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004008
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004009 assert(shsurf);
4010
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004011 if (!shsurf->resource)
4012 return;
4013
4014 wl_array_init(&states);
4015 if (shsurf->requested_state.fullscreen) {
4016 s = wl_array_add(&states, sizeof *s);
4017 *s = XDG_SURFACE_STATE_FULLSCREEN;
4018 } else if (shsurf->requested_state.maximized) {
4019 s = wl_array_add(&states, sizeof *s);
4020 *s = XDG_SURFACE_STATE_MAXIMIZED;
4021 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004022 if (shsurf->resize_edges != 0) {
4023 s = wl_array_add(&states, sizeof *s);
4024 *s = XDG_SURFACE_STATE_RESIZING;
4025 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004026 if (shsurf->focus_count > 0) {
4027 s = wl_array_add(&states, sizeof *s);
4028 *s = XDG_SURFACE_STATE_ACTIVATED;
4029 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004030
4031 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4032 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4033
4034 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004035}
4036
4037static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004038 xdg_send_configure,
4039 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004040};
4041
4042static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004043xdg_shell_destroy(struct wl_client *client,
4044 struct wl_resource *resource)
4045{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004046 struct shell_client *sc = wl_resource_get_user_data(resource);
4047 struct wl_resource *shsurf_resource;
4048 struct shell_surface *shsurf;
4049
4050 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4051 shsurf = wl_resource_get_user_data(shsurf_resource);
4052 if (shsurf->owner_resource == resource) {
4053 wl_resource_post_error(
4054 resource,
4055 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4056 "not all child surface objects destroyed");
4057 return;
4058 }
4059 }
4060
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004061 wl_resource_destroy(resource);
4062}
4063
4064static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004065xdg_use_unstable_version(struct wl_client *client,
4066 struct wl_resource *resource,
4067 int32_t version)
4068{
4069 if (version > 1) {
4070 wl_resource_post_error(resource,
4071 1,
4072 "xdg-shell:: version not implemented yet.");
4073 return;
4074 }
4075}
4076
4077static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004078create_xdg_surface(struct shell_client *owner, void *shell,
4079 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004080 const struct weston_shell_client *client)
4081{
4082 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004083
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004084 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004085 if (!shsurf)
4086 return NULL;
4087
Pekka Paalanenb5026542014-11-12 15:09:24 +02004088 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004089
4090 return shsurf;
4091}
4092
4093static void
4094xdg_get_xdg_surface(struct wl_client *client,
4095 struct wl_resource *resource,
4096 uint32_t id,
4097 struct wl_resource *surface_resource)
4098{
4099 struct weston_surface *surface =
4100 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004101 struct shell_client *sc = wl_resource_get_user_data(resource);
4102 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004103 struct shell_surface *shsurf;
4104
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004105 shsurf = get_shell_surface(surface);
4106 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4107 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4108 "This wl_surface is already an "
4109 "xdg_surface");
4110 return;
4111 }
4112
Pekka Paalanen50b67472014-10-01 15:02:41 +03004113 if (weston_surface_set_role(surface, "xdg_surface",
4114 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004115 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004116
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004117 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004118 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004119 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004120 return;
4121 }
4122
4123 shsurf->resource =
4124 wl_resource_create(client,
4125 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004126 if (!shsurf->resource) {
4127 wl_resource_post_no_memory(surface_resource);
4128 return;
4129 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004130 wl_resource_set_implementation(shsurf->resource,
4131 &xdg_surface_implementation,
4132 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004133 shsurf->owner_resource = resource;
4134 wl_list_insert(&sc->surface_list,
4135 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004136}
4137
4138static bool
4139shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4140{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004141 return shsurf->resource &&
4142 wl_resource_instance_of(shsurf->resource,
4143 &xdg_surface_interface,
4144 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004145}
4146
4147/* xdg-popup implementation */
4148
4149static void
4150xdg_popup_destroy(struct wl_client *client,
4151 struct wl_resource *resource)
4152{
4153 wl_resource_destroy(resource);
4154}
4155
Rafael Antognollie2a34552013-12-03 15:35:45 -02004156static const struct xdg_popup_interface xdg_popup_implementation = {
4157 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004158};
4159
4160static void
4161xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004162 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004163{
4164}
4165
4166static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004167 xdg_popup_send_configure,
4168 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004169};
4170
4171static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004172create_xdg_popup(struct shell_client *owner, void *shell,
4173 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004174 const struct weston_shell_client *client,
4175 struct weston_surface *parent,
4176 struct shell_seat *seat,
4177 uint32_t serial,
4178 int32_t x, int32_t y)
4179{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004180 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004181
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004182 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004183 if (!shsurf)
4184 return NULL;
4185
Pekka Paalanenb5026542014-11-12 15:09:24 +02004186 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004187 shsurf->popup.shseat = seat;
4188 shsurf->popup.serial = serial;
4189 shsurf->popup.x = x;
4190 shsurf->popup.y = y;
4191 shell_surface_set_parent(shsurf, parent);
4192
4193 return shsurf;
4194}
4195
4196static void
4197xdg_get_xdg_popup(struct wl_client *client,
4198 struct wl_resource *resource,
4199 uint32_t id,
4200 struct wl_resource *surface_resource,
4201 struct wl_resource *parent_resource,
4202 struct wl_resource *seat_resource,
4203 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004204 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004205{
4206 struct weston_surface *surface =
4207 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004208 struct shell_client *sc = wl_resource_get_user_data(resource);
4209 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004210 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004211 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004212 struct weston_surface *parent;
4213 struct shell_seat *seat;
4214
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004215 shsurf = get_shell_surface(surface);
4216 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4217 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4218 "This wl_surface is already an "
4219 "xdg_popup");
4220 return;
4221 }
4222
Pekka Paalanen50b67472014-10-01 15:02:41 +03004223 if (weston_surface_set_role(surface, "xdg_popup",
4224 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004225 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004226
4227 if (!parent_resource) {
4228 wl_resource_post_error(surface_resource,
4229 WL_DISPLAY_ERROR_INVALID_OBJECT,
4230 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004231 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004232 }
4233
4234 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004235 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004236
Jonas Ådahlee45a552015-03-18 16:37:47 +08004237 /* Verify that we are creating the top most popup when mapping,
4238 * as it's not until then we know whether it was mapped as most
4239 * top level or not. */
4240
4241 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004242 if (!parent_shsurf ||
4243 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4244 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004245 wl_resource_post_error(resource,
4246 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4247 "xdg_popup parent was invalid");
4248 return;
4249 }
4250
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004251 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004252 parent, seat, serial, x, y);
4253 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004254 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255 return;
4256 }
4257
4258 shsurf->resource =
4259 wl_resource_create(client,
4260 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004261 if (!shsurf->resource) {
4262 wl_resource_post_no_memory(surface_resource);
4263 return;
4264 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004265 wl_resource_set_implementation(shsurf->resource,
4266 &xdg_popup_implementation,
4267 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004268 shsurf->owner_resource = resource;
4269 wl_list_insert(&sc->surface_list,
4270 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004271}
4272
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004273static void
4274xdg_pong(struct wl_client *client,
4275 struct wl_resource *resource, uint32_t serial)
4276{
4277 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004278
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004279 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004280}
4281
Rafael Antognollie2a34552013-12-03 15:35:45 -02004282static bool
4283shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4284{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004285 return (shsurf->resource &&
4286 wl_resource_instance_of(shsurf->resource,
4287 &xdg_popup_interface,
4288 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004289}
4290
4291static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004292 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004293 xdg_use_unstable_version,
4294 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004295 xdg_get_xdg_popup,
4296 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004297};
4298
4299static int
4300xdg_shell_unversioned_dispatch(const void *implementation,
4301 void *_target, uint32_t opcode,
4302 const struct wl_message *message,
4303 union wl_argument *args)
4304{
4305 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004306 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004307
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004308 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004309 wl_resource_post_error(resource,
4310 WL_DISPLAY_ERROR_INVALID_OBJECT,
4311 "must call use_unstable_version first");
4312 return 0;
4313 }
4314
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004315#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004316
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004317 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4318 "shell implementation doesn't match protocol version");
4319
Rafael Antognollie2a34552013-12-03 15:35:45 -02004320 if (args[0].i != XDG_SERVER_VERSION) {
4321 wl_resource_post_error(resource,
4322 WL_DISPLAY_ERROR_INVALID_OBJECT,
4323 "incompatible version, server is %d "
4324 "client wants %d",
4325 XDG_SERVER_VERSION, args[0].i);
4326 return 0;
4327 }
4328
4329 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004330 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004331
4332 return 1;
4333}
4334
4335/* end of xdg-shell implementation */
4336/***********************************/
4337
Kristian Høgsberg07937562011-04-12 17:25:42 -04004338static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004339shell_fade(struct desktop_shell *shell, enum fade_type type);
4340
Pekka Paalanen77346a62011-11-30 16:26:35 +02004341static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004342configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004343{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004345
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004346 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004347 if (v->output == ev->output && v != ev) {
4348 weston_view_unmap(v);
4349 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004350 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004351 }
4352 }
4353
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004354 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004355
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004356 if (wl_list_empty(&ev->layer_link.link)) {
4357 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004358 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004359 }
4360}
4361
Pekka Paalanen8274d902014-08-06 19:36:51 +03004362static int
4363background_get_label(struct weston_surface *surface, char *buf, size_t len)
4364{
4365 return snprintf(buf, len, "background for output %s",
4366 surface->output->name);
4367}
4368
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004369static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004370background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004371{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004372 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004373 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004374
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 view = container_of(es->views.next, struct weston_view, surface_link);
4376
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004377 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004378}
4379
4380static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004381desktop_shell_set_background(struct wl_client *client,
4382 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004383 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004384 struct wl_resource *surface_resource)
4385{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004386 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004387 struct weston_surface *surface =
4388 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004389 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004390
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004391 if (surface->configure) {
4392 wl_resource_post_error(surface_resource,
4393 WL_DISPLAY_ERROR_INVALID_OBJECT,
4394 "surface role already assigned");
4395 return;
4396 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004397
Jason Ekstranda7af7042013-10-12 22:38:11 -05004398 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4399 weston_view_destroy(view);
4400 view = weston_view_create(surface);
4401
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004402 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004403 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004404 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004405 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004407 weston_desktop_shell_send_configure(resource, 0,
4408 surface_resource,
4409 surface->output->width,
4410 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004411}
4412
Pekka Paalanen8274d902014-08-06 19:36:51 +03004413static int
4414panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4415{
4416 return snprintf(buf, len, "panel for output %s",
4417 surface->output->name);
4418}
4419
Kristian Høgsberg75840622011-09-06 13:48:16 -04004420static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004421panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004422{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004423 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004425
Jason Ekstranda7af7042013-10-12 22:38:11 -05004426 view = container_of(es->views.next, struct weston_view, surface_link);
4427
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004428 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004429}
4430
4431static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004432desktop_shell_set_panel(struct wl_client *client,
4433 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004434 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004435 struct wl_resource *surface_resource)
4436{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004437 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004438 struct weston_surface *surface =
4439 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004440 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004441
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004442 if (surface->configure) {
4443 wl_resource_post_error(surface_resource,
4444 WL_DISPLAY_ERROR_INVALID_OBJECT,
4445 "surface role already assigned");
4446 return;
4447 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004448
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4450 weston_view_destroy(view);
4451 view = weston_view_create(surface);
4452
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004453 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004454 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004455 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004456 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004458 weston_desktop_shell_send_configure(resource, 0,
4459 surface_resource,
4460 surface->output->width,
4461 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004462}
4463
Pekka Paalanen8274d902014-08-06 19:36:51 +03004464static int
4465lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4466{
4467 return snprintf(buf, len, "lock window");
4468}
4469
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004470static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004471lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004472{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004473 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004474 struct weston_view *view;
4475
4476 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004477
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004478 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004479 return;
4480
Jason Ekstranda7af7042013-10-12 22:38:11 -05004481 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004482
4483 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004484 weston_layer_entry_insert(&shell->lock_layer.view_list,
4485 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004486 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004487 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004488 }
4489}
4490
4491static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004492handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004493{
Tiago Vignattibe143262012-04-16 17:31:41 +03004494 struct desktop_shell *shell =
4495 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004496
Martin Minarik6d118362012-06-07 18:01:59 +02004497 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004498 shell->lock_surface = NULL;
4499}
4500
4501static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004502desktop_shell_set_lock_surface(struct wl_client *client,
4503 struct wl_resource *resource,
4504 struct wl_resource *surface_resource)
4505{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004506 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004507 struct weston_surface *surface =
4508 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004509
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004510 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004511
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004512 if (!shell->locked)
4513 return;
4514
Pekka Paalanen98262232011-12-01 10:42:22 +02004515 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004516
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004517 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004518 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004519 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004520
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004521 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004522 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004523 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004524 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004525}
4526
4527static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004528resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004529{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004530 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004531
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004532 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004533 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004534 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004535 &shell->input_panel_layer.link);
4536 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004537 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004538 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004539 wl_list_insert(&shell->compositor->cursor_layer.link,
4540 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004541 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004542 wl_list_insert(&shell->fullscreen_layer.link,
4543 &shell->panel_layer.link);
4544 wl_list_insert(&shell->panel_layer.link,
4545 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004546
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004547 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004548
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004549 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004550 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004551 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004552}
4553
4554static void
4555desktop_shell_unlock(struct wl_client *client,
4556 struct wl_resource *resource)
4557{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004558 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004559
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004560 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004561
4562 if (shell->locked)
4563 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004564}
4565
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004566static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004567desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004568 struct wl_resource *resource,
4569 struct wl_resource *surface_resource)
4570{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004571 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004572
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004573 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004574 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004575}
4576
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004577static void
4578desktop_shell_desktop_ready(struct wl_client *client,
4579 struct wl_resource *resource)
4580{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004581 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004582
4583 shell_fade_startup(shell);
4584}
4585
Jonny Lamb765760d2014-08-20 15:53:19 +02004586static void
4587desktop_shell_set_panel_position(struct wl_client *client,
4588 struct wl_resource *resource,
4589 uint32_t position)
4590{
4591 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4592
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004593 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4594 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4595 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4596 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004597 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004598 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004599 "bad position argument");
4600 return;
4601 }
4602
4603 shell->panel_position = position;
4604}
4605
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004606static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004607 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004608 desktop_shell_set_panel,
4609 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004610 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004611 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004612 desktop_shell_desktop_ready,
4613 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004614};
4615
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004616static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004617get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004618{
4619 struct shell_surface *shsurf;
4620
4621 shsurf = get_shell_surface(surface);
4622 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004623 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004624 return shsurf->type;
4625}
4626
Kristian Høgsberg75840622011-09-06 13:48:16 -04004627static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004628move_binding(struct weston_pointer *pointer, uint32_t time,
4629 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004630{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004631 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004632 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004633 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004634
Derek Foreman8ae2db52015-07-15 13:00:36 -05004635 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004636 return;
4637
Derek Foreman8ae2db52015-07-15 13:00:36 -05004638 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004639
Pekka Paalanen01388e22013-04-25 13:57:44 +03004640 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004641 if (surface == NULL)
4642 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004643
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004644 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004645 if (shsurf == NULL || shsurf->state.fullscreen ||
4646 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004647 return;
4648
Derek Foreman794fa0e2015-07-15 13:00:38 -05004649 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004650}
4651
4652static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004653maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4654 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004655{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004656 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004657 struct weston_surface *surface;
4658 struct shell_surface *shsurf;
4659
4660 surface = weston_surface_get_main_surface(focus);
4661 if (surface == NULL)
4662 return;
4663
4664 shsurf = get_shell_surface(surface);
4665 if (shsurf == NULL)
4666 return;
4667
4668 if (!shell_surface_is_xdg_surface(shsurf))
4669 return;
4670
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004671 shsurf->state_requested = true;
4672 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004673 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004674}
4675
4676static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004677fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4678 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004679{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004680 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004681 struct weston_surface *surface;
4682 struct shell_surface *shsurf;
4683
4684 surface = weston_surface_get_main_surface(focus);
4685 if (surface == NULL)
4686 return;
4687
4688 shsurf = get_shell_surface(surface);
4689 if (shsurf == NULL)
4690 return;
4691
4692 if (!shell_surface_is_xdg_surface(shsurf))
4693 return;
4694
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004695 shsurf->state_requested = true;
4696 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004697 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004698 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004699}
4700
4701static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004702touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004703{
Derek Foreman362656b2014-09-04 10:23:05 -05004704 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004705 struct weston_surface *surface;
4706 struct shell_surface *shsurf;
4707
Derek Foreman8ae2db52015-07-15 13:00:36 -05004708 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004709 return;
4710
Derek Foreman8ae2db52015-07-15 13:00:36 -05004711 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004712 surface = weston_surface_get_main_surface(focus);
4713 if (surface == NULL)
4714 return;
4715
4716 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004717 if (shsurf == NULL || shsurf->state.fullscreen ||
4718 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004719 return;
4720
Derek Foremanb7674ae2015-07-15 13:00:37 -05004721 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004722}
4723
4724static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004725resize_binding(struct weston_pointer *pointer, uint32_t time,
4726 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004727{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004728 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004729 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004730 uint32_t edges = 0;
4731 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004732 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004733
Derek Foreman8ae2db52015-07-15 13:00:36 -05004734 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004735 return;
4736
Derek Foreman8ae2db52015-07-15 13:00:36 -05004737 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004738
Pekka Paalanen01388e22013-04-25 13:57:44 +03004739 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004740 if (surface == NULL)
4741 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004742
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004743 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004744 if (shsurf == NULL || shsurf->state.fullscreen ||
4745 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004746 return;
4747
Jason Ekstranda7af7042013-10-12 22:38:11 -05004748 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004749 wl_fixed_to_int(pointer->grab_x),
4750 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004751 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004752
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004753 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004754 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004755 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004756 edges |= 0;
4757 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004758 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004759
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004760 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004761 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004762 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004763 edges |= 0;
4764 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004765 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004766
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004767 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004768}
4769
4770static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004771surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004772 struct weston_pointer_axis_event *event,
4773 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004774{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004775 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004776 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004777 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004778 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004779
Pekka Paalanen01388e22013-04-25 13:57:44 +03004780 /* XXX: broken for windows containing sub-surfaces */
4781 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004782 if (surface == NULL)
4783 return;
4784
4785 shsurf = get_shell_surface(surface);
4786 if (!shsurf)
4787 return;
4788
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004789 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004790
Jason Ekstranda7af7042013-10-12 22:38:11 -05004791 if (shsurf->view->alpha > 1.0)
4792 shsurf->view->alpha = 1.0;
4793 if (shsurf->view->alpha < step)
4794 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004795
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004797 weston_surface_damage(surface);
4798}
4799
4800static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004801do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004802 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004803{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004804 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004805 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004806 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004807 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004808
Derek Foreman1281a362015-07-31 16:55:32 -05004809 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004810 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4811 return;
4812 }
4813
Scott Moreauccbf29d2012-02-22 14:21:41 -07004814 wl_list_for_each(output, &compositor->output_list, link) {
4815 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004816 wl_fixed_to_double(pointer->x),
4817 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004818 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004819 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004820 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004821 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004822 increment = -output->zoom.increment;
4823 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004824 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004825 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004826 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004827 else
4828 increment = 0;
4829
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004830 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004831
Scott Moreaue6603982012-06-11 13:07:51 -06004832 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004833 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004834 else if (output->zoom.level > output->zoom.max_level)
4835 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004836
4837 if (!output->zoom.active) {
4838 if (output->zoom.level <= 0.0)
4839 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004840 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004841 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004842
Scott Moreaue6603982012-06-11 13:07:51 -06004843 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004844
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004846 }
4847 }
4848}
4849
Scott Moreauccbf29d2012-02-22 14:21:41 -07004850static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004851zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004852 struct weston_pointer_axis_event *event,
4853 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004854{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004855 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004856}
4857
4858static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004859zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4860 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004861{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004862 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004863}
4864
4865static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004866terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4867 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004868{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004869 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004870
Daniel Stone325fc2d2012-05-30 16:31:58 +01004871 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004872}
4873
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004874static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004875rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004876 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004877{
4878 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004879 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004880 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004881 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004882 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004883
Jonas Ådahld2510102014-10-05 21:39:14 +02004884 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004885
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004886 if (!shsurf)
4887 return;
4888
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004889 cx = 0.5f * shsurf->surface->width;
4890 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004891
Daniel Stone37816df2012-05-16 18:45:18 +01004892 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4893 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004894 r = sqrtf(dx * dx + dy * dy);
4895
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004896 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004897 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004898
4899 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004900 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004901 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004902
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004903 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004904 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004905
4906 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004907 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004908 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004909 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004910 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004911
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004912 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004913 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004914 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004915 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004916 wl_list_init(&shsurf->rotation.transform.link);
4917 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004918 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004919 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004920
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004921 /* We need to adjust the position of the surface
4922 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004923 cposx = shsurf->view->geometry.x + cx;
4924 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004925 dposx = rotate->center.x - cposx;
4926 dposy = rotate->center.y - cposy;
4927 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004928 weston_view_set_position(shsurf->view,
4929 shsurf->view->geometry.x + dposx,
4930 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004931 }
4932
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004933 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004934 * lazily applies the damage due to rotation update.
4935 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004936 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004937}
4938
4939static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004940rotate_grab_button(struct weston_pointer_grab *grab,
4941 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004942{
4943 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004944 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004945 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004946 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004947 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004948
Daniel Stone4dbadb12012-05-30 16:31:51 +01004949 if (pointer->button_count == 0 &&
4950 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004951 if (shsurf)
4952 weston_matrix_multiply(&shsurf->rotation.rotation,
4953 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004954 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004955 free(rotate);
4956 }
4957}
4958
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004959static void
4960rotate_grab_cancel(struct weston_pointer_grab *grab)
4961{
4962 struct rotate_grab *rotate =
4963 container_of(grab, struct rotate_grab, base.grab);
4964
4965 shell_grab_end(&rotate->base);
4966 free(rotate);
4967}
4968
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004969static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004970 noop_grab_focus,
4971 rotate_grab_motion,
4972 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02004973 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10004974 noop_grab_axis_source,
4975 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004976 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004977};
4978
4979static void
Derek Foreman74de4692015-07-15 13:00:39 -05004980surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004981{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004982 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004983 float dx, dy;
4984 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004985
Derek Foreman45a7c272015-09-11 14:27:40 -05004986 surface = find_toplevel_surface(surface);
4987
Pekka Paalanen460099f2012-01-20 16:48:25 +02004988 rotate = malloc(sizeof *rotate);
4989 if (!rotate)
4990 return;
4991
Jason Ekstranda7af7042013-10-12 22:38:11 -05004992 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004993 surface->surface->width * 0.5f,
4994 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004995 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004996
Derek Foreman74de4692015-07-15 13:00:39 -05004997 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4998 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004999 r = sqrtf(dx * dx + dy * dy);
5000 if (r > 20.0f) {
5001 struct weston_matrix inverse;
5002
5003 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005004 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005005 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005006
5007 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005008 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005009 } else {
5010 weston_matrix_init(&surface->rotation.rotation);
5011 weston_matrix_init(&rotate->rotation);
5012 }
5013
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005014 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005015 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005016}
5017
5018static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005019rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005020 void *data)
5021{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005022 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005023 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005024 struct shell_surface *surface;
5025
Derek Foreman8ae2db52015-07-15 13:00:36 -05005026 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005027 return;
5028
Derek Foreman8ae2db52015-07-15 13:00:36 -05005029 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005030
Pekka Paalanen01388e22013-04-25 13:57:44 +03005031 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005032 if (base_surface == NULL)
5033 return;
5034
5035 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005036 if (surface == NULL || surface->state.fullscreen ||
5037 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005038 return;
5039
Derek Foreman74de4692015-07-15 13:00:39 -05005040 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005041}
5042
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005043/* Move all fullscreen layers down to the current workspace and hide their
5044 * black views. The surfaces' state is set to both fullscreen and lowered,
5045 * and this is reversed when such a surface is re-configured, see
5046 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5047 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005048 * lowering_output = NULL - Lower on all outputs, else only lower on the
5049 * specified output.
5050 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005051 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005052 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005053void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005054lower_fullscreen_layer(struct desktop_shell *shell,
5055 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005056{
5057 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005058 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005059
5060 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005061 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005062 &shell->fullscreen_layer.view_list.link,
5063 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005064 struct shell_surface *shsurf = get_shell_surface(view->surface);
5065
5066 if (!shsurf)
5067 continue;
5068
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005069 /* Only lower surfaces which have lowering_output as their fullscreen
5070 * output, unless a NULL output asks for lowering on all outputs.
5071 */
5072 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5073 continue;
5074
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005075 /* We can have a non-fullscreen popup for a fullscreen surface
5076 * in the fullscreen layer. */
5077 if (shsurf->state.fullscreen) {
5078 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005079 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5080 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005081 weston_view_damage_below(shsurf->fullscreen.black_view);
5082
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005083 }
5084
5085 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005086 weston_layer_entry_remove(&view->layer_link);
5087 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005088 weston_view_damage_below(view);
5089 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005090
5091 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005092 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005093}
5094
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005095void
Tiago Vignattibe143262012-04-16 17:31:41 +03005096activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005097 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005098{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005099 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005100 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005101 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005102 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005103 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005104
Pekka Paalanen01388e22013-04-25 13:57:44 +03005105 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005106 shsurf = get_shell_surface(main_surface);
5107 assert(shsurf);
5108
5109 /* Only demote fullscreen surfaces on the output of activated shsurf.
5110 * Leave fullscreen surfaces on unrelated outputs alone. */
5111 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005112
Daniel Stone37816df2012-05-16 18:45:18 +01005113 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005114
Jonas Ådahl8538b222012-08-29 22:13:03 +02005115 state = ensure_focus_state(shell, seat);
5116 if (state == NULL)
5117 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005118
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005119 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005120 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005121
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005122 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005123 shell_configure_fullscreen(shsurf);
5124 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005125 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005126
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005127 /* Update the surface’s layer. This brings it to the top of the stacking
5128 * order as appropriate. */
5129 shell_surface_update_layer(shsurf);
5130
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005131 if (shell->focus_animation_type != ANIMATION_NONE) {
5132 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005133 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005134 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005135}
5136
Alex Wu21858432012-04-01 20:13:08 +08005137/* no-op func for checking black surface */
5138static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005139black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005140{
5141}
5142
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005143static bool
Alex Wu21858432012-04-01 20:13:08 +08005144is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5145{
5146 if (es->configure == black_surface_configure) {
5147 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005148 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005149 return true;
5150 }
5151 return false;
5152}
5153
Kristian Høgsberg75840622011-09-06 13:48:16 -04005154static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005155activate_binding(struct weston_seat *seat,
5156 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005157 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005158{
Derek Foremanbbde6432015-12-10 13:08:01 -06005159 struct focus_state *state;
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005160 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005161 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005162
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005163 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005164
Pekka Paalanen01388e22013-04-25 13:57:44 +03005165 if (is_black_surface(focus, &main_surface))
5166 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005167
Pekka Paalanen01388e22013-04-25 13:57:44 +03005168 main_surface = weston_surface_get_main_surface(focus);
5169 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005170 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005171
Derek Foremanbbde6432015-12-10 13:08:01 -06005172 state = ensure_focus_state(shell, seat);
5173 if (state == NULL)
5174 return;
5175
5176 if (state->keyboard_focus == focus)
5177 return;
5178
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005179 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005180}
5181
5182static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005183click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5184 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005185{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005186 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005187 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005188 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005189 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005190
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005191 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005192}
5193
5194static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005195touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5196 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005197{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005198 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005199 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005200 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005201 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005202
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005203 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005204}
5205
5206static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005207unfocus_all_seats(struct desktop_shell *shell)
5208{
5209 struct weston_seat *seat, *next;
5210
5211 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005212 struct weston_keyboard *keyboard =
5213 weston_seat_get_keyboard(seat);
5214
5215 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005216 continue;
5217
Derek Foreman1281a362015-07-31 16:55:32 -05005218 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005219 }
5220}
5221
5222static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005223lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005224{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005225 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005226
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005227 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005228 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005229 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005230 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005231
5232 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005233
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005234 /* Hide all surfaces by removing the fullscreen, panel and
5235 * toplevel layers. This way nothing else can show or receive
5236 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005237
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005238 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005239 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005240 if (shell->showing_input_panels)
5241 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005242 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005243 wl_list_insert(&shell->compositor->cursor_layer.link,
5244 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005245
Derek Foreman004b4a12015-07-20 16:28:13 -05005246 weston_compositor_sleep(shell->compositor);
5247
Neil Robertsb4a91702014-04-09 16:33:32 +01005248 /* Remove the keyboard focus on all seats. This will be
5249 * restored to the workspace's saved state via
5250 * restore_focus_state when the compositor is unlocked */
5251 unfocus_all_seats(shell);
5252
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005253 /* TODO: disable bindings that should not work while locked. */
5254
5255 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005256}
5257
5258static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005259unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005260{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005261 struct wl_resource *shell_resource;
5262
Pekka Paalanend81c2162011-11-16 13:47:34 +02005263 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005264 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005265 return;
5266 }
5267
5268 /* If desktop-shell client has gone away, unlock immediately. */
5269 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005270 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005271 return;
5272 }
5273
5274 if (shell->prepare_event_sent)
5275 return;
5276
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005277 shell_resource = shell->child.desktop_shell;
5278 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005279 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005280}
5281
5282static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005283shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005284{
5285 struct desktop_shell *shell = data;
5286
5287 shell->fade.animation = NULL;
5288
5289 switch (shell->fade.type) {
5290 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005291 weston_surface_destroy(shell->fade.view->surface);
5292 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005293 break;
5294 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005295 lock(shell);
5296 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005297 default:
5298 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005299 }
5300}
5301
Jason Ekstranda7af7042013-10-12 22:38:11 -05005302static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005303shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005304{
5305 struct weston_compositor *compositor = shell->compositor;
5306 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005307 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005308
5309 surface = weston_surface_create(compositor);
5310 if (!surface)
5311 return NULL;
5312
Jason Ekstranda7af7042013-10-12 22:38:11 -05005313 view = weston_view_create(surface);
5314 if (!view) {
5315 weston_surface_destroy(surface);
5316 return NULL;
5317 }
5318
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005319 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005320 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005321 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005322 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5323 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005324 pixman_region32_init(&surface->input);
5325
Jason Ekstranda7af7042013-10-12 22:38:11 -05005326 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005327}
5328
5329static void
5330shell_fade(struct desktop_shell *shell, enum fade_type type)
5331{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005332 float tint;
5333
5334 switch (type) {
5335 case FADE_IN:
5336 tint = 0.0;
5337 break;
5338 case FADE_OUT:
5339 tint = 1.0;
5340 break;
5341 default:
5342 weston_log("shell: invalid fade type\n");
5343 return;
5344 }
5345
5346 shell->fade.type = type;
5347
Jason Ekstranda7af7042013-10-12 22:38:11 -05005348 if (shell->fade.view == NULL) {
5349 shell->fade.view = shell_fade_create_surface(shell);
5350 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005351 return;
5352
Jason Ekstranda7af7042013-10-12 22:38:11 -05005353 shell->fade.view->alpha = 1.0 - tint;
5354 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005355 }
5356
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005357 if (shell->fade.view->output == NULL) {
5358 /* If the black view gets a NULL output, we lost the
5359 * last output and we'll just cancel the fade. This
5360 * happens when you close the last window under the
5361 * X11 or Wayland backends. */
5362 shell->locked = false;
5363 weston_surface_destroy(shell->fade.view->surface);
5364 shell->fade.view = NULL;
5365 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005366 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005367 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005368 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005369 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005370 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005371 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005372 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005373}
5374
5375static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005376do_shell_fade_startup(void *data)
5377{
5378 struct desktop_shell *shell = data;
5379
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005380 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005381 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005382 } else {
5383 weston_log("desktop shell: "
5384 "unexpected fade-in animation type %d\n",
5385 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005386 weston_surface_destroy(shell->fade.view->surface);
5387 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005388 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005389}
5390
5391static void
5392shell_fade_startup(struct desktop_shell *shell)
5393{
5394 struct wl_event_loop *loop;
5395
5396 if (!shell->fade.startup_timer)
5397 return;
5398
5399 wl_event_source_remove(shell->fade.startup_timer);
5400 shell->fade.startup_timer = NULL;
5401
5402 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5403 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5404}
5405
5406static int
5407fade_startup_timeout(void *data)
5408{
5409 struct desktop_shell *shell = data;
5410
5411 shell_fade_startup(shell);
5412 return 0;
5413}
5414
5415static void
5416shell_fade_init(struct desktop_shell *shell)
5417{
5418 /* Make compositor output all black, and wait for the desktop-shell
5419 * client to signal it is ready, then fade in. The timer triggers a
5420 * fade-in, in case the desktop-shell client takes too long.
5421 */
5422
5423 struct wl_event_loop *loop;
5424
Jason Ekstranda7af7042013-10-12 22:38:11 -05005425 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005426 weston_log("%s: warning: fade surface already exists\n",
5427 __func__);
5428 return;
5429 }
5430
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005431 if (shell->startup_animation_type == ANIMATION_NONE)
5432 return;
5433
Jason Ekstranda7af7042013-10-12 22:38:11 -05005434 shell->fade.view = shell_fade_create_surface(shell);
5435 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005436 return;
5437
Jason Ekstranda7af7042013-10-12 22:38:11 -05005438 weston_view_update_transform(shell->fade.view);
5439 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005440
5441 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5442 shell->fade.startup_timer =
5443 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5444 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5445}
5446
5447static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005448idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005449{
5450 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005451 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005452 struct weston_seat *seat;
5453
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005454 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005455 struct weston_touch *touch = weston_seat_get_touch(seat);
5456 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5457
5458 if (pointer)
5459 popup_grab_end(pointer);
5460 if (touch)
5461 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005462 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005463
5464 shell_fade(shell, FADE_OUT);
5465 /* lock() is called from shell_fade_done() */
5466}
5467
5468static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005469wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005470{
5471 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005472 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005473
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005474 unlock(shell);
5475}
5476
5477static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005478transform_handler(struct wl_listener *listener, void *data)
5479{
5480 struct weston_surface *surface = data;
5481 struct shell_surface *shsurf = get_shell_surface(surface);
5482 struct weston_view *view;;
5483 int x, y;
5484
5485 if (!shsurf || !shsurf->client->send_position)
5486 return;
5487
5488 view = shsurf->view;
5489 if (!view || !weston_view_is_mapped(view))
5490 return;
5491
5492 x = view->geometry.x;
5493 y = view->geometry.y;
5494
5495 shsurf->client->send_position(surface, x, y);
5496}
5497
5498static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005499center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005500{
Giulio Camuffob8366642013-04-25 13:57:46 +03005501 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005502 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005503
Jason Ekstranda7af7042013-10-12 22:38:11 -05005504 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005505
5506 x = output->x + (output->width - width) / 2 - surf_x / 2;
5507 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005508
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005509 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005510}
5511
5512static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005513weston_view_set_initial_position(struct weston_view *view,
5514 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005515{
5516 struct weston_compositor *compositor = shell->compositor;
5517 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005518 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005519 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005520 struct weston_output *output, *target_output = NULL;
5521 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005522 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005523
5524 /* As a heuristic place the new window on the same output as the
5525 * pointer. Falling back to the output containing 0, 0.
5526 *
5527 * TODO: Do something clever for touch too?
5528 */
5529 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005530 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5531
5532 if (pointer) {
5533 ix = wl_fixed_to_int(pointer->x);
5534 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005535 break;
5536 }
5537 }
5538
5539 wl_list_for_each(output, &compositor->output_list, link) {
5540 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5541 target_output = output;
5542 break;
5543 }
5544 }
5545
5546 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005547 weston_view_set_position(view, 10 + random() % 400,
5548 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005549 return;
5550 }
5551
5552 /* Valid range within output where the surface will still be onscreen.
5553 * If this is negative it means that the surface is bigger than
5554 * output.
5555 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005556 get_output_work_area(shell, target_output, &area);
5557
Derek Foremanf814c5d2015-04-15 12:23:54 -05005558 x = area.x;
5559 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005560 range_x = area.width - view->surface->width;
5561 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005562
Rob Bradford4cb88c72012-08-13 15:18:44 +01005563 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005564 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005565
5566 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005567 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005568
Jason Ekstranda7af7042013-10-12 22:38:11 -05005569 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005570}
5571
5572static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005573set_maximized_position(struct desktop_shell *shell,
5574 struct shell_surface *shsurf)
5575{
5576 int32_t surf_x, surf_y;
5577 pixman_rectangle32_t area;
5578
Jonny Lambd73c6942014-08-20 15:53:20 +02005579 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005580 if (shsurf->has_set_geometry) {
5581 surf_x = shsurf->geometry.x;
5582 surf_y = shsurf->geometry.y;
5583 } else {
5584 surface_subsurfaces_boundingbox(shsurf->surface,
5585 &surf_x, &surf_y, NULL, NULL);
5586 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005587
5588 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005589 area.x - surf_x,
5590 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005591}
5592
5593static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005594map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005595 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005597 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005598 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005599
Pekka Paalanen77346a62011-11-30 16:26:35 +02005600 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005601 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005602 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005603 if (shsurf->state.fullscreen) {
5604 center_on_output(shsurf->view, shsurf->fullscreen_output);
5605 shell_map_fullscreen(shsurf);
5606 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005607 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005608 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005609 weston_view_set_initial_position(shsurf->view, shell);
5610 }
Juan Zhao96879df2012-02-07 08:45:41 +08005611 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005612 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005613 if (shell_map_popup(shsurf) != 0)
5614 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005615 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005616 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005617 weston_view_set_position(shsurf->view,
5618 shsurf->view->geometry.x + sx,
5619 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005620 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005621 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005622 default:
5623 ;
5624 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005625
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005626 /* Surface stacking order, see also activate(). */
5627 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005628
Jason Ekstranda7af7042013-10-12 22:38:11 -05005629 if (shsurf->type != SHELL_SURFACE_NONE) {
5630 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005631 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005632 shsurf->surface->output = shsurf->output;
5633 shsurf->view->output = shsurf->output;
5634 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005635 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005636
Jason Ekstranda7af7042013-10-12 22:38:11 -05005637 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005638 /* XXX: xwayland's using the same fields for transient type */
5639 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005640 if (shsurf->transient.flags ==
5641 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5642 break;
5643 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005644 if (shsurf->state.relative &&
5645 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5646 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005647 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005648 break;
5649 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005650 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005651 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005652 case SHELL_SURFACE_POPUP:
5653 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005654 default:
5655 break;
5656 }
5657
Rafael Antognolli03b16592013-12-03 15:35:42 -02005658 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5659 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005660 {
5661 switch (shell->win_animation_type) {
5662 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005663 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005664 break;
5665 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005666 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005667 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005668 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005669 default:
5670 break;
5671 }
5672 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005673}
5674
5675static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005676configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005677 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005678{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005679 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005680 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005681
Pekka Paalanen77346a62011-11-30 16:26:35 +02005682 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005683
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005684 assert(shsurf);
5685
Rafael Antognolli03b16592013-12-03 15:35:42 -02005686 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005687 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005688 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005689 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005690 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005691 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005692 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005693
Alex Wu4539b082012-03-01 12:57:46 +08005694 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005695 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005696 wl_list_for_each(view, &surface->views, surface_link)
5697 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005698
Rafael Antognolli03b16592013-12-03 15:35:42 -02005699 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005700 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005701 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005702}
5703
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005704static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005705shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005706{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005707 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005708 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005709 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005710
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005711 assert(shsurf);
5712
5713 shell = shsurf->shell;
5714
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005715 if (!weston_surface_is_mapped(es) &&
5716 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005717 remove_popup_grab(shsurf);
5718 }
5719
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005720 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005721 return;
5722
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005723 if (shsurf->has_next_geometry) {
5724 shsurf->geometry = shsurf->next_geometry;
5725 shsurf->has_next_geometry = false;
5726 shsurf->has_set_geometry = true;
5727 } else if (!shsurf->has_set_geometry) {
5728 surface_subsurfaces_boundingbox(shsurf->surface,
5729 &shsurf->geometry.x,
5730 &shsurf->geometry.y,
5731 &shsurf->geometry.width,
5732 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005733 }
5734
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005735 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005736 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005737 type_changed = 1;
5738 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005739
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005740 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005741 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005742 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005743 shsurf->last_width != es->width ||
5744 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005745 float from_x, from_y;
5746 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005747
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005748 if (shsurf->resize_edges) {
5749 sx = 0;
5750 sy = 0;
5751 }
5752
5753 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5754 sx = shsurf->last_width - es->width;
5755 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5756 sy = shsurf->last_height - es->height;
5757
5758 shsurf->last_width = es->width;
5759 shsurf->last_height = es->height;
5760
Jason Ekstranda7af7042013-10-12 22:38:11 -05005761 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5762 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005763 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005764 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005765 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005766 }
5767}
5768
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005769static bool
5770check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5771{
5772 struct timespec now;
5773
5774 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5775 return false;
5776
5777 /*
5778 * If the shell helper client dies before the session has been
5779 * up for roughly 30 seconds, better just make Weston shut down,
5780 * because the user likely has no way to interact with the desktop
5781 * anyway.
5782 */
5783 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5784 weston_log("Error: %s apparently cannot run at all.\n",
5785 shell->client);
5786 weston_log_continue(STAMP_SPACE "Quitting...");
5787 wl_display_terminate(shell->compositor->wl_display);
5788
5789 return true;
5790 }
5791
5792 return false;
5793}
5794
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005795static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005796
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005797static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005798respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005799{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005800 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005801
5802 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5803 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005804 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005805 shell->child.deathstamp = time;
5806 shell->child.deathcount = 0;
5807 }
5808
5809 shell->child.deathcount++;
5810 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005811 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005812 return;
5813 }
5814
Pekka Paalanen826dc142014-08-27 12:11:53 +03005815 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005816 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005817}
5818
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005819static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005820desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5821{
5822 struct desktop_shell *shell;
5823
5824 shell = container_of(listener, struct desktop_shell,
5825 child.client_destroy_listener);
5826
Pekka Paalanen826dc142014-08-27 12:11:53 +03005827 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005828 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005829 /*
5830 * unbind_desktop_shell() will reset shell->child.desktop_shell
5831 * before the respawned process has a chance to create a new
5832 * desktop_shell object, because we are being called from the
5833 * wl_client destructor which destroys all wl_resources before
5834 * returning.
5835 */
5836
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005837 if (!check_desktop_shell_crash_too_early(shell))
5838 respawn_desktop_shell_process(shell);
5839
Pekka Paalanen826dc142014-08-27 12:11:53 +03005840 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005841}
5842
5843static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005844launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005845{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005846 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005847
Pekka Paalanen826dc142014-08-27 12:11:53 +03005848 shell->child.client = weston_client_start(shell->compositor,
5849 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005850
Arnaud Vrac42631192014-08-25 20:56:46 +02005851 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005852 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005853 return;
5854 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005855
5856 shell->child.client_destroy_listener.notify =
5857 desktop_shell_client_destroy;
5858 wl_client_add_destroy_listener(shell->child.client,
5859 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005860}
5861
5862static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005863handle_shell_client_destroy(struct wl_listener *listener, void *data)
5864{
5865 struct shell_client *sc =
5866 container_of(listener, struct shell_client, destroy_listener);
5867
5868 if (sc->ping_timer)
5869 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005870
5871 /* Since we're about to free shell_client, we remove it from the
5872 * head of the surface list so we don't use that freed list node
5873 * during surface clean up later on.
5874 */
5875 wl_list_remove(&sc->surface_list);
5876
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005877 free(sc);
5878}
5879
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005880static struct shell_client *
5881shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5882 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005883{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005884 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005885
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005886 sc = zalloc(sizeof *sc);
5887 if (sc == NULL) {
5888 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005889 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005890 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005891
5892 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005893 if (sc->resource == NULL) {
5894 free(sc);
5895 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005896 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005897 }
5898
5899 sc->client = client;
5900 sc->shell = shell;
5901 sc->destroy_listener.notify = handle_shell_client_destroy;
5902 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005903 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005904
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005905 return sc;
5906}
5907
5908static void
5909bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5910{
5911 struct desktop_shell *shell = data;
5912 struct shell_client *sc;
5913
5914 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5915 if (sc)
5916 wl_resource_set_implementation(sc->resource,
5917 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005918 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005919}
5920
5921static void
5922bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5923{
5924 struct desktop_shell *shell = data;
5925 struct shell_client *sc;
5926
5927 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5928 if (sc)
5929 wl_resource_set_dispatcher(sc->resource,
5930 xdg_shell_unversioned_dispatch,
5931 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005932}
5933
5934static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005935unbind_desktop_shell(struct wl_resource *resource)
5936{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005937 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005938
5939 if (shell->locked)
5940 resume_desktop(shell);
5941
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005942 shell->child.desktop_shell = NULL;
5943 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005944}
5945
5946static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005947bind_desktop_shell(struct wl_client *client,
5948 void *data, uint32_t version, uint32_t id)
5949{
Tiago Vignattibe143262012-04-16 17:31:41 +03005950 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005951 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005952
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005953 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5954 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005955
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005956 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005957 wl_resource_set_implementation(resource,
5958 &desktop_shell_implementation,
5959 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005960 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005961 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005962 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005963
5964 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5965 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005966}
5967
Kristian Høgsberg07045392012-02-19 18:52:44 -05005968struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005969 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005970 struct weston_surface *current;
5971 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005972 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005973 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005974};
5975
5976static void
5977switcher_next(struct switcher *switcher)
5978{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005979 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005980 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005981 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005982 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005983
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005984 /* temporary re-display minimized surfaces */
5985 struct weston_view *tmp;
5986 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005987 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5988 weston_layer_entry_remove(&view->layer_link);
5989 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005990 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5991 *minimized = view;
5992 }
5993
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005994 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005995 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005996 if (shsurf &&
5997 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5998 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005999 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006000 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006001 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006002 next = view->surface;
6003 prev = view->surface;
6004 view->alpha = 0.25;
6005 weston_view_geometry_dirty(view);
6006 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006007 }
Alex Wu1659daa2012-04-01 20:13:09 +08006008
Jason Ekstranda7af7042013-10-12 22:38:11 -05006009 if (is_black_surface(view->surface, NULL)) {
6010 view->alpha = 0.25;
6011 weston_view_geometry_dirty(view);
6012 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006013 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006014 }
6015
6016 if (next == NULL)
6017 next = first;
6018
Alex Wu07b26062012-03-12 16:06:01 +08006019 if (next == NULL)
6020 return;
6021
Kristian Høgsberg07045392012-02-19 18:52:44 -05006022 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006023 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006024
6025 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006026 wl_list_for_each(view, &next->views, surface_link)
6027 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006028
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006029 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006030 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006031 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006032}
6033
6034static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006035switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006036{
6037 struct switcher *switcher =
6038 container_of(listener, struct switcher, listener);
6039
6040 switcher_next(switcher);
6041}
6042
6043static void
Daniel Stone351eb612012-05-31 15:27:47 -04006044switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006045{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006046 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006047 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006048 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006049
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006050 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006051 if (is_focus_view(view))
6052 continue;
6053
Jason Ekstranda7af7042013-10-12 22:38:11 -05006054 view->alpha = 1.0;
6055 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006056 }
6057
Alex Wu07b26062012-03-12 16:06:01 +08006058 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006059 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006060 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006061 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006062 weston_keyboard_end_grab(keyboard);
6063 if (keyboard->input_method_resource)
6064 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006065
6066 /* re-hide surfaces that were temporary shown during the switch */
6067 struct weston_view **minimized;
6068 wl_array_for_each(minimized, &switcher->minimized_array) {
6069 /* with the exception of the current selected */
6070 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006071 weston_layer_entry_remove(&(*minimized)->layer_link);
6072 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006073 weston_view_damage_below(*minimized);
6074 }
6075 }
6076 wl_array_release(&switcher->minimized_array);
6077
Kristian Høgsberg07045392012-02-19 18:52:44 -05006078 free(switcher);
6079}
6080
6081static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006082switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006083 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006084{
6085 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006086 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006087
Daniel Stonec9785ea2012-05-30 16:31:52 +01006088 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006089 switcher_next(switcher);
6090}
6091
6092static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006093switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006094 uint32_t mods_depressed, uint32_t mods_latched,
6095 uint32_t mods_locked, uint32_t group)
6096{
6097 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006098 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006099
Daniel Stone351eb612012-05-31 15:27:47 -04006100 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6101 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006102}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006103
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006104static void
6105switcher_cancel(struct weston_keyboard_grab *grab)
6106{
6107 struct switcher *switcher = container_of(grab, struct switcher, grab);
6108
6109 switcher_destroy(switcher);
6110}
6111
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006112static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006113 switcher_key,
6114 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006115 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006116};
6117
6118static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006119switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6120 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006121{
Tiago Vignattibe143262012-04-16 17:31:41 +03006122 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006123 struct switcher *switcher;
6124
6125 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006126 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006127 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006128 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006129 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006130 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006131
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006132 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006133 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006134 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006135 weston_keyboard_start_grab(keyboard, &switcher->grab);
6136 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006137 switcher_next(switcher);
6138}
6139
Pekka Paalanen3c647232011-12-22 13:43:43 +02006140static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006141backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6142 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006143{
6144 struct weston_compositor *compositor = data;
6145 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006146 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006147
6148 /* TODO: we're limiting to simple use cases, where we assume just
6149 * control on the primary display. We'd have to extend later if we
6150 * ever get support for setting backlights on random desktop LCD
6151 * panels though */
6152 output = get_default_output(compositor);
6153 if (!output)
6154 return;
6155
6156 if (!output->set_backlight)
6157 return;
6158
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006159 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6160 backlight_new = output->backlight_current - 25;
6161 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6162 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006163
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006164 if (backlight_new < 5)
6165 backlight_new = 5;
6166 if (backlight_new > 255)
6167 backlight_new = 255;
6168
6169 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006170 output->set_backlight(output, output->backlight_current);
6171}
6172
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006173static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006174force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6175 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006176{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006177 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006178 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006179 struct desktop_shell *shell = data;
6180 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006181 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006182
Derek Foreman8ae2db52015-07-15 13:00:36 -05006183 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006184 if (!focus_surface)
6185 return;
6186
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006187 wl_signal_emit(&compositor->kill_signal, focus_surface);
6188
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006189 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006190 wl_client_get_credentials(client, &pid, NULL, NULL);
6191
6192 /* Skip clients that we launched ourselves (the credentials of
6193 * the socketpair is ours) */
6194 if (pid == getpid())
6195 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006196
Daniel Stone325fc2d2012-05-30 16:31:58 +01006197 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006198}
6199
6200static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006201workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006202 uint32_t key, void *data)
6203{
6204 struct desktop_shell *shell = data;
6205 unsigned int new_index = shell->workspaces.current;
6206
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006207 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006208 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006209 if (new_index != 0)
6210 new_index--;
6211
6212 change_workspace(shell, new_index);
6213}
6214
6215static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006216workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006217 uint32_t key, void *data)
6218{
6219 struct desktop_shell *shell = data;
6220 unsigned int new_index = shell->workspaces.current;
6221
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006222 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006223 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006224 if (new_index < shell->workspaces.num - 1)
6225 new_index++;
6226
6227 change_workspace(shell, new_index);
6228}
6229
6230static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006231workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006232 uint32_t key, void *data)
6233{
6234 struct desktop_shell *shell = data;
6235 unsigned int new_index;
6236
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006237 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006238 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006239 new_index = key - KEY_F1;
6240 if (new_index >= shell->workspaces.num)
6241 new_index = shell->workspaces.num - 1;
6242
6243 change_workspace(shell, new_index);
6244}
6245
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006246static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006247workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6248 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006249{
6250 struct desktop_shell *shell = data;
6251 unsigned int new_index = shell->workspaces.current;
6252
6253 if (shell->locked)
6254 return;
6255
6256 if (new_index != 0)
6257 new_index--;
6258
Derek Foreman8ae2db52015-07-15 13:00:36 -05006259 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006260}
6261
6262static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006263workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6264 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006265{
6266 struct desktop_shell *shell = data;
6267 unsigned int new_index = shell->workspaces.current;
6268
6269 if (shell->locked)
6270 return;
6271
6272 if (new_index < shell->workspaces.num - 1)
6273 new_index++;
6274
Derek Foreman8ae2db52015-07-15 13:00:36 -05006275 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006276}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006277
6278static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006279shell_reposition_view_on_output_destroy(struct weston_view *view)
6280{
6281 struct weston_output *output, *first_output;
6282 struct weston_compositor *ec = view->surface->compositor;
6283 struct shell_surface *shsurf;
6284 float x, y;
6285 int visible;
6286
6287 x = view->geometry.x;
6288 y = view->geometry.y;
6289
6290 /* At this point the destroyed output is not in the list anymore.
6291 * If the view is still visible somewhere, we leave where it is,
6292 * otherwise, move it to the first output. */
6293 visible = 0;
6294 wl_list_for_each(output, &ec->output_list, link) {
6295 if (pixman_region32_contains_point(&output->region,
6296 x, y, NULL)) {
6297 visible = 1;
6298 break;
6299 }
6300 }
6301
6302 if (!visible) {
6303 first_output = container_of(ec->output_list.next,
6304 struct weston_output, link);
6305
6306 x = first_output->x + first_output->width / 4;
6307 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006308
6309 weston_view_set_position(view, x, y);
6310 } else {
6311 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006312 }
6313
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006314
6315 shsurf = get_shell_surface(view->surface);
6316
6317 if (shsurf) {
6318 shsurf->saved_position_valid = false;
6319 shsurf->next_state.maximized = false;
6320 shsurf->next_state.fullscreen = false;
6321 shsurf->state_changed = true;
6322 }
6323}
6324
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006325void
6326shell_for_each_layer(struct desktop_shell *shell,
6327 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006328{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006329 struct workspace **ws;
6330
6331 func(shell, &shell->fullscreen_layer, data);
6332 func(shell, &shell->panel_layer, data);
6333 func(shell, &shell->background_layer, data);
6334 func(shell, &shell->lock_layer, data);
6335 func(shell, &shell->input_panel_layer, data);
6336
6337 wl_array_for_each(ws, &shell->workspaces.array)
6338 func(shell, &(*ws)->layer, data);
6339}
6340
6341static void
6342shell_output_destroy_move_layer(struct desktop_shell *shell,
6343 struct weston_layer *layer,
6344 void *data)
6345{
6346 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006347 struct weston_view *view;
6348
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006349 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006350 if (view->output != output)
6351 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006352
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006353 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006354 }
6355}
6356
6357static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006358handle_output_destroy(struct wl_listener *listener, void *data)
6359{
6360 struct shell_output *output_listener =
6361 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006362 struct weston_output *output = output_listener->output;
6363 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006364
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006365 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006366
Xiong Zhang6b481422013-10-23 13:58:32 +08006367 wl_list_remove(&output_listener->destroy_listener.link);
6368 wl_list_remove(&output_listener->link);
6369 free(output_listener);
6370}
6371
6372static void
6373create_shell_output(struct desktop_shell *shell,
6374 struct weston_output *output)
6375{
6376 struct shell_output *shell_output;
6377
6378 shell_output = zalloc(sizeof *shell_output);
6379 if (shell_output == NULL)
6380 return;
6381
6382 shell_output->output = output;
6383 shell_output->shell = shell;
6384 shell_output->destroy_listener.notify = handle_output_destroy;
6385 wl_signal_add(&output->destroy_signal,
6386 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006387 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006388}
6389
6390static void
6391handle_output_create(struct wl_listener *listener, void *data)
6392{
6393 struct desktop_shell *shell =
6394 container_of(listener, struct desktop_shell, output_create_listener);
6395 struct weston_output *output = (struct weston_output *)data;
6396
6397 create_shell_output(shell, output);
6398}
6399
6400static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006401handle_output_move_layer(struct desktop_shell *shell,
6402 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006403{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006404 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006405 struct weston_view *view;
6406 float x, y;
6407
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006408 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006409 if (view->output != output)
6410 continue;
6411
6412 x = view->geometry.x + output->move_x;
6413 y = view->geometry.y + output->move_y;
6414 weston_view_set_position(view, x, y);
6415 }
6416}
6417
6418static void
6419handle_output_move(struct wl_listener *listener, void *data)
6420{
6421 struct desktop_shell *shell;
6422
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006423 shell = container_of(listener, struct desktop_shell,
6424 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006425
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006426 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006427}
6428
6429static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006430setup_output_destroy_handler(struct weston_compositor *ec,
6431 struct desktop_shell *shell)
6432{
6433 struct weston_output *output;
6434
6435 wl_list_init(&shell->output_list);
6436 wl_list_for_each(output, &ec->output_list, link)
6437 create_shell_output(shell, output);
6438
6439 shell->output_create_listener.notify = handle_output_create;
6440 wl_signal_add(&ec->output_created_signal,
6441 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006442
6443 shell->output_move_listener.notify = handle_output_move;
6444 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006445}
6446
6447static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006448shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006449{
Tiago Vignattibe143262012-04-16 17:31:41 +03006450 struct desktop_shell *shell =
6451 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006452 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006453 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006454
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006455 /* Force state to unlocked so we don't try to fade */
6456 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006457
6458 if (shell->child.client) {
6459 /* disable respawn */
6460 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006461 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006462 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006463
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006464 wl_list_remove(&shell->idle_listener.link);
6465 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006466 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006467
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006468 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006469 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006470
Xiong Zhang6b481422013-10-23 13:58:32 +08006471 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6472 wl_list_remove(&shell_output->destroy_listener.link);
6473 wl_list_remove(&shell_output->link);
6474 free(shell_output);
6475 }
6476
6477 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006478 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006479
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006480 wl_array_for_each(ws, &shell->workspaces.array)
6481 workspace_destroy(*ws);
6482 wl_array_release(&shell->workspaces.array);
6483
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006484 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006485 free(shell);
6486}
6487
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006488static void
6489shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6490{
6491 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006492 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006493
Bob Ham744e6532016-01-12 10:21:48 +00006494 if (shell->allow_zap)
6495 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6496 MODIFIER_CTRL | MODIFIER_ALT,
6497 terminate_binding, ec);
6498
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006499 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006500 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6501 click_to_activate_binding,
6502 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006503 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6504 click_to_activate_binding,
6505 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006506 weston_compositor_add_touch_binding(ec, 0,
6507 touch_to_activate_binding,
6508 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006509 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6510 MODIFIER_SUPER | MODIFIER_ALT,
6511 surface_opacity_binding, NULL);
6512 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6513 MODIFIER_SUPER, zoom_axis_binding,
6514 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006515 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6516 backlight_binding, ec);
6517 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6518 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006519
6520 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006521 if (shell->exposay_modifier)
6522 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6523 exposay_binding, shell);
6524
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006525 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006526 if (!mod)
6527 return;
6528
Daniel Stone325fc2d2012-05-30 16:31:58 +01006529 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6530 zoom_key_binding, NULL);
6531 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6532 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006533 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6534 maximize_binding, NULL);
6535 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6536 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006537 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6538 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006539 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006540 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006541 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006542 weston_compositor_add_button_binding(ec, BTN_LEFT,
6543 mod | MODIFIER_SHIFT,
6544 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006545
6546 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006547 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006548 rotate_binding, NULL);
6549
Daniel Stone325fc2d2012-05-30 16:31:58 +01006550 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6551 shell);
6552 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6553 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006554 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6555 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006556 weston_compositor_add_key_binding(ec, KEY_K, mod,
6557 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006558 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6559 workspace_up_binding, shell);
6560 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6561 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006562 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6563 workspace_move_surface_up_binding,
6564 shell);
6565 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6566 workspace_move_surface_down_binding,
6567 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006568
6569 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6570 if (shell->workspaces.num > 1) {
6571 num_workspace_bindings = shell->workspaces.num;
6572 if (num_workspace_bindings > 6)
6573 num_workspace_bindings = 6;
6574 for (i = 0; i < num_workspace_bindings; i++)
6575 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6576 workspace_f_binding,
6577 shell);
6578 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006579
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006580 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006581}
6582
Jason Ekstrand024177c2014-04-21 19:42:58 -05006583static void
6584handle_seat_created(struct wl_listener *listener, void *data)
6585{
6586 struct weston_seat *seat = data;
6587
6588 create_shell_seat(seat);
6589}
6590
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006591WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006592module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006593 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006594{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006595 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006596 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006597 struct workspace **pws;
6598 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006599 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006600
Peter Huttererf3d62272013-08-08 11:57:05 +10006601 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006602 if (shell == NULL)
6603 return -1;
6604
Kristian Høgsberg75840622011-09-06 13:48:16 -04006605 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006606
6607 shell->destroy_listener.notify = shell_destroy;
6608 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006609 shell->idle_listener.notify = idle_handler;
6610 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6611 shell->wake_listener.notify = wake_handler;
6612 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006613 shell->transform_listener.notify = transform_handler;
6614 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006615
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006616 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006617 ec->shell_interface.create_shell_surface = create_shell_surface;
6618 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006619 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006620 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006621 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006622 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006623 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006624 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006625 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006626 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006627 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006628
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006629 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6630 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006631 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6632 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006633 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006634
6635 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006636 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006637
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006638 if (input_panel_setup(shell) < 0)
6639 return -1;
6640
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006641 shell->text_backend = text_backend_init(ec);
6642 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006643 return -1;
6644
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006645 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006646
Daniel Stonedf8133b2013-11-19 11:37:14 +01006647 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6648 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6649
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006650 for (i = 0; i < shell->workspaces.num; i++) {
6651 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6652 if (pws == NULL)
6653 return -1;
6654
6655 *pws = workspace_create();
6656 if (*pws == NULL)
6657 return -1;
6658 }
6659 activate_workspace(shell, 0);
6660
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006661 weston_layer_init(&shell->minimized_layer, NULL);
6662
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006663 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006664 wl_list_init(&shell->workspaces.animation.link);
6665 shell->workspaces.animation.frame = animate_workspace_change_frame;
6666
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006667 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006668 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006669 return -1;
6670
Rafael Antognollie2a34552013-12-03 15:35:45 -02006671 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6672 shell, bind_xdg_shell) == NULL)
6673 return -1;
6674
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006675 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006676 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006677 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006678 return -1;
6679
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006680 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006681
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006682 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006683
Xiong Zhang6b481422013-10-23 13:58:32 +08006684 setup_output_destroy_handler(ec, shell);
6685
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006686 loop = wl_display_get_event_loop(ec->wl_display);
6687 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006688
Jason Ekstrand024177c2014-04-21 19:42:58 -05006689 wl_list_for_each(seat, &ec->seat_list, link)
6690 handle_seat_created(NULL, seat);
6691 shell->seat_create_listener.notify = handle_seat_created;
6692 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006693
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006694 screenshooter_create(ec);
6695
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006696 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006697
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006698 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006699
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006700 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6701
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006702 return 0;
6703}