blob: d58a830be868395ac991518165e2da950e1dcc17 [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;
613 else
614 return MODIFIER_SUPER;
615}
616
Juan Zhaoe10d2792012-04-25 19:09:52 +0800617static enum animation_type
618get_animation_type(char *animation)
619{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800620 if (!animation)
621 return ANIMATION_NONE;
622
Juan Zhaoe10d2792012-04-25 19:09:52 +0800623 if (!strcmp("zoom", animation))
624 return ANIMATION_ZOOM;
625 else if (!strcmp("fade", animation))
626 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100627 else if (!strcmp("dim-layer", animation))
628 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800629 else
630 return ANIMATION_NONE;
631}
632
Alex Wu4539b082012-03-01 12:57:46 +0800633static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400634shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200635{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400636 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500637 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300638 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200639
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400640 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400641 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300642 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
643 WESTON_SHELL_CLIENT);
644 if (ret < 0)
645 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400646 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500647 "client", &s, client);
648 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100649 shell->client = s;
650 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400651 "binding-modifier", &s, "super");
652 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200653 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800654
655 weston_config_section_get_string(section,
656 "exposay-modifier", &s, "none");
657 if (strcmp(s, "none") == 0)
658 shell->exposay_modifier = 0;
659 else
660 shell->exposay_modifier = get_modifier(s);
661 free(s);
662
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400663 weston_config_section_get_string(section, "animation", &s, "none");
664 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200665 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200666 weston_config_section_get_string(section, "close-animation", &s, "fade");
667 shell->win_close_animation_type = get_animation_type(s);
668 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700669 weston_config_section_get_string(section,
670 "startup-animation", &s, "fade");
671 shell->startup_animation_type = get_animation_type(s);
672 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700673 if (shell->startup_animation_type == ANIMATION_ZOOM)
674 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100675 weston_config_section_get_string(section, "focus-animation", &s, "none");
676 shell->focus_animation_type = get_animation_type(s);
677 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400678 weston_config_section_get_uint(section, "num-workspaces",
679 &shell->workspaces.num,
680 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200681}
682
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800683struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100684get_default_output(struct weston_compositor *compositor)
685{
686 return container_of(compositor->output_list.next,
687 struct weston_output, link);
688}
689
Pekka Paalanen8274d902014-08-06 19:36:51 +0300690static int
691focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
692{
693 return snprintf(buf, len, "focus highlight effect for output %s",
694 surface->output->name);
695}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696
697/* no-op func for checking focus surface */
698static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600699focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100700{
701}
702
703static struct focus_surface *
704get_focus_surface(struct weston_surface *surface)
705{
706 if (surface->configure == focus_surface_configure)
707 return surface->configure_private;
708 else
709 return NULL;
710}
711
712static bool
713is_focus_surface (struct weston_surface *es)
714{
715 return (es->configure == focus_surface_configure);
716}
717
718static bool
719is_focus_view (struct weston_view *view)
720{
721 return is_focus_surface (view->surface);
722}
723
724static struct focus_surface *
725create_focus_surface(struct weston_compositor *ec,
726 struct weston_output *output)
727{
728 struct focus_surface *fsurf = NULL;
729 struct weston_surface *surface = NULL;
730
731 fsurf = malloc(sizeof *fsurf);
732 if (!fsurf)
733 return NULL;
734
735 fsurf->surface = weston_surface_create(ec);
736 surface = fsurf->surface;
737 if (surface == NULL) {
738 free(fsurf);
739 return NULL;
740 }
741
742 surface->configure = focus_surface_configure;
743 surface->output = output;
744 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300745 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100746
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800747 fsurf->view = weston_view_create(surface);
748 if (fsurf->view == NULL) {
749 weston_surface_destroy(surface);
750 free(fsurf);
751 return NULL;
752 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100753 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100754
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600755 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600756 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
758 pixman_region32_fini(&surface->opaque);
759 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
760 output->width, output->height);
761 pixman_region32_fini(&surface->input);
762 pixman_region32_init(&surface->input);
763
764 wl_list_init(&fsurf->workspace_transform.link);
765
766 return fsurf;
767}
768
769static void
770focus_surface_destroy(struct focus_surface *fsurf)
771{
772 weston_surface_destroy(fsurf->surface);
773 free(fsurf);
774}
775
776static void
777focus_animation_done(struct weston_view_animation *animation, void *data)
778{
779 struct workspace *ws = data;
780
781 ws->focus_animation = NULL;
782}
783
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200784static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200785focus_state_destroy(struct focus_state *state)
786{
787 wl_list_remove(&state->seat_destroy_listener.link);
788 wl_list_remove(&state->surface_destroy_listener.link);
789 free(state);
790}
791
792static void
793focus_state_seat_destroy(struct wl_listener *listener, void *data)
794{
795 struct focus_state *state = container_of(listener,
796 struct focus_state,
797 seat_destroy_listener);
798
799 wl_list_remove(&state->link);
800 focus_state_destroy(state);
801}
802
803static void
804focus_state_surface_destroy(struct wl_listener *listener, void *data)
805{
806 struct focus_state *state = container_of(listener,
807 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400808 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400809 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500810 struct weston_surface *main_surface, *next;
811 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200812
Pekka Paalanen01388e22013-04-25 13:57:44 +0300813 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
814
Kristian Høgsberge3778222012-07-31 17:29:30 -0400815 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300816 wl_list_for_each(view,
817 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500818 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400819 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100820 if (is_focus_view(view))
821 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400822
Jason Ekstranda7af7042013-10-12 22:38:11 -0500823 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400824 break;
825 }
826
Pekka Paalanen01388e22013-04-25 13:57:44 +0300827 /* if the focus was a sub-surface, activate its main surface */
828 if (main_surface != state->keyboard_focus)
829 next = main_surface;
830
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400832 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100834 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400835 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
837 if (state->ws->focus_animation)
838 weston_view_animation_destroy(state->ws->focus_animation);
839
840 state->ws->focus_animation = weston_fade_run(
841 state->ws->fsurf_front->view,
842 state->ws->fsurf_front->view->alpha, 0.0, 300,
843 focus_animation_done, state->ws);
844 }
845
Kristian Høgsberge3778222012-07-31 17:29:30 -0400846 wl_list_remove(&state->link);
847 focus_state_destroy(state);
848 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200849}
850
851static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400852focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200853{
Jonas Ådahl04769742012-06-13 00:01:24 +0200854 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200855
856 state = malloc(sizeof *state);
857 if (state == NULL)
858 return NULL;
859
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400861 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200862 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400863 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200864
865 state->seat_destroy_listener.notify = focus_state_seat_destroy;
866 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400867 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200868 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400869 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200870
871 return state;
872}
873
Jonas Ådahl8538b222012-08-29 22:13:03 +0200874static struct focus_state *
875ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
876{
877 struct workspace *ws = get_current_workspace(shell);
878 struct focus_state *state;
879
880 wl_list_for_each(state, &ws->focus_list, link)
881 if (state->seat == seat)
882 break;
883
884 if (&state->link == &ws->focus_list)
885 state = focus_state_create(seat, ws);
886
887 return state;
888}
889
Jonas Ådahl04769742012-06-13 00:01:24 +0200890static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800891focus_state_set_focus(struct focus_state *state,
892 struct weston_surface *surface)
893{
894 if (state->keyboard_focus) {
895 wl_list_remove(&state->surface_destroy_listener.link);
896 wl_list_init(&state->surface_destroy_listener.link);
897 }
898
899 state->keyboard_focus = surface;
900 if (surface)
901 wl_signal_add(&surface->destroy_signal,
902 &state->surface_destroy_listener);
903}
904
905static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400906restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200907{
908 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400909 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100910 struct wl_list pending_seat_list;
911 struct weston_seat *seat, *next_seat;
912
913 /* Temporarily steal the list of seats so that we can keep
914 * track of the seats we've already processed */
915 wl_list_init(&pending_seat_list);
916 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
917 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200918
919 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500920 struct weston_keyboard *keyboard =
921 weston_seat_get_keyboard(state->seat);
922
Neil Roberts4237f502014-04-09 16:33:31 +0100923 wl_list_remove(&state->seat->link);
924 wl_list_insert(&shell->compositor->seat_list,
925 &state->seat->link);
926
Derek Foreman1281a362015-07-31 16:55:32 -0500927 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800928 continue;
929
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400930 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200931
Derek Foreman1281a362015-07-31 16:55:32 -0500932 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200933 }
Neil Roberts4237f502014-04-09 16:33:31 +0100934
935 /* For any remaining seats that we don't have a focus state
936 * for we'll reset the keyboard focus to NULL */
937 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500938 struct weston_keyboard *keyboard =
939 weston_seat_get_keyboard(seat);
940
Neil Roberts4237f502014-04-09 16:33:31 +0100941 wl_list_insert(&shell->compositor->seat_list, &seat->link);
942
Derek Foreman1281a362015-07-31 16:55:32 -0500943 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100944 continue;
945
Derek Foreman1281a362015-07-31 16:55:32 -0500946 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100947 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200948}
949
950static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200951replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
952 struct weston_seat *seat)
953{
Derek Foreman1281a362015-07-31 16:55:32 -0500954 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200955 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200956
957 wl_list_for_each(state, &ws->focus_list, link) {
958 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500959 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200960 return;
961 }
962 }
963}
964
965static void
966drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
967 struct weston_surface *surface)
968{
969 struct focus_state *state;
970
971 wl_list_for_each(state, &ws->focus_list, link)
972 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800973 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200974}
975
976static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
978 struct weston_view *from, struct weston_view *to)
979{
980 struct weston_output *output;
981 bool focus_surface_created = false;
982
983 /* FIXME: Only support dim animation using two layers */
984 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
985 return;
986
987 output = get_default_output(shell->compositor);
988 if (ws->fsurf_front == NULL && (from || to)) {
989 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800990 if (ws->fsurf_front == NULL)
991 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800993
994 ws->fsurf_back = create_focus_surface(shell->compositor, output);
995 if (ws->fsurf_back == NULL) {
996 focus_surface_destroy(ws->fsurf_front);
997 return;
998 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001000
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001001 focus_surface_created = true;
1002 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001003 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1004 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001005 }
1006
1007 if (ws->focus_animation) {
1008 weston_view_animation_destroy(ws->focus_animation);
1009 ws->focus_animation = NULL;
1010 }
1011
1012 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001013 weston_layer_entry_insert(&to->layer_link,
1014 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001016 weston_layer_entry_insert(&ws->layer.view_list,
1017 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001018
1019 if (focus_surface_created) {
1020 ws->focus_animation = weston_fade_run(
1021 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001022 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001023 focus_animation_done, ws);
1024 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001025 weston_layer_entry_insert(&from->layer_link,
1026 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001027 ws->focus_animation = weston_stable_fade_run(
1028 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001029 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001030 focus_animation_done, ws);
1031 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001032 weston_layer_entry_insert(&ws->layer.view_list,
1033 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 ws->focus_animation = weston_stable_fade_run(
1035 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001036 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 focus_animation_done, ws);
1038 }
1039}
1040
1041static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001042workspace_destroy(struct workspace *ws)
1043{
Jonas Ådahl04769742012-06-13 00:01:24 +02001044 struct focus_state *state, *next;
1045
1046 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1047 focus_state_destroy(state);
1048
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001049 if (ws->fsurf_front)
1050 focus_surface_destroy(ws->fsurf_front);
1051 if (ws->fsurf_back)
1052 focus_surface_destroy(ws->fsurf_back);
1053
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001054 free(ws);
1055}
1056
Jonas Ådahl04769742012-06-13 00:01:24 +02001057static void
1058seat_destroyed(struct wl_listener *listener, void *data)
1059{
1060 struct weston_seat *seat = data;
1061 struct focus_state *state, *next;
1062 struct workspace *ws = container_of(listener,
1063 struct workspace,
1064 seat_destroyed_listener);
1065
1066 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1067 if (state->seat == seat)
1068 wl_list_remove(&state->link);
1069}
1070
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001071static struct workspace *
1072workspace_create(void)
1073{
1074 struct workspace *ws = malloc(sizeof *ws);
1075 if (ws == NULL)
1076 return NULL;
1077
1078 weston_layer_init(&ws->layer, NULL);
1079
Jonas Ådahl04769742012-06-13 00:01:24 +02001080 wl_list_init(&ws->focus_list);
1081 wl_list_init(&ws->seat_destroyed_listener.link);
1082 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001083 ws->fsurf_front = NULL;
1084 ws->fsurf_back = NULL;
1085 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001086
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001087 return ws;
1088}
1089
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001090static int
1091workspace_is_empty(struct workspace *ws)
1092{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001093 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001094}
1095
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001096static struct workspace *
1097get_workspace(struct desktop_shell *shell, unsigned int index)
1098{
1099 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001100 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001101 pws += index;
1102 return *pws;
1103}
1104
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001105struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001106get_current_workspace(struct desktop_shell *shell)
1107{
1108 return get_workspace(shell, shell->workspaces.current);
1109}
1110
1111static void
1112activate_workspace(struct desktop_shell *shell, unsigned int index)
1113{
1114 struct workspace *ws;
1115
1116 ws = get_workspace(shell, index);
1117 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1118
1119 shell->workspaces.current = index;
1120}
1121
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122static unsigned int
1123get_output_height(struct weston_output *output)
1124{
1125 return abs(output->region.extents.y1 - output->region.extents.y2);
1126}
1127
1128static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001129view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131 struct weston_transform *transform;
1132
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001133 if (is_focus_view(view)) {
1134 struct focus_surface *fsurf = get_focus_surface(view->surface);
1135 transform = &fsurf->workspace_transform;
1136 } else {
1137 struct shell_surface *shsurf = get_shell_surface(view->surface);
1138 transform = &shsurf->workspace_transform;
1139 }
1140
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001141 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001143 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001145 weston_matrix_init(&transform->matrix);
1146 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149}
1150
1151static void
1152workspace_translate_out(struct workspace *ws, double fraction)
1153{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155 unsigned int height;
1156 double d;
1157
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001158 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 d = height * fraction;
1161
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001162 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163 }
1164}
1165
1166static void
1167workspace_translate_in(struct workspace *ws, double fraction)
1168{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001170 unsigned int height;
1171 double d;
1172
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001173 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001175
1176 if (fraction > 0)
1177 d = -(height - height * fraction);
1178 else
1179 d = height + height * fraction;
1180
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001181 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 }
1183}
1184
1185static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001186reverse_workspace_change_animation(struct desktop_shell *shell,
1187 unsigned int index,
1188 struct workspace *from,
1189 struct workspace *to)
1190{
1191 shell->workspaces.current = index;
1192
1193 shell->workspaces.anim_to = to;
1194 shell->workspaces.anim_from = from;
1195 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1196 shell->workspaces.anim_timestamp = 0;
1197
Scott Moreau4272e632012-08-13 09:58:41 -06001198 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001199}
1200
1201static void
1202workspace_deactivate_transforms(struct workspace *ws)
1203{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001205 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001206
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001207 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001208 if (is_focus_view(view)) {
1209 struct focus_surface *fsurf = get_focus_surface(view->surface);
1210 transform = &fsurf->workspace_transform;
1211 } else {
1212 struct shell_surface *shsurf = get_shell_surface(view->surface);
1213 transform = &shsurf->workspace_transform;
1214 }
1215
1216 if (!wl_list_empty(&transform->link)) {
1217 wl_list_remove(&transform->link);
1218 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001219 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001221 }
1222}
1223
1224static void
1225finish_workspace_change_animation(struct desktop_shell *shell,
1226 struct workspace *from,
1227 struct workspace *to)
1228{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001229 struct weston_view *view;
1230
Scott Moreau4272e632012-08-13 09:58:41 -06001231 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001232
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001233 /* Views that extend past the bottom of the output are still
1234 * visible after the workspace animation ends but before its layer
1235 * is hidden. In that case, we need to damage below those views so
1236 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001237 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001238 weston_view_damage_below(view);
1239
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001240 wl_list_remove(&shell->workspaces.animation.link);
1241 workspace_deactivate_transforms(from);
1242 workspace_deactivate_transforms(to);
1243 shell->workspaces.anim_to = NULL;
1244
1245 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1246}
1247
1248static void
1249animate_workspace_change_frame(struct weston_animation *animation,
1250 struct weston_output *output, uint32_t msecs)
1251{
1252 struct desktop_shell *shell =
1253 container_of(animation, struct desktop_shell,
1254 workspaces.animation);
1255 struct workspace *from = shell->workspaces.anim_from;
1256 struct workspace *to = shell->workspaces.anim_to;
1257 uint32_t t;
1258 double x, y;
1259
1260 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1261 finish_workspace_change_animation(shell, from, to);
1262 return;
1263 }
1264
1265 if (shell->workspaces.anim_timestamp == 0) {
1266 if (shell->workspaces.anim_current == 0.0)
1267 shell->workspaces.anim_timestamp = msecs;
1268 else
1269 shell->workspaces.anim_timestamp =
1270 msecs -
1271 /* Invers of movement function 'y' below. */
1272 (asin(1.0 - shell->workspaces.anim_current) *
1273 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1274 M_2_PI);
1275 }
1276
1277 t = msecs - shell->workspaces.anim_timestamp;
1278
1279 /*
1280 * x = [0, π/2]
1281 * y(x) = sin(x)
1282 */
1283 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1284 y = sin(x);
1285
1286 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001287 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001288
1289 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1290 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1291 shell->workspaces.anim_current = y;
1292
Scott Moreau4272e632012-08-13 09:58:41 -06001293 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001294 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001295 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001296 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001297}
1298
1299static void
1300animate_workspace_change(struct desktop_shell *shell,
1301 unsigned int index,
1302 struct workspace *from,
1303 struct workspace *to)
1304{
1305 struct weston_output *output;
1306
1307 int dir;
1308
1309 if (index > shell->workspaces.current)
1310 dir = -1;
1311 else
1312 dir = 1;
1313
1314 shell->workspaces.current = index;
1315
1316 shell->workspaces.anim_dir = dir;
1317 shell->workspaces.anim_from = from;
1318 shell->workspaces.anim_to = to;
1319 shell->workspaces.anim_current = 0.0;
1320 shell->workspaces.anim_timestamp = 0;
1321
1322 output = container_of(shell->compositor->output_list.next,
1323 struct weston_output, link);
1324 wl_list_insert(&output->animation_list,
1325 &shell->workspaces.animation.link);
1326
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001327 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001328
1329 workspace_translate_in(to, 0);
1330
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001331 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001332
Scott Moreau4272e632012-08-13 09:58:41 -06001333 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001334}
1335
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001336static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001337update_workspace(struct desktop_shell *shell, unsigned int index,
1338 struct workspace *from, struct workspace *to)
1339{
1340 shell->workspaces.current = index;
1341 wl_list_insert(&from->layer.link, &to->layer.link);
1342 wl_list_remove(&from->layer.link);
1343}
1344
1345static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001346change_workspace(struct desktop_shell *shell, unsigned int index)
1347{
1348 struct workspace *from;
1349 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001350 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001351
1352 if (index == shell->workspaces.current)
1353 return;
1354
1355 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001356 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001357 return;
1358
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001359 from = get_current_workspace(shell);
1360 to = get_workspace(shell, index);
1361
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001362 if (shell->workspaces.anim_from == to &&
1363 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001364 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001365 reverse_workspace_change_animation(shell, index, from, to);
1366 return;
1367 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001368
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001369 if (shell->workspaces.anim_to != NULL)
1370 finish_workspace_change_animation(shell,
1371 shell->workspaces.anim_from,
1372 shell->workspaces.anim_to);
1373
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001374 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001375
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001376 if (shell->focus_animation_type != ANIMATION_NONE) {
1377 wl_list_for_each(state, &from->focus_list, link)
1378 if (state->keyboard_focus)
1379 animate_focus_change(shell, from,
1380 get_default_view(state->keyboard_focus), NULL);
1381
1382 wl_list_for_each(state, &to->focus_list, link)
1383 if (state->keyboard_focus)
1384 animate_focus_change(shell, to,
1385 NULL, get_default_view(state->keyboard_focus));
1386 }
1387
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001388 if (workspace_is_empty(to) && workspace_is_empty(from))
1389 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001390 else
1391 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001392}
1393
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001394static bool
1395workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1396{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001397 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001398 struct wl_list *e;
1399
1400 if (wl_list_empty(list))
1401 return false;
1402
1403 e = list->next;
1404
1405 if (e->next != list)
1406 return false;
1407
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001408 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001409}
1410
1411static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001412surface_keyboard_focus_lost(struct weston_surface *surface)
1413{
1414 struct weston_compositor *compositor = surface->compositor;
1415 struct weston_seat *seat;
1416 struct weston_surface *focus;
1417
1418 wl_list_for_each(seat, &compositor->seat_list, link) {
1419 struct weston_keyboard *keyboard =
1420 weston_seat_get_keyboard(seat);
1421
1422 if (!keyboard)
1423 continue;
1424
1425 focus = weston_surface_get_main_surface(keyboard->focus);
1426 if (focus == surface)
1427 weston_keyboard_set_focus(keyboard, NULL);
1428 }
1429}
1430
1431static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001432take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001433 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001434 unsigned int index)
1435{
Derek Foreman1281a362015-07-31 16:55:32 -05001436 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001437 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001439 struct shell_surface *shsurf;
1440 struct workspace *from;
1441 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001442 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001443
Derek Foreman1281a362015-07-31 16:55:32 -05001444 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 view = get_default_view(surface);
1446 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001447 index == shell->workspaces.current ||
1448 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001449 return;
1450
1451 from = get_current_workspace(shell);
1452 to = get_workspace(shell, index);
1453
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001454 weston_layer_entry_remove(&view->layer_link);
1455 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001456
Philip Withnall659163d2013-11-25 18:01:36 +00001457 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001458 if (shsurf != NULL)
1459 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001460
Jonas Ådahle9d22502012-08-29 22:13:01 +02001461 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001462 drop_focus_state(shell, from, surface);
1463
1464 if (shell->workspaces.anim_from == to &&
1465 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001466 wl_list_remove(&to->layer.link);
1467 wl_list_insert(from->layer.link.prev, &to->layer.link);
1468
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001469 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001470
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001471 return;
1472 }
1473
1474 if (shell->workspaces.anim_to != NULL)
1475 finish_workspace_change_animation(shell,
1476 shell->workspaces.anim_from,
1477 shell->workspaces.anim_to);
1478
1479 if (workspace_is_empty(from) &&
1480 workspace_has_only(to, surface))
1481 update_workspace(shell, index, from, to);
1482 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001483 if (shsurf != NULL &&
1484 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001485 wl_list_insert(&shell->workspaces.anim_sticky_list,
1486 &shsurf->workspace_transform.link);
1487
1488 animate_workspace_change(shell, index, from, to);
1489 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001490
Jonas Ådahl8538b222012-08-29 22:13:03 +02001491 state = ensure_focus_state(shell, seat);
1492 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001493 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001494}
1495
1496static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001497touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001498 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001499{
1500}
1501
1502static void
1503touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1504{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001505 struct weston_touch_move_grab *move =
1506 (struct weston_touch_move_grab *) container_of(
1507 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001508
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001509 if (touch_id == 0)
1510 move->active = 0;
1511
Jonas Ådahl9484b692013-12-02 22:05:03 +01001512 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001513 shell_touch_grab_end(&move->base);
1514 free(move);
1515 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001516}
1517
1518static void
1519touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001520 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001521{
1522 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1523 struct shell_surface *shsurf = move->base.shsurf;
1524 struct weston_surface *es;
1525 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1526 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1527
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001528 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001529 return;
1530
1531 es = shsurf->surface;
1532
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001533 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001534
1535 weston_compositor_schedule_repaint(es->compositor);
1536}
1537
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001538static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001539touch_move_grab_frame(struct weston_touch_grab *grab)
1540{
1541}
1542
1543static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001544touch_move_grab_cancel(struct weston_touch_grab *grab)
1545{
1546 struct weston_touch_move_grab *move =
1547 (struct weston_touch_move_grab *) container_of(
1548 grab, struct shell_touch_grab, grab);
1549
1550 shell_touch_grab_end(&move->base);
1551 free(move);
1552}
1553
Rusty Lynch1084da52013-08-15 09:10:08 -07001554static const struct weston_touch_grab_interface touch_move_grab_interface = {
1555 touch_move_grab_down,
1556 touch_move_grab_up,
1557 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001558 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001559 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001560};
1561
1562static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001563surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001564{
1565 struct weston_touch_move_grab *move;
1566
1567 if (!shsurf)
1568 return -1;
1569
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001570 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001571 return 0;
1572
1573 move = malloc(sizeof *move);
1574 if (!move)
1575 return -1;
1576
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001577 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001579 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001581 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001582
1583 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001584 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001585
1586 return 0;
1587}
1588
1589static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001590noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001591{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001592}
1593
1594static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001595noop_grab_axis(struct weston_pointer_grab *grab,
1596 uint32_t time, uint32_t axis, wl_fixed_t value)
1597{
1598}
1599
1600static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001601constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1602{
1603 struct shell_surface *shsurf = move->base.shsurf;
1604 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001605 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001606 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001607 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001608
1609 x = wl_fixed_to_int(pointer->x + move->dx);
1610 y = wl_fixed_to_int(pointer->y + move->dy);
1611
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001612 if (shsurf->shell->panel_position ==
1613 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001614 get_output_work_area(shsurf->shell,
1615 shsurf->surface->output,
1616 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001617
Derek Foreman6feb0f92015-04-15 12:23:56 -05001618 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1619 if (bottom - safety < area.y)
1620 y = area.y + safety - shsurf->geometry.height
1621 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001622
1623 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001624 y + shsurf->geometry.y < area.y)
1625 y = area.y - shsurf->geometry.y;
1626
Jonny Lambd73c6942014-08-20 15:53:20 +02001627 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001628
1629 *cx = x;
1630 *cy = y;
1631}
1632
1633static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001634move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001635 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001636{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001637 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001638 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001639 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001640 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001641
Jonas Ådahld2510102014-10-05 21:39:14 +02001642 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001643 if (!shsurf)
1644 return;
1645
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001646 constrain_position(move, &cx, &cy);
1647
1648 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001649
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001651}
1652
1653static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001655 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001656{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001657 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1658 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001659 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001660 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001661
Daniel Stone4dbadb12012-05-30 16:31:51 +01001662 if (pointer->button_count == 0 &&
1663 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001664 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001665 free(grab);
1666 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001667}
1668
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001669static void
1670move_grab_cancel(struct weston_pointer_grab *grab)
1671{
1672 struct shell_grab *shell_grab =
1673 container_of(grab, struct shell_grab, grab);
1674
1675 shell_grab_end(shell_grab);
1676 free(grab);
1677}
1678
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001679static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001680 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001681 move_grab_motion,
1682 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001683 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001684 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001685};
1686
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001687static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001688surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001689 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001690{
1691 struct weston_move_grab *move;
1692
1693 if (!shsurf)
1694 return -1;
1695
Derek Foreman45a7c272015-09-11 14:27:40 -05001696 shsurf = find_toplevel_surface(shsurf);
1697
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001698 if (shsurf->grabbed ||
1699 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001700 return 0;
1701
1702 move = malloc(sizeof *move);
1703 if (!move)
1704 return -1;
1705
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001707 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001709 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001710 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711
1712 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001713 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714
1715 return 0;
1716}
1717
1718static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001719common_surface_move(struct wl_resource *resource,
1720 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001721{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001722 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001723 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1724 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001725 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001726 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
Derek Foreman1281a362015-07-31 16:55:32 -05001728 if (pointer &&
1729 pointer->focus &&
1730 pointer->button_count > 0 &&
1731 pointer->grab_serial == serial) {
1732 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001733 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001734 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001735 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001736 } else if (touch &&
1737 touch->focus &&
1738 touch->grab_serial == serial) {
1739 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001740 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001741 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001742 wl_resource_post_no_memory(resource);
1743 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744}
1745
Rafael Antognollie2a34552013-12-03 15:35:45 -02001746static void
1747shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1748 struct wl_resource *seat_resource, uint32_t serial)
1749{
1750 common_surface_move(resource, seat_resource, serial);
1751}
1752
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753struct weston_resize_grab {
1754 struct shell_grab base;
1755 uint32_t edges;
1756 int32_t width, height;
1757};
1758
1759static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001760resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001761 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762{
1763 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001764 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001765 struct shell_surface *shsurf = resize->base.shsurf;
1766 int32_t width, height;
1767 wl_fixed_t from_x, from_y;
1768 wl_fixed_t to_x, to_y;
1769
Jonas Ådahld2510102014-10-05 21:39:14 +02001770 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001771
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001772 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773 return;
1774
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 weston_view_from_global_fixed(shsurf->view,
1776 pointer->grab_x, pointer->grab_y,
1777 &from_x, &from_y);
1778 weston_view_from_global_fixed(shsurf->view,
1779 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001780
1781 width = resize->width;
1782 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1783 width += wl_fixed_to_int(from_x - to_x);
1784 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1785 width += wl_fixed_to_int(to_x - from_x);
1786 }
1787
1788 height = resize->height;
1789 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1790 height += wl_fixed_to_int(from_y - to_y);
1791 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1792 height += wl_fixed_to_int(to_y - from_y);
1793 }
1794
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001795 if (width < 1)
1796 width = 1;
1797 if (height < 1)
1798 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001799 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001800}
1801
1802static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001803send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001804{
1805 struct shell_surface *shsurf = get_shell_surface(surface);
1806
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001807 assert(shsurf);
1808
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001809 if (shsurf->resource)
1810 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001811 shsurf->resize_edges,
1812 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001813}
1814
1815static const struct weston_shell_client shell_client = {
1816 send_configure
1817};
1818
1819static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001820resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821 uint32_t time, uint32_t button, uint32_t state_w)
1822{
1823 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001824 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001825 enum wl_pointer_button_state state = state_w;
1826
1827 if (pointer->button_count == 0 &&
1828 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1829 shell_grab_end(&resize->base);
1830 free(grab);
1831 }
1832}
1833
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001834static void
1835resize_grab_cancel(struct weston_pointer_grab *grab)
1836{
1837 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1838
1839 shell_grab_end(&resize->base);
1840 free(grab);
1841}
1842
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001843static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001844 noop_grab_focus,
1845 resize_grab_motion,
1846 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001847 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001848 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001849};
1850
Giulio Camuffob8366642013-04-25 13:57:46 +03001851/*
1852 * Returns the bounding box of a surface and all its sub-surfaces,
1853 * in the surface coordinates system. */
1854static void
1855surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1856 int32_t *y, int32_t *w, int32_t *h) {
1857 pixman_region32_t region;
1858 pixman_box32_t *box;
1859 struct weston_subsurface *subsurface;
1860
1861 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 surface->width,
1863 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001864
1865 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1866 pixman_region32_union_rect(&region, &region,
1867 subsurface->position.x,
1868 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 subsurface->surface->width,
1870 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001871 }
1872
1873 box = pixman_region32_extents(&region);
1874 if (x)
1875 *x = box->x1;
1876 if (y)
1877 *y = box->y1;
1878 if (w)
1879 *w = box->x2 - box->x1;
1880 if (h)
1881 *h = box->y2 - box->y1;
1882
1883 pixman_region32_fini(&region);
1884}
1885
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001886static int
1887surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001888 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001889{
1890 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001891 const unsigned resize_topbottom =
1892 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1893 const unsigned resize_leftright =
1894 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1895 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001896
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001897 if (shsurf->grabbed ||
1898 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001899 return 0;
1900
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001901 /* Check for invalid edge combinations. */
1902 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1903 (edges & resize_topbottom) == resize_topbottom ||
1904 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001905 return 0;
1906
1907 resize = malloc(sizeof *resize);
1908 if (!resize)
1909 return -1;
1910
1911 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001912
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001913 resize->width = shsurf->geometry.width;
1914 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001915
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001916 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001917 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001918 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001919 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001920
1921 return 0;
1922}
1923
1924static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001925common_surface_resize(struct wl_resource *resource,
1926 struct wl_resource *seat_resource, uint32_t serial,
1927 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001928{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001929 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001930 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001931 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001932 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001933
Derek Foreman1281a362015-07-31 16:55:32 -05001934 if (!pointer ||
1935 pointer->button_count == 0 ||
1936 pointer->grab_serial != serial ||
1937 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001938 return;
1939
Derek Foreman1281a362015-07-31 16:55:32 -05001940 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001941 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001942 return;
1943
Derek Foreman1281a362015-07-31 16:55:32 -05001944 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001945 wl_resource_post_no_memory(resource);
1946}
1947
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001948static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001949shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1950 struct wl_resource *seat_resource, uint32_t serial,
1951 uint32_t edges)
1952{
1953 common_surface_resize(resource, seat_resource, serial, edges);
1954}
1955
1956static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001957busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001958{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001959 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001960 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001961 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001962 wl_fixed_t sx, sy;
1963
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964 view = weston_compositor_pick_view(pointer->seat->compositor,
1965 pointer->x, pointer->y,
1966 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001967
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001968 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1969 shell_grab_end(grab);
1970 free(grab);
1971 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001972}
1973
1974static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001975busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001976 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001977{
Jonas Ådahld2510102014-10-05 21:39:14 +02001978 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001979}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001980
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001981static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001982busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001983 uint32_t time, uint32_t button, uint32_t state)
1984{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001985 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001986 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001987 struct weston_pointer *pointer = grab->grab.pointer;
1988 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001989
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001990 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001991 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001992 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001993 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001994 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05001995 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001996 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001997}
1998
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001999static void
2000busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2001{
2002 struct shell_grab *grab = (struct shell_grab *) base;
2003
2004 shell_grab_end(grab);
2005 free(grab);
2006}
2007
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002008static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002009 busy_cursor_grab_focus,
2010 busy_cursor_grab_motion,
2011 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002012 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002013 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002014};
2015
2016static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002017set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002018{
2019 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002020
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002021 if (pointer->grab->interface == &busy_cursor_grab_interface)
2022 return;
2023
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002024 grab = malloc(sizeof *grab);
2025 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002026 return;
2027
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002028 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002029 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002030 /* Mark the shsurf as ungrabbed so that button binding is able
2031 * to move it. */
2032 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002033}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002034
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002035static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002036end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002037{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002038 struct shell_grab *grab;
2039 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002041 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002042 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2043
2044 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002045 continue;
2046
Derek Foreman1281a362015-07-31 16:55:32 -05002047 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002048 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002049 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002050 wl_resource_get_client(grab->shsurf->resource) == client) {
2051 shell_grab_end(grab);
2052 free(grab);
2053 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002054 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002055}
2056
Scott Moreau9521d5e2012-04-19 13:06:17 -06002057static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002058handle_shell_client_destroy(struct wl_listener *listener, void *data);
2059
2060static int
2061xdg_ping_timeout_handler(void *data)
2062{
2063 struct shell_client *sc = data;
2064 struct weston_seat *seat;
2065 struct shell_surface *shsurf;
2066
2067 /* Client is not responding */
2068 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002069 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002070 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2071
2072 if (!pointer ||
2073 !pointer->focus ||
2074 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002075 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002076
Derek Foreman1281a362015-07-31 16:55:32 -05002077 shsurf = get_shell_surface(pointer->focus->surface);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002078 if (shsurf &&
2079 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002080 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002081 }
2082
2083 return 1;
2084}
2085
2086static void
2087handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2088{
2089 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002090 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002091 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002092 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002093
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002094 if (sc->unresponsive) {
2095 xdg_ping_timeout_handler(sc);
2096 return;
2097 }
2098
2099 sc->ping_serial = serial;
2100 loop = wl_display_get_event_loop(compositor->wl_display);
2101 if (sc->ping_timer == NULL)
2102 sc->ping_timer =
2103 wl_event_loop_add_timer(loop,
2104 xdg_ping_timeout_handler, sc);
2105 if (sc->ping_timer == NULL)
2106 return;
2107
2108 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2109
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002110 if (shell_surface_is_xdg_surface(shsurf) ||
2111 shell_surface_is_xdg_popup(shsurf))
2112 xdg_shell_send_ping(sc->resource, serial);
2113 else if (shell_surface_is_wl_shell_surface(shsurf))
2114 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002115}
2116
Scott Moreauff1db4a2012-04-17 19:06:18 -06002117static void
2118ping_handler(struct weston_surface *surface, uint32_t serial)
2119{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002120 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002121
2122 if (!shsurf)
2123 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002124 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002125 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002126 if (shsurf->surface == shsurf->shell->grab_surface)
2127 return;
2128
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002129 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002130}
2131
2132static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002133handle_pointer_focus(struct wl_listener *listener, void *data)
2134{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002135 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002137 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002138 uint32_t serial;
2139
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002141 return;
2142
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002144 serial = wl_display_next_serial(compositor->wl_display);
2145 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002146}
2147
2148static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002149shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2150{
2151 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002152 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002153}
2154
2155static void
2156shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2157{
2158 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002159 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002160}
2161
2162static void
2163handle_keyboard_focus(struct wl_listener *listener, void *data)
2164{
2165 struct weston_keyboard *keyboard = data;
2166 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2167
2168 if (seat->focused_surface) {
2169 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2170 if (shsurf)
2171 shell_surface_lose_keyboard_focus(shsurf);
2172 }
2173
2174 seat->focused_surface = keyboard->focus;
2175
2176 if (seat->focused_surface) {
2177 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2178 if (shsurf)
2179 shell_surface_gain_keyboard_focus(shsurf);
2180 }
2181}
2182
2183static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002184shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002185{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002186 if (sc->ping_serial != serial)
2187 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002188
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002189 sc->unresponsive = 0;
2190 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002191
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002192 if (sc->ping_timer) {
2193 wl_event_source_remove(sc->ping_timer);
2194 sc->ping_timer = NULL;
2195 }
2196
2197}
2198
2199static void
2200shell_surface_pong(struct wl_client *client,
2201 struct wl_resource *resource, uint32_t serial)
2202{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002203 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2204 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002205
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002206 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002207}
2208
2209static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002210set_title(struct shell_surface *shsurf, const char *title)
2211{
2212 free(shsurf->title);
2213 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002214 shsurf->surface->timeline.force_refresh = 1;
2215}
2216
2217static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002218set_pid(struct shell_surface *shsurf, pid_t pid)
2219{
2220 /* We have no use for it */
2221}
2222
2223static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002224set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2225{
2226 shsurf->type = t;
2227 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002228}
2229
2230static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002231set_window_geometry(struct shell_surface *shsurf,
2232 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002233{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002234 shsurf->next_geometry.x = x;
2235 shsurf->next_geometry.y = y;
2236 shsurf->next_geometry.width = width;
2237 shsurf->next_geometry.height = height;
2238 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002239}
2240
2241static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002242shell_surface_set_title(struct wl_client *client,
2243 struct wl_resource *resource, const char *title)
2244{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002245 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002246
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002247 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002248}
2249
2250static void
2251shell_surface_set_class(struct wl_client *client,
2252 struct wl_resource *resource, const char *class)
2253{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002254 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002255
2256 free(shsurf->class);
2257 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002258 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002259}
2260
Alex Wu4539b082012-03-01 12:57:46 +08002261static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002262restore_output_mode(struct weston_output *output)
2263{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002264 if (output->original_mode)
2265 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002266}
2267
2268static void
2269restore_all_output_modes(struct weston_compositor *compositor)
2270{
2271 struct weston_output *output;
2272
2273 wl_list_for_each(output, &compositor->output_list, link)
2274 restore_output_mode(output);
2275}
2276
Philip Withnall07926d92013-11-25 18:01:40 +00002277/* The surface will be inserted into the list immediately after the link
2278 * returned by this function (i.e. will be stacked immediately above the
2279 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002280static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002281shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2282{
2283 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002284 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002285
2286 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002287 case SHELL_SURFACE_XWAYLAND:
2288 return &shsurf->shell->fullscreen_layer.view_list;
2289
2290 case SHELL_SURFACE_NONE:
2291 return NULL;
2292
Kristian Høgsbergead52422014-01-01 13:51:52 -08002293 case SHELL_SURFACE_POPUP:
2294 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002295 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002296 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002297 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002298 /* Move the surface to its parent layer so
2299 * that surfaces which are transient for
2300 * fullscreen surfaces don't get hidden by the
2301 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002302
2303 /* TODO: Handle a parent with multiple views */
2304 parent = get_default_view(shsurf->parent);
2305 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002306 return container_of(parent->layer_link.link.prev,
2307 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002308 }
Philip Withnall07926d92013-11-25 18:01:40 +00002309
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002310 /* Move the surface to a normal workspace layer so that surfaces
2311 * which were previously fullscreen or transient are no longer
2312 * rendered on top. */
2313 ws = get_current_workspace(shsurf->shell);
2314 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002315 }
2316
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002317 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002318}
2319
Philip Withnall648a4dd2013-11-25 18:01:44 +00002320static void
2321shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2322{
2323 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002324 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002325
2326 /* Move the child layers to the same workspace as shsurf. They will be
2327 * stacked above shsurf. */
2328 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002329 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002330 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002331 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002332 prev = container_of(shsurf->view->layer_link.link.prev,
2333 struct weston_layer_entry, link);
2334 weston_layer_entry_remove(&child->view->layer_link);
2335 weston_layer_entry_insert(prev,
2336 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002337 weston_view_geometry_dirty(child->view);
2338 weston_surface_damage(child->surface);
2339
2340 /* Recurse. We don’t expect this to recurse very far (if
2341 * at all) because that would imply we have transient
2342 * (or popup) children of transient surfaces, which
2343 * would be unusual. */
2344 shell_surface_update_child_surface_layers(child);
2345 }
2346 }
2347}
2348
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002349/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002350 * geometry to ensure the changes are redrawn.
2351 *
2352 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2353 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002354static void
2355shell_surface_update_layer(struct shell_surface *shsurf)
2356{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002357 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002358
2359 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2360
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002361 if (new_layer_link == NULL)
2362 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002363 if (new_layer_link == &shsurf->view->layer_link)
2364 return;
2365
2366 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002367 weston_layer_entry_remove(&shsurf->view->layer_link);
2368 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002369 weston_view_geometry_dirty(shsurf->view);
2370 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002371
2372 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002373}
2374
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002375static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002376shell_surface_set_parent(struct shell_surface *shsurf,
2377 struct weston_surface *parent)
2378{
2379 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002380
2381 wl_list_remove(&shsurf->children_link);
2382 wl_list_init(&shsurf->children_link);
2383
2384 /* Insert into the parent surface’s child list. */
2385 if (parent != NULL) {
2386 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2387 if (parent_shsurf != NULL)
2388 wl_list_insert(&parent_shsurf->children_list,
2389 &shsurf->children_link);
2390 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002391}
2392
2393static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002394shell_surface_set_output(struct shell_surface *shsurf,
2395 struct weston_output *output)
2396{
2397 struct weston_surface *es = shsurf->surface;
2398
2399 /* get the default output, if the client set it as NULL
2400 check whether the ouput is available */
2401 if (output)
2402 shsurf->output = output;
2403 else if (es->output)
2404 shsurf->output = es->output;
2405 else
2406 shsurf->output = get_default_output(es->compositor);
2407}
2408
2409static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002410surface_clear_next_states(struct shell_surface *shsurf)
2411{
2412 shsurf->next_state.maximized = false;
2413 shsurf->next_state.fullscreen = false;
2414
2415 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2416 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2417 shsurf->state_changed = true;
2418}
2419
2420static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002421set_toplevel(struct shell_surface *shsurf)
2422{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002423 shell_surface_set_parent(shsurf, NULL);
2424 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002425 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002426
2427 /* The layer_link is updated in set_surface_type(),
2428 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002429}
2430
2431static void
2432shell_surface_set_toplevel(struct wl_client *client,
2433 struct wl_resource *resource)
2434{
2435 struct shell_surface *surface = wl_resource_get_user_data(resource);
2436
2437 set_toplevel(surface);
2438}
2439
2440static void
2441set_transient(struct shell_surface *shsurf,
2442 struct weston_surface *parent, int x, int y, uint32_t flags)
2443{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002444 assert(parent != NULL);
2445
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002446 shell_surface_set_parent(shsurf, parent);
2447
2448 surface_clear_next_states(shsurf);
2449
Philip Withnallbecb77e2013-11-25 18:01:30 +00002450 shsurf->transient.x = x;
2451 shsurf->transient.y = y;
2452 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002453
Rafael Antognollied207b42013-12-03 15:35:43 -02002454 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002455 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002456 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002457
2458 /* The layer_link is updated in set_surface_type(),
2459 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460}
2461
2462static void
2463shell_surface_set_transient(struct wl_client *client,
2464 struct wl_resource *resource,
2465 struct wl_resource *parent_resource,
2466 int x, int y, uint32_t flags)
2467{
2468 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2469 struct weston_surface *parent =
2470 wl_resource_get_user_data(parent_resource);
2471
2472 set_transient(shsurf, parent, x, y, flags);
2473}
2474
2475static void
2476set_fullscreen(struct shell_surface *shsurf,
2477 uint32_t method,
2478 uint32_t framerate,
2479 struct weston_output *output)
2480{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002481 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002482 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002483
2484 shsurf->fullscreen_output = shsurf->output;
2485 shsurf->fullscreen.type = method;
2486 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002487
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002488 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002489}
2490
2491static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002492weston_view_set_initial_position(struct weston_view *view,
2493 struct desktop_shell *shell);
2494
2495static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002496unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002497{
Philip Withnallf85fe842013-11-25 18:01:37 +00002498 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002499 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2500 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002501 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002502 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002503
Alex Wu4539b082012-03-01 12:57:46 +08002504 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2505 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002506
Alex Wu4539b082012-03-01 12:57:46 +08002507 wl_list_remove(&shsurf->fullscreen.transform.link);
2508 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002509
Jason Ekstranda7af7042013-10-12 22:38:11 -05002510 if (shsurf->fullscreen.black_view)
2511 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2512 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002513
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002514 if (shsurf->saved_position_valid)
2515 weston_view_set_position(shsurf->view,
2516 shsurf->saved_x, shsurf->saved_y);
2517 else
2518 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2519
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002520 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002521 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002522 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002523 shsurf->saved_rotation_valid = false;
2524 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002525
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002526 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002527}
2528
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002529static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002530shell_surface_set_fullscreen(struct wl_client *client,
2531 struct wl_resource *resource,
2532 uint32_t method,
2533 uint32_t framerate,
2534 struct wl_resource *output_resource)
2535{
2536 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2537 struct weston_output *output;
2538
2539 if (output_resource)
2540 output = wl_resource_get_user_data(output_resource);
2541 else
2542 output = NULL;
2543
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002544 shell_surface_set_parent(shsurf, NULL);
2545
Rafael Antognolli03b16592013-12-03 15:35:42 -02002546 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002547 shsurf->next_state.fullscreen = true;
2548 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002549 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002550}
2551
2552static void
2553set_popup(struct shell_surface *shsurf,
2554 struct weston_surface *parent,
2555 struct weston_seat *seat,
2556 uint32_t serial,
2557 int32_t x,
2558 int32_t y)
2559{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002560 assert(parent != NULL);
2561
Philip Withnallbecb77e2013-11-25 18:01:30 +00002562 shsurf->popup.shseat = get_shell_seat(seat);
2563 shsurf->popup.serial = serial;
2564 shsurf->popup.x = x;
2565 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002566
Pekka Paalanenb5026542014-11-12 15:09:24 +02002567 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002568}
2569
2570static void
2571shell_surface_set_popup(struct wl_client *client,
2572 struct wl_resource *resource,
2573 struct wl_resource *seat_resource,
2574 uint32_t serial,
2575 struct wl_resource *parent_resource,
2576 int32_t x, int32_t y, uint32_t flags)
2577{
2578 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002579 struct weston_surface *parent =
2580 wl_resource_get_user_data(parent_resource);
2581
2582 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002583
Rafael Antognolli03b16592013-12-03 15:35:42 -02002584 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002585 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002586 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002587 wl_resource_get_user_data(seat_resource),
2588 serial, x, y);
2589}
2590
2591static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002593{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002594 /* undo all maximized things here */
2595 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002596
2597 if (shsurf->saved_position_valid)
2598 weston_view_set_position(shsurf->view,
2599 shsurf->saved_x, shsurf->saved_y);
2600 else
2601 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002602
2603 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002604 wl_list_insert(&shsurf->view->geometry.transformation_list,
2605 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002606 shsurf->saved_rotation_valid = false;
2607 }
2608
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002609 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002610}
2611
Philip Withnallbecb77e2013-11-25 18:01:30 +00002612static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002613set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002614{
2615 struct shell_surface *shsurf;
2616 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002617 struct weston_view *view;
2618
2619 view = get_default_view(surface);
2620 if (!view)
2621 return;
2622
2623 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2624
2625 shsurf = get_shell_surface(surface);
2626 current_ws = get_current_workspace(shsurf->shell);
2627
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002628 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002629 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002630
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002631 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002632 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002633
2634 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002635 weston_view_damage_below(view);
2636}
2637
2638static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002639shell_surface_set_maximized(struct wl_client *client,
2640 struct wl_resource *resource,
2641 struct wl_resource *output_resource)
2642{
2643 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2644 struct weston_output *output;
2645
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002646 surface_clear_next_states(shsurf);
2647 shsurf->next_state.maximized = true;
2648 shsurf->state_changed = true;
2649
Pekka Paalanenb5026542014-11-12 15:09:24 +02002650 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002651 shell_surface_set_parent(shsurf, NULL);
2652
Philip Withnallbecb77e2013-11-25 18:01:30 +00002653 if (output_resource)
2654 output = wl_resource_get_user_data(output_resource);
2655 else
2656 output = NULL;
2657
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002658 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002659
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002660 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002661}
2662
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002663/* This is only ever called from set_surface_type(), so there’s no need to
2664 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002665static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002666reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002667{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002668 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002669 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002670 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002671 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002672
Pekka Paalanen98262232011-12-01 10:42:22 +02002673 return 0;
2674}
2675
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002676static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002677set_full_output(struct shell_surface *shsurf)
2678{
2679 shsurf->saved_x = shsurf->view->geometry.x;
2680 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002681 shsurf->saved_width = shsurf->surface->width;
2682 shsurf->saved_height = shsurf->surface->height;
2683 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002684 shsurf->saved_position_valid = true;
2685
2686 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2687 wl_list_remove(&shsurf->rotation.transform.link);
2688 wl_list_init(&shsurf->rotation.transform.link);
2689 weston_view_geometry_dirty(shsurf->view);
2690 shsurf->saved_rotation_valid = true;
2691 }
2692}
2693
2694static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002695set_surface_type(struct shell_surface *shsurf)
2696{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002697 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002698 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002699
Philip Withnallbecb77e2013-11-25 18:01:30 +00002700 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002701
Rafael Antognolli03b16592013-12-03 15:35:42 -02002702 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002703 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002704
2705 switch (shsurf->type) {
2706 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002707 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002708 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002709 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 weston_view_set_position(shsurf->view,
2711 pev->geometry.x + shsurf->transient.x,
2712 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002713 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002714 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002715
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002716 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002717 weston_view_set_position(shsurf->view, shsurf->transient.x,
2718 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002719 break;
2720
Philip Withnall0f640e22013-11-25 18:01:31 +00002721 case SHELL_SURFACE_POPUP:
2722 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002723 default:
2724 break;
2725 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002726
2727 /* Update the surface’s layer. */
2728 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002729}
2730
Tiago Vignattibe143262012-04-16 17:31:41 +03002731static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002732shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002733{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002734 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002735}
2736
Pekka Paalanen8274d902014-08-06 19:36:51 +03002737static int
2738black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2739{
2740 struct weston_surface *fs_surface = surface->configure_private;
2741 int n;
2742 int rem;
2743 int ret;
2744
2745 n = snprintf(buf, len, "black background surface for ");
2746 if (n < 0)
2747 return n;
2748
2749 rem = (int)len - n;
2750 if (rem < 0)
2751 rem = 0;
2752
2753 if (fs_surface->get_label)
2754 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2755 else
2756 ret = snprintf(buf + n, rem, "<unknown>");
2757
2758 if (ret < 0)
2759 return n;
2760
2761 return n + ret;
2762}
2763
Alex Wu21858432012-04-01 20:13:08 +08002764static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002765black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002766
Jason Ekstranda7af7042013-10-12 22:38:11 -05002767static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002768create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002769 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002770 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002771{
2772 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002773 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002774
2775 surface = weston_surface_create(ec);
2776 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002777 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002778 return NULL;
2779 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002780 view = weston_view_create(surface);
2781 if (surface == NULL) {
2782 weston_log("no memory\n");
2783 weston_surface_destroy(surface);
2784 return NULL;
2785 }
Alex Wu4539b082012-03-01 12:57:46 +08002786
Alex Wu21858432012-04-01 20:13:08 +08002787 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002788 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002789 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002790 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002791 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002792 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002793 pixman_region32_fini(&surface->input);
2794 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002795
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002796 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002797 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798
2799 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002800}
2801
Philip Withnalled8f1a92013-11-25 18:01:43 +00002802static void
2803shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2804{
2805 struct weston_output *output = shsurf->fullscreen_output;
2806
Rafael Antognolli03b16592013-12-03 15:35:42 -02002807 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002808
2809 if (!shsurf->fullscreen.black_view)
2810 shsurf->fullscreen.black_view =
2811 create_black_surface(shsurf->surface->compositor,
2812 shsurf->surface,
2813 output->x, output->y,
2814 output->width,
2815 output->height);
2816
2817 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002818 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2819 weston_layer_entry_insert(&shsurf->view->layer_link,
2820 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002821 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2822 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002823
2824 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002825}
2826
Alex Wu4539b082012-03-01 12:57:46 +08002827/* Create black surface and append it to the associated fullscreen surface.
2828 * Handle size dismatch and positioning according to the method. */
2829static void
2830shell_configure_fullscreen(struct shell_surface *shsurf)
2831{
2832 struct weston_output *output = shsurf->fullscreen_output;
2833 struct weston_surface *surface = shsurf->surface;
2834 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002835 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002836 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002837
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002838 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2839 restore_output_mode(output);
2840
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002841 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002842 weston_layer_entry_remove(&shsurf->view->layer_link);
2843 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002844
Philip Withnalled8f1a92013-11-25 18:01:43 +00002845 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002846
Jason Ekstranda7af7042013-10-12 22:38:11 -05002847 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002848 &surf_width, &surf_height);
2849
Alex Wu4539b082012-03-01 12:57:46 +08002850 switch (shsurf->fullscreen.type) {
2851 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002852 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002854 break;
2855 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002856 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002857 if (output->width == surf_width &&
2858 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002859 weston_view_set_position(shsurf->view,
2860 output->x - surf_x,
2861 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002862 break;
2863 }
2864
Alex Wu4539b082012-03-01 12:57:46 +08002865 matrix = &shsurf->fullscreen.transform.matrix;
2866 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002867
Scott Moreau1bad5db2012-08-18 01:04:05 -06002868 output_aspect = (float) output->width /
2869 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002870 /* XXX: Use surf_width and surf_height here? */
2871 surface_aspect = (float) surface->width /
2872 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002873 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002874 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002875 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002876 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002877 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002878 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002879
Alex Wu4539b082012-03-01 12:57:46 +08002880 weston_matrix_scale(matrix, scale, scale, 1);
2881 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002883 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002884 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2885 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002886 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002887
Alex Wu4539b082012-03-01 12:57:46 +08002888 break;
2889 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002890 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002891 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002892 surf_width * surface->buffer_viewport.buffer.scale,
2893 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002894 shsurf->fullscreen.framerate};
2895
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002896 if (weston_output_mode_switch_to_temporary(output, &mode,
2897 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002898 weston_view_set_position(shsurf->view,
2899 output->x - surf_x,
2900 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002901 shsurf->fullscreen.black_view->surface->width = output->width;
2902 shsurf->fullscreen.black_view->surface->height = output->height;
2903 weston_view_set_position(shsurf->fullscreen.black_view,
2904 output->x - surf_x,
2905 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002906 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002907 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002908 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002909 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002910 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002911 }
Alex Wubd3354b2012-04-17 17:20:49 +08002912 }
Alex Wu4539b082012-03-01 12:57:46 +08002913 break;
2914 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002915 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002916 break;
2917 default:
2918 break;
2919 }
2920}
2921
Alex Wu4539b082012-03-01 12:57:46 +08002922static void
2923shell_map_fullscreen(struct shell_surface *shsurf)
2924{
Alex Wubd3354b2012-04-17 17:20:49 +08002925 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002926}
2927
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002928static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002929set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2930{
2931 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002932 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002933 shsurf->transient.x = x;
2934 shsurf->transient.y = y;
2935 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002936
2937 shell_surface_set_parent(shsurf, NULL);
2938
Pekka Paalanenb5026542014-11-12 15:09:24 +02002939 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002940 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002941}
2942
Kristian Høgsberg47792412014-05-04 13:47:06 -07002943static void
2944shell_interface_set_fullscreen(struct shell_surface *shsurf,
2945 uint32_t method,
2946 uint32_t framerate,
2947 struct weston_output *output)
2948{
2949 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002950 shsurf->next_state.fullscreen = true;
2951 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002952
2953 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002954}
2955
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002956static struct weston_output *
2957get_focused_output(struct weston_compositor *compositor)
2958{
2959 struct weston_seat *seat;
2960 struct weston_output *output = NULL;
2961
2962 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002963 struct weston_touch *touch = weston_seat_get_touch(seat);
2964 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2965 struct weston_keyboard *keyboard =
2966 weston_seat_get_keyboard(seat);
2967
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002968 /* Priority has touch focus, then pointer and
2969 * then keyboard focus. We should probably have
2970 * three for loops and check frist for touch,
2971 * then for pointer, etc. but unless somebody has some
2972 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002973 if (touch && touch->focus)
2974 output = touch->focus->output;
2975 else if (pointer && pointer->focus)
2976 output = pointer->focus->output;
2977 else if (keyboard && keyboard->focus)
2978 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002979
2980 if (output)
2981 break;
2982 }
2983
2984 return output;
2985}
2986
2987static void
2988shell_interface_set_maximized(struct shell_surface *shsurf)
2989{
2990 struct weston_output *output;
2991
2992 surface_clear_next_states(shsurf);
2993 shsurf->next_state.maximized = true;
2994 shsurf->state_changed = true;
2995 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2996
2997 if (!weston_surface_is_mapped(shsurf->surface))
2998 output = get_focused_output(shsurf->surface->compositor);
2999 else
3000 output = shsurf->surface->output;
3001
3002 shell_surface_set_output(shsurf, output);
3003 send_configure_for_surface(shsurf);
3004}
3005
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003006static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003007shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003008{
Derek Foremane4d6c832015-08-05 14:48:11 -05003009 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003010}
3011
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003012static int
3013shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003014 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003015 uint32_t edges)
3016{
Derek Foremane4d6c832015-08-05 14:48:11 -05003017 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003018}
3019
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003020static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003021
3022static void
3023destroy_shell_seat(struct wl_listener *listener, void *data)
3024{
3025 struct shell_seat *shseat =
3026 container_of(listener,
3027 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003028 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003029
3030 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003031 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003032 shseat->popup_grab.client = NULL;
3033
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003034 wl_list_for_each_safe(shsurf, next,
3035 &shseat->popup_grab.surfaces_list,
3036 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003037 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003038 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003039 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003040 }
3041
3042 wl_list_remove(&shseat->seat_destroy_listener.link);
3043 free(shseat);
3044}
3045
Jason Ekstrand024177c2014-04-21 19:42:58 -05003046static void
3047shell_seat_caps_changed(struct wl_listener *listener, void *data)
3048{
Derek Foreman1281a362015-07-31 16:55:32 -05003049 struct weston_keyboard *keyboard;
3050 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003051 struct shell_seat *seat;
3052
3053 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003054 keyboard = weston_seat_get_keyboard(seat->seat);
3055 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003056
Derek Foreman1281a362015-07-31 16:55:32 -05003057 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003058 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003059 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003060 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003061 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003062 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003063 wl_list_init(&seat->keyboard_focus_listener.link);
3064 }
3065
Derek Foreman1281a362015-07-31 16:55:32 -05003066 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003067 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003068 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003069 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003070 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003071 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003072 wl_list_init(&seat->pointer_focus_listener.link);
3073 }
3074}
3075
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076static struct shell_seat *
3077create_shell_seat(struct weston_seat *seat)
3078{
3079 struct shell_seat *shseat;
3080
3081 shseat = calloc(1, sizeof *shseat);
3082 if (!shseat) {
3083 weston_log("no memory to allocate shell seat\n");
3084 return NULL;
3085 }
3086
3087 shseat->seat = seat;
3088 wl_list_init(&shseat->popup_grab.surfaces_list);
3089
3090 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3091 wl_signal_add(&seat->destroy_signal,
3092 &shseat->seat_destroy_listener);
3093
Jason Ekstrand024177c2014-04-21 19:42:58 -05003094 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3095 wl_list_init(&shseat->keyboard_focus_listener.link);
3096
3097 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3098 wl_list_init(&shseat->pointer_focus_listener.link);
3099
3100 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3101 wl_signal_add(&seat->updated_caps_signal,
3102 &shseat->caps_changed_listener);
3103 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3104
Giulio Camuffo5085a752013-03-25 21:42:45 +01003105 return shseat;
3106}
3107
3108static struct shell_seat *
3109get_shell_seat(struct weston_seat *seat)
3110{
3111 struct wl_listener *listener;
3112
3113 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003114 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003115
3116 return container_of(listener,
3117 struct shell_seat, seat_destroy_listener);
3118}
3119
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003120static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003121popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003122{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003123 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003124 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003125 struct shell_seat *shseat =
3126 container_of(grab, struct shell_seat, popup_grab.grab);
3127 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003128 wl_fixed_t sx, sy;
3129
Jason Ekstranda7af7042013-10-12 22:38:11 -05003130 view = weston_compositor_pick_view(pointer->seat->compositor,
3131 pointer->x, pointer->y,
3132 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003133
Jason Ekstranda7af7042013-10-12 22:38:11 -05003134 if (view && view->surface->resource &&
3135 wl_resource_get_client(view->surface->resource) == client) {
3136 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003137 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003138 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003139 }
3140}
3141
3142static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003143popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003144 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003145{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003146 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003147 struct wl_resource *resource;
Jonas Ådahld2510102014-10-05 21:39:14 +02003148 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003149 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003150
Jonas Ådahl61917c82014-08-11 22:44:02 +02003151 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003152 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003153 weston_view_from_global_fixed(pointer->focus, x, y,
3154 &pointer->sx, &pointer->sy);
3155 }
3156
Jonas Ådahld2510102014-10-05 21:39:14 +02003157 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003158
Neil Roberts96d790e2013-09-19 17:32:00 +01003159 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 weston_view_from_global_fixed(pointer->focus,
3161 pointer->x, pointer->y,
3162 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003163 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003164 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003165}
3166
3167static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003168popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003169 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003170{
3171 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003172 struct shell_seat *shseat =
3173 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003174 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003175 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003176 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003177 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003178
Neil Roberts96d790e2013-09-19 17:32:00 +01003179 resource_list = &grab->pointer->focus_resource_list;
3180 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003181 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003182 wl_resource_for_each(resource, resource_list) {
3183 wl_pointer_send_button(resource, serial,
3184 time, button, state);
3185 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003186 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003187 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003188 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003189 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003190 }
3191
Daniel Stone4dbadb12012-05-30 16:31:51 +01003192 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003193 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003194}
3195
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003196static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003197popup_grab_axis(struct weston_pointer_grab *grab,
3198 uint32_t time, uint32_t axis, wl_fixed_t value)
3199{
Peter Hutterer11189522015-11-16 12:35:57 +10003200 weston_pointer_send_axis(grab->pointer, time, axis, value);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003201}
3202
3203static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003204popup_grab_cancel(struct weston_pointer_grab *grab)
3205{
3206 popup_grab_end(grab->pointer);
3207}
3208
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003209static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003210 popup_grab_focus,
3211 popup_grab_motion,
3212 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003213 popup_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003214 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003215};
3216
3217static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003218touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003219 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003220{
3221 struct wl_resource *resource;
3222 struct shell_seat *shseat =
3223 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3224 struct wl_display *display = shseat->seat->compositor->wl_display;
3225 uint32_t serial;
3226 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003227 wl_fixed_t sx, sy;
3228
3229 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003230
3231 resource_list = &grab->touch->focus_resource_list;
3232 if (!wl_list_empty(resource_list)) {
3233 serial = wl_display_get_serial(display);
3234 wl_resource_for_each(resource, resource_list) {
3235 wl_touch_send_down(resource, serial, time,
3236 grab->touch->focus->surface->resource,
3237 touch_id, sx, sy);
3238 }
3239 }
3240}
3241
3242static void
3243touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3244{
3245 struct wl_resource *resource;
3246 struct shell_seat *shseat =
3247 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3248 struct wl_display *display = shseat->seat->compositor->wl_display;
3249 uint32_t serial;
3250 struct wl_list *resource_list;
3251
3252 resource_list = &grab->touch->focus_resource_list;
3253 if (!wl_list_empty(resource_list)) {
3254 serial = wl_display_get_serial(display);
3255 wl_resource_for_each(resource, resource_list) {
3256 wl_touch_send_up(resource, serial, time, touch_id);
3257 }
3258 }
3259}
3260
3261static void
3262touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003263 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003264{
3265 struct wl_resource *resource;
3266 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003267 wl_fixed_t sx, sy;
3268
3269 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003270
3271 resource_list = &grab->touch->focus_resource_list;
3272 if (!wl_list_empty(resource_list)) {
3273 wl_resource_for_each(resource, resource_list) {
3274 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3275 }
3276 }
3277}
3278
3279static void
3280touch_popup_grab_frame(struct weston_touch_grab *grab)
3281{
3282}
3283
3284static void
3285touch_popup_grab_cancel(struct weston_touch_grab *grab)
3286{
3287 touch_popup_grab_end(grab->touch);
3288}
3289
3290static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3291 touch_popup_grab_down,
3292 touch_popup_grab_up,
3293 touch_popup_grab_motion,
3294 touch_popup_grab_frame,
3295 touch_popup_grab_cancel,
3296};
3297
3298static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003299shell_surface_send_popup_done(struct shell_surface *shsurf)
3300{
3301 if (shell_surface_is_wl_shell_surface(shsurf))
3302 wl_shell_surface_send_popup_done(shsurf->resource);
3303 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003304 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003305}
3306
3307static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003308popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003309{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003310 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003311 struct shell_seat *shseat =
3312 container_of(grab, struct shell_seat, popup_grab.grab);
3313 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003314 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003315
3316 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003317 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003318 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003319 shseat->popup_grab.grab.interface = NULL;
3320 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003321 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003322 wl_list_for_each_safe(shsurf, next,
3323 &shseat->popup_grab.surfaces_list,
3324 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003325 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003326 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003327 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003328 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003329 wl_list_init(&shseat->popup_grab.surfaces_list);
3330 }
3331}
3332
3333static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003334touch_popup_grab_end(struct weston_touch *touch)
3335{
3336 struct weston_touch_grab *grab = touch->grab;
3337 struct shell_seat *shseat =
3338 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3339 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003340 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003341
3342 if (touch->grab->interface == &touch_popup_grab_interface) {
3343 weston_touch_end_grab(grab->touch);
3344 shseat->popup_grab.client = NULL;
3345 shseat->popup_grab.touch_grab.interface = NULL;
3346 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3347 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003348 wl_list_for_each_safe(shsurf, next,
3349 &shseat->popup_grab.surfaces_list,
3350 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003351 shell_surface_send_popup_done(shsurf);
3352 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003353 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003354 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003355 wl_list_init(&shseat->popup_grab.surfaces_list);
3356 }
3357}
3358
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003359static struct shell_surface *
3360get_top_popup(struct shell_seat *shseat)
3361{
3362 struct shell_surface *shsurf;
3363
3364 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3365 return NULL;
3366 } else {
3367 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3368 struct shell_surface,
3369 popup.grab_link);
3370 return shsurf;
3371 }
3372}
3373
3374static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003375add_popup_grab(struct shell_surface *shsurf,
3376 struct shell_seat *shseat,
3377 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003378{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003379 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003380 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003381 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3382 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003383
3384 parent = get_shell_surface(shsurf->parent);
3385 top_surface = get_top_popup(shseat);
3386 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003387 (!parent ||
3388 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003389 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003390 wl_resource_post_error(shsurf->owner_resource,
3391 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003392 "xdg_popup was not created on the "
3393 "topmost popup");
3394 return -1;
3395 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003396
3397 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003398 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003399 shseat->popup_grab.client =
3400 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003401
3402 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003403 shseat->popup_grab.grab.interface =
3404 &popup_grab_interface;
3405
3406 /* We must make sure here that this popup was opened
3407 * after a mouse press, and not just by moving around
3408 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003409 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003410 shseat->popup_grab.initial_up = 0;
3411 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003412 shseat->popup_grab.touch_grab.interface =
3413 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003414 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003415
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003416 wl_list_insert(&shseat->popup_grab.surfaces_list,
3417 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003418
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003419 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003420 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003421 &shseat->popup_grab.grab);
3422 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003423 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003424 &shseat->popup_grab.touch_grab);
3425 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003426 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003427 wl_list_insert(&shseat->popup_grab.surfaces_list,
3428 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003429 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003430
3431 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003432}
3433
3434static void
3435remove_popup_grab(struct shell_surface *shsurf)
3436{
3437 struct shell_seat *shseat = shsurf->popup.shseat;
3438
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003439 if (shell_surface_is_xdg_popup(shsurf) &&
3440 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003441 wl_resource_post_error(shsurf->owner_resource,
3442 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003443 "xdg_popup was destroyed while it was "
3444 "not the topmost popup.");
3445 return;
3446 }
3447
Giulio Camuffo5085a752013-03-25 21:42:45 +01003448 wl_list_remove(&shsurf->popup.grab_link);
3449 wl_list_init(&shsurf->popup.grab_link);
3450 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003451 if (shseat->popup_grab.type == POINTER) {
3452 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3453 shseat->popup_grab.grab.interface = NULL;
3454 } else if (shseat->popup_grab.type == TOUCH) {
3455 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3456 shseat->popup_grab.touch_grab.interface = NULL;
3457 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003458 }
3459}
3460
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003461static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003462shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003463{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003464 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003465 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003466 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3467 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003468
Jason Ekstranda7af7042013-10-12 22:38:11 -05003469 shsurf->surface->output = parent_view->output;
3470 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003471
Jason Ekstranda7af7042013-10-12 22:38:11 -05003472 weston_view_set_transform_parent(shsurf->view, parent_view);
3473 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3474 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003475
Derek Foreman1281a362015-07-31 16:55:32 -05003476 if (pointer &&
3477 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003478 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3479 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003480 } else if (touch &&
3481 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003482 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3483 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003484 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003485 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003486 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003487 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003488
3489 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003490}
3491
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003492static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003493 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003494 shell_surface_move,
3495 shell_surface_resize,
3496 shell_surface_set_toplevel,
3497 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003498 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003499 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003500 shell_surface_set_maximized,
3501 shell_surface_set_title,
3502 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003503};
3504
3505static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003506destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003507{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003508 struct shell_surface *child, *next;
3509
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003510 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3511
Giulio Camuffo5085a752013-03-25 21:42:45 +01003512 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3513 remove_popup_grab(shsurf);
3514 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003515
Alex Wubd3354b2012-04-17 17:20:49 +08003516 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003517 shell_surface_is_top_fullscreen(shsurf))
3518 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003519
Jason Ekstranda7af7042013-10-12 22:38:11 -05003520 if (shsurf->fullscreen.black_view)
3521 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003522
Alex Wubd3354b2012-04-17 17:20:49 +08003523 /* As destroy_resource() use wl_list_for_each_safe(),
3524 * we can always remove the listener.
3525 */
3526 wl_list_remove(&shsurf->surface_destroy_listener.link);
3527 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003528 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003529 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003530
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 weston_view_destroy(shsurf->view);
3532
Philip Withnall648a4dd2013-11-25 18:01:44 +00003533 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003534 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3535 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003536
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003537 wl_list_remove(&shsurf->link);
3538 free(shsurf);
3539}
3540
3541static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003542shell_destroy_shell_surface(struct wl_resource *resource)
3543{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003544 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003545
Bryan Caina46b9462014-04-04 17:41:24 -05003546 if (!wl_list_empty(&shsurf->popup.grab_link))
3547 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003548 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003549 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003550}
3551
3552static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003553shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003554{
3555 struct shell_surface *shsurf = container_of(listener,
3556 struct shell_surface,
3557 surface_destroy_listener);
3558
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003559 if (shsurf->resource)
3560 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003561
3562 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003563}
3564
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003565static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003566fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003567{
3568 struct shell_surface *shsurf = data;
3569
3570 weston_surface_destroy(shsurf->surface);
3571}
3572
3573static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003574fade_out_done(struct weston_view_animation *animation, void *data)
3575{
3576 struct shell_surface *shsurf = data;
3577 struct wl_event_loop *loop;
3578
3579 loop = wl_display_get_event_loop(
3580 shsurf->surface->compositor->wl_display);
3581
3582 if (!shsurf->destroying) {
3583 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3584 shsurf->destroying = true;
3585 }
3586}
3587
3588static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003589handle_resource_destroy(struct wl_listener *listener, void *data)
3590{
3591 struct shell_surface *shsurf =
3592 container_of(listener, struct shell_surface,
3593 resource_destroy_listener);
3594
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003595 if (!weston_surface_is_mapped(shsurf->surface))
3596 return;
3597
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003598 shsurf->surface->ref_count++;
3599
3600 pixman_region32_fini(&shsurf->surface->pending.input);
3601 pixman_region32_init(&shsurf->surface->pending.input);
3602 pixman_region32_fini(&shsurf->surface->input);
3603 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003604 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3605 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3606 fade_out_done, shsurf);
3607 } else {
3608 weston_surface_destroy(shsurf->surface);
3609 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003610}
3611
3612static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003613shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003614
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003615struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003616get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003617{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003618 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003619 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003620 else
3621 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003622}
3623
Rafael Antognollie2a34552013-12-03 15:35:45 -02003624static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003625create_common_surface(struct shell_client *owner, void *shell,
3626 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003627 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003628{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003629 struct shell_surface *shsurf;
3630
Pekka Paalanen50b67472014-10-01 15:02:41 +03003631 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003632
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003633 shsurf = calloc(1, sizeof *shsurf);
3634 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003635 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003636 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003637 }
3638
Jason Ekstranda7af7042013-10-12 22:38:11 -05003639 shsurf->view = weston_view_create(surface);
3640 if (!shsurf->view) {
3641 weston_log("no memory to allocate shell surface\n");
3642 free(shsurf);
3643 return NULL;
3644 }
3645
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003646 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003647 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003648 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003649
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003650 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3651 wl_resource_add_destroy_listener(surface->resource,
3652 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003653 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003654
Tiago Vignattibc052c92012-04-19 16:18:18 +03003655 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003656 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003657 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003658 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003659 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003660 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003661 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3662 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003663 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003664 wl_list_init(&shsurf->fullscreen.transform.link);
3665
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003666 shsurf->output = get_default_output(shsurf->shell->compositor);
3667
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003668 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003669 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003670 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003671 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003672
3673 /* init link so its safe to always remove it in destroy_shell_surface */
3674 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003675 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003676
Pekka Paalanen460099f2012-01-20 16:48:25 +02003677 /* empty when not in use */
3678 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003679 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003680
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003681 wl_list_init(&shsurf->workspace_transform.link);
3682
Philip Withnall648a4dd2013-11-25 18:01:44 +00003683 wl_list_init(&shsurf->children_link);
3684 wl_list_init(&shsurf->children_list);
3685 shsurf->parent = NULL;
3686
Pekka Paalanenb5026542014-11-12 15:09:24 +02003687 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003688
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003689 shsurf->client = client;
3690
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003691 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003692}
3693
Rafael Antognollie2a34552013-12-03 15:35:45 -02003694static struct shell_surface *
3695create_shell_surface(void *shell, struct weston_surface *surface,
3696 const struct weston_shell_client *client)
3697{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003698 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003699}
3700
Jason Ekstranda7af7042013-10-12 22:38:11 -05003701static struct weston_view *
3702get_primary_view(void *shell, struct shell_surface *shsurf)
3703{
3704 return shsurf->view;
3705}
3706
Tiago Vignattibc052c92012-04-19 16:18:18 +03003707static void
3708shell_get_shell_surface(struct wl_client *client,
3709 struct wl_resource *resource,
3710 uint32_t id,
3711 struct wl_resource *surface_resource)
3712{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003713 struct weston_surface *surface =
3714 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003715 struct shell_client *sc = wl_resource_get_user_data(resource);
3716 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003717 struct shell_surface *shsurf;
3718
Pekka Paalanen50b67472014-10-01 15:02:41 +03003719 if (weston_surface_set_role(surface, "wl_shell_surface",
3720 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003721 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003722
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003723 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003724 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003725 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003726 return;
3727 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003728
Jason Ekstranda85118c2013-06-27 20:17:02 -05003729 shsurf->resource =
3730 wl_resource_create(client,
3731 &wl_shell_surface_interface, 1, id);
3732 wl_resource_set_implementation(shsurf->resource,
3733 &shell_surface_implementation,
3734 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003735 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003736}
3737
Rafael Antognollie2a34552013-12-03 15:35:45 -02003738static bool
3739shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3740{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003741 /* A shell surface without a resource is created from xwayland
3742 * and is considered a wl_shell surface for now. */
3743
3744 return shsurf->resource == NULL ||
3745 wl_resource_instance_of(shsurf->resource,
3746 &wl_shell_surface_interface,
3747 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003748}
3749
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003750static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003751 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003752};
3753
Rafael Antognollie2a34552013-12-03 15:35:45 -02003754/****************************
3755 * xdg-shell implementation */
3756
3757static void
3758xdg_surface_destroy(struct wl_client *client,
3759 struct wl_resource *resource)
3760{
3761 wl_resource_destroy(resource);
3762}
3763
3764static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003765xdg_surface_set_parent(struct wl_client *client,
3766 struct wl_resource *resource,
3767 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003768{
3769 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003770 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003771
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003772 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003773 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003774 shell_surface_set_parent(shsurf, parent->surface);
3775 } else {
3776 shell_surface_set_parent(shsurf, NULL);
3777 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003778}
3779
3780static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003781xdg_surface_set_app_id(struct wl_client *client,
3782 struct wl_resource *resource,
3783 const char *app_id)
3784{
3785 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3786
3787 free(shsurf->class);
3788 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003789 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003790}
3791
3792static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003793xdg_surface_show_window_menu(struct wl_client *client,
3794 struct wl_resource *surface_resource,
3795 struct wl_resource *seat_resource,
3796 uint32_t serial,
3797 int32_t x,
3798 int32_t y)
3799{
3800 /* TODO */
3801}
3802
3803static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003804xdg_surface_set_title(struct wl_client *client,
3805 struct wl_resource *resource, const char *title)
3806{
3807 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3808
3809 set_title(shsurf, title);
3810}
3811
3812static void
3813xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3814 struct wl_resource *seat_resource, uint32_t serial)
3815{
3816 common_surface_move(resource, seat_resource, serial);
3817}
3818
3819static void
3820xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3821 struct wl_resource *seat_resource, uint32_t serial,
3822 uint32_t edges)
3823{
3824 common_surface_resize(resource, seat_resource, serial, edges);
3825}
3826
3827static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003828xdg_surface_ack_configure(struct wl_client *client,
3829 struct wl_resource *resource,
3830 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003831{
3832 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3833
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003834 if (shsurf->state_requested) {
3835 shsurf->next_state = shsurf->requested_state;
3836 shsurf->state_changed = true;
3837 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003838 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003839}
3840
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003841static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003842xdg_surface_set_window_geometry(struct wl_client *client,
3843 struct wl_resource *resource,
3844 int32_t x,
3845 int32_t y,
3846 int32_t width,
3847 int32_t height)
3848{
3849 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3850
3851 set_window_geometry(shsurf, x, y, width, height);
3852}
3853
3854static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003855xdg_surface_set_maximized(struct wl_client *client,
3856 struct wl_resource *resource)
3857{
3858 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003859 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003860
3861 shsurf->state_requested = true;
3862 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003863
3864 if (!weston_surface_is_mapped(shsurf->surface))
3865 output = get_focused_output(shsurf->surface->compositor);
3866 else
3867 output = shsurf->surface->output;
3868
3869 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003870 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003871}
3872
3873static void
3874xdg_surface_unset_maximized(struct wl_client *client,
3875 struct wl_resource *resource)
3876{
3877 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3878
3879 shsurf->state_requested = true;
3880 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003881 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003882}
3883
3884static void
3885xdg_surface_set_fullscreen(struct wl_client *client,
3886 struct wl_resource *resource,
3887 struct wl_resource *output_resource)
3888{
3889 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3890 struct weston_output *output;
3891
3892 shsurf->state_requested = true;
3893 shsurf->requested_state.fullscreen = true;
3894
3895 if (output_resource)
3896 output = wl_resource_get_user_data(output_resource);
3897 else
3898 output = NULL;
3899
Marek Chalupa052917a2014-09-02 11:35:12 +02003900 /* handle clients launching in fullscreen */
3901 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3902 /* Set the output to the one that has focus currently. */
3903 assert(shsurf->surface);
3904 output = get_focused_output(shsurf->surface->compositor);
3905 }
3906
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003907 shell_surface_set_output(shsurf, output);
3908 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003909
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003910 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003911}
3912
3913static void
3914xdg_surface_unset_fullscreen(struct wl_client *client,
3915 struct wl_resource *resource)
3916{
3917 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3918
3919 shsurf->state_requested = true;
3920 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003921 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003922}
3923
3924static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003925xdg_surface_set_minimized(struct wl_client *client,
3926 struct wl_resource *resource)
3927{
3928 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3929
3930 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3931 return;
3932
3933 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003934 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003935}
3936
Rafael Antognollie2a34552013-12-03 15:35:45 -02003937static const struct xdg_surface_interface xdg_surface_implementation = {
3938 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003939 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003940 xdg_surface_set_title,
3941 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003942 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003943 xdg_surface_move,
3944 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003945 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003946 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003947 xdg_surface_set_maximized,
3948 xdg_surface_unset_maximized,
3949 xdg_surface_set_fullscreen,
3950 xdg_surface_unset_fullscreen,
3951 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003952};
3953
3954static void
3955xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003956 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003957{
3958 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003959 uint32_t *s;
3960 struct wl_array states;
3961 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003962
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003963 assert(shsurf);
3964
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003965 if (!shsurf->resource)
3966 return;
3967
3968 wl_array_init(&states);
3969 if (shsurf->requested_state.fullscreen) {
3970 s = wl_array_add(&states, sizeof *s);
3971 *s = XDG_SURFACE_STATE_FULLSCREEN;
3972 } else if (shsurf->requested_state.maximized) {
3973 s = wl_array_add(&states, sizeof *s);
3974 *s = XDG_SURFACE_STATE_MAXIMIZED;
3975 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003976 if (shsurf->resize_edges != 0) {
3977 s = wl_array_add(&states, sizeof *s);
3978 *s = XDG_SURFACE_STATE_RESIZING;
3979 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003980 if (shsurf->focus_count > 0) {
3981 s = wl_array_add(&states, sizeof *s);
3982 *s = XDG_SURFACE_STATE_ACTIVATED;
3983 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003984
3985 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3986 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3987
3988 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003989}
3990
3991static const struct weston_shell_client xdg_client = {
3992 xdg_send_configure
3993};
3994
3995static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08003996xdg_shell_destroy(struct wl_client *client,
3997 struct wl_resource *resource)
3998{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003999 struct shell_client *sc = wl_resource_get_user_data(resource);
4000 struct wl_resource *shsurf_resource;
4001 struct shell_surface *shsurf;
4002
4003 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4004 shsurf = wl_resource_get_user_data(shsurf_resource);
4005 if (shsurf->owner_resource == resource) {
4006 wl_resource_post_error(
4007 resource,
4008 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4009 "not all child surface objects destroyed");
4010 return;
4011 }
4012 }
4013
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004014 wl_resource_destroy(resource);
4015}
4016
4017static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004018xdg_use_unstable_version(struct wl_client *client,
4019 struct wl_resource *resource,
4020 int32_t version)
4021{
4022 if (version > 1) {
4023 wl_resource_post_error(resource,
4024 1,
4025 "xdg-shell:: version not implemented yet.");
4026 return;
4027 }
4028}
4029
4030static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004031create_xdg_surface(struct shell_client *owner, void *shell,
4032 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004033 const struct weston_shell_client *client)
4034{
4035 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004036
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004037 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004038 if (!shsurf)
4039 return NULL;
4040
Pekka Paalanenb5026542014-11-12 15:09:24 +02004041 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004042
4043 return shsurf;
4044}
4045
4046static void
4047xdg_get_xdg_surface(struct wl_client *client,
4048 struct wl_resource *resource,
4049 uint32_t id,
4050 struct wl_resource *surface_resource)
4051{
4052 struct weston_surface *surface =
4053 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004054 struct shell_client *sc = wl_resource_get_user_data(resource);
4055 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004056 struct shell_surface *shsurf;
4057
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004058 shsurf = get_shell_surface(surface);
4059 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4060 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4061 "This wl_surface is already an "
4062 "xdg_surface");
4063 return;
4064 }
4065
Pekka Paalanen50b67472014-10-01 15:02:41 +03004066 if (weston_surface_set_role(surface, "xdg_surface",
4067 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004068 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004069
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004070 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004071 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004072 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004073 return;
4074 }
4075
4076 shsurf->resource =
4077 wl_resource_create(client,
4078 &xdg_surface_interface, 1, id);
4079 wl_resource_set_implementation(shsurf->resource,
4080 &xdg_surface_implementation,
4081 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004082 shsurf->owner_resource = resource;
4083 wl_list_insert(&sc->surface_list,
4084 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004085}
4086
4087static bool
4088shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4089{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004090 return shsurf->resource &&
4091 wl_resource_instance_of(shsurf->resource,
4092 &xdg_surface_interface,
4093 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004094}
4095
4096/* xdg-popup implementation */
4097
4098static void
4099xdg_popup_destroy(struct wl_client *client,
4100 struct wl_resource *resource)
4101{
4102 wl_resource_destroy(resource);
4103}
4104
Rafael Antognollie2a34552013-12-03 15:35:45 -02004105static const struct xdg_popup_interface xdg_popup_implementation = {
4106 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004107};
4108
4109static void
4110xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004111 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004112{
4113}
4114
4115static const struct weston_shell_client xdg_popup_client = {
4116 xdg_popup_send_configure
4117};
4118
4119static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004120create_xdg_popup(struct shell_client *owner, void *shell,
4121 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004122 const struct weston_shell_client *client,
4123 struct weston_surface *parent,
4124 struct shell_seat *seat,
4125 uint32_t serial,
4126 int32_t x, int32_t y)
4127{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004128 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004129
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004130 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004131 if (!shsurf)
4132 return NULL;
4133
Pekka Paalanenb5026542014-11-12 15:09:24 +02004134 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004135 shsurf->popup.shseat = seat;
4136 shsurf->popup.serial = serial;
4137 shsurf->popup.x = x;
4138 shsurf->popup.y = y;
4139 shell_surface_set_parent(shsurf, parent);
4140
4141 return shsurf;
4142}
4143
4144static void
4145xdg_get_xdg_popup(struct wl_client *client,
4146 struct wl_resource *resource,
4147 uint32_t id,
4148 struct wl_resource *surface_resource,
4149 struct wl_resource *parent_resource,
4150 struct wl_resource *seat_resource,
4151 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004152 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004153{
4154 struct weston_surface *surface =
4155 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004156 struct shell_client *sc = wl_resource_get_user_data(resource);
4157 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004158 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004159 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004160 struct weston_surface *parent;
4161 struct shell_seat *seat;
4162
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004163 shsurf = get_shell_surface(surface);
4164 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4165 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4166 "This wl_surface is already an "
4167 "xdg_popup");
4168 return;
4169 }
4170
Pekka Paalanen50b67472014-10-01 15:02:41 +03004171 if (weston_surface_set_role(surface, "xdg_popup",
4172 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004173 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004174
4175 if (!parent_resource) {
4176 wl_resource_post_error(surface_resource,
4177 WL_DISPLAY_ERROR_INVALID_OBJECT,
4178 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004179 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004180 }
4181
4182 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004183 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004184
Jonas Ådahlee45a552015-03-18 16:37:47 +08004185 /* Verify that we are creating the top most popup when mapping,
4186 * as it's not until then we know whether it was mapped as most
4187 * top level or not. */
4188
4189 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004190 if (!parent_shsurf ||
4191 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4192 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004193 wl_resource_post_error(resource,
4194 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4195 "xdg_popup parent was invalid");
4196 return;
4197 }
4198
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004199 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004200 parent, seat, serial, x, y);
4201 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004202 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004203 return;
4204 }
4205
4206 shsurf->resource =
4207 wl_resource_create(client,
4208 &xdg_popup_interface, 1, id);
4209 wl_resource_set_implementation(shsurf->resource,
4210 &xdg_popup_implementation,
4211 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004212 shsurf->owner_resource = resource;
4213 wl_list_insert(&sc->surface_list,
4214 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004215}
4216
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004217static void
4218xdg_pong(struct wl_client *client,
4219 struct wl_resource *resource, uint32_t serial)
4220{
4221 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004222
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004223 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004224}
4225
Rafael Antognollie2a34552013-12-03 15:35:45 -02004226static bool
4227shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4228{
4229 return wl_resource_instance_of(shsurf->resource,
4230 &xdg_popup_interface,
4231 &xdg_popup_implementation);
4232}
4233
4234static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004235 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004236 xdg_use_unstable_version,
4237 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004238 xdg_get_xdg_popup,
4239 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004240};
4241
4242static int
4243xdg_shell_unversioned_dispatch(const void *implementation,
4244 void *_target, uint32_t opcode,
4245 const struct wl_message *message,
4246 union wl_argument *args)
4247{
4248 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004249 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004250
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004251 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004252 wl_resource_post_error(resource,
4253 WL_DISPLAY_ERROR_INVALID_OBJECT,
4254 "must call use_unstable_version first");
4255 return 0;
4256 }
4257
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004258#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004259
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004260 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4261 "shell implementation doesn't match protocol version");
4262
Rafael Antognollie2a34552013-12-03 15:35:45 -02004263 if (args[0].i != XDG_SERVER_VERSION) {
4264 wl_resource_post_error(resource,
4265 WL_DISPLAY_ERROR_INVALID_OBJECT,
4266 "incompatible version, server is %d "
4267 "client wants %d",
4268 XDG_SERVER_VERSION, args[0].i);
4269 return 0;
4270 }
4271
4272 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004273 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004274
4275 return 1;
4276}
4277
4278/* end of xdg-shell implementation */
4279/***********************************/
4280
Kristian Høgsberg07937562011-04-12 17:25:42 -04004281static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004282shell_fade(struct desktop_shell *shell, enum fade_type type);
4283
Pekka Paalanen77346a62011-11-30 16:26:35 +02004284static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004285configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004286{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004287 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004288
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004289 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004290 if (v->output == ev->output && v != ev) {
4291 weston_view_unmap(v);
4292 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004293 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004294 }
4295 }
4296
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004297 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004298
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004299 if (wl_list_empty(&ev->layer_link.link)) {
4300 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004301 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004302 }
4303}
4304
Pekka Paalanen8274d902014-08-06 19:36:51 +03004305static int
4306background_get_label(struct weston_surface *surface, char *buf, size_t len)
4307{
4308 return snprintf(buf, len, "background for output %s",
4309 surface->output->name);
4310}
4311
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004312static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004313background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004314{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004315 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004316 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004317
Jason Ekstranda7af7042013-10-12 22:38:11 -05004318 view = container_of(es->views.next, struct weston_view, surface_link);
4319
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004320 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004321}
4322
4323static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004324desktop_shell_set_background(struct wl_client *client,
4325 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004326 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004327 struct wl_resource *surface_resource)
4328{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004329 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004330 struct weston_surface *surface =
4331 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004332 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004333
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004334 if (surface->configure) {
4335 wl_resource_post_error(surface_resource,
4336 WL_DISPLAY_ERROR_INVALID_OBJECT,
4337 "surface role already assigned");
4338 return;
4339 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004340
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4342 weston_view_destroy(view);
4343 view = weston_view_create(surface);
4344
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004345 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004346 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004347 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004348 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004350 weston_desktop_shell_send_configure(resource, 0,
4351 surface_resource,
4352 surface->output->width,
4353 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004354}
4355
Pekka Paalanen8274d902014-08-06 19:36:51 +03004356static int
4357panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4358{
4359 return snprintf(buf, len, "panel for output %s",
4360 surface->output->name);
4361}
4362
Kristian Høgsberg75840622011-09-06 13:48:16 -04004363static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004364panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004365{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004366 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004367 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004368
Jason Ekstranda7af7042013-10-12 22:38:11 -05004369 view = container_of(es->views.next, struct weston_view, surface_link);
4370
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004371 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004372}
4373
4374static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004375desktop_shell_set_panel(struct wl_client *client,
4376 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004377 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004378 struct wl_resource *surface_resource)
4379{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004380 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004381 struct weston_surface *surface =
4382 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004383 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004384
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004385 if (surface->configure) {
4386 wl_resource_post_error(surface_resource,
4387 WL_DISPLAY_ERROR_INVALID_OBJECT,
4388 "surface role already assigned");
4389 return;
4390 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004391
Jason Ekstranda7af7042013-10-12 22:38:11 -05004392 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4393 weston_view_destroy(view);
4394 view = weston_view_create(surface);
4395
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004396 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004397 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004398 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004399 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004401 weston_desktop_shell_send_configure(resource, 0,
4402 surface_resource,
4403 surface->output->width,
4404 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004405}
4406
Pekka Paalanen8274d902014-08-06 19:36:51 +03004407static int
4408lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4409{
4410 return snprintf(buf, len, "lock window");
4411}
4412
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004413static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004414lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004415{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004416 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 struct weston_view *view;
4418
4419 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004420
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004421 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004422 return;
4423
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004425
4426 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004427 weston_layer_entry_insert(&shell->lock_layer.view_list,
4428 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004429 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004430 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004431 }
4432}
4433
4434static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004435handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004436{
Tiago Vignattibe143262012-04-16 17:31:41 +03004437 struct desktop_shell *shell =
4438 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004439
Martin Minarik6d118362012-06-07 18:01:59 +02004440 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004441 shell->lock_surface = NULL;
4442}
4443
4444static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004445desktop_shell_set_lock_surface(struct wl_client *client,
4446 struct wl_resource *resource,
4447 struct wl_resource *surface_resource)
4448{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004449 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004450 struct weston_surface *surface =
4451 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004452
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004453 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004454
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004455 if (!shell->locked)
4456 return;
4457
Pekka Paalanen98262232011-12-01 10:42:22 +02004458 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004459
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004460 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004461 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004462 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004463
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004464 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004465 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004466 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004467 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004468}
4469
4470static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004471resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004472{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004473 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004474
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004475 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004476 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004477 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004478 &shell->input_panel_layer.link);
4479 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004480 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004481 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004482 wl_list_insert(&shell->compositor->cursor_layer.link,
4483 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004484 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004485 wl_list_insert(&shell->fullscreen_layer.link,
4486 &shell->panel_layer.link);
4487 wl_list_insert(&shell->panel_layer.link,
4488 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004489
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004490 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004491
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004492 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004493 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004494 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004495}
4496
4497static void
4498desktop_shell_unlock(struct wl_client *client,
4499 struct wl_resource *resource)
4500{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004501 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004502
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004503 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004504
4505 if (shell->locked)
4506 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004507}
4508
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004509static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004510desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004511 struct wl_resource *resource,
4512 struct wl_resource *surface_resource)
4513{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004514 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004515
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004516 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004517 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004518}
4519
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004520static void
4521desktop_shell_desktop_ready(struct wl_client *client,
4522 struct wl_resource *resource)
4523{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004524 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004525
4526 shell_fade_startup(shell);
4527}
4528
Jonny Lamb765760d2014-08-20 15:53:19 +02004529static void
4530desktop_shell_set_panel_position(struct wl_client *client,
4531 struct wl_resource *resource,
4532 uint32_t position)
4533{
4534 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4535
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004536 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4537 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4538 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4539 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004540 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004541 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004542 "bad position argument");
4543 return;
4544 }
4545
4546 shell->panel_position = position;
4547}
4548
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004549static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004550 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004551 desktop_shell_set_panel,
4552 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004553 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004554 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004555 desktop_shell_desktop_ready,
4556 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004557};
4558
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004559static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004560get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004561{
4562 struct shell_surface *shsurf;
4563
4564 shsurf = get_shell_surface(surface);
4565 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004566 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004567 return shsurf->type;
4568}
4569
Kristian Høgsberg75840622011-09-06 13:48:16 -04004570static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004571move_binding(struct weston_pointer *pointer, uint32_t time,
4572 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004573{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004574 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004575 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004576 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004577
Derek Foreman8ae2db52015-07-15 13:00:36 -05004578 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004579 return;
4580
Derek Foreman8ae2db52015-07-15 13:00:36 -05004581 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004582
Pekka Paalanen01388e22013-04-25 13:57:44 +03004583 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004584 if (surface == NULL)
4585 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004586
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004587 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004588 if (shsurf == NULL || shsurf->state.fullscreen ||
4589 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004590 return;
4591
Derek Foreman794fa0e2015-07-15 13:00:38 -05004592 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004593}
4594
4595static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004596maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4597 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004598{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004599 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004600 struct weston_surface *surface;
4601 struct shell_surface *shsurf;
4602
4603 surface = weston_surface_get_main_surface(focus);
4604 if (surface == NULL)
4605 return;
4606
4607 shsurf = get_shell_surface(surface);
4608 if (shsurf == NULL)
4609 return;
4610
4611 if (!shell_surface_is_xdg_surface(shsurf))
4612 return;
4613
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004614 shsurf->state_requested = true;
4615 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004616 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004617}
4618
4619static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004620fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4621 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004622{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004623 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004624 struct weston_surface *surface;
4625 struct shell_surface *shsurf;
4626
4627 surface = weston_surface_get_main_surface(focus);
4628 if (surface == NULL)
4629 return;
4630
4631 shsurf = get_shell_surface(surface);
4632 if (shsurf == NULL)
4633 return;
4634
4635 if (!shell_surface_is_xdg_surface(shsurf))
4636 return;
4637
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004638 shsurf->state_requested = true;
4639 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004640 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004641 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004642}
4643
4644static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004645touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004646{
Derek Foreman362656b2014-09-04 10:23:05 -05004647 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004648 struct weston_surface *surface;
4649 struct shell_surface *shsurf;
4650
Derek Foreman8ae2db52015-07-15 13:00:36 -05004651 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004652 return;
4653
Derek Foreman8ae2db52015-07-15 13:00:36 -05004654 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004655 surface = weston_surface_get_main_surface(focus);
4656 if (surface == NULL)
4657 return;
4658
4659 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004660 if (shsurf == NULL || shsurf->state.fullscreen ||
4661 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004662 return;
4663
Derek Foremanb7674ae2015-07-15 13:00:37 -05004664 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004665}
4666
4667static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004668resize_binding(struct weston_pointer *pointer, uint32_t time,
4669 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004670{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004671 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004672 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004673 uint32_t edges = 0;
4674 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004675 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004676
Derek Foreman8ae2db52015-07-15 13:00:36 -05004677 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004678 return;
4679
Derek Foreman8ae2db52015-07-15 13:00:36 -05004680 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004681
Pekka Paalanen01388e22013-04-25 13:57:44 +03004682 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004683 if (surface == NULL)
4684 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004685
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004686 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004687 if (shsurf == NULL || shsurf->state.fullscreen ||
4688 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004689 return;
4690
Jason Ekstranda7af7042013-10-12 22:38:11 -05004691 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004692 wl_fixed_to_int(pointer->grab_x),
4693 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004694 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004695
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004696 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004697 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004698 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004699 edges |= 0;
4700 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004701 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004702
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004703 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004704 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004705 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004706 edges |= 0;
4707 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004708 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004709
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004710 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004711}
4712
4713static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004714surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4715 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004716{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004717 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004718 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004719 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004720 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004721
Pekka Paalanen01388e22013-04-25 13:57:44 +03004722 /* XXX: broken for windows containing sub-surfaces */
4723 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004724 if (surface == NULL)
4725 return;
4726
4727 shsurf = get_shell_surface(surface);
4728 if (!shsurf)
4729 return;
4730
Jason Ekstranda7af7042013-10-12 22:38:11 -05004731 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004732
Jason Ekstranda7af7042013-10-12 22:38:11 -05004733 if (shsurf->view->alpha > 1.0)
4734 shsurf->view->alpha = 1.0;
4735 if (shsurf->view->alpha < step)
4736 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004737
Jason Ekstranda7af7042013-10-12 22:38:11 -05004738 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004739 weston_surface_damage(surface);
4740}
4741
4742static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004743do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004744 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004745{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004746 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004747 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004748 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004749 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004750
Derek Foreman1281a362015-07-31 16:55:32 -05004751 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004752 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4753 return;
4754 }
4755
Scott Moreauccbf29d2012-02-22 14:21:41 -07004756 wl_list_for_each(output, &compositor->output_list, link) {
4757 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004758 wl_fixed_to_double(pointer->x),
4759 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004760 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004761 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004762 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004763 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004764 increment = -output->zoom.increment;
4765 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004766 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004767 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004768 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004769 else
4770 increment = 0;
4771
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004772 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004773
Scott Moreaue6603982012-06-11 13:07:51 -06004774 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004775 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004776 else if (output->zoom.level > output->zoom.max_level)
4777 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004778
4779 if (!output->zoom.active) {
4780 if (output->zoom.level <= 0.0)
4781 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004782 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004783 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004784
Scott Moreaue6603982012-06-11 13:07:51 -06004785 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004786
Jason Ekstranda7af7042013-10-12 22:38:11 -05004787 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004788 }
4789 }
4790}
4791
Scott Moreauccbf29d2012-02-22 14:21:41 -07004792static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004793zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4794 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004795{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004796 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004797}
4798
4799static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004800zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4801 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004802{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004803 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004804}
4805
4806static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004807terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4808 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004809{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004810 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004811
Daniel Stone325fc2d2012-05-30 16:31:58 +01004812 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004813}
4814
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004815static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004816rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004817 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004818{
4819 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004820 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004821 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004822 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004823 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004824
Jonas Ådahld2510102014-10-05 21:39:14 +02004825 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004826
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004827 if (!shsurf)
4828 return;
4829
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004830 cx = 0.5f * shsurf->surface->width;
4831 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004832
Daniel Stone37816df2012-05-16 18:45:18 +01004833 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4834 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004835 r = sqrtf(dx * dx + dy * dy);
4836
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004837 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004838 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004839
4840 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004841 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004842 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004843
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004844 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004845 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004846
4847 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004848 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004849 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004850 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004851 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004852
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004853 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004855 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004856 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004857 wl_list_init(&shsurf->rotation.transform.link);
4858 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004859 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004860 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004861
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004862 /* We need to adjust the position of the surface
4863 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004864 cposx = shsurf->view->geometry.x + cx;
4865 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004866 dposx = rotate->center.x - cposx;
4867 dposy = rotate->center.y - cposy;
4868 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869 weston_view_set_position(shsurf->view,
4870 shsurf->view->geometry.x + dposx,
4871 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004872 }
4873
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004874 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004875 * lazily applies the damage due to rotation update.
4876 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004877 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004878}
4879
4880static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004881rotate_grab_button(struct weston_pointer_grab *grab,
4882 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004883{
4884 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004885 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004886 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004887 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004888 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004889
Daniel Stone4dbadb12012-05-30 16:31:51 +01004890 if (pointer->button_count == 0 &&
4891 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004892 if (shsurf)
4893 weston_matrix_multiply(&shsurf->rotation.rotation,
4894 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004895 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004896 free(rotate);
4897 }
4898}
4899
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004900static void
4901rotate_grab_cancel(struct weston_pointer_grab *grab)
4902{
4903 struct rotate_grab *rotate =
4904 container_of(grab, struct rotate_grab, base.grab);
4905
4906 shell_grab_end(&rotate->base);
4907 free(rotate);
4908}
4909
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004910static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004911 noop_grab_focus,
4912 rotate_grab_motion,
4913 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02004914 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004915 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004916};
4917
4918static void
Derek Foreman74de4692015-07-15 13:00:39 -05004919surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004920{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004921 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004922 float dx, dy;
4923 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004924
Derek Foreman45a7c272015-09-11 14:27:40 -05004925 surface = find_toplevel_surface(surface);
4926
Pekka Paalanen460099f2012-01-20 16:48:25 +02004927 rotate = malloc(sizeof *rotate);
4928 if (!rotate)
4929 return;
4930
Jason Ekstranda7af7042013-10-12 22:38:11 -05004931 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004932 surface->surface->width * 0.5f,
4933 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004934 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935
Derek Foreman74de4692015-07-15 13:00:39 -05004936 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4937 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004938 r = sqrtf(dx * dx + dy * dy);
4939 if (r > 20.0f) {
4940 struct weston_matrix inverse;
4941
4942 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004943 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004944 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004945
4946 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004947 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004948 } else {
4949 weston_matrix_init(&surface->rotation.rotation);
4950 weston_matrix_init(&rotate->rotation);
4951 }
4952
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004953 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004954 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004955}
4956
4957static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004958rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004959 void *data)
4960{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004961 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004962 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004963 struct shell_surface *surface;
4964
Derek Foreman8ae2db52015-07-15 13:00:36 -05004965 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004966 return;
4967
Derek Foreman8ae2db52015-07-15 13:00:36 -05004968 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004969
Pekka Paalanen01388e22013-04-25 13:57:44 +03004970 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004971 if (base_surface == NULL)
4972 return;
4973
4974 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004975 if (surface == NULL || surface->state.fullscreen ||
4976 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004977 return;
4978
Derek Foreman74de4692015-07-15 13:00:39 -05004979 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004980}
4981
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004982/* Move all fullscreen layers down to the current workspace and hide their
4983 * black views. The surfaces' state is set to both fullscreen and lowered,
4984 * and this is reversed when such a surface is re-configured, see
4985 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4986 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004987 * lowering_output = NULL - Lower on all outputs, else only lower on the
4988 * specified output.
4989 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004990 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004991 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004992void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004993lower_fullscreen_layer(struct desktop_shell *shell,
4994 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004995{
4996 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004997 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004998
4999 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005000 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005001 &shell->fullscreen_layer.view_list.link,
5002 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005003 struct shell_surface *shsurf = get_shell_surface(view->surface);
5004
5005 if (!shsurf)
5006 continue;
5007
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005008 /* Only lower surfaces which have lowering_output as their fullscreen
5009 * output, unless a NULL output asks for lowering on all outputs.
5010 */
5011 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5012 continue;
5013
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005014 /* We can have a non-fullscreen popup for a fullscreen surface
5015 * in the fullscreen layer. */
5016 if (shsurf->state.fullscreen) {
5017 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005018 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5019 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005020 weston_view_damage_below(shsurf->fullscreen.black_view);
5021
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005022 }
5023
5024 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005025 weston_layer_entry_remove(&view->layer_link);
5026 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005027 weston_view_damage_below(view);
5028 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005029
5030 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005031 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005032}
5033
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005034void
Tiago Vignattibe143262012-04-16 17:31:41 +03005035activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005036 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005037{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005038 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005039 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005040 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005041 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005042 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005043
Pekka Paalanen01388e22013-04-25 13:57:44 +03005044 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005045 shsurf = get_shell_surface(main_surface);
5046 assert(shsurf);
5047
5048 /* Only demote fullscreen surfaces on the output of activated shsurf.
5049 * Leave fullscreen surfaces on unrelated outputs alone. */
5050 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005051
Daniel Stone37816df2012-05-16 18:45:18 +01005052 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005053
Jonas Ådahl8538b222012-08-29 22:13:03 +02005054 state = ensure_focus_state(shell, seat);
5055 if (state == NULL)
5056 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005057
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005058 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005059 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005060
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005061 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005062 shell_configure_fullscreen(shsurf);
5063 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005064 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005065
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005066 /* Update the surface’s layer. This brings it to the top of the stacking
5067 * order as appropriate. */
5068 shell_surface_update_layer(shsurf);
5069
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005070 if (shell->focus_animation_type != ANIMATION_NONE) {
5071 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005072 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005073 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005074}
5075
Alex Wu21858432012-04-01 20:13:08 +08005076/* no-op func for checking black surface */
5077static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005078black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005079{
5080}
5081
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005082static bool
Alex Wu21858432012-04-01 20:13:08 +08005083is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5084{
5085 if (es->configure == black_surface_configure) {
5086 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005087 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005088 return true;
5089 }
5090 return false;
5091}
5092
Kristian Høgsberg75840622011-09-06 13:48:16 -04005093static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005094activate_binding(struct weston_seat *seat,
5095 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005096 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005097{
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005098 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005099 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005100
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005101 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005102
Pekka Paalanen01388e22013-04-25 13:57:44 +03005103 if (is_black_surface(focus, &main_surface))
5104 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005105
Pekka Paalanen01388e22013-04-25 13:57:44 +03005106 main_surface = weston_surface_get_main_surface(focus);
5107 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005108 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005109
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005110 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005111}
5112
5113static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005114click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5115 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005116{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005117 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005118 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005119 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005120 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005121
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005122 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005123}
5124
5125static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005126touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5127 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005128{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005129 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005130 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005131 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005132 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005133
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005134 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005135}
5136
5137static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005138unfocus_all_seats(struct desktop_shell *shell)
5139{
5140 struct weston_seat *seat, *next;
5141
5142 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005143 struct weston_keyboard *keyboard =
5144 weston_seat_get_keyboard(seat);
5145
5146 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005147 continue;
5148
Derek Foreman1281a362015-07-31 16:55:32 -05005149 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005150 }
5151}
5152
5153static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005154lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005155{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005156 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005157
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005158 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005159 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005160 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005161 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005162
5163 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005164
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005165 /* Hide all surfaces by removing the fullscreen, panel and
5166 * toplevel layers. This way nothing else can show or receive
5167 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005168
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005169 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005170 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005171 if (shell->showing_input_panels)
5172 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005173 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005174 wl_list_insert(&shell->compositor->cursor_layer.link,
5175 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005176
Derek Foreman004b4a12015-07-20 16:28:13 -05005177 weston_compositor_sleep(shell->compositor);
5178
Neil Robertsb4a91702014-04-09 16:33:32 +01005179 /* Remove the keyboard focus on all seats. This will be
5180 * restored to the workspace's saved state via
5181 * restore_focus_state when the compositor is unlocked */
5182 unfocus_all_seats(shell);
5183
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005184 /* TODO: disable bindings that should not work while locked. */
5185
5186 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005187}
5188
5189static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005190unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005191{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005192 struct wl_resource *shell_resource;
5193
Pekka Paalanend81c2162011-11-16 13:47:34 +02005194 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005195 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005196 return;
5197 }
5198
5199 /* If desktop-shell client has gone away, unlock immediately. */
5200 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005201 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005202 return;
5203 }
5204
5205 if (shell->prepare_event_sent)
5206 return;
5207
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005208 shell_resource = shell->child.desktop_shell;
5209 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005210 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005211}
5212
5213static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005214shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005215{
5216 struct desktop_shell *shell = data;
5217
5218 shell->fade.animation = NULL;
5219
5220 switch (shell->fade.type) {
5221 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005222 weston_surface_destroy(shell->fade.view->surface);
5223 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005224 break;
5225 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005226 lock(shell);
5227 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005228 default:
5229 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005230 }
5231}
5232
Jason Ekstranda7af7042013-10-12 22:38:11 -05005233static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005234shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005235{
5236 struct weston_compositor *compositor = shell->compositor;
5237 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005238 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005239
5240 surface = weston_surface_create(compositor);
5241 if (!surface)
5242 return NULL;
5243
Jason Ekstranda7af7042013-10-12 22:38:11 -05005244 view = weston_view_create(surface);
5245 if (!view) {
5246 weston_surface_destroy(surface);
5247 return NULL;
5248 }
5249
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005250 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005251 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005252 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005253 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5254 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005255 pixman_region32_init(&surface->input);
5256
Jason Ekstranda7af7042013-10-12 22:38:11 -05005257 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005258}
5259
5260static void
5261shell_fade(struct desktop_shell *shell, enum fade_type type)
5262{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005263 float tint;
5264
5265 switch (type) {
5266 case FADE_IN:
5267 tint = 0.0;
5268 break;
5269 case FADE_OUT:
5270 tint = 1.0;
5271 break;
5272 default:
5273 weston_log("shell: invalid fade type\n");
5274 return;
5275 }
5276
5277 shell->fade.type = type;
5278
Jason Ekstranda7af7042013-10-12 22:38:11 -05005279 if (shell->fade.view == NULL) {
5280 shell->fade.view = shell_fade_create_surface(shell);
5281 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005282 return;
5283
Jason Ekstranda7af7042013-10-12 22:38:11 -05005284 shell->fade.view->alpha = 1.0 - tint;
5285 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005286 }
5287
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005288 if (shell->fade.view->output == NULL) {
5289 /* If the black view gets a NULL output, we lost the
5290 * last output and we'll just cancel the fade. This
5291 * happens when you close the last window under the
5292 * X11 or Wayland backends. */
5293 shell->locked = false;
5294 weston_surface_destroy(shell->fade.view->surface);
5295 shell->fade.view = NULL;
5296 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005297 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005298 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005299 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005300 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005301 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005302 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005303 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005304}
5305
5306static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005307do_shell_fade_startup(void *data)
5308{
5309 struct desktop_shell *shell = data;
5310
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005311 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005312 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005313 } else {
5314 weston_log("desktop shell: "
5315 "unexpected fade-in animation type %d\n",
5316 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005317 weston_surface_destroy(shell->fade.view->surface);
5318 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005319 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005320}
5321
5322static void
5323shell_fade_startup(struct desktop_shell *shell)
5324{
5325 struct wl_event_loop *loop;
5326
5327 if (!shell->fade.startup_timer)
5328 return;
5329
5330 wl_event_source_remove(shell->fade.startup_timer);
5331 shell->fade.startup_timer = NULL;
5332
5333 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5334 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5335}
5336
5337static int
5338fade_startup_timeout(void *data)
5339{
5340 struct desktop_shell *shell = data;
5341
5342 shell_fade_startup(shell);
5343 return 0;
5344}
5345
5346static void
5347shell_fade_init(struct desktop_shell *shell)
5348{
5349 /* Make compositor output all black, and wait for the desktop-shell
5350 * client to signal it is ready, then fade in. The timer triggers a
5351 * fade-in, in case the desktop-shell client takes too long.
5352 */
5353
5354 struct wl_event_loop *loop;
5355
Jason Ekstranda7af7042013-10-12 22:38:11 -05005356 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005357 weston_log("%s: warning: fade surface already exists\n",
5358 __func__);
5359 return;
5360 }
5361
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005362 if (shell->startup_animation_type == ANIMATION_NONE)
5363 return;
5364
Jason Ekstranda7af7042013-10-12 22:38:11 -05005365 shell->fade.view = shell_fade_create_surface(shell);
5366 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005367 return;
5368
Jason Ekstranda7af7042013-10-12 22:38:11 -05005369 weston_view_update_transform(shell->fade.view);
5370 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005371
5372 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5373 shell->fade.startup_timer =
5374 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5375 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5376}
5377
5378static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005379idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005380{
5381 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005382 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005383 struct weston_seat *seat;
5384
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005385 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005386 struct weston_touch *touch = weston_seat_get_touch(seat);
5387 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5388
5389 if (pointer)
5390 popup_grab_end(pointer);
5391 if (touch)
5392 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005393 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005394
5395 shell_fade(shell, FADE_OUT);
5396 /* lock() is called from shell_fade_done() */
5397}
5398
5399static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005400wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005401{
5402 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005403 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005404
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005405 unlock(shell);
5406}
5407
5408static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005409center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005410{
Giulio Camuffob8366642013-04-25 13:57:46 +03005411 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005412 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005413
Jason Ekstranda7af7042013-10-12 22:38:11 -05005414 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005415
5416 x = output->x + (output->width - width) / 2 - surf_x / 2;
5417 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005418
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005419 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005420}
5421
5422static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005423weston_view_set_initial_position(struct weston_view *view,
5424 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005425{
5426 struct weston_compositor *compositor = shell->compositor;
5427 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005428 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005429 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005430 struct weston_output *output, *target_output = NULL;
5431 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005432 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005433
5434 /* As a heuristic place the new window on the same output as the
5435 * pointer. Falling back to the output containing 0, 0.
5436 *
5437 * TODO: Do something clever for touch too?
5438 */
5439 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005440 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5441
5442 if (pointer) {
5443 ix = wl_fixed_to_int(pointer->x);
5444 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005445 break;
5446 }
5447 }
5448
5449 wl_list_for_each(output, &compositor->output_list, link) {
5450 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5451 target_output = output;
5452 break;
5453 }
5454 }
5455
5456 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005457 weston_view_set_position(view, 10 + random() % 400,
5458 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005459 return;
5460 }
5461
5462 /* Valid range within output where the surface will still be onscreen.
5463 * If this is negative it means that the surface is bigger than
5464 * output.
5465 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005466 get_output_work_area(shell, target_output, &area);
5467
Derek Foremanf814c5d2015-04-15 12:23:54 -05005468 x = area.x;
5469 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005470 range_x = area.width - view->surface->width;
5471 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005472
Rob Bradford4cb88c72012-08-13 15:18:44 +01005473 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005474 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005475
5476 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005477 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005478
Jason Ekstranda7af7042013-10-12 22:38:11 -05005479 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005480}
5481
5482static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005483set_maximized_position(struct desktop_shell *shell,
5484 struct shell_surface *shsurf)
5485{
5486 int32_t surf_x, surf_y;
5487 pixman_rectangle32_t area;
5488
Jonny Lambd73c6942014-08-20 15:53:20 +02005489 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005490 if (shsurf->has_set_geometry) {
5491 surf_x = shsurf->geometry.x;
5492 surf_y = shsurf->geometry.y;
5493 } else {
5494 surface_subsurfaces_boundingbox(shsurf->surface,
5495 &surf_x, &surf_y, NULL, NULL);
5496 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005497
5498 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005499 area.x - surf_x,
5500 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005501}
5502
5503static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005504map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005505 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005506{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005507 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005508 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005509
Pekka Paalanen77346a62011-11-30 16:26:35 +02005510 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005511 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005512 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005513 if (shsurf->state.fullscreen) {
5514 center_on_output(shsurf->view, shsurf->fullscreen_output);
5515 shell_map_fullscreen(shsurf);
5516 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005517 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005518 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005519 weston_view_set_initial_position(shsurf->view, shell);
5520 }
Juan Zhao96879df2012-02-07 08:45:41 +08005521 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005522 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005523 if (shell_map_popup(shsurf) != 0)
5524 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005525 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005526 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005527 weston_view_set_position(shsurf->view,
5528 shsurf->view->geometry.x + sx,
5529 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005530 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005531 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005532 default:
5533 ;
5534 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005535
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005536 /* Surface stacking order, see also activate(). */
5537 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005538
Jason Ekstranda7af7042013-10-12 22:38:11 -05005539 if (shsurf->type != SHELL_SURFACE_NONE) {
5540 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005541 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005542 shsurf->surface->output = shsurf->output;
5543 shsurf->view->output = shsurf->output;
5544 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005545 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005546
Jason Ekstranda7af7042013-10-12 22:38:11 -05005547 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005548 /* XXX: xwayland's using the same fields for transient type */
5549 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005550 if (shsurf->transient.flags ==
5551 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5552 break;
5553 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005554 if (shsurf->state.relative &&
5555 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5556 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005557 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005558 break;
5559 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005560 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005561 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005562 case SHELL_SURFACE_POPUP:
5563 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005564 default:
5565 break;
5566 }
5567
Rafael Antognolli03b16592013-12-03 15:35:42 -02005568 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5569 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005570 {
5571 switch (shell->win_animation_type) {
5572 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005573 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005574 break;
5575 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005576 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005577 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005578 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005579 default:
5580 break;
5581 }
5582 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005583}
5584
5585static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005586configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005587 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005588{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005589 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005590 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005591
Pekka Paalanen77346a62011-11-30 16:26:35 +02005592 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005593
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005594 assert(shsurf);
5595
Rafael Antognolli03b16592013-12-03 15:35:42 -02005596 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005597 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005598 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005599 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005600 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005601 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005602 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005603
Alex Wu4539b082012-03-01 12:57:46 +08005604 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005605 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005606 wl_list_for_each(view, &surface->views, surface_link)
5607 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005608
Rafael Antognolli03b16592013-12-03 15:35:42 -02005609 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005610 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005611 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005612}
5613
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005614static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005615shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005616{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005617 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005618 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005619 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005620
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005621 assert(shsurf);
5622
5623 shell = shsurf->shell;
5624
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005625 if (!weston_surface_is_mapped(es) &&
5626 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005627 remove_popup_grab(shsurf);
5628 }
5629
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005630 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005631 return;
5632
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005633 if (shsurf->has_next_geometry) {
5634 shsurf->geometry = shsurf->next_geometry;
5635 shsurf->has_next_geometry = false;
5636 shsurf->has_set_geometry = true;
5637 } else if (!shsurf->has_set_geometry) {
5638 surface_subsurfaces_boundingbox(shsurf->surface,
5639 &shsurf->geometry.x,
5640 &shsurf->geometry.y,
5641 &shsurf->geometry.width,
5642 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005643 }
5644
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005645 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005646 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005647 type_changed = 1;
5648 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005649
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005650 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005651 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005652 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005653 shsurf->last_width != es->width ||
5654 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005655 float from_x, from_y;
5656 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005657
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005658 if (shsurf->resize_edges) {
5659 sx = 0;
5660 sy = 0;
5661 }
5662
5663 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5664 sx = shsurf->last_width - es->width;
5665 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5666 sy = shsurf->last_height - es->height;
5667
5668 shsurf->last_width = es->width;
5669 shsurf->last_height = es->height;
5670
Jason Ekstranda7af7042013-10-12 22:38:11 -05005671 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5672 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005673 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005674 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005675 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005676 }
5677}
5678
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005679static bool
5680check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5681{
5682 struct timespec now;
5683
5684 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5685 return false;
5686
5687 /*
5688 * If the shell helper client dies before the session has been
5689 * up for roughly 30 seconds, better just make Weston shut down,
5690 * because the user likely has no way to interact with the desktop
5691 * anyway.
5692 */
5693 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5694 weston_log("Error: %s apparently cannot run at all.\n",
5695 shell->client);
5696 weston_log_continue(STAMP_SPACE "Quitting...");
5697 wl_display_terminate(shell->compositor->wl_display);
5698
5699 return true;
5700 }
5701
5702 return false;
5703}
5704
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005705static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005706
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005707static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005708respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005709{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005710 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005711
5712 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5713 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005714 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005715 shell->child.deathstamp = time;
5716 shell->child.deathcount = 0;
5717 }
5718
5719 shell->child.deathcount++;
5720 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005721 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005722 return;
5723 }
5724
Pekka Paalanen826dc142014-08-27 12:11:53 +03005725 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005726 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005727}
5728
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005729static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005730desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5731{
5732 struct desktop_shell *shell;
5733
5734 shell = container_of(listener, struct desktop_shell,
5735 child.client_destroy_listener);
5736
Pekka Paalanen826dc142014-08-27 12:11:53 +03005737 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005738 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005739 /*
5740 * unbind_desktop_shell() will reset shell->child.desktop_shell
5741 * before the respawned process has a chance to create a new
5742 * desktop_shell object, because we are being called from the
5743 * wl_client destructor which destroys all wl_resources before
5744 * returning.
5745 */
5746
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005747 if (!check_desktop_shell_crash_too_early(shell))
5748 respawn_desktop_shell_process(shell);
5749
Pekka Paalanen826dc142014-08-27 12:11:53 +03005750 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005751}
5752
5753static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005754launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005755{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005756 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005757
Pekka Paalanen826dc142014-08-27 12:11:53 +03005758 shell->child.client = weston_client_start(shell->compositor,
5759 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005760
Arnaud Vrac42631192014-08-25 20:56:46 +02005761 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005762 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005763 return;
5764 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005765
5766 shell->child.client_destroy_listener.notify =
5767 desktop_shell_client_destroy;
5768 wl_client_add_destroy_listener(shell->child.client,
5769 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005770}
5771
5772static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005773handle_shell_client_destroy(struct wl_listener *listener, void *data)
5774{
5775 struct shell_client *sc =
5776 container_of(listener, struct shell_client, destroy_listener);
5777
5778 if (sc->ping_timer)
5779 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005780
5781 /* Since we're about to free shell_client, we remove it from the
5782 * head of the surface list so we don't use that freed list node
5783 * during surface clean up later on.
5784 */
5785 wl_list_remove(&sc->surface_list);
5786
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005787 free(sc);
5788}
5789
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005790static struct shell_client *
5791shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5792 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005793{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005794 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005795
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005796 sc = zalloc(sizeof *sc);
5797 if (sc == NULL) {
5798 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005799 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005800 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005801
5802 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005803 if (sc->resource == NULL) {
5804 free(sc);
5805 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005806 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005807 }
5808
5809 sc->client = client;
5810 sc->shell = shell;
5811 sc->destroy_listener.notify = handle_shell_client_destroy;
5812 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005813 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005814
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005815 return sc;
5816}
5817
5818static void
5819bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5820{
5821 struct desktop_shell *shell = data;
5822 struct shell_client *sc;
5823
5824 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5825 if (sc)
5826 wl_resource_set_implementation(sc->resource,
5827 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005828 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005829}
5830
5831static void
5832bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5833{
5834 struct desktop_shell *shell = data;
5835 struct shell_client *sc;
5836
5837 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5838 if (sc)
5839 wl_resource_set_dispatcher(sc->resource,
5840 xdg_shell_unversioned_dispatch,
5841 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005842}
5843
5844static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005845unbind_desktop_shell(struct wl_resource *resource)
5846{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005847 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005848
5849 if (shell->locked)
5850 resume_desktop(shell);
5851
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005852 shell->child.desktop_shell = NULL;
5853 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005854}
5855
5856static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005857bind_desktop_shell(struct wl_client *client,
5858 void *data, uint32_t version, uint32_t id)
5859{
Tiago Vignattibe143262012-04-16 17:31:41 +03005860 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005861 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005862
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005863 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5864 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005865
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005866 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005867 wl_resource_set_implementation(resource,
5868 &desktop_shell_implementation,
5869 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005870 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005871 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005872 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005873
5874 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5875 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005876}
5877
Kristian Høgsberg07045392012-02-19 18:52:44 -05005878struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005879 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005880 struct weston_surface *current;
5881 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005882 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005883 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005884};
5885
5886static void
5887switcher_next(struct switcher *switcher)
5888{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005889 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005890 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005891 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005892 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005893
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005894 /* temporary re-display minimized surfaces */
5895 struct weston_view *tmp;
5896 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005897 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5898 weston_layer_entry_remove(&view->layer_link);
5899 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005900 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5901 *minimized = view;
5902 }
5903
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005904 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005905 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005906 if (shsurf &&
5907 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5908 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005909 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005910 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005911 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005912 next = view->surface;
5913 prev = view->surface;
5914 view->alpha = 0.25;
5915 weston_view_geometry_dirty(view);
5916 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005917 }
Alex Wu1659daa2012-04-01 20:13:09 +08005918
Jason Ekstranda7af7042013-10-12 22:38:11 -05005919 if (is_black_surface(view->surface, NULL)) {
5920 view->alpha = 0.25;
5921 weston_view_geometry_dirty(view);
5922 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005923 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005924 }
5925
5926 if (next == NULL)
5927 next = first;
5928
Alex Wu07b26062012-03-12 16:06:01 +08005929 if (next == NULL)
5930 return;
5931
Kristian Høgsberg07045392012-02-19 18:52:44 -05005932 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005933 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005934
5935 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005936 wl_list_for_each(view, &next->views, surface_link)
5937 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005938
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005939 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005940 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005941 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005942}
5943
5944static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005945switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005946{
5947 struct switcher *switcher =
5948 container_of(listener, struct switcher, listener);
5949
5950 switcher_next(switcher);
5951}
5952
5953static void
Daniel Stone351eb612012-05-31 15:27:47 -04005954switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005955{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005956 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005957 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005958 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005959
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005960 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005961 if (is_focus_view(view))
5962 continue;
5963
Jason Ekstranda7af7042013-10-12 22:38:11 -05005964 view->alpha = 1.0;
5965 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005966 }
5967
Alex Wu07b26062012-03-12 16:06:01 +08005968 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005969 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005970 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005971 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005972 weston_keyboard_end_grab(keyboard);
5973 if (keyboard->input_method_resource)
5974 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005975
5976 /* re-hide surfaces that were temporary shown during the switch */
5977 struct weston_view **minimized;
5978 wl_array_for_each(minimized, &switcher->minimized_array) {
5979 /* with the exception of the current selected */
5980 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005981 weston_layer_entry_remove(&(*minimized)->layer_link);
5982 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005983 weston_view_damage_below(*minimized);
5984 }
5985 }
5986 wl_array_release(&switcher->minimized_array);
5987
Kristian Høgsberg07045392012-02-19 18:52:44 -05005988 free(switcher);
5989}
5990
5991static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005992switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005993 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005994{
5995 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005996 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005997
Daniel Stonec9785ea2012-05-30 16:31:52 +01005998 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005999 switcher_next(switcher);
6000}
6001
6002static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006003switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006004 uint32_t mods_depressed, uint32_t mods_latched,
6005 uint32_t mods_locked, uint32_t group)
6006{
6007 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006008 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006009
Daniel Stone351eb612012-05-31 15:27:47 -04006010 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6011 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006012}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006013
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006014static void
6015switcher_cancel(struct weston_keyboard_grab *grab)
6016{
6017 struct switcher *switcher = container_of(grab, struct switcher, grab);
6018
6019 switcher_destroy(switcher);
6020}
6021
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006022static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006023 switcher_key,
6024 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006025 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006026};
6027
6028static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006029switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6030 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006031{
Tiago Vignattibe143262012-04-16 17:31:41 +03006032 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006033 struct switcher *switcher;
6034
6035 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006036 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006037 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006038 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006039 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006040 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006041
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006042 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006043 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006045 weston_keyboard_start_grab(keyboard, &switcher->grab);
6046 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047 switcher_next(switcher);
6048}
6049
Pekka Paalanen3c647232011-12-22 13:43:43 +02006050static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006051backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6052 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006053{
6054 struct weston_compositor *compositor = data;
6055 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006056 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006057
6058 /* TODO: we're limiting to simple use cases, where we assume just
6059 * control on the primary display. We'd have to extend later if we
6060 * ever get support for setting backlights on random desktop LCD
6061 * panels though */
6062 output = get_default_output(compositor);
6063 if (!output)
6064 return;
6065
6066 if (!output->set_backlight)
6067 return;
6068
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006069 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6070 backlight_new = output->backlight_current - 25;
6071 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6072 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006073
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006074 if (backlight_new < 5)
6075 backlight_new = 5;
6076 if (backlight_new > 255)
6077 backlight_new = 255;
6078
6079 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006080 output->set_backlight(output, output->backlight_current);
6081}
6082
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006083static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006084force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6085 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006086{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006087 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006088 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006089 struct desktop_shell *shell = data;
6090 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006091 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006092
Derek Foreman8ae2db52015-07-15 13:00:36 -05006093 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006094 if (!focus_surface)
6095 return;
6096
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006097 wl_signal_emit(&compositor->kill_signal, focus_surface);
6098
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006099 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006100 wl_client_get_credentials(client, &pid, NULL, NULL);
6101
6102 /* Skip clients that we launched ourselves (the credentials of
6103 * the socketpair is ours) */
6104 if (pid == getpid())
6105 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006106
Daniel Stone325fc2d2012-05-30 16:31:58 +01006107 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006108}
6109
6110static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006111workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006112 uint32_t key, void *data)
6113{
6114 struct desktop_shell *shell = data;
6115 unsigned int new_index = shell->workspaces.current;
6116
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006117 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006118 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006119 if (new_index != 0)
6120 new_index--;
6121
6122 change_workspace(shell, new_index);
6123}
6124
6125static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006126workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006127 uint32_t key, void *data)
6128{
6129 struct desktop_shell *shell = data;
6130 unsigned int new_index = shell->workspaces.current;
6131
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006132 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006133 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006134 if (new_index < shell->workspaces.num - 1)
6135 new_index++;
6136
6137 change_workspace(shell, new_index);
6138}
6139
6140static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006141workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006142 uint32_t key, void *data)
6143{
6144 struct desktop_shell *shell = data;
6145 unsigned int new_index;
6146
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006147 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006148 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006149 new_index = key - KEY_F1;
6150 if (new_index >= shell->workspaces.num)
6151 new_index = shell->workspaces.num - 1;
6152
6153 change_workspace(shell, new_index);
6154}
6155
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006156static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006157workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6158 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006159{
6160 struct desktop_shell *shell = data;
6161 unsigned int new_index = shell->workspaces.current;
6162
6163 if (shell->locked)
6164 return;
6165
6166 if (new_index != 0)
6167 new_index--;
6168
Derek Foreman8ae2db52015-07-15 13:00:36 -05006169 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006170}
6171
6172static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006173workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6174 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006175{
6176 struct desktop_shell *shell = data;
6177 unsigned int new_index = shell->workspaces.current;
6178
6179 if (shell->locked)
6180 return;
6181
6182 if (new_index < shell->workspaces.num - 1)
6183 new_index++;
6184
Derek Foreman8ae2db52015-07-15 13:00:36 -05006185 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006186}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006187
6188static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006189shell_reposition_view_on_output_destroy(struct weston_view *view)
6190{
6191 struct weston_output *output, *first_output;
6192 struct weston_compositor *ec = view->surface->compositor;
6193 struct shell_surface *shsurf;
6194 float x, y;
6195 int visible;
6196
6197 x = view->geometry.x;
6198 y = view->geometry.y;
6199
6200 /* At this point the destroyed output is not in the list anymore.
6201 * If the view is still visible somewhere, we leave where it is,
6202 * otherwise, move it to the first output. */
6203 visible = 0;
6204 wl_list_for_each(output, &ec->output_list, link) {
6205 if (pixman_region32_contains_point(&output->region,
6206 x, y, NULL)) {
6207 visible = 1;
6208 break;
6209 }
6210 }
6211
6212 if (!visible) {
6213 first_output = container_of(ec->output_list.next,
6214 struct weston_output, link);
6215
6216 x = first_output->x + first_output->width / 4;
6217 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006218
6219 weston_view_set_position(view, x, y);
6220 } else {
6221 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006222 }
6223
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006224
6225 shsurf = get_shell_surface(view->surface);
6226
6227 if (shsurf) {
6228 shsurf->saved_position_valid = false;
6229 shsurf->next_state.maximized = false;
6230 shsurf->next_state.fullscreen = false;
6231 shsurf->state_changed = true;
6232 }
6233}
6234
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006235void
6236shell_for_each_layer(struct desktop_shell *shell,
6237 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006238{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006239 struct workspace **ws;
6240
6241 func(shell, &shell->fullscreen_layer, data);
6242 func(shell, &shell->panel_layer, data);
6243 func(shell, &shell->background_layer, data);
6244 func(shell, &shell->lock_layer, data);
6245 func(shell, &shell->input_panel_layer, data);
6246
6247 wl_array_for_each(ws, &shell->workspaces.array)
6248 func(shell, &(*ws)->layer, data);
6249}
6250
6251static void
6252shell_output_destroy_move_layer(struct desktop_shell *shell,
6253 struct weston_layer *layer,
6254 void *data)
6255{
6256 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006257 struct weston_view *view;
6258
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006259 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006260 if (view->output != output)
6261 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006262
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006263 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006264 }
6265}
6266
6267static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006268handle_output_destroy(struct wl_listener *listener, void *data)
6269{
6270 struct shell_output *output_listener =
6271 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006272 struct weston_output *output = output_listener->output;
6273 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006274
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006275 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006276
Xiong Zhang6b481422013-10-23 13:58:32 +08006277 wl_list_remove(&output_listener->destroy_listener.link);
6278 wl_list_remove(&output_listener->link);
6279 free(output_listener);
6280}
6281
6282static void
6283create_shell_output(struct desktop_shell *shell,
6284 struct weston_output *output)
6285{
6286 struct shell_output *shell_output;
6287
6288 shell_output = zalloc(sizeof *shell_output);
6289 if (shell_output == NULL)
6290 return;
6291
6292 shell_output->output = output;
6293 shell_output->shell = shell;
6294 shell_output->destroy_listener.notify = handle_output_destroy;
6295 wl_signal_add(&output->destroy_signal,
6296 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006297 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006298}
6299
6300static void
6301handle_output_create(struct wl_listener *listener, void *data)
6302{
6303 struct desktop_shell *shell =
6304 container_of(listener, struct desktop_shell, output_create_listener);
6305 struct weston_output *output = (struct weston_output *)data;
6306
6307 create_shell_output(shell, output);
6308}
6309
6310static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006311handle_output_move_layer(struct desktop_shell *shell,
6312 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006313{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006314 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006315 struct weston_view *view;
6316 float x, y;
6317
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006318 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006319 if (view->output != output)
6320 continue;
6321
6322 x = view->geometry.x + output->move_x;
6323 y = view->geometry.y + output->move_y;
6324 weston_view_set_position(view, x, y);
6325 }
6326}
6327
6328static void
6329handle_output_move(struct wl_listener *listener, void *data)
6330{
6331 struct desktop_shell *shell;
6332
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006333 shell = container_of(listener, struct desktop_shell,
6334 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006335
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006336 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006337}
6338
6339static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006340setup_output_destroy_handler(struct weston_compositor *ec,
6341 struct desktop_shell *shell)
6342{
6343 struct weston_output *output;
6344
6345 wl_list_init(&shell->output_list);
6346 wl_list_for_each(output, &ec->output_list, link)
6347 create_shell_output(shell, output);
6348
6349 shell->output_create_listener.notify = handle_output_create;
6350 wl_signal_add(&ec->output_created_signal,
6351 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006352
6353 shell->output_move_listener.notify = handle_output_move;
6354 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006355}
6356
6357static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006358shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006359{
Tiago Vignattibe143262012-04-16 17:31:41 +03006360 struct desktop_shell *shell =
6361 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006362 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006363 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006364
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006365 /* Force state to unlocked so we don't try to fade */
6366 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006367
6368 if (shell->child.client) {
6369 /* disable respawn */
6370 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006371 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006372 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006373
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006374 wl_list_remove(&shell->idle_listener.link);
6375 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006376
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006377 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006378 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006379
Xiong Zhang6b481422013-10-23 13:58:32 +08006380 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6381 wl_list_remove(&shell_output->destroy_listener.link);
6382 wl_list_remove(&shell_output->link);
6383 free(shell_output);
6384 }
6385
6386 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006387 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006388
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006389 wl_array_for_each(ws, &shell->workspaces.array)
6390 workspace_destroy(*ws);
6391 wl_array_release(&shell->workspaces.array);
6392
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006393 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006394 free(shell);
6395}
6396
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006397static void
6398shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6399{
6400 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006401 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006402
6403 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006404 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6405 MODIFIER_CTRL | MODIFIER_ALT,
6406 terminate_binding, ec);
6407 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6408 click_to_activate_binding,
6409 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006410 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6411 click_to_activate_binding,
6412 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006413 weston_compositor_add_touch_binding(ec, 0,
6414 touch_to_activate_binding,
6415 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006416 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6417 MODIFIER_SUPER | MODIFIER_ALT,
6418 surface_opacity_binding, NULL);
6419 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6420 MODIFIER_SUPER, zoom_axis_binding,
6421 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006422
6423 /* configurable bindings */
6424 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006425 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6426 zoom_key_binding, NULL);
6427 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6428 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006429 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6430 maximize_binding, NULL);
6431 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6432 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006433 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6434 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006435 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006436 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006437 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006438 weston_compositor_add_button_binding(ec, BTN_LEFT,
6439 mod | MODIFIER_SHIFT,
6440 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006441
6442 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006443 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006444 rotate_binding, NULL);
6445
Daniel Stone325fc2d2012-05-30 16:31:58 +01006446 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6447 shell);
6448 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6449 ec);
6450 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6451 backlight_binding, ec);
6452 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6453 ec);
6454 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6455 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006456 weston_compositor_add_key_binding(ec, KEY_K, mod,
6457 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006458 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6459 workspace_up_binding, shell);
6460 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6461 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006462 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6463 workspace_move_surface_up_binding,
6464 shell);
6465 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6466 workspace_move_surface_down_binding,
6467 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006468
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006469 if (shell->exposay_modifier)
6470 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6471 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006472
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006473 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6474 if (shell->workspaces.num > 1) {
6475 num_workspace_bindings = shell->workspaces.num;
6476 if (num_workspace_bindings > 6)
6477 num_workspace_bindings = 6;
6478 for (i = 0; i < num_workspace_bindings; i++)
6479 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6480 workspace_f_binding,
6481 shell);
6482 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006483
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006484 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006485}
6486
Jason Ekstrand024177c2014-04-21 19:42:58 -05006487static void
6488handle_seat_created(struct wl_listener *listener, void *data)
6489{
6490 struct weston_seat *seat = data;
6491
6492 create_shell_seat(seat);
6493}
6494
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006495WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006496module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006497 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006498{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006499 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006500 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006501 struct workspace **pws;
6502 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006503 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006504
Peter Huttererf3d62272013-08-08 11:57:05 +10006505 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006506 if (shell == NULL)
6507 return -1;
6508
Kristian Høgsberg75840622011-09-06 13:48:16 -04006509 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006510
6511 shell->destroy_listener.notify = shell_destroy;
6512 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006513 shell->idle_listener.notify = idle_handler;
6514 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6515 shell->wake_listener.notify = wake_handler;
6516 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006517
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006518 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006519 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006520 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006521 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006522 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006523 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006524 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006525 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006526 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006527 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006528 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006529 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006530 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006531
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006532 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6533 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006534 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6535 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006536 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006537
6538 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006539 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006540
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006541 if (input_panel_setup(shell) < 0)
6542 return -1;
6543
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006544 shell->text_backend = text_backend_init(ec);
6545 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006546 return -1;
6547
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006548 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006549
Daniel Stonedf8133b2013-11-19 11:37:14 +01006550 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6551 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6552
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006553 for (i = 0; i < shell->workspaces.num; i++) {
6554 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6555 if (pws == NULL)
6556 return -1;
6557
6558 *pws = workspace_create();
6559 if (*pws == NULL)
6560 return -1;
6561 }
6562 activate_workspace(shell, 0);
6563
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006564 weston_layer_init(&shell->minimized_layer, NULL);
6565
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006566 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006567 wl_list_init(&shell->workspaces.animation.link);
6568 shell->workspaces.animation.frame = animate_workspace_change_frame;
6569
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006570 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006571 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006572 return -1;
6573
Rafael Antognollie2a34552013-12-03 15:35:45 -02006574 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6575 shell, bind_xdg_shell) == NULL)
6576 return -1;
6577
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006578 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006579 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006580 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006581 return -1;
6582
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006583 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006584
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006585 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006586
Xiong Zhang6b481422013-10-23 13:58:32 +08006587 setup_output_destroy_handler(ec, shell);
6588
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006589 loop = wl_display_get_event_loop(ec->wl_display);
6590 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006591
Jason Ekstrand024177c2014-04-21 19:42:58 -05006592 wl_list_for_each(seat, &ec->seat_list, link)
6593 handle_seat_created(NULL, seat);
6594 shell->seat_create_listener.notify = handle_seat_created;
6595 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006596
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006597 screenshooter_create(ec);
6598
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006599 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006600
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006601 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006602
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006603 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6604
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006605 return 0;
6606}