blob: 474d118f6571cb7ee2d02c5b39a44e1b7f46f641 [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;
Bob Ham744e6532016-01-12 10:21:48 +0000639 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200640
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400641 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400642 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300643 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
644 WESTON_SHELL_CLIENT);
645 if (ret < 0)
646 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400647 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500648 "client", &s, client);
649 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100650 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000651
652 weston_config_section_get_bool(section,
653 "allow-zap", &allow_zap, true);
654 shell->allow_zap = allow_zap;
655
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100656 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400657 "binding-modifier", &s, "super");
658 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200659 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800660
661 weston_config_section_get_string(section,
662 "exposay-modifier", &s, "none");
663 if (strcmp(s, "none") == 0)
664 shell->exposay_modifier = 0;
665 else
666 shell->exposay_modifier = get_modifier(s);
667 free(s);
668
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400669 weston_config_section_get_string(section, "animation", &s, "none");
670 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200671 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200672 weston_config_section_get_string(section, "close-animation", &s, "fade");
673 shell->win_close_animation_type = get_animation_type(s);
674 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700675 weston_config_section_get_string(section,
676 "startup-animation", &s, "fade");
677 shell->startup_animation_type = get_animation_type(s);
678 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700679 if (shell->startup_animation_type == ANIMATION_ZOOM)
680 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681 weston_config_section_get_string(section, "focus-animation", &s, "none");
682 shell->focus_animation_type = get_animation_type(s);
683 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400684 weston_config_section_get_uint(section, "num-workspaces",
685 &shell->workspaces.num,
686 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200687}
688
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800689struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100690get_default_output(struct weston_compositor *compositor)
691{
692 return container_of(compositor->output_list.next,
693 struct weston_output, link);
694}
695
Pekka Paalanen8274d902014-08-06 19:36:51 +0300696static int
697focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
698{
699 return snprintf(buf, len, "focus highlight effect for output %s",
700 surface->output->name);
701}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100702
703/* no-op func for checking focus surface */
704static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600705focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100706{
707}
708
709static struct focus_surface *
710get_focus_surface(struct weston_surface *surface)
711{
712 if (surface->configure == focus_surface_configure)
713 return surface->configure_private;
714 else
715 return NULL;
716}
717
718static bool
719is_focus_surface (struct weston_surface *es)
720{
721 return (es->configure == focus_surface_configure);
722}
723
724static bool
725is_focus_view (struct weston_view *view)
726{
727 return is_focus_surface (view->surface);
728}
729
730static struct focus_surface *
731create_focus_surface(struct weston_compositor *ec,
732 struct weston_output *output)
733{
734 struct focus_surface *fsurf = NULL;
735 struct weston_surface *surface = NULL;
736
737 fsurf = malloc(sizeof *fsurf);
738 if (!fsurf)
739 return NULL;
740
741 fsurf->surface = weston_surface_create(ec);
742 surface = fsurf->surface;
743 if (surface == NULL) {
744 free(fsurf);
745 return NULL;
746 }
747
748 surface->configure = focus_surface_configure;
749 surface->output = output;
750 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300751 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100752
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800753 fsurf->view = weston_view_create(surface);
754 if (fsurf->view == NULL) {
755 weston_surface_destroy(surface);
756 free(fsurf);
757 return NULL;
758 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100759 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100760
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600761 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600762 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100763 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
764 pixman_region32_fini(&surface->opaque);
765 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
766 output->width, output->height);
767 pixman_region32_fini(&surface->input);
768 pixman_region32_init(&surface->input);
769
770 wl_list_init(&fsurf->workspace_transform.link);
771
772 return fsurf;
773}
774
775static void
776focus_surface_destroy(struct focus_surface *fsurf)
777{
778 weston_surface_destroy(fsurf->surface);
779 free(fsurf);
780}
781
782static void
783focus_animation_done(struct weston_view_animation *animation, void *data)
784{
785 struct workspace *ws = data;
786
787 ws->focus_animation = NULL;
788}
789
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200790static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200791focus_state_destroy(struct focus_state *state)
792{
793 wl_list_remove(&state->seat_destroy_listener.link);
794 wl_list_remove(&state->surface_destroy_listener.link);
795 free(state);
796}
797
798static void
799focus_state_seat_destroy(struct wl_listener *listener, void *data)
800{
801 struct focus_state *state = container_of(listener,
802 struct focus_state,
803 seat_destroy_listener);
804
805 wl_list_remove(&state->link);
806 focus_state_destroy(state);
807}
808
809static void
810focus_state_surface_destroy(struct wl_listener *listener, void *data)
811{
812 struct focus_state *state = container_of(listener,
813 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400814 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400815 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500816 struct weston_surface *main_surface, *next;
817 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200818
Pekka Paalanen01388e22013-04-25 13:57:44 +0300819 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
820
Kristian Høgsberge3778222012-07-31 17:29:30 -0400821 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300822 wl_list_for_each(view,
823 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500824 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400825 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100826 if (is_focus_view(view))
827 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400828
Jason Ekstranda7af7042013-10-12 22:38:11 -0500829 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400830 break;
831 }
832
Pekka Paalanen01388e22013-04-25 13:57:44 +0300833 /* if the focus was a sub-surface, activate its main surface */
834 if (main_surface != state->keyboard_focus)
835 next = main_surface;
836
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400838 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100840 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400841 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100842 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
843 if (state->ws->focus_animation)
844 weston_view_animation_destroy(state->ws->focus_animation);
845
846 state->ws->focus_animation = weston_fade_run(
847 state->ws->fsurf_front->view,
848 state->ws->fsurf_front->view->alpha, 0.0, 300,
849 focus_animation_done, state->ws);
850 }
851
Kristian Høgsberge3778222012-07-31 17:29:30 -0400852 wl_list_remove(&state->link);
853 focus_state_destroy(state);
854 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200855}
856
857static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400858focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200859{
Jonas Ådahl04769742012-06-13 00:01:24 +0200860 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200861
862 state = malloc(sizeof *state);
863 if (state == NULL)
864 return NULL;
865
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400867 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200868 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400869 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200870
871 state->seat_destroy_listener.notify = focus_state_seat_destroy;
872 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400873 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200874 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400875 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200876
877 return state;
878}
879
Jonas Ådahl8538b222012-08-29 22:13:03 +0200880static struct focus_state *
881ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
882{
883 struct workspace *ws = get_current_workspace(shell);
884 struct focus_state *state;
885
886 wl_list_for_each(state, &ws->focus_list, link)
887 if (state->seat == seat)
888 break;
889
890 if (&state->link == &ws->focus_list)
891 state = focus_state_create(seat, ws);
892
893 return state;
894}
895
Jonas Ådahl04769742012-06-13 00:01:24 +0200896static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800897focus_state_set_focus(struct focus_state *state,
898 struct weston_surface *surface)
899{
900 if (state->keyboard_focus) {
901 wl_list_remove(&state->surface_destroy_listener.link);
902 wl_list_init(&state->surface_destroy_listener.link);
903 }
904
905 state->keyboard_focus = surface;
906 if (surface)
907 wl_signal_add(&surface->destroy_signal,
908 &state->surface_destroy_listener);
909}
910
911static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400912restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200913{
914 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400915 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100916 struct wl_list pending_seat_list;
917 struct weston_seat *seat, *next_seat;
918
919 /* Temporarily steal the list of seats so that we can keep
920 * track of the seats we've already processed */
921 wl_list_init(&pending_seat_list);
922 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
923 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200924
925 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500926 struct weston_keyboard *keyboard =
927 weston_seat_get_keyboard(state->seat);
928
Neil Roberts4237f502014-04-09 16:33:31 +0100929 wl_list_remove(&state->seat->link);
930 wl_list_insert(&shell->compositor->seat_list,
931 &state->seat->link);
932
Derek Foreman1281a362015-07-31 16:55:32 -0500933 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800934 continue;
935
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400936 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200937
Derek Foreman1281a362015-07-31 16:55:32 -0500938 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200939 }
Neil Roberts4237f502014-04-09 16:33:31 +0100940
941 /* For any remaining seats that we don't have a focus state
942 * for we'll reset the keyboard focus to NULL */
943 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500944 struct weston_keyboard *keyboard =
945 weston_seat_get_keyboard(seat);
946
Neil Roberts4237f502014-04-09 16:33:31 +0100947 wl_list_insert(&shell->compositor->seat_list, &seat->link);
948
Derek Foreman1281a362015-07-31 16:55:32 -0500949 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100950 continue;
951
Derek Foreman1281a362015-07-31 16:55:32 -0500952 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100953 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200954}
955
956static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200957replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
958 struct weston_seat *seat)
959{
Derek Foreman1281a362015-07-31 16:55:32 -0500960 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200961 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200962
963 wl_list_for_each(state, &ws->focus_list, link) {
964 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500965 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200966 return;
967 }
968 }
969}
970
971static void
972drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
973 struct weston_surface *surface)
974{
975 struct focus_state *state;
976
977 wl_list_for_each(state, &ws->focus_list, link)
978 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800979 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200980}
981
982static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
984 struct weston_view *from, struct weston_view *to)
985{
986 struct weston_output *output;
987 bool focus_surface_created = false;
988
989 /* FIXME: Only support dim animation using two layers */
990 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
991 return;
992
993 output = get_default_output(shell->compositor);
994 if (ws->fsurf_front == NULL && (from || to)) {
995 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800996 if (ws->fsurf_front == NULL)
997 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800999
1000 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1001 if (ws->fsurf_back == NULL) {
1002 focus_surface_destroy(ws->fsurf_front);
1003 return;
1004 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001005 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001006
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001007 focus_surface_created = true;
1008 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001009 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1010 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001011 }
1012
1013 if (ws->focus_animation) {
1014 weston_view_animation_destroy(ws->focus_animation);
1015 ws->focus_animation = NULL;
1016 }
1017
1018 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001019 weston_layer_entry_insert(&to->layer_link,
1020 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001021 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001022 weston_layer_entry_insert(&ws->layer.view_list,
1023 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001024
1025 if (focus_surface_created) {
1026 ws->focus_animation = weston_fade_run(
1027 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001028 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001029 focus_animation_done, ws);
1030 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001031 weston_layer_entry_insert(&from->layer_link,
1032 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001033 ws->focus_animation = weston_stable_fade_run(
1034 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001035 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001036 focus_animation_done, ws);
1037 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001038 weston_layer_entry_insert(&ws->layer.view_list,
1039 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001040 ws->focus_animation = weston_stable_fade_run(
1041 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001042 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001043 focus_animation_done, ws);
1044 }
1045}
1046
1047static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001048workspace_destroy(struct workspace *ws)
1049{
Jonas Ådahl04769742012-06-13 00:01:24 +02001050 struct focus_state *state, *next;
1051
1052 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1053 focus_state_destroy(state);
1054
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001055 if (ws->fsurf_front)
1056 focus_surface_destroy(ws->fsurf_front);
1057 if (ws->fsurf_back)
1058 focus_surface_destroy(ws->fsurf_back);
1059
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001060 free(ws);
1061}
1062
Jonas Ådahl04769742012-06-13 00:01:24 +02001063static void
1064seat_destroyed(struct wl_listener *listener, void *data)
1065{
1066 struct weston_seat *seat = data;
1067 struct focus_state *state, *next;
1068 struct workspace *ws = container_of(listener,
1069 struct workspace,
1070 seat_destroyed_listener);
1071
1072 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1073 if (state->seat == seat)
1074 wl_list_remove(&state->link);
1075}
1076
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001077static struct workspace *
1078workspace_create(void)
1079{
1080 struct workspace *ws = malloc(sizeof *ws);
1081 if (ws == NULL)
1082 return NULL;
1083
1084 weston_layer_init(&ws->layer, NULL);
1085
Jonas Ådahl04769742012-06-13 00:01:24 +02001086 wl_list_init(&ws->focus_list);
1087 wl_list_init(&ws->seat_destroyed_listener.link);
1088 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001089 ws->fsurf_front = NULL;
1090 ws->fsurf_back = NULL;
1091 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001092
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001093 return ws;
1094}
1095
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096static int
1097workspace_is_empty(struct workspace *ws)
1098{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001099 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100}
1101
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001102static struct workspace *
1103get_workspace(struct desktop_shell *shell, unsigned int index)
1104{
1105 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001106 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001107 pws += index;
1108 return *pws;
1109}
1110
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001111struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001112get_current_workspace(struct desktop_shell *shell)
1113{
1114 return get_workspace(shell, shell->workspaces.current);
1115}
1116
1117static void
1118activate_workspace(struct desktop_shell *shell, unsigned int index)
1119{
1120 struct workspace *ws;
1121
1122 ws = get_workspace(shell, index);
1123 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1124
1125 shell->workspaces.current = index;
1126}
1127
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128static unsigned int
1129get_output_height(struct weston_output *output)
1130{
1131 return abs(output->region.extents.y1 - output->region.extents.y2);
1132}
1133
1134static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001135view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137 struct weston_transform *transform;
1138
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001139 if (is_focus_view(view)) {
1140 struct focus_surface *fsurf = get_focus_surface(view->surface);
1141 transform = &fsurf->workspace_transform;
1142 } else {
1143 struct shell_surface *shsurf = get_shell_surface(view->surface);
1144 transform = &shsurf->workspace_transform;
1145 }
1146
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001149 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001151 weston_matrix_init(&transform->matrix);
1152 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001153 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155}
1156
1157static void
1158workspace_translate_out(struct workspace *ws, double fraction)
1159{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161 unsigned int height;
1162 double d;
1163
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001164 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166 d = height * fraction;
1167
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001168 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001169 }
1170}
1171
1172static void
1173workspace_translate_in(struct workspace *ws, double fraction)
1174{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176 unsigned int height;
1177 double d;
1178
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001179 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001181
1182 if (fraction > 0)
1183 d = -(height - height * fraction);
1184 else
1185 d = height + height * fraction;
1186
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001187 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001188 }
1189}
1190
1191static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192reverse_workspace_change_animation(struct desktop_shell *shell,
1193 unsigned int index,
1194 struct workspace *from,
1195 struct workspace *to)
1196{
1197 shell->workspaces.current = index;
1198
1199 shell->workspaces.anim_to = to;
1200 shell->workspaces.anim_from = from;
1201 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1202 shell->workspaces.anim_timestamp = 0;
1203
Scott Moreau4272e632012-08-13 09:58:41 -06001204 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001205}
1206
1207static void
1208workspace_deactivate_transforms(struct workspace *ws)
1209{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001211 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001212
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001213 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001214 if (is_focus_view(view)) {
1215 struct focus_surface *fsurf = get_focus_surface(view->surface);
1216 transform = &fsurf->workspace_transform;
1217 } else {
1218 struct shell_surface *shsurf = get_shell_surface(view->surface);
1219 transform = &shsurf->workspace_transform;
1220 }
1221
1222 if (!wl_list_empty(&transform->link)) {
1223 wl_list_remove(&transform->link);
1224 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001225 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001227 }
1228}
1229
1230static void
1231finish_workspace_change_animation(struct desktop_shell *shell,
1232 struct workspace *from,
1233 struct workspace *to)
1234{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001235 struct weston_view *view;
1236
Scott Moreau4272e632012-08-13 09:58:41 -06001237 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001238
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001239 /* Views that extend past the bottom of the output are still
1240 * visible after the workspace animation ends but before its layer
1241 * is hidden. In that case, we need to damage below those views so
1242 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001243 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001244 weston_view_damage_below(view);
1245
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001246 wl_list_remove(&shell->workspaces.animation.link);
1247 workspace_deactivate_transforms(from);
1248 workspace_deactivate_transforms(to);
1249 shell->workspaces.anim_to = NULL;
1250
1251 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1252}
1253
1254static void
1255animate_workspace_change_frame(struct weston_animation *animation,
1256 struct weston_output *output, uint32_t msecs)
1257{
1258 struct desktop_shell *shell =
1259 container_of(animation, struct desktop_shell,
1260 workspaces.animation);
1261 struct workspace *from = shell->workspaces.anim_from;
1262 struct workspace *to = shell->workspaces.anim_to;
1263 uint32_t t;
1264 double x, y;
1265
1266 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1267 finish_workspace_change_animation(shell, from, to);
1268 return;
1269 }
1270
1271 if (shell->workspaces.anim_timestamp == 0) {
1272 if (shell->workspaces.anim_current == 0.0)
1273 shell->workspaces.anim_timestamp = msecs;
1274 else
1275 shell->workspaces.anim_timestamp =
1276 msecs -
1277 /* Invers of movement function 'y' below. */
1278 (asin(1.0 - shell->workspaces.anim_current) *
1279 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1280 M_2_PI);
1281 }
1282
1283 t = msecs - shell->workspaces.anim_timestamp;
1284
1285 /*
1286 * x = [0, π/2]
1287 * y(x) = sin(x)
1288 */
1289 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1290 y = sin(x);
1291
1292 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001293 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001294
1295 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1296 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1297 shell->workspaces.anim_current = y;
1298
Scott Moreau4272e632012-08-13 09:58:41 -06001299 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001300 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001301 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001302 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001303}
1304
1305static void
1306animate_workspace_change(struct desktop_shell *shell,
1307 unsigned int index,
1308 struct workspace *from,
1309 struct workspace *to)
1310{
1311 struct weston_output *output;
1312
1313 int dir;
1314
1315 if (index > shell->workspaces.current)
1316 dir = -1;
1317 else
1318 dir = 1;
1319
1320 shell->workspaces.current = index;
1321
1322 shell->workspaces.anim_dir = dir;
1323 shell->workspaces.anim_from = from;
1324 shell->workspaces.anim_to = to;
1325 shell->workspaces.anim_current = 0.0;
1326 shell->workspaces.anim_timestamp = 0;
1327
1328 output = container_of(shell->compositor->output_list.next,
1329 struct weston_output, link);
1330 wl_list_insert(&output->animation_list,
1331 &shell->workspaces.animation.link);
1332
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001333 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001334
1335 workspace_translate_in(to, 0);
1336
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001337 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001338
Scott Moreau4272e632012-08-13 09:58:41 -06001339 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001340}
1341
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001342static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001343update_workspace(struct desktop_shell *shell, unsigned int index,
1344 struct workspace *from, struct workspace *to)
1345{
1346 shell->workspaces.current = index;
1347 wl_list_insert(&from->layer.link, &to->layer.link);
1348 wl_list_remove(&from->layer.link);
1349}
1350
1351static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001352change_workspace(struct desktop_shell *shell, unsigned int index)
1353{
1354 struct workspace *from;
1355 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001356 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001357
1358 if (index == shell->workspaces.current)
1359 return;
1360
1361 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001362 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001363 return;
1364
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001365 from = get_current_workspace(shell);
1366 to = get_workspace(shell, index);
1367
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001368 if (shell->workspaces.anim_from == to &&
1369 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001370 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001371 reverse_workspace_change_animation(shell, index, from, to);
1372 return;
1373 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001374
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001375 if (shell->workspaces.anim_to != NULL)
1376 finish_workspace_change_animation(shell,
1377 shell->workspaces.anim_from,
1378 shell->workspaces.anim_to);
1379
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001380 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001381
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001382 if (shell->focus_animation_type != ANIMATION_NONE) {
1383 wl_list_for_each(state, &from->focus_list, link)
1384 if (state->keyboard_focus)
1385 animate_focus_change(shell, from,
1386 get_default_view(state->keyboard_focus), NULL);
1387
1388 wl_list_for_each(state, &to->focus_list, link)
1389 if (state->keyboard_focus)
1390 animate_focus_change(shell, to,
1391 NULL, get_default_view(state->keyboard_focus));
1392 }
1393
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001394 if (workspace_is_empty(to) && workspace_is_empty(from))
1395 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001396 else
1397 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001398}
1399
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001400static bool
1401workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1402{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001403 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001404 struct wl_list *e;
1405
1406 if (wl_list_empty(list))
1407 return false;
1408
1409 e = list->next;
1410
1411 if (e->next != list)
1412 return false;
1413
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001414 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001415}
1416
1417static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001418surface_keyboard_focus_lost(struct weston_surface *surface)
1419{
1420 struct weston_compositor *compositor = surface->compositor;
1421 struct weston_seat *seat;
1422 struct weston_surface *focus;
1423
1424 wl_list_for_each(seat, &compositor->seat_list, link) {
1425 struct weston_keyboard *keyboard =
1426 weston_seat_get_keyboard(seat);
1427
1428 if (!keyboard)
1429 continue;
1430
1431 focus = weston_surface_get_main_surface(keyboard->focus);
1432 if (focus == surface)
1433 weston_keyboard_set_focus(keyboard, NULL);
1434 }
1435}
1436
1437static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001438take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001439 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001440 unsigned int index)
1441{
Derek Foreman1281a362015-07-31 16:55:32 -05001442 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001443 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001445 struct shell_surface *shsurf;
1446 struct workspace *from;
1447 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001448 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001449
Derek Foreman1281a362015-07-31 16:55:32 -05001450 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 view = get_default_view(surface);
1452 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001453 index == shell->workspaces.current ||
1454 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001455 return;
1456
1457 from = get_current_workspace(shell);
1458 to = get_workspace(shell, index);
1459
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001460 weston_layer_entry_remove(&view->layer_link);
1461 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001462
Philip Withnall659163d2013-11-25 18:01:36 +00001463 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001464 if (shsurf != NULL)
1465 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001466
Jonas Ådahle9d22502012-08-29 22:13:01 +02001467 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001468 drop_focus_state(shell, from, surface);
1469
1470 if (shell->workspaces.anim_from == to &&
1471 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001472 wl_list_remove(&to->layer.link);
1473 wl_list_insert(from->layer.link.prev, &to->layer.link);
1474
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001475 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001476
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001477 return;
1478 }
1479
1480 if (shell->workspaces.anim_to != NULL)
1481 finish_workspace_change_animation(shell,
1482 shell->workspaces.anim_from,
1483 shell->workspaces.anim_to);
1484
1485 if (workspace_is_empty(from) &&
1486 workspace_has_only(to, surface))
1487 update_workspace(shell, index, from, to);
1488 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001489 if (shsurf != NULL &&
1490 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001491 wl_list_insert(&shell->workspaces.anim_sticky_list,
1492 &shsurf->workspace_transform.link);
1493
1494 animate_workspace_change(shell, index, from, to);
1495 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001496
Jonas Ådahl8538b222012-08-29 22:13:03 +02001497 state = ensure_focus_state(shell, seat);
1498 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001499 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001500}
1501
1502static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001503touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001504 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001505{
1506}
1507
1508static void
1509touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1510{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001511 struct weston_touch_move_grab *move =
1512 (struct weston_touch_move_grab *) container_of(
1513 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001514
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001515 if (touch_id == 0)
1516 move->active = 0;
1517
Jonas Ådahl9484b692013-12-02 22:05:03 +01001518 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001519 shell_touch_grab_end(&move->base);
1520 free(move);
1521 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001522}
1523
1524static void
1525touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001526 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001527{
1528 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1529 struct shell_surface *shsurf = move->base.shsurf;
1530 struct weston_surface *es;
1531 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1532 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1533
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001534 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001535 return;
1536
1537 es = shsurf->surface;
1538
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001539 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001540
1541 weston_compositor_schedule_repaint(es->compositor);
1542}
1543
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001544static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001545touch_move_grab_frame(struct weston_touch_grab *grab)
1546{
1547}
1548
1549static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001550touch_move_grab_cancel(struct weston_touch_grab *grab)
1551{
1552 struct weston_touch_move_grab *move =
1553 (struct weston_touch_move_grab *) container_of(
1554 grab, struct shell_touch_grab, grab);
1555
1556 shell_touch_grab_end(&move->base);
1557 free(move);
1558}
1559
Rusty Lynch1084da52013-08-15 09:10:08 -07001560static const struct weston_touch_grab_interface touch_move_grab_interface = {
1561 touch_move_grab_down,
1562 touch_move_grab_up,
1563 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001564 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001565 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001566};
1567
1568static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001569surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001570{
1571 struct weston_touch_move_grab *move;
1572
1573 if (!shsurf)
1574 return -1;
1575
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001576 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001577 return 0;
1578
1579 move = malloc(sizeof *move);
1580 if (!move)
1581 return -1;
1582
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001583 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001584 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001585 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001587 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001588
1589 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001590 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001591
1592 return 0;
1593}
1594
1595static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001596noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001597{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001598}
1599
1600static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001601noop_grab_axis(struct weston_pointer_grab *grab,
1602 uint32_t time, uint32_t axis, wl_fixed_t value)
1603{
1604}
1605
1606static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001607constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1608{
1609 struct shell_surface *shsurf = move->base.shsurf;
1610 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001611 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001612 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001613 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001614
1615 x = wl_fixed_to_int(pointer->x + move->dx);
1616 y = wl_fixed_to_int(pointer->y + move->dy);
1617
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001618 if (shsurf->shell->panel_position ==
1619 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001620 get_output_work_area(shsurf->shell,
1621 shsurf->surface->output,
1622 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001623
Derek Foreman6feb0f92015-04-15 12:23:56 -05001624 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1625 if (bottom - safety < area.y)
1626 y = area.y + safety - shsurf->geometry.height
1627 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001628
1629 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001630 y + shsurf->geometry.y < area.y)
1631 y = area.y - shsurf->geometry.y;
1632
Jonny Lambd73c6942014-08-20 15:53:20 +02001633 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001634
1635 *cx = x;
1636 *cy = y;
1637}
1638
1639static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001640move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001641 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001642{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001643 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001644 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001645 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001646 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001647
Jonas Ådahld2510102014-10-05 21:39:14 +02001648 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001649 if (!shsurf)
1650 return;
1651
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001652 constrain_position(move, &cx, &cy);
1653
1654 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001655
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001657}
1658
1659static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001660move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001661 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001662{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001663 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1664 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001665 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001666 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001667
Daniel Stone4dbadb12012-05-30 16:31:51 +01001668 if (pointer->button_count == 0 &&
1669 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001670 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001671 free(grab);
1672 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001673}
1674
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001675static void
1676move_grab_cancel(struct weston_pointer_grab *grab)
1677{
1678 struct shell_grab *shell_grab =
1679 container_of(grab, struct shell_grab, grab);
1680
1681 shell_grab_end(shell_grab);
1682 free(grab);
1683}
1684
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001685static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001686 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001687 move_grab_motion,
1688 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001689 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001690 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001691};
1692
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001693static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001694surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001695 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696{
1697 struct weston_move_grab *move;
1698
1699 if (!shsurf)
1700 return -1;
1701
Derek Foreman45a7c272015-09-11 14:27:40 -05001702 shsurf = find_toplevel_surface(shsurf);
1703
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001704 if (shsurf->grabbed ||
1705 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706 return 0;
1707
1708 move = malloc(sizeof *move);
1709 if (!move)
1710 return -1;
1711
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001713 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001715 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001716 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717
1718 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001719 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001720
1721 return 0;
1722}
1723
1724static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001725common_surface_move(struct wl_resource *resource,
1726 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001728 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001729 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1730 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001731 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001732 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733
Derek Foreman1281a362015-07-31 16:55:32 -05001734 if (pointer &&
1735 pointer->focus &&
1736 pointer->button_count > 0 &&
1737 pointer->grab_serial == serial) {
1738 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001739 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001740 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001741 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001742 } else if (touch &&
1743 touch->focus &&
1744 touch->grab_serial == serial) {
1745 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001746 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001747 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001748 wl_resource_post_no_memory(resource);
1749 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750}
1751
Rafael Antognollie2a34552013-12-03 15:35:45 -02001752static void
1753shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1754 struct wl_resource *seat_resource, uint32_t serial)
1755{
1756 common_surface_move(resource, seat_resource, serial);
1757}
1758
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001759struct weston_resize_grab {
1760 struct shell_grab base;
1761 uint32_t edges;
1762 int32_t width, height;
1763};
1764
1765static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001766resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001767 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001768{
1769 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001770 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771 struct shell_surface *shsurf = resize->base.shsurf;
1772 int32_t width, height;
1773 wl_fixed_t from_x, from_y;
1774 wl_fixed_t to_x, to_y;
1775
Jonas Ådahld2510102014-10-05 21:39:14 +02001776 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001777
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001778 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001779 return;
1780
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 weston_view_from_global_fixed(shsurf->view,
1782 pointer->grab_x, pointer->grab_y,
1783 &from_x, &from_y);
1784 weston_view_from_global_fixed(shsurf->view,
1785 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786
1787 width = resize->width;
1788 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1789 width += wl_fixed_to_int(from_x - to_x);
1790 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1791 width += wl_fixed_to_int(to_x - from_x);
1792 }
1793
1794 height = resize->height;
1795 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1796 height += wl_fixed_to_int(from_y - to_y);
1797 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1798 height += wl_fixed_to_int(to_y - from_y);
1799 }
1800
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001801 if (width < 1)
1802 width = 1;
1803 if (height < 1)
1804 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001805 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001806}
1807
1808static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001809send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001810{
1811 struct shell_surface *shsurf = get_shell_surface(surface);
1812
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001813 assert(shsurf);
1814
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001815 if (shsurf->resource)
1816 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001817 shsurf->resize_edges,
1818 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001819}
1820
1821static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001822 send_configure,
1823 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001824};
1825
1826static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001827resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001828 uint32_t time, uint32_t button, uint32_t state_w)
1829{
1830 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001831 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001832 enum wl_pointer_button_state state = state_w;
1833
1834 if (pointer->button_count == 0 &&
1835 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1836 shell_grab_end(&resize->base);
1837 free(grab);
1838 }
1839}
1840
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001841static void
1842resize_grab_cancel(struct weston_pointer_grab *grab)
1843{
1844 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1845
1846 shell_grab_end(&resize->base);
1847 free(grab);
1848}
1849
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001850static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001851 noop_grab_focus,
1852 resize_grab_motion,
1853 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001854 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001855 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001856};
1857
Giulio Camuffob8366642013-04-25 13:57:46 +03001858/*
1859 * Returns the bounding box of a surface and all its sub-surfaces,
1860 * in the surface coordinates system. */
1861static void
1862surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1863 int32_t *y, int32_t *w, int32_t *h) {
1864 pixman_region32_t region;
1865 pixman_box32_t *box;
1866 struct weston_subsurface *subsurface;
1867
1868 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 surface->width,
1870 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001871
1872 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1873 pixman_region32_union_rect(&region, &region,
1874 subsurface->position.x,
1875 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001876 subsurface->surface->width,
1877 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001878 }
1879
1880 box = pixman_region32_extents(&region);
1881 if (x)
1882 *x = box->x1;
1883 if (y)
1884 *y = box->y1;
1885 if (w)
1886 *w = box->x2 - box->x1;
1887 if (h)
1888 *h = box->y2 - box->y1;
1889
1890 pixman_region32_fini(&region);
1891}
1892
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001893static int
1894surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001895 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001896{
1897 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001898 const unsigned resize_topbottom =
1899 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1900 const unsigned resize_leftright =
1901 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1902 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001903
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001904 if (shsurf->grabbed ||
1905 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001906 return 0;
1907
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001908 /* Check for invalid edge combinations. */
1909 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1910 (edges & resize_topbottom) == resize_topbottom ||
1911 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001912 return 0;
1913
1914 resize = malloc(sizeof *resize);
1915 if (!resize)
1916 return -1;
1917
1918 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001919
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001920 resize->width = shsurf->geometry.width;
1921 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001922
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001923 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001924 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001925 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001926 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001927
1928 return 0;
1929}
1930
1931static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001932common_surface_resize(struct wl_resource *resource,
1933 struct wl_resource *seat_resource, uint32_t serial,
1934 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001935{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001936 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001937 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001938 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001939 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001940
Derek Foreman1281a362015-07-31 16:55:32 -05001941 if (!pointer ||
1942 pointer->button_count == 0 ||
1943 pointer->grab_serial != serial ||
1944 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001945 return;
1946
Derek Foreman1281a362015-07-31 16:55:32 -05001947 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001948 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001949 return;
1950
Derek Foreman1281a362015-07-31 16:55:32 -05001951 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001952 wl_resource_post_no_memory(resource);
1953}
1954
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001955static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001956shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1957 struct wl_resource *seat_resource, uint32_t serial,
1958 uint32_t edges)
1959{
1960 common_surface_resize(resource, seat_resource, serial, edges);
1961}
1962
1963static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001964busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001965{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001966 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001967 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001968 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001969 wl_fixed_t sx, sy;
1970
Jason Ekstranda7af7042013-10-12 22:38:11 -05001971 view = weston_compositor_pick_view(pointer->seat->compositor,
1972 pointer->x, pointer->y,
1973 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001974
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001975 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1976 shell_grab_end(grab);
1977 free(grab);
1978 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001979}
1980
1981static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001982busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001983 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001984{
Jonas Ådahld2510102014-10-05 21:39:14 +02001985 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001986}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001987
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001988static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001989busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001990 uint32_t time, uint32_t button, uint32_t state)
1991{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001992 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001993 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001994 struct weston_pointer *pointer = grab->grab.pointer;
1995 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001996
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001997 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001998 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001999 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002000 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002001 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002002 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002003 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002004}
2005
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002006static void
2007busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2008{
2009 struct shell_grab *grab = (struct shell_grab *) base;
2010
2011 shell_grab_end(grab);
2012 free(grab);
2013}
2014
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002015static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002016 busy_cursor_grab_focus,
2017 busy_cursor_grab_motion,
2018 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002019 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002020 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002021};
2022
2023static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002024set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002025{
2026 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002027
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002028 if (pointer->grab->interface == &busy_cursor_grab_interface)
2029 return;
2030
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002031 grab = malloc(sizeof *grab);
2032 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002033 return;
2034
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002035 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002036 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002037 /* Mark the shsurf as ungrabbed so that button binding is able
2038 * to move it. */
2039 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002041
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002042static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002043end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002044{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002045 struct shell_grab *grab;
2046 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002048 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002049 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2050
2051 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002052 continue;
2053
Derek Foreman1281a362015-07-31 16:55:32 -05002054 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002055 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002056 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002057 wl_resource_get_client(grab->shsurf->resource) == client) {
2058 shell_grab_end(grab);
2059 free(grab);
2060 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002061 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002062}
2063
Scott Moreau9521d5e2012-04-19 13:06:17 -06002064static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002065handle_shell_client_destroy(struct wl_listener *listener, void *data);
2066
2067static int
2068xdg_ping_timeout_handler(void *data)
2069{
2070 struct shell_client *sc = data;
2071 struct weston_seat *seat;
2072 struct shell_surface *shsurf;
2073
2074 /* Client is not responding */
2075 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002076 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002077 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2078
2079 if (!pointer ||
2080 !pointer->focus ||
2081 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002082 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002083
Derek Foreman1281a362015-07-31 16:55:32 -05002084 shsurf = get_shell_surface(pointer->focus->surface);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002085 if (shsurf &&
2086 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002087 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002088 }
2089
2090 return 1;
2091}
2092
2093static void
2094handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2095{
2096 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002097 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002098 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002099 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002100
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002101 if (sc->unresponsive) {
2102 xdg_ping_timeout_handler(sc);
2103 return;
2104 }
2105
2106 sc->ping_serial = serial;
2107 loop = wl_display_get_event_loop(compositor->wl_display);
2108 if (sc->ping_timer == NULL)
2109 sc->ping_timer =
2110 wl_event_loop_add_timer(loop,
2111 xdg_ping_timeout_handler, sc);
2112 if (sc->ping_timer == NULL)
2113 return;
2114
2115 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2116
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002117 if (shell_surface_is_xdg_surface(shsurf) ||
2118 shell_surface_is_xdg_popup(shsurf))
2119 xdg_shell_send_ping(sc->resource, serial);
2120 else if (shell_surface_is_wl_shell_surface(shsurf))
2121 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002122}
2123
Scott Moreauff1db4a2012-04-17 19:06:18 -06002124static void
2125ping_handler(struct weston_surface *surface, uint32_t serial)
2126{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002127 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002128
2129 if (!shsurf)
2130 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002131 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002132 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002133 if (shsurf->surface == shsurf->shell->grab_surface)
2134 return;
2135
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002136 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002137}
2138
2139static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002140handle_pointer_focus(struct wl_listener *listener, void *data)
2141{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002142 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002144 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002145 uint32_t serial;
2146
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002148 return;
2149
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002151 serial = wl_display_next_serial(compositor->wl_display);
2152 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002153}
2154
2155static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002156shell_surface_lose_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
2163shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2164{
2165 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002166 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002167}
2168
2169static void
2170handle_keyboard_focus(struct wl_listener *listener, void *data)
2171{
2172 struct weston_keyboard *keyboard = data;
2173 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2174
2175 if (seat->focused_surface) {
2176 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2177 if (shsurf)
2178 shell_surface_lose_keyboard_focus(shsurf);
2179 }
2180
2181 seat->focused_surface = keyboard->focus;
2182
2183 if (seat->focused_surface) {
2184 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2185 if (shsurf)
2186 shell_surface_gain_keyboard_focus(shsurf);
2187 }
2188}
2189
2190static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002191shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002192{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002193 if (sc->ping_serial != serial)
2194 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002195
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002196 sc->unresponsive = 0;
2197 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002198
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002199 if (sc->ping_timer) {
2200 wl_event_source_remove(sc->ping_timer);
2201 sc->ping_timer = NULL;
2202 }
2203
2204}
2205
2206static void
2207shell_surface_pong(struct wl_client *client,
2208 struct wl_resource *resource, uint32_t serial)
2209{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002210 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2211 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002212
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002213 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002214}
2215
2216static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002217set_title(struct shell_surface *shsurf, const char *title)
2218{
2219 free(shsurf->title);
2220 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002221 shsurf->surface->timeline.force_refresh = 1;
2222}
2223
2224static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002225set_pid(struct shell_surface *shsurf, pid_t pid)
2226{
2227 /* We have no use for it */
2228}
2229
2230static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002231set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2232{
2233 shsurf->type = t;
2234 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002235}
2236
2237static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002238set_window_geometry(struct shell_surface *shsurf,
2239 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002240{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002241 shsurf->next_geometry.x = x;
2242 shsurf->next_geometry.y = y;
2243 shsurf->next_geometry.width = width;
2244 shsurf->next_geometry.height = height;
2245 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002246}
2247
2248static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002249shell_surface_set_title(struct wl_client *client,
2250 struct wl_resource *resource, const char *title)
2251{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002252 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002253
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002254 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002255}
2256
2257static void
2258shell_surface_set_class(struct wl_client *client,
2259 struct wl_resource *resource, const char *class)
2260{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002261 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002262
2263 free(shsurf->class);
2264 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002265 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002266}
2267
Alex Wu4539b082012-03-01 12:57:46 +08002268static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002269restore_output_mode(struct weston_output *output)
2270{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002271 if (output->original_mode)
2272 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002273}
2274
2275static void
2276restore_all_output_modes(struct weston_compositor *compositor)
2277{
2278 struct weston_output *output;
2279
2280 wl_list_for_each(output, &compositor->output_list, link)
2281 restore_output_mode(output);
2282}
2283
Philip Withnall07926d92013-11-25 18:01:40 +00002284/* The surface will be inserted into the list immediately after the link
2285 * returned by this function (i.e. will be stacked immediately above the
2286 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002287static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002288shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2289{
2290 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002291 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002292
2293 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002294 case SHELL_SURFACE_XWAYLAND:
2295 return &shsurf->shell->fullscreen_layer.view_list;
2296
2297 case SHELL_SURFACE_NONE:
2298 return NULL;
2299
Kristian Høgsbergead52422014-01-01 13:51:52 -08002300 case SHELL_SURFACE_POPUP:
2301 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002302 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002303 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002304 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002305 /* Move the surface to its parent layer so
2306 * that surfaces which are transient for
2307 * fullscreen surfaces don't get hidden by the
2308 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002309
2310 /* TODO: Handle a parent with multiple views */
2311 parent = get_default_view(shsurf->parent);
2312 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002313 return container_of(parent->layer_link.link.prev,
2314 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002315 }
Philip Withnall07926d92013-11-25 18:01:40 +00002316
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002317 /* Move the surface to a normal workspace layer so that surfaces
2318 * which were previously fullscreen or transient are no longer
2319 * rendered on top. */
2320 ws = get_current_workspace(shsurf->shell);
2321 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002322 }
2323
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002324 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002325}
2326
Philip Withnall648a4dd2013-11-25 18:01:44 +00002327static void
2328shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2329{
2330 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002331 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002332
2333 /* Move the child layers to the same workspace as shsurf. They will be
2334 * stacked above shsurf. */
2335 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002336 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002337 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002338 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002339 prev = container_of(shsurf->view->layer_link.link.prev,
2340 struct weston_layer_entry, link);
2341 weston_layer_entry_remove(&child->view->layer_link);
2342 weston_layer_entry_insert(prev,
2343 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002344 weston_view_geometry_dirty(child->view);
2345 weston_surface_damage(child->surface);
2346
2347 /* Recurse. We don’t expect this to recurse very far (if
2348 * at all) because that would imply we have transient
2349 * (or popup) children of transient surfaces, which
2350 * would be unusual. */
2351 shell_surface_update_child_surface_layers(child);
2352 }
2353 }
2354}
2355
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002356/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002357 * geometry to ensure the changes are redrawn.
2358 *
2359 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2360 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002361static void
2362shell_surface_update_layer(struct shell_surface *shsurf)
2363{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002364 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002365
2366 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2367
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002368 if (new_layer_link == NULL)
2369 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002370 if (new_layer_link == &shsurf->view->layer_link)
2371 return;
2372
2373 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374 weston_layer_entry_remove(&shsurf->view->layer_link);
2375 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002376 weston_view_geometry_dirty(shsurf->view);
2377 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002378
2379 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002380}
2381
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002382static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002383shell_surface_set_parent(struct shell_surface *shsurf,
2384 struct weston_surface *parent)
2385{
2386 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002387
2388 wl_list_remove(&shsurf->children_link);
2389 wl_list_init(&shsurf->children_link);
2390
2391 /* Insert into the parent surface’s child list. */
2392 if (parent != NULL) {
2393 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2394 if (parent_shsurf != NULL)
2395 wl_list_insert(&parent_shsurf->children_list,
2396 &shsurf->children_link);
2397 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002398}
2399
2400static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002401shell_surface_set_output(struct shell_surface *shsurf,
2402 struct weston_output *output)
2403{
2404 struct weston_surface *es = shsurf->surface;
2405
2406 /* get the default output, if the client set it as NULL
2407 check whether the ouput is available */
2408 if (output)
2409 shsurf->output = output;
2410 else if (es->output)
2411 shsurf->output = es->output;
2412 else
2413 shsurf->output = get_default_output(es->compositor);
2414}
2415
2416static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002417surface_clear_next_states(struct shell_surface *shsurf)
2418{
2419 shsurf->next_state.maximized = false;
2420 shsurf->next_state.fullscreen = false;
2421
2422 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2423 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2424 shsurf->state_changed = true;
2425}
2426
2427static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002428set_toplevel(struct shell_surface *shsurf)
2429{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002430 shell_surface_set_parent(shsurf, NULL);
2431 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002432 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002433
2434 /* The layer_link is updated in set_surface_type(),
2435 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002436}
2437
2438static void
2439shell_surface_set_toplevel(struct wl_client *client,
2440 struct wl_resource *resource)
2441{
2442 struct shell_surface *surface = wl_resource_get_user_data(resource);
2443
2444 set_toplevel(surface);
2445}
2446
2447static void
2448set_transient(struct shell_surface *shsurf,
2449 struct weston_surface *parent, int x, int y, uint32_t flags)
2450{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002451 assert(parent != NULL);
2452
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002453 shell_surface_set_parent(shsurf, parent);
2454
2455 surface_clear_next_states(shsurf);
2456
Philip Withnallbecb77e2013-11-25 18:01:30 +00002457 shsurf->transient.x = x;
2458 shsurf->transient.y = y;
2459 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002460
Rafael Antognollied207b42013-12-03 15:35:43 -02002461 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002462 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002463 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002464
2465 /* The layer_link is updated in set_surface_type(),
2466 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002467}
2468
2469static void
2470shell_surface_set_transient(struct wl_client *client,
2471 struct wl_resource *resource,
2472 struct wl_resource *parent_resource,
2473 int x, int y, uint32_t flags)
2474{
2475 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2476 struct weston_surface *parent =
2477 wl_resource_get_user_data(parent_resource);
2478
2479 set_transient(shsurf, parent, x, y, flags);
2480}
2481
2482static void
2483set_fullscreen(struct shell_surface *shsurf,
2484 uint32_t method,
2485 uint32_t framerate,
2486 struct weston_output *output)
2487{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002488 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002489 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002490
2491 shsurf->fullscreen_output = shsurf->output;
2492 shsurf->fullscreen.type = method;
2493 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002494
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002495 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002496}
2497
2498static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002499weston_view_set_initial_position(struct weston_view *view,
2500 struct desktop_shell *shell);
2501
2502static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002503unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002504{
Philip Withnallf85fe842013-11-25 18:01:37 +00002505 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002506 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2507 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002508 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002509 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002510
Alex Wu4539b082012-03-01 12:57:46 +08002511 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2512 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002513
Alex Wu4539b082012-03-01 12:57:46 +08002514 wl_list_remove(&shsurf->fullscreen.transform.link);
2515 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002516
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 if (shsurf->fullscreen.black_view)
2518 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2519 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002520
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002521 if (shsurf->saved_position_valid)
2522 weston_view_set_position(shsurf->view,
2523 shsurf->saved_x, shsurf->saved_y);
2524 else
2525 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2526
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002527 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002528 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002529 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002530 shsurf->saved_rotation_valid = false;
2531 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002532
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002533 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002534}
2535
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002536static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002537shell_surface_set_fullscreen(struct wl_client *client,
2538 struct wl_resource *resource,
2539 uint32_t method,
2540 uint32_t framerate,
2541 struct wl_resource *output_resource)
2542{
2543 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2544 struct weston_output *output;
2545
2546 if (output_resource)
2547 output = wl_resource_get_user_data(output_resource);
2548 else
2549 output = NULL;
2550
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002551 shell_surface_set_parent(shsurf, NULL);
2552
Rafael Antognolli03b16592013-12-03 15:35:42 -02002553 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002554 shsurf->next_state.fullscreen = true;
2555 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002556 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002557}
2558
2559static void
2560set_popup(struct shell_surface *shsurf,
2561 struct weston_surface *parent,
2562 struct weston_seat *seat,
2563 uint32_t serial,
2564 int32_t x,
2565 int32_t y)
2566{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002567 assert(parent != NULL);
2568
Philip Withnallbecb77e2013-11-25 18:01:30 +00002569 shsurf->popup.shseat = get_shell_seat(seat);
2570 shsurf->popup.serial = serial;
2571 shsurf->popup.x = x;
2572 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002573
Pekka Paalanenb5026542014-11-12 15:09:24 +02002574 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002575}
2576
2577static void
2578shell_surface_set_popup(struct wl_client *client,
2579 struct wl_resource *resource,
2580 struct wl_resource *seat_resource,
2581 uint32_t serial,
2582 struct wl_resource *parent_resource,
2583 int32_t x, int32_t y, uint32_t flags)
2584{
2585 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002586 struct weston_surface *parent =
2587 wl_resource_get_user_data(parent_resource);
2588
2589 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002590
Rafael Antognolli03b16592013-12-03 15:35:42 -02002591 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002593 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002594 wl_resource_get_user_data(seat_resource),
2595 serial, x, y);
2596}
2597
2598static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002599unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002600{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002601 /* undo all maximized things here */
2602 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002603
2604 if (shsurf->saved_position_valid)
2605 weston_view_set_position(shsurf->view,
2606 shsurf->saved_x, shsurf->saved_y);
2607 else
2608 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002609
2610 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002611 wl_list_insert(&shsurf->view->geometry.transformation_list,
2612 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002613 shsurf->saved_rotation_valid = false;
2614 }
2615
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002616 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002617}
2618
Philip Withnallbecb77e2013-11-25 18:01:30 +00002619static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002620set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002621{
2622 struct shell_surface *shsurf;
2623 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002624 struct weston_view *view;
2625
2626 view = get_default_view(surface);
2627 if (!view)
2628 return;
2629
2630 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2631
2632 shsurf = get_shell_surface(surface);
2633 current_ws = get_current_workspace(shsurf->shell);
2634
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002635 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002636 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002637
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002638 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002639 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002640
2641 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002642 weston_view_damage_below(view);
2643}
2644
2645static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002646shell_surface_set_maximized(struct wl_client *client,
2647 struct wl_resource *resource,
2648 struct wl_resource *output_resource)
2649{
2650 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2651 struct weston_output *output;
2652
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002653 surface_clear_next_states(shsurf);
2654 shsurf->next_state.maximized = true;
2655 shsurf->state_changed = true;
2656
Pekka Paalanenb5026542014-11-12 15:09:24 +02002657 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002658 shell_surface_set_parent(shsurf, NULL);
2659
Philip Withnallbecb77e2013-11-25 18:01:30 +00002660 if (output_resource)
2661 output = wl_resource_get_user_data(output_resource);
2662 else
2663 output = NULL;
2664
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002665 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002666
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002667 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002668}
2669
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002670/* This is only ever called from set_surface_type(), so there’s no need to
2671 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002672static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002673reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002674{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002675 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002676 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002677 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002678 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002679
Pekka Paalanen98262232011-12-01 10:42:22 +02002680 return 0;
2681}
2682
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002683static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002684set_full_output(struct shell_surface *shsurf)
2685{
2686 shsurf->saved_x = shsurf->view->geometry.x;
2687 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002688 shsurf->saved_width = shsurf->surface->width;
2689 shsurf->saved_height = shsurf->surface->height;
2690 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002691 shsurf->saved_position_valid = true;
2692
2693 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2694 wl_list_remove(&shsurf->rotation.transform.link);
2695 wl_list_init(&shsurf->rotation.transform.link);
2696 weston_view_geometry_dirty(shsurf->view);
2697 shsurf->saved_rotation_valid = true;
2698 }
2699}
2700
2701static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002702set_surface_type(struct shell_surface *shsurf)
2703{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002704 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002706
Philip Withnallbecb77e2013-11-25 18:01:30 +00002707 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002708
Rafael Antognolli03b16592013-12-03 15:35:42 -02002709 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002710 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002711
2712 switch (shsurf->type) {
2713 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002714 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002715 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002716 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002717 weston_view_set_position(shsurf->view,
2718 pev->geometry.x + shsurf->transient.x,
2719 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002720 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002721 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002722
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002723 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 weston_view_set_position(shsurf->view, shsurf->transient.x,
2725 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002726 break;
2727
Philip Withnall0f640e22013-11-25 18:01:31 +00002728 case SHELL_SURFACE_POPUP:
2729 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002730 default:
2731 break;
2732 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002733
2734 /* Update the surface’s layer. */
2735 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002736}
2737
Tiago Vignattibe143262012-04-16 17:31:41 +03002738static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002739shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002740{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002741 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002742}
2743
Pekka Paalanen8274d902014-08-06 19:36:51 +03002744static int
2745black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2746{
2747 struct weston_surface *fs_surface = surface->configure_private;
2748 int n;
2749 int rem;
2750 int ret;
2751
2752 n = snprintf(buf, len, "black background surface for ");
2753 if (n < 0)
2754 return n;
2755
2756 rem = (int)len - n;
2757 if (rem < 0)
2758 rem = 0;
2759
2760 if (fs_surface->get_label)
2761 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2762 else
2763 ret = snprintf(buf + n, rem, "<unknown>");
2764
2765 if (ret < 0)
2766 return n;
2767
2768 return n + ret;
2769}
2770
Alex Wu21858432012-04-01 20:13:08 +08002771static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002772black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002773
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002775create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002776 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002777 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002778{
2779 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002780 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002781
2782 surface = weston_surface_create(ec);
2783 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002784 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002785 return NULL;
2786 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002787 view = weston_view_create(surface);
2788 if (surface == NULL) {
2789 weston_log("no memory\n");
2790 weston_surface_destroy(surface);
2791 return NULL;
2792 }
Alex Wu4539b082012-03-01 12:57:46 +08002793
Alex Wu21858432012-04-01 20:13:08 +08002794 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002795 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002796 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002797 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002798 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002799 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002800 pixman_region32_fini(&surface->input);
2801 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002802
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002803 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002804 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002805
2806 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002807}
2808
Philip Withnalled8f1a92013-11-25 18:01:43 +00002809static void
2810shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2811{
2812 struct weston_output *output = shsurf->fullscreen_output;
2813
Rafael Antognolli03b16592013-12-03 15:35:42 -02002814 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002815
2816 if (!shsurf->fullscreen.black_view)
2817 shsurf->fullscreen.black_view =
2818 create_black_surface(shsurf->surface->compositor,
2819 shsurf->surface,
2820 output->x, output->y,
2821 output->width,
2822 output->height);
2823
2824 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002825 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2826 weston_layer_entry_insert(&shsurf->view->layer_link,
2827 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002828 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2829 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002830
2831 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002832}
2833
Alex Wu4539b082012-03-01 12:57:46 +08002834/* Create black surface and append it to the associated fullscreen surface.
2835 * Handle size dismatch and positioning according to the method. */
2836static void
2837shell_configure_fullscreen(struct shell_surface *shsurf)
2838{
2839 struct weston_output *output = shsurf->fullscreen_output;
2840 struct weston_surface *surface = shsurf->surface;
2841 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002842 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002843 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002844
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002845 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2846 restore_output_mode(output);
2847
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002848 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002849 weston_layer_entry_remove(&shsurf->view->layer_link);
2850 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002851
Philip Withnalled8f1a92013-11-25 18:01:43 +00002852 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002853
Jason Ekstranda7af7042013-10-12 22:38:11 -05002854 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002855 &surf_width, &surf_height);
2856
Alex Wu4539b082012-03-01 12:57:46 +08002857 switch (shsurf->fullscreen.type) {
2858 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002859 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002860 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002861 break;
2862 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002863 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002864 if (output->width == surf_width &&
2865 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002866 weston_view_set_position(shsurf->view,
2867 output->x - surf_x,
2868 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002869 break;
2870 }
2871
Alex Wu4539b082012-03-01 12:57:46 +08002872 matrix = &shsurf->fullscreen.transform.matrix;
2873 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002874
Scott Moreau1bad5db2012-08-18 01:04:05 -06002875 output_aspect = (float) output->width /
2876 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002877 /* XXX: Use surf_width and surf_height here? */
2878 surface_aspect = (float) surface->width /
2879 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002880 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002881 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002882 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002883 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002884 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002885 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002886
Alex Wu4539b082012-03-01 12:57:46 +08002887 weston_matrix_scale(matrix, scale, scale, 1);
2888 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002890 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002891 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2892 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002893 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002894
Alex Wu4539b082012-03-01 12:57:46 +08002895 break;
2896 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002897 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002898 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002899 surf_width * surface->buffer_viewport.buffer.scale,
2900 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002901 shsurf->fullscreen.framerate};
2902
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002903 if (weston_output_mode_switch_to_temporary(output, &mode,
2904 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002905 weston_view_set_position(shsurf->view,
2906 output->x - surf_x,
2907 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002908 shsurf->fullscreen.black_view->surface->width = output->width;
2909 shsurf->fullscreen.black_view->surface->height = output->height;
2910 weston_view_set_position(shsurf->fullscreen.black_view,
2911 output->x - surf_x,
2912 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002913 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002914 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002915 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002916 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002917 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002918 }
Alex Wubd3354b2012-04-17 17:20:49 +08002919 }
Alex Wu4539b082012-03-01 12:57:46 +08002920 break;
2921 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002922 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002923 break;
2924 default:
2925 break;
2926 }
2927}
2928
Alex Wu4539b082012-03-01 12:57:46 +08002929static void
2930shell_map_fullscreen(struct shell_surface *shsurf)
2931{
Alex Wubd3354b2012-04-17 17:20:49 +08002932 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002933}
2934
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002935static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002936set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2937{
2938 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002939 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002940 shsurf->transient.x = x;
2941 shsurf->transient.y = y;
2942 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002943
2944 shell_surface_set_parent(shsurf, NULL);
2945
Pekka Paalanenb5026542014-11-12 15:09:24 +02002946 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002947 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002948}
2949
Kristian Høgsberg47792412014-05-04 13:47:06 -07002950static void
2951shell_interface_set_fullscreen(struct shell_surface *shsurf,
2952 uint32_t method,
2953 uint32_t framerate,
2954 struct weston_output *output)
2955{
2956 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002957 shsurf->next_state.fullscreen = true;
2958 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002959
2960 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002961}
2962
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002963static struct weston_output *
2964get_focused_output(struct weston_compositor *compositor)
2965{
2966 struct weston_seat *seat;
2967 struct weston_output *output = NULL;
2968
2969 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002970 struct weston_touch *touch = weston_seat_get_touch(seat);
2971 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2972 struct weston_keyboard *keyboard =
2973 weston_seat_get_keyboard(seat);
2974
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002975 /* Priority has touch focus, then pointer and
2976 * then keyboard focus. We should probably have
2977 * three for loops and check frist for touch,
2978 * then for pointer, etc. but unless somebody has some
2979 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002980 if (touch && touch->focus)
2981 output = touch->focus->output;
2982 else if (pointer && pointer->focus)
2983 output = pointer->focus->output;
2984 else if (keyboard && keyboard->focus)
2985 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002986
2987 if (output)
2988 break;
2989 }
2990
2991 return output;
2992}
2993
2994static void
2995shell_interface_set_maximized(struct shell_surface *shsurf)
2996{
2997 struct weston_output *output;
2998
2999 surface_clear_next_states(shsurf);
3000 shsurf->next_state.maximized = true;
3001 shsurf->state_changed = true;
3002 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3003
3004 if (!weston_surface_is_mapped(shsurf->surface))
3005 output = get_focused_output(shsurf->surface->compositor);
3006 else
3007 output = shsurf->surface->output;
3008
3009 shell_surface_set_output(shsurf, output);
3010 send_configure_for_surface(shsurf);
3011}
3012
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003013static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003014shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003015{
Derek Foremane4d6c832015-08-05 14:48:11 -05003016 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003017}
3018
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003019static int
3020shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003021 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003022 uint32_t edges)
3023{
Derek Foremane4d6c832015-08-05 14:48:11 -05003024 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003025}
3026
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003027static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003028
3029static void
3030destroy_shell_seat(struct wl_listener *listener, void *data)
3031{
3032 struct shell_seat *shseat =
3033 container_of(listener,
3034 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003035 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003036
3037 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003038 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003039 shseat->popup_grab.client = NULL;
3040
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003041 wl_list_for_each_safe(shsurf, next,
3042 &shseat->popup_grab.surfaces_list,
3043 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003044 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003045 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003046 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003047 }
3048
3049 wl_list_remove(&shseat->seat_destroy_listener.link);
3050 free(shseat);
3051}
3052
Jason Ekstrand024177c2014-04-21 19:42:58 -05003053static void
3054shell_seat_caps_changed(struct wl_listener *listener, void *data)
3055{
Derek Foreman1281a362015-07-31 16:55:32 -05003056 struct weston_keyboard *keyboard;
3057 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003058 struct shell_seat *seat;
3059
3060 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003061 keyboard = weston_seat_get_keyboard(seat->seat);
3062 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003063
Derek Foreman1281a362015-07-31 16:55:32 -05003064 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003065 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003066 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003067 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003068 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003069 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003070 wl_list_init(&seat->keyboard_focus_listener.link);
3071 }
3072
Derek Foreman1281a362015-07-31 16:55:32 -05003073 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003074 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003075 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003076 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003077 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003078 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003079 wl_list_init(&seat->pointer_focus_listener.link);
3080 }
3081}
3082
Giulio Camuffo5085a752013-03-25 21:42:45 +01003083static struct shell_seat *
3084create_shell_seat(struct weston_seat *seat)
3085{
3086 struct shell_seat *shseat;
3087
3088 shseat = calloc(1, sizeof *shseat);
3089 if (!shseat) {
3090 weston_log("no memory to allocate shell seat\n");
3091 return NULL;
3092 }
3093
3094 shseat->seat = seat;
3095 wl_list_init(&shseat->popup_grab.surfaces_list);
3096
3097 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3098 wl_signal_add(&seat->destroy_signal,
3099 &shseat->seat_destroy_listener);
3100
Jason Ekstrand024177c2014-04-21 19:42:58 -05003101 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3102 wl_list_init(&shseat->keyboard_focus_listener.link);
3103
3104 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3105 wl_list_init(&shseat->pointer_focus_listener.link);
3106
3107 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3108 wl_signal_add(&seat->updated_caps_signal,
3109 &shseat->caps_changed_listener);
3110 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3111
Giulio Camuffo5085a752013-03-25 21:42:45 +01003112 return shseat;
3113}
3114
3115static struct shell_seat *
3116get_shell_seat(struct weston_seat *seat)
3117{
3118 struct wl_listener *listener;
3119
3120 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003121 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003122
3123 return container_of(listener,
3124 struct shell_seat, seat_destroy_listener);
3125}
3126
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003127static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003128popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003129{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003130 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003131 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003132 struct shell_seat *shseat =
3133 container_of(grab, struct shell_seat, popup_grab.grab);
3134 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003135 wl_fixed_t sx, sy;
3136
Jason Ekstranda7af7042013-10-12 22:38:11 -05003137 view = weston_compositor_pick_view(pointer->seat->compositor,
3138 pointer->x, pointer->y,
3139 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003140
Jason Ekstranda7af7042013-10-12 22:38:11 -05003141 if (view && view->surface->resource &&
3142 wl_resource_get_client(view->surface->resource) == client) {
3143 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003144 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003145 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003146 }
3147}
3148
3149static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003150popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003151 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003152{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003153 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003154 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003155 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003156 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003157 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003158
Jonas Ådahl61917c82014-08-11 22:44:02 +02003159 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003160 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003161 weston_view_from_global_fixed(pointer->focus, x, y,
3162 &pointer->sx, &pointer->sy);
3163 }
3164
Jonas Ådahld2510102014-10-05 21:39:14 +02003165 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003166
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003167 if (!pointer->focus_client)
3168 return;
3169
3170 resource_list = &pointer->focus_client->pointer_resources;
3171 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 weston_view_from_global_fixed(pointer->focus,
3173 pointer->x, pointer->y,
3174 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003175 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003176 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003177}
3178
3179static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003180popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003181 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003182{
3183 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003184 struct shell_seat *shseat =
3185 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003186 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003187 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003188 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003189 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003190
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003191 if (grab->pointer->focus_client)
3192 resource_list = &grab->pointer->focus_client->pointer_resources;
3193 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003194 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003195 wl_resource_for_each(resource, resource_list) {
3196 wl_pointer_send_button(resource, serial,
3197 time, button, state);
3198 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003199 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003200 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003201 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003202 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003203 }
3204
Daniel Stone4dbadb12012-05-30 16:31:51 +01003205 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003206 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003207}
3208
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003209static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003210popup_grab_axis(struct weston_pointer_grab *grab,
3211 uint32_t time, uint32_t axis, wl_fixed_t value)
3212{
Peter Hutterer11189522015-11-16 12:35:57 +10003213 weston_pointer_send_axis(grab->pointer, time, axis, value);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003214}
3215
3216static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003217popup_grab_cancel(struct weston_pointer_grab *grab)
3218{
3219 popup_grab_end(grab->pointer);
3220}
3221
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003222static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003223 popup_grab_focus,
3224 popup_grab_motion,
3225 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003226 popup_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003227 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003228};
3229
3230static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003231touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003232 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003233{
3234 struct wl_resource *resource;
3235 struct shell_seat *shseat =
3236 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3237 struct wl_display *display = shseat->seat->compositor->wl_display;
3238 uint32_t serial;
3239 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003240 wl_fixed_t sx, sy;
3241
3242 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003243
3244 resource_list = &grab->touch->focus_resource_list;
3245 if (!wl_list_empty(resource_list)) {
3246 serial = wl_display_get_serial(display);
3247 wl_resource_for_each(resource, resource_list) {
3248 wl_touch_send_down(resource, serial, time,
3249 grab->touch->focus->surface->resource,
3250 touch_id, sx, sy);
3251 }
3252 }
3253}
3254
3255static void
3256touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3257{
3258 struct wl_resource *resource;
3259 struct shell_seat *shseat =
3260 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3261 struct wl_display *display = shseat->seat->compositor->wl_display;
3262 uint32_t serial;
3263 struct wl_list *resource_list;
3264
3265 resource_list = &grab->touch->focus_resource_list;
3266 if (!wl_list_empty(resource_list)) {
3267 serial = wl_display_get_serial(display);
3268 wl_resource_for_each(resource, resource_list) {
3269 wl_touch_send_up(resource, serial, time, touch_id);
3270 }
3271 }
3272}
3273
3274static void
3275touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003276 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003277{
3278 struct wl_resource *resource;
3279 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003280 wl_fixed_t sx, sy;
3281
3282 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003283
3284 resource_list = &grab->touch->focus_resource_list;
3285 if (!wl_list_empty(resource_list)) {
3286 wl_resource_for_each(resource, resource_list) {
3287 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3288 }
3289 }
3290}
3291
3292static void
3293touch_popup_grab_frame(struct weston_touch_grab *grab)
3294{
3295}
3296
3297static void
3298touch_popup_grab_cancel(struct weston_touch_grab *grab)
3299{
3300 touch_popup_grab_end(grab->touch);
3301}
3302
3303static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3304 touch_popup_grab_down,
3305 touch_popup_grab_up,
3306 touch_popup_grab_motion,
3307 touch_popup_grab_frame,
3308 touch_popup_grab_cancel,
3309};
3310
3311static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003312shell_surface_send_popup_done(struct shell_surface *shsurf)
3313{
3314 if (shell_surface_is_wl_shell_surface(shsurf))
3315 wl_shell_surface_send_popup_done(shsurf->resource);
3316 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003317 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003318}
3319
3320static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003321popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003322{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003323 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003324 struct shell_seat *shseat =
3325 container_of(grab, struct shell_seat, popup_grab.grab);
3326 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003327 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003328
3329 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003330 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003331 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003332 shseat->popup_grab.grab.interface = NULL;
3333 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003334 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003335 wl_list_for_each_safe(shsurf, next,
3336 &shseat->popup_grab.surfaces_list,
3337 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003338 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003339 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003340 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003341 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003342 wl_list_init(&shseat->popup_grab.surfaces_list);
3343 }
3344}
3345
3346static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003347touch_popup_grab_end(struct weston_touch *touch)
3348{
3349 struct weston_touch_grab *grab = touch->grab;
3350 struct shell_seat *shseat =
3351 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3352 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003353 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003354
3355 if (touch->grab->interface == &touch_popup_grab_interface) {
3356 weston_touch_end_grab(grab->touch);
3357 shseat->popup_grab.client = NULL;
3358 shseat->popup_grab.touch_grab.interface = NULL;
3359 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3360 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003361 wl_list_for_each_safe(shsurf, next,
3362 &shseat->popup_grab.surfaces_list,
3363 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003364 shell_surface_send_popup_done(shsurf);
3365 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003366 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003367 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003368 wl_list_init(&shseat->popup_grab.surfaces_list);
3369 }
3370}
3371
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003372static struct shell_surface *
3373get_top_popup(struct shell_seat *shseat)
3374{
3375 struct shell_surface *shsurf;
3376
3377 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3378 return NULL;
3379 } else {
3380 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3381 struct shell_surface,
3382 popup.grab_link);
3383 return shsurf;
3384 }
3385}
3386
3387static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003388add_popup_grab(struct shell_surface *shsurf,
3389 struct shell_seat *shseat,
3390 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003391{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003392 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003393 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003394 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3395 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003396
3397 parent = get_shell_surface(shsurf->parent);
3398 top_surface = get_top_popup(shseat);
3399 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003400 (!parent ||
3401 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003402 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003403 wl_resource_post_error(shsurf->owner_resource,
3404 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003405 "xdg_popup was not created on the "
3406 "topmost popup");
3407 return -1;
3408 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003409
3410 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003411 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003412 shseat->popup_grab.client =
3413 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003414
3415 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003416 shseat->popup_grab.grab.interface =
3417 &popup_grab_interface;
3418
3419 /* We must make sure here that this popup was opened
3420 * after a mouse press, and not just by moving around
3421 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003422 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003423 shseat->popup_grab.initial_up = 0;
3424 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003425 shseat->popup_grab.touch_grab.interface =
3426 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003427 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003428
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003429 wl_list_insert(&shseat->popup_grab.surfaces_list,
3430 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003431
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003432 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003433 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003434 &shseat->popup_grab.grab);
3435 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003436 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003437 &shseat->popup_grab.touch_grab);
3438 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003439 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003440 wl_list_insert(&shseat->popup_grab.surfaces_list,
3441 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003442 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003443
3444 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003445}
3446
3447static void
3448remove_popup_grab(struct shell_surface *shsurf)
3449{
3450 struct shell_seat *shseat = shsurf->popup.shseat;
3451
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003452 if (shell_surface_is_xdg_popup(shsurf) &&
3453 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003454 wl_resource_post_error(shsurf->owner_resource,
3455 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003456 "xdg_popup was destroyed while it was "
3457 "not the topmost popup.");
3458 return;
3459 }
3460
Giulio Camuffo5085a752013-03-25 21:42:45 +01003461 wl_list_remove(&shsurf->popup.grab_link);
3462 wl_list_init(&shsurf->popup.grab_link);
3463 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003464 if (shseat->popup_grab.type == POINTER) {
3465 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3466 shseat->popup_grab.grab.interface = NULL;
3467 } else if (shseat->popup_grab.type == TOUCH) {
3468 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3469 shseat->popup_grab.touch_grab.interface = NULL;
3470 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003471 }
3472}
3473
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003474static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003475shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003476{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003477 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003478 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003479 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3480 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003481
Jason Ekstranda7af7042013-10-12 22:38:11 -05003482 shsurf->surface->output = parent_view->output;
3483 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003484
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 weston_view_set_transform_parent(shsurf->view, parent_view);
3486 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3487 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003488
Derek Foreman1281a362015-07-31 16:55:32 -05003489 if (pointer &&
3490 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003491 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3492 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003493 } else if (touch &&
3494 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003495 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3496 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003497 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003498 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003499 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003500 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003501
3502 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003503}
3504
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003505static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003506 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003507 shell_surface_move,
3508 shell_surface_resize,
3509 shell_surface_set_toplevel,
3510 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003511 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003512 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003513 shell_surface_set_maximized,
3514 shell_surface_set_title,
3515 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003516};
3517
3518static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003519destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003520{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003521 struct shell_surface *child, *next;
3522
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003523 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3524
Giulio Camuffo5085a752013-03-25 21:42:45 +01003525 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3526 remove_popup_grab(shsurf);
3527 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003528
Alex Wubd3354b2012-04-17 17:20:49 +08003529 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003530 shell_surface_is_top_fullscreen(shsurf))
3531 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003532
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 if (shsurf->fullscreen.black_view)
3534 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003535
Alex Wubd3354b2012-04-17 17:20:49 +08003536 /* As destroy_resource() use wl_list_for_each_safe(),
3537 * we can always remove the listener.
3538 */
3539 wl_list_remove(&shsurf->surface_destroy_listener.link);
3540 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003541 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003542 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003543
Jason Ekstranda7af7042013-10-12 22:38:11 -05003544 weston_view_destroy(shsurf->view);
3545
Philip Withnall648a4dd2013-11-25 18:01:44 +00003546 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003547 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3548 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003549
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003550 wl_list_remove(&shsurf->link);
3551 free(shsurf);
3552}
3553
3554static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003555shell_destroy_shell_surface(struct wl_resource *resource)
3556{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003557 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003558
Bryan Caina46b9462014-04-04 17:41:24 -05003559 if (!wl_list_empty(&shsurf->popup.grab_link))
3560 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003561 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003562 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003563}
3564
3565static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003566shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003567{
3568 struct shell_surface *shsurf = container_of(listener,
3569 struct shell_surface,
3570 surface_destroy_listener);
3571
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003572 if (shsurf->resource)
3573 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003574
3575 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003576}
3577
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003578static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003579fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003580{
3581 struct shell_surface *shsurf = data;
3582
3583 weston_surface_destroy(shsurf->surface);
3584}
3585
3586static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003587fade_out_done(struct weston_view_animation *animation, void *data)
3588{
3589 struct shell_surface *shsurf = data;
3590 struct wl_event_loop *loop;
3591
3592 loop = wl_display_get_event_loop(
3593 shsurf->surface->compositor->wl_display);
3594
3595 if (!shsurf->destroying) {
3596 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3597 shsurf->destroying = true;
3598 }
3599}
3600
3601static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003602handle_resource_destroy(struct wl_listener *listener, void *data)
3603{
3604 struct shell_surface *shsurf =
3605 container_of(listener, struct shell_surface,
3606 resource_destroy_listener);
3607
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003608 if (!weston_surface_is_mapped(shsurf->surface))
3609 return;
3610
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003611 shsurf->surface->ref_count++;
3612
3613 pixman_region32_fini(&shsurf->surface->pending.input);
3614 pixman_region32_init(&shsurf->surface->pending.input);
3615 pixman_region32_fini(&shsurf->surface->input);
3616 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003617 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3618 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3619 fade_out_done, shsurf);
3620 } else {
3621 weston_surface_destroy(shsurf->surface);
3622 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003623}
3624
3625static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003626shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003627
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003628struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003629get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003630{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003631 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003632 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003633 else
3634 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003635}
3636
Rafael Antognollie2a34552013-12-03 15:35:45 -02003637static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003638create_common_surface(struct shell_client *owner, void *shell,
3639 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003640 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003641{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003642 struct shell_surface *shsurf;
3643
Pekka Paalanen50b67472014-10-01 15:02:41 +03003644 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003645
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003646 shsurf = calloc(1, sizeof *shsurf);
3647 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003648 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003649 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003650 }
3651
Jason Ekstranda7af7042013-10-12 22:38:11 -05003652 shsurf->view = weston_view_create(surface);
3653 if (!shsurf->view) {
3654 weston_log("no memory to allocate shell surface\n");
3655 free(shsurf);
3656 return NULL;
3657 }
3658
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003659 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003660 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003661 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003662
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003663 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3664 wl_resource_add_destroy_listener(surface->resource,
3665 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003666 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003667
Tiago Vignattibc052c92012-04-19 16:18:18 +03003668 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003669 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003670 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003671 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003672 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003673 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003674 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3675 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003676 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003677 wl_list_init(&shsurf->fullscreen.transform.link);
3678
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003679 shsurf->output = get_default_output(shsurf->shell->compositor);
3680
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003681 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003682 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003683 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003684 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003685
3686 /* init link so its safe to always remove it in destroy_shell_surface */
3687 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003688 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003689
Pekka Paalanen460099f2012-01-20 16:48:25 +02003690 /* empty when not in use */
3691 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003692 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003693
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003694 wl_list_init(&shsurf->workspace_transform.link);
3695
Philip Withnall648a4dd2013-11-25 18:01:44 +00003696 wl_list_init(&shsurf->children_link);
3697 wl_list_init(&shsurf->children_list);
3698 shsurf->parent = NULL;
3699
Pekka Paalanenb5026542014-11-12 15:09:24 +02003700 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003701
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003702 shsurf->client = client;
3703
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003704 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003705}
3706
Rafael Antognollie2a34552013-12-03 15:35:45 -02003707static struct shell_surface *
3708create_shell_surface(void *shell, struct weston_surface *surface,
3709 const struct weston_shell_client *client)
3710{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003711 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003712}
3713
Tiago Vignattibc052c92012-04-19 16:18:18 +03003714static void
3715shell_get_shell_surface(struct wl_client *client,
3716 struct wl_resource *resource,
3717 uint32_t id,
3718 struct wl_resource *surface_resource)
3719{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003720 struct weston_surface *surface =
3721 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003722 struct shell_client *sc = wl_resource_get_user_data(resource);
3723 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003724 struct shell_surface *shsurf;
3725
Pekka Paalanen50b67472014-10-01 15:02:41 +03003726 if (weston_surface_set_role(surface, "wl_shell_surface",
3727 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003728 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003729
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003730 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003731 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003732 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003733 return;
3734 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003735
Jason Ekstranda85118c2013-06-27 20:17:02 -05003736 shsurf->resource =
3737 wl_resource_create(client,
3738 &wl_shell_surface_interface, 1, id);
3739 wl_resource_set_implementation(shsurf->resource,
3740 &shell_surface_implementation,
3741 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003742 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003743}
3744
Rafael Antognollie2a34552013-12-03 15:35:45 -02003745static bool
3746shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3747{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003748 /* A shell surface without a resource is created from xwayland
3749 * and is considered a wl_shell surface for now. */
3750
3751 return shsurf->resource == NULL ||
3752 wl_resource_instance_of(shsurf->resource,
3753 &wl_shell_surface_interface,
3754 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003755}
3756
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003757static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003758 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003759};
3760
Rafael Antognollie2a34552013-12-03 15:35:45 -02003761/****************************
3762 * xdg-shell implementation */
3763
3764static void
3765xdg_surface_destroy(struct wl_client *client,
3766 struct wl_resource *resource)
3767{
3768 wl_resource_destroy(resource);
3769}
3770
3771static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003772xdg_surface_set_parent(struct wl_client *client,
3773 struct wl_resource *resource,
3774 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003775{
3776 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003777 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003778
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003779 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003780 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003781 shell_surface_set_parent(shsurf, parent->surface);
3782 } else {
3783 shell_surface_set_parent(shsurf, NULL);
3784 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003785}
3786
3787static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003788xdg_surface_set_app_id(struct wl_client *client,
3789 struct wl_resource *resource,
3790 const char *app_id)
3791{
3792 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3793
3794 free(shsurf->class);
3795 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003796 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003797}
3798
3799static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003800xdg_surface_show_window_menu(struct wl_client *client,
3801 struct wl_resource *surface_resource,
3802 struct wl_resource *seat_resource,
3803 uint32_t serial,
3804 int32_t x,
3805 int32_t y)
3806{
3807 /* TODO */
3808}
3809
3810static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003811xdg_surface_set_title(struct wl_client *client,
3812 struct wl_resource *resource, const char *title)
3813{
3814 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3815
3816 set_title(shsurf, title);
3817}
3818
3819static void
3820xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3821 struct wl_resource *seat_resource, uint32_t serial)
3822{
3823 common_surface_move(resource, seat_resource, serial);
3824}
3825
3826static void
3827xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3828 struct wl_resource *seat_resource, uint32_t serial,
3829 uint32_t edges)
3830{
3831 common_surface_resize(resource, seat_resource, serial, edges);
3832}
3833
3834static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003835xdg_surface_ack_configure(struct wl_client *client,
3836 struct wl_resource *resource,
3837 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003838{
3839 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3840
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003841 if (shsurf->state_requested) {
3842 shsurf->next_state = shsurf->requested_state;
3843 shsurf->state_changed = true;
3844 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003845 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003846}
3847
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003848static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003849xdg_surface_set_window_geometry(struct wl_client *client,
3850 struct wl_resource *resource,
3851 int32_t x,
3852 int32_t y,
3853 int32_t width,
3854 int32_t height)
3855{
3856 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3857
3858 set_window_geometry(shsurf, x, y, width, height);
3859}
3860
3861static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003862xdg_surface_set_maximized(struct wl_client *client,
3863 struct wl_resource *resource)
3864{
3865 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003866 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003867
3868 shsurf->state_requested = true;
3869 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003870
3871 if (!weston_surface_is_mapped(shsurf->surface))
3872 output = get_focused_output(shsurf->surface->compositor);
3873 else
3874 output = shsurf->surface->output;
3875
3876 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003877 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003878}
3879
3880static void
3881xdg_surface_unset_maximized(struct wl_client *client,
3882 struct wl_resource *resource)
3883{
3884 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3885
3886 shsurf->state_requested = true;
3887 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003888 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003889}
3890
3891static void
3892xdg_surface_set_fullscreen(struct wl_client *client,
3893 struct wl_resource *resource,
3894 struct wl_resource *output_resource)
3895{
3896 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3897 struct weston_output *output;
3898
3899 shsurf->state_requested = true;
3900 shsurf->requested_state.fullscreen = true;
3901
3902 if (output_resource)
3903 output = wl_resource_get_user_data(output_resource);
3904 else
3905 output = NULL;
3906
Marek Chalupa052917a2014-09-02 11:35:12 +02003907 /* handle clients launching in fullscreen */
3908 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3909 /* Set the output to the one that has focus currently. */
3910 assert(shsurf->surface);
3911 output = get_focused_output(shsurf->surface->compositor);
3912 }
3913
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003914 shell_surface_set_output(shsurf, output);
3915 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003916
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003917 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003918}
3919
3920static void
3921xdg_surface_unset_fullscreen(struct wl_client *client,
3922 struct wl_resource *resource)
3923{
3924 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3925
3926 shsurf->state_requested = true;
3927 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003928 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003929}
3930
3931static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003932xdg_surface_set_minimized(struct wl_client *client,
3933 struct wl_resource *resource)
3934{
3935 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3936
3937 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3938 return;
3939
3940 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003941 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003942}
3943
Rafael Antognollie2a34552013-12-03 15:35:45 -02003944static const struct xdg_surface_interface xdg_surface_implementation = {
3945 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003946 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003947 xdg_surface_set_title,
3948 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003949 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003950 xdg_surface_move,
3951 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003952 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003953 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003954 xdg_surface_set_maximized,
3955 xdg_surface_unset_maximized,
3956 xdg_surface_set_fullscreen,
3957 xdg_surface_unset_fullscreen,
3958 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003959};
3960
3961static void
3962xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003963 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003964{
3965 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003966 uint32_t *s;
3967 struct wl_array states;
3968 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003969
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003970 assert(shsurf);
3971
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003972 if (!shsurf->resource)
3973 return;
3974
3975 wl_array_init(&states);
3976 if (shsurf->requested_state.fullscreen) {
3977 s = wl_array_add(&states, sizeof *s);
3978 *s = XDG_SURFACE_STATE_FULLSCREEN;
3979 } else if (shsurf->requested_state.maximized) {
3980 s = wl_array_add(&states, sizeof *s);
3981 *s = XDG_SURFACE_STATE_MAXIMIZED;
3982 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003983 if (shsurf->resize_edges != 0) {
3984 s = wl_array_add(&states, sizeof *s);
3985 *s = XDG_SURFACE_STATE_RESIZING;
3986 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003987 if (shsurf->focus_count > 0) {
3988 s = wl_array_add(&states, sizeof *s);
3989 *s = XDG_SURFACE_STATE_ACTIVATED;
3990 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003991
3992 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3993 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3994
3995 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003996}
3997
3998static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003999 xdg_send_configure,
4000 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004001};
4002
4003static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004004xdg_shell_destroy(struct wl_client *client,
4005 struct wl_resource *resource)
4006{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004007 struct shell_client *sc = wl_resource_get_user_data(resource);
4008 struct wl_resource *shsurf_resource;
4009 struct shell_surface *shsurf;
4010
4011 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4012 shsurf = wl_resource_get_user_data(shsurf_resource);
4013 if (shsurf->owner_resource == resource) {
4014 wl_resource_post_error(
4015 resource,
4016 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4017 "not all child surface objects destroyed");
4018 return;
4019 }
4020 }
4021
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004022 wl_resource_destroy(resource);
4023}
4024
4025static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004026xdg_use_unstable_version(struct wl_client *client,
4027 struct wl_resource *resource,
4028 int32_t version)
4029{
4030 if (version > 1) {
4031 wl_resource_post_error(resource,
4032 1,
4033 "xdg-shell:: version not implemented yet.");
4034 return;
4035 }
4036}
4037
4038static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004039create_xdg_surface(struct shell_client *owner, void *shell,
4040 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004041 const struct weston_shell_client *client)
4042{
4043 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004044
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004045 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004046 if (!shsurf)
4047 return NULL;
4048
Pekka Paalanenb5026542014-11-12 15:09:24 +02004049 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004050
4051 return shsurf;
4052}
4053
4054static void
4055xdg_get_xdg_surface(struct wl_client *client,
4056 struct wl_resource *resource,
4057 uint32_t id,
4058 struct wl_resource *surface_resource)
4059{
4060 struct weston_surface *surface =
4061 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004062 struct shell_client *sc = wl_resource_get_user_data(resource);
4063 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004064 struct shell_surface *shsurf;
4065
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004066 shsurf = get_shell_surface(surface);
4067 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4068 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4069 "This wl_surface is already an "
4070 "xdg_surface");
4071 return;
4072 }
4073
Pekka Paalanen50b67472014-10-01 15:02:41 +03004074 if (weston_surface_set_role(surface, "xdg_surface",
4075 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004076 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004077
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004078 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004079 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004080 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004081 return;
4082 }
4083
4084 shsurf->resource =
4085 wl_resource_create(client,
4086 &xdg_surface_interface, 1, id);
4087 wl_resource_set_implementation(shsurf->resource,
4088 &xdg_surface_implementation,
4089 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004090 shsurf->owner_resource = resource;
4091 wl_list_insert(&sc->surface_list,
4092 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004093}
4094
4095static bool
4096shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4097{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004098 return shsurf->resource &&
4099 wl_resource_instance_of(shsurf->resource,
4100 &xdg_surface_interface,
4101 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102}
4103
4104/* xdg-popup implementation */
4105
4106static void
4107xdg_popup_destroy(struct wl_client *client,
4108 struct wl_resource *resource)
4109{
4110 wl_resource_destroy(resource);
4111}
4112
Rafael Antognollie2a34552013-12-03 15:35:45 -02004113static const struct xdg_popup_interface xdg_popup_implementation = {
4114 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004115};
4116
4117static void
4118xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004119 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004120{
4121}
4122
4123static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004124 xdg_popup_send_configure,
4125 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004126};
4127
4128static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004129create_xdg_popup(struct shell_client *owner, void *shell,
4130 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004131 const struct weston_shell_client *client,
4132 struct weston_surface *parent,
4133 struct shell_seat *seat,
4134 uint32_t serial,
4135 int32_t x, int32_t y)
4136{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004137 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004138
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004139 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004140 if (!shsurf)
4141 return NULL;
4142
Pekka Paalanenb5026542014-11-12 15:09:24 +02004143 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004144 shsurf->popup.shseat = seat;
4145 shsurf->popup.serial = serial;
4146 shsurf->popup.x = x;
4147 shsurf->popup.y = y;
4148 shell_surface_set_parent(shsurf, parent);
4149
4150 return shsurf;
4151}
4152
4153static void
4154xdg_get_xdg_popup(struct wl_client *client,
4155 struct wl_resource *resource,
4156 uint32_t id,
4157 struct wl_resource *surface_resource,
4158 struct wl_resource *parent_resource,
4159 struct wl_resource *seat_resource,
4160 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004161 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004162{
4163 struct weston_surface *surface =
4164 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004165 struct shell_client *sc = wl_resource_get_user_data(resource);
4166 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004167 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004168 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004169 struct weston_surface *parent;
4170 struct shell_seat *seat;
4171
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004172 shsurf = get_shell_surface(surface);
4173 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4174 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4175 "This wl_surface is already an "
4176 "xdg_popup");
4177 return;
4178 }
4179
Pekka Paalanen50b67472014-10-01 15:02:41 +03004180 if (weston_surface_set_role(surface, "xdg_popup",
4181 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004182 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004183
4184 if (!parent_resource) {
4185 wl_resource_post_error(surface_resource,
4186 WL_DISPLAY_ERROR_INVALID_OBJECT,
4187 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004188 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189 }
4190
4191 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004192 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004193
Jonas Ådahlee45a552015-03-18 16:37:47 +08004194 /* Verify that we are creating the top most popup when mapping,
4195 * as it's not until then we know whether it was mapped as most
4196 * top level or not. */
4197
4198 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004199 if (!parent_shsurf ||
4200 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4201 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004202 wl_resource_post_error(resource,
4203 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4204 "xdg_popup parent was invalid");
4205 return;
4206 }
4207
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004208 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004209 parent, seat, serial, x, y);
4210 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004211 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004212 return;
4213 }
4214
4215 shsurf->resource =
4216 wl_resource_create(client,
4217 &xdg_popup_interface, 1, id);
4218 wl_resource_set_implementation(shsurf->resource,
4219 &xdg_popup_implementation,
4220 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004221 shsurf->owner_resource = resource;
4222 wl_list_insert(&sc->surface_list,
4223 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004224}
4225
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004226static void
4227xdg_pong(struct wl_client *client,
4228 struct wl_resource *resource, uint32_t serial)
4229{
4230 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004231
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004232 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004233}
4234
Rafael Antognollie2a34552013-12-03 15:35:45 -02004235static bool
4236shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4237{
4238 return wl_resource_instance_of(shsurf->resource,
4239 &xdg_popup_interface,
4240 &xdg_popup_implementation);
4241}
4242
4243static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004244 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004245 xdg_use_unstable_version,
4246 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004247 xdg_get_xdg_popup,
4248 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004249};
4250
4251static int
4252xdg_shell_unversioned_dispatch(const void *implementation,
4253 void *_target, uint32_t opcode,
4254 const struct wl_message *message,
4255 union wl_argument *args)
4256{
4257 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004258 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004259
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004260 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004261 wl_resource_post_error(resource,
4262 WL_DISPLAY_ERROR_INVALID_OBJECT,
4263 "must call use_unstable_version first");
4264 return 0;
4265 }
4266
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004267#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004268
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004269 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4270 "shell implementation doesn't match protocol version");
4271
Rafael Antognollie2a34552013-12-03 15:35:45 -02004272 if (args[0].i != XDG_SERVER_VERSION) {
4273 wl_resource_post_error(resource,
4274 WL_DISPLAY_ERROR_INVALID_OBJECT,
4275 "incompatible version, server is %d "
4276 "client wants %d",
4277 XDG_SERVER_VERSION, args[0].i);
4278 return 0;
4279 }
4280
4281 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004282 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004283
4284 return 1;
4285}
4286
4287/* end of xdg-shell implementation */
4288/***********************************/
4289
Kristian Høgsberg07937562011-04-12 17:25:42 -04004290static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004291shell_fade(struct desktop_shell *shell, enum fade_type type);
4292
Pekka Paalanen77346a62011-11-30 16:26:35 +02004293static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004294configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004295{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004296 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004297
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004298 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004299 if (v->output == ev->output && v != ev) {
4300 weston_view_unmap(v);
4301 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004302 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004303 }
4304 }
4305
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004306 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004307
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004308 if (wl_list_empty(&ev->layer_link.link)) {
4309 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004310 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004311 }
4312}
4313
Pekka Paalanen8274d902014-08-06 19:36:51 +03004314static int
4315background_get_label(struct weston_surface *surface, char *buf, size_t len)
4316{
4317 return snprintf(buf, len, "background for output %s",
4318 surface->output->name);
4319}
4320
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004321static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004322background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004323{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004324 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004325 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004326
Jason Ekstranda7af7042013-10-12 22:38:11 -05004327 view = container_of(es->views.next, struct weston_view, surface_link);
4328
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004329 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004330}
4331
4332static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004333desktop_shell_set_background(struct wl_client *client,
4334 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004335 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004336 struct wl_resource *surface_resource)
4337{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004338 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004339 struct weston_surface *surface =
4340 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004342
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004343 if (surface->configure) {
4344 wl_resource_post_error(surface_resource,
4345 WL_DISPLAY_ERROR_INVALID_OBJECT,
4346 "surface role already assigned");
4347 return;
4348 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004349
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4351 weston_view_destroy(view);
4352 view = weston_view_create(surface);
4353
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004354 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004355 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004356 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004357 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004358 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004359 weston_desktop_shell_send_configure(resource, 0,
4360 surface_resource,
4361 surface->output->width,
4362 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004363}
4364
Pekka Paalanen8274d902014-08-06 19:36:51 +03004365static int
4366panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4367{
4368 return snprintf(buf, len, "panel for output %s",
4369 surface->output->name);
4370}
4371
Kristian Høgsberg75840622011-09-06 13:48:16 -04004372static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004373panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004374{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004375 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004377
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 view = container_of(es->views.next, struct weston_view, surface_link);
4379
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004380 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004381}
4382
4383static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004384desktop_shell_set_panel(struct wl_client *client,
4385 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004386 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004387 struct wl_resource *surface_resource)
4388{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004389 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004390 struct weston_surface *surface =
4391 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004392 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004393
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004394 if (surface->configure) {
4395 wl_resource_post_error(surface_resource,
4396 WL_DISPLAY_ERROR_INVALID_OBJECT,
4397 "surface role already assigned");
4398 return;
4399 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004400
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4402 weston_view_destroy(view);
4403 view = weston_view_create(surface);
4404
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004405 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004406 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004407 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004408 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004409 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004410 weston_desktop_shell_send_configure(resource, 0,
4411 surface_resource,
4412 surface->output->width,
4413 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004414}
4415
Pekka Paalanen8274d902014-08-06 19:36:51 +03004416static int
4417lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4418{
4419 return snprintf(buf, len, "lock window");
4420}
4421
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004422static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004423lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004424{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004425 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004426 struct weston_view *view;
4427
4428 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004429
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004430 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004431 return;
4432
Jason Ekstranda7af7042013-10-12 22:38:11 -05004433 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004434
4435 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004436 weston_layer_entry_insert(&shell->lock_layer.view_list,
4437 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004438 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004439 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004440 }
4441}
4442
4443static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004444handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004445{
Tiago Vignattibe143262012-04-16 17:31:41 +03004446 struct desktop_shell *shell =
4447 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004448
Martin Minarik6d118362012-06-07 18:01:59 +02004449 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004450 shell->lock_surface = NULL;
4451}
4452
4453static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004454desktop_shell_set_lock_surface(struct wl_client *client,
4455 struct wl_resource *resource,
4456 struct wl_resource *surface_resource)
4457{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004458 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004459 struct weston_surface *surface =
4460 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004461
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004462 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004463
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004464 if (!shell->locked)
4465 return;
4466
Pekka Paalanen98262232011-12-01 10:42:22 +02004467 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004468
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004469 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004470 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004471 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004472
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004473 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004474 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004475 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004476 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004477}
4478
4479static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004480resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004481{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004482 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004483
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004484 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004485 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004486 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004487 &shell->input_panel_layer.link);
4488 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004489 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004490 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004491 wl_list_insert(&shell->compositor->cursor_layer.link,
4492 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004493 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004494 wl_list_insert(&shell->fullscreen_layer.link,
4495 &shell->panel_layer.link);
4496 wl_list_insert(&shell->panel_layer.link,
4497 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004498
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004499 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004500
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004501 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004502 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004503 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004504}
4505
4506static void
4507desktop_shell_unlock(struct wl_client *client,
4508 struct wl_resource *resource)
4509{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004510 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004511
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004512 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004513
4514 if (shell->locked)
4515 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004516}
4517
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004518static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004519desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004520 struct wl_resource *resource,
4521 struct wl_resource *surface_resource)
4522{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004523 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004524
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004525 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004526 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004527}
4528
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004529static void
4530desktop_shell_desktop_ready(struct wl_client *client,
4531 struct wl_resource *resource)
4532{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004533 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004534
4535 shell_fade_startup(shell);
4536}
4537
Jonny Lamb765760d2014-08-20 15:53:19 +02004538static void
4539desktop_shell_set_panel_position(struct wl_client *client,
4540 struct wl_resource *resource,
4541 uint32_t position)
4542{
4543 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4544
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004545 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4546 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4547 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4548 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004549 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004550 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004551 "bad position argument");
4552 return;
4553 }
4554
4555 shell->panel_position = position;
4556}
4557
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004558static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004559 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004560 desktop_shell_set_panel,
4561 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004562 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004563 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004564 desktop_shell_desktop_ready,
4565 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004566};
4567
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004568static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004569get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004570{
4571 struct shell_surface *shsurf;
4572
4573 shsurf = get_shell_surface(surface);
4574 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004575 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004576 return shsurf->type;
4577}
4578
Kristian Høgsberg75840622011-09-06 13:48:16 -04004579static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004580move_binding(struct weston_pointer *pointer, uint32_t time,
4581 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004582{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004583 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004584 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004585 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004586
Derek Foreman8ae2db52015-07-15 13:00:36 -05004587 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004588 return;
4589
Derek Foreman8ae2db52015-07-15 13:00:36 -05004590 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004591
Pekka Paalanen01388e22013-04-25 13:57:44 +03004592 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004593 if (surface == NULL)
4594 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004595
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004596 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004597 if (shsurf == NULL || shsurf->state.fullscreen ||
4598 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004599 return;
4600
Derek Foreman794fa0e2015-07-15 13:00:38 -05004601 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004602}
4603
4604static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004605maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4606 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004607{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004608 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004609 struct weston_surface *surface;
4610 struct shell_surface *shsurf;
4611
4612 surface = weston_surface_get_main_surface(focus);
4613 if (surface == NULL)
4614 return;
4615
4616 shsurf = get_shell_surface(surface);
4617 if (shsurf == NULL)
4618 return;
4619
4620 if (!shell_surface_is_xdg_surface(shsurf))
4621 return;
4622
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004623 shsurf->state_requested = true;
4624 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004625 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004626}
4627
4628static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004629fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4630 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004631{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004632 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004633 struct weston_surface *surface;
4634 struct shell_surface *shsurf;
4635
4636 surface = weston_surface_get_main_surface(focus);
4637 if (surface == NULL)
4638 return;
4639
4640 shsurf = get_shell_surface(surface);
4641 if (shsurf == NULL)
4642 return;
4643
4644 if (!shell_surface_is_xdg_surface(shsurf))
4645 return;
4646
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004647 shsurf->state_requested = true;
4648 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004649 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004650 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004651}
4652
4653static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004654touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004655{
Derek Foreman362656b2014-09-04 10:23:05 -05004656 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004657 struct weston_surface *surface;
4658 struct shell_surface *shsurf;
4659
Derek Foreman8ae2db52015-07-15 13:00:36 -05004660 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004661 return;
4662
Derek Foreman8ae2db52015-07-15 13:00:36 -05004663 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004664 surface = weston_surface_get_main_surface(focus);
4665 if (surface == NULL)
4666 return;
4667
4668 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004669 if (shsurf == NULL || shsurf->state.fullscreen ||
4670 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004671 return;
4672
Derek Foremanb7674ae2015-07-15 13:00:37 -05004673 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004674}
4675
4676static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004677resize_binding(struct weston_pointer *pointer, uint32_t time,
4678 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004679{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004680 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004681 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004682 uint32_t edges = 0;
4683 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004684 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004685
Derek Foreman8ae2db52015-07-15 13:00:36 -05004686 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004687 return;
4688
Derek Foreman8ae2db52015-07-15 13:00:36 -05004689 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004690
Pekka Paalanen01388e22013-04-25 13:57:44 +03004691 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004692 if (surface == NULL)
4693 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004694
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004695 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004696 if (shsurf == NULL || shsurf->state.fullscreen ||
4697 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004698 return;
4699
Jason Ekstranda7af7042013-10-12 22:38:11 -05004700 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004701 wl_fixed_to_int(pointer->grab_x),
4702 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004703 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004704
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004705 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004706 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004707 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004708 edges |= 0;
4709 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004710 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004711
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004712 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004713 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004714 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004715 edges |= 0;
4716 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004717 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004718
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004719 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004720}
4721
4722static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004723surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4724 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004725{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004726 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004727 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004728 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004729 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004730
Pekka Paalanen01388e22013-04-25 13:57:44 +03004731 /* XXX: broken for windows containing sub-surfaces */
4732 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004733 if (surface == NULL)
4734 return;
4735
4736 shsurf = get_shell_surface(surface);
4737 if (!shsurf)
4738 return;
4739
Jason Ekstranda7af7042013-10-12 22:38:11 -05004740 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004741
Jason Ekstranda7af7042013-10-12 22:38:11 -05004742 if (shsurf->view->alpha > 1.0)
4743 shsurf->view->alpha = 1.0;
4744 if (shsurf->view->alpha < step)
4745 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004746
Jason Ekstranda7af7042013-10-12 22:38:11 -05004747 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004748 weston_surface_damage(surface);
4749}
4750
4751static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004752do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004753 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004754{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004755 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004756 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004757 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004758 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004759
Derek Foreman1281a362015-07-31 16:55:32 -05004760 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004761 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4762 return;
4763 }
4764
Scott Moreauccbf29d2012-02-22 14:21:41 -07004765 wl_list_for_each(output, &compositor->output_list, link) {
4766 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004767 wl_fixed_to_double(pointer->x),
4768 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004769 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004770 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004771 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004772 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004773 increment = -output->zoom.increment;
4774 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004775 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004776 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004777 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004778 else
4779 increment = 0;
4780
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004781 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004782
Scott Moreaue6603982012-06-11 13:07:51 -06004783 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004784 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004785 else if (output->zoom.level > output->zoom.max_level)
4786 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004787
4788 if (!output->zoom.active) {
4789 if (output->zoom.level <= 0.0)
4790 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004791 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004792 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004793
Scott Moreaue6603982012-06-11 13:07:51 -06004794 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004795
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004797 }
4798 }
4799}
4800
Scott Moreauccbf29d2012-02-22 14:21:41 -07004801static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004802zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4803 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004804{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004805 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004806}
4807
4808static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004809zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4810 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004811{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004812 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004813}
4814
4815static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004816terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4817 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004818{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004819 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004820
Daniel Stone325fc2d2012-05-30 16:31:58 +01004821 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004822}
4823
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004824static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004825rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004826 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004827{
4828 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004829 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004830 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004831 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004832 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004833
Jonas Ådahld2510102014-10-05 21:39:14 +02004834 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004835
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004836 if (!shsurf)
4837 return;
4838
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004839 cx = 0.5f * shsurf->surface->width;
4840 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004841
Daniel Stone37816df2012-05-16 18:45:18 +01004842 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4843 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004844 r = sqrtf(dx * dx + dy * dy);
4845
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004846 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004847 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004848
4849 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004850 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004851 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004852
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004853 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004854 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004855
4856 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004857 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004858 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004859 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004860 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004861
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004862 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004864 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004865 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004866 wl_list_init(&shsurf->rotation.transform.link);
4867 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004868 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004869 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004870
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004871 /* We need to adjust the position of the surface
4872 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004873 cposx = shsurf->view->geometry.x + cx;
4874 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004875 dposx = rotate->center.x - cposx;
4876 dposy = rotate->center.y - cposy;
4877 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004878 weston_view_set_position(shsurf->view,
4879 shsurf->view->geometry.x + dposx,
4880 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004881 }
4882
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004883 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004884 * lazily applies the damage due to rotation update.
4885 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004886 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004887}
4888
4889static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004890rotate_grab_button(struct weston_pointer_grab *grab,
4891 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004892{
4893 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004894 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004895 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004896 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004897 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004898
Daniel Stone4dbadb12012-05-30 16:31:51 +01004899 if (pointer->button_count == 0 &&
4900 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004901 if (shsurf)
4902 weston_matrix_multiply(&shsurf->rotation.rotation,
4903 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004904 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004905 free(rotate);
4906 }
4907}
4908
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004909static void
4910rotate_grab_cancel(struct weston_pointer_grab *grab)
4911{
4912 struct rotate_grab *rotate =
4913 container_of(grab, struct rotate_grab, base.grab);
4914
4915 shell_grab_end(&rotate->base);
4916 free(rotate);
4917}
4918
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004919static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004920 noop_grab_focus,
4921 rotate_grab_motion,
4922 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02004923 noop_grab_axis,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004924 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004925};
4926
4927static void
Derek Foreman74de4692015-07-15 13:00:39 -05004928surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004929{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004930 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004931 float dx, dy;
4932 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004933
Derek Foreman45a7c272015-09-11 14:27:40 -05004934 surface = find_toplevel_surface(surface);
4935
Pekka Paalanen460099f2012-01-20 16:48:25 +02004936 rotate = malloc(sizeof *rotate);
4937 if (!rotate)
4938 return;
4939
Jason Ekstranda7af7042013-10-12 22:38:11 -05004940 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004941 surface->surface->width * 0.5f,
4942 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004943 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004944
Derek Foreman74de4692015-07-15 13:00:39 -05004945 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4946 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004947 r = sqrtf(dx * dx + dy * dy);
4948 if (r > 20.0f) {
4949 struct weston_matrix inverse;
4950
4951 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004952 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004953 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004954
4955 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004956 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004957 } else {
4958 weston_matrix_init(&surface->rotation.rotation);
4959 weston_matrix_init(&rotate->rotation);
4960 }
4961
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004962 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004963 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004964}
4965
4966static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004967rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004968 void *data)
4969{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004970 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004971 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004972 struct shell_surface *surface;
4973
Derek Foreman8ae2db52015-07-15 13:00:36 -05004974 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004975 return;
4976
Derek Foreman8ae2db52015-07-15 13:00:36 -05004977 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004978
Pekka Paalanen01388e22013-04-25 13:57:44 +03004979 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004980 if (base_surface == NULL)
4981 return;
4982
4983 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004984 if (surface == NULL || surface->state.fullscreen ||
4985 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004986 return;
4987
Derek Foreman74de4692015-07-15 13:00:39 -05004988 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004989}
4990
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004991/* Move all fullscreen layers down to the current workspace and hide their
4992 * black views. The surfaces' state is set to both fullscreen and lowered,
4993 * and this is reversed when such a surface is re-configured, see
4994 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4995 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004996 * lowering_output = NULL - Lower on all outputs, else only lower on the
4997 * specified output.
4998 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004999 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005000 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005001void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005002lower_fullscreen_layer(struct desktop_shell *shell,
5003 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005004{
5005 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005006 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005007
5008 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005009 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005010 &shell->fullscreen_layer.view_list.link,
5011 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005012 struct shell_surface *shsurf = get_shell_surface(view->surface);
5013
5014 if (!shsurf)
5015 continue;
5016
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005017 /* Only lower surfaces which have lowering_output as their fullscreen
5018 * output, unless a NULL output asks for lowering on all outputs.
5019 */
5020 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5021 continue;
5022
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005023 /* We can have a non-fullscreen popup for a fullscreen surface
5024 * in the fullscreen layer. */
5025 if (shsurf->state.fullscreen) {
5026 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005027 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5028 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005029 weston_view_damage_below(shsurf->fullscreen.black_view);
5030
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005031 }
5032
5033 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005034 weston_layer_entry_remove(&view->layer_link);
5035 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005036 weston_view_damage_below(view);
5037 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005038
5039 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005040 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005041}
5042
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005043void
Tiago Vignattibe143262012-04-16 17:31:41 +03005044activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005045 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005046{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005047 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005048 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005049 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005050 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005051 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005052
Pekka Paalanen01388e22013-04-25 13:57:44 +03005053 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005054 shsurf = get_shell_surface(main_surface);
5055 assert(shsurf);
5056
5057 /* Only demote fullscreen surfaces on the output of activated shsurf.
5058 * Leave fullscreen surfaces on unrelated outputs alone. */
5059 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005060
Daniel Stone37816df2012-05-16 18:45:18 +01005061 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005062
Jonas Ådahl8538b222012-08-29 22:13:03 +02005063 state = ensure_focus_state(shell, seat);
5064 if (state == NULL)
5065 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005066
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005067 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005068 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005069
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005070 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005071 shell_configure_fullscreen(shsurf);
5072 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005073 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005074
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005075 /* Update the surface’s layer. This brings it to the top of the stacking
5076 * order as appropriate. */
5077 shell_surface_update_layer(shsurf);
5078
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005079 if (shell->focus_animation_type != ANIMATION_NONE) {
5080 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005081 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005082 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005083}
5084
Alex Wu21858432012-04-01 20:13:08 +08005085/* no-op func for checking black surface */
5086static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005087black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005088{
5089}
5090
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005091static bool
Alex Wu21858432012-04-01 20:13:08 +08005092is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5093{
5094 if (es->configure == black_surface_configure) {
5095 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005096 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005097 return true;
5098 }
5099 return false;
5100}
5101
Kristian Høgsberg75840622011-09-06 13:48:16 -04005102static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005103activate_binding(struct weston_seat *seat,
5104 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005105 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005106{
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005107 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005108 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005109
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005110 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005111
Pekka Paalanen01388e22013-04-25 13:57:44 +03005112 if (is_black_surface(focus, &main_surface))
5113 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005114
Pekka Paalanen01388e22013-04-25 13:57:44 +03005115 main_surface = weston_surface_get_main_surface(focus);
5116 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005117 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005118
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005119 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005120}
5121
5122static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005123click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5124 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005125{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005126 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005127 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005128 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005129 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005130
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005131 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005132}
5133
5134static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005135touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5136 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005137{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005138 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005139 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005140 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005141 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005142
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005143 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005144}
5145
5146static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005147unfocus_all_seats(struct desktop_shell *shell)
5148{
5149 struct weston_seat *seat, *next;
5150
5151 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005152 struct weston_keyboard *keyboard =
5153 weston_seat_get_keyboard(seat);
5154
5155 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005156 continue;
5157
Derek Foreman1281a362015-07-31 16:55:32 -05005158 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005159 }
5160}
5161
5162static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005163lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005164{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005165 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005166
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005167 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005168 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005169 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005170 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005171
5172 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005173
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005174 /* Hide all surfaces by removing the fullscreen, panel and
5175 * toplevel layers. This way nothing else can show or receive
5176 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005177
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005178 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005179 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005180 if (shell->showing_input_panels)
5181 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005182 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005183 wl_list_insert(&shell->compositor->cursor_layer.link,
5184 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005185
Derek Foreman004b4a12015-07-20 16:28:13 -05005186 weston_compositor_sleep(shell->compositor);
5187
Neil Robertsb4a91702014-04-09 16:33:32 +01005188 /* Remove the keyboard focus on all seats. This will be
5189 * restored to the workspace's saved state via
5190 * restore_focus_state when the compositor is unlocked */
5191 unfocus_all_seats(shell);
5192
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005193 /* TODO: disable bindings that should not work while locked. */
5194
5195 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005196}
5197
5198static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005199unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005200{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005201 struct wl_resource *shell_resource;
5202
Pekka Paalanend81c2162011-11-16 13:47:34 +02005203 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005204 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005205 return;
5206 }
5207
5208 /* If desktop-shell client has gone away, unlock immediately. */
5209 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005210 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005211 return;
5212 }
5213
5214 if (shell->prepare_event_sent)
5215 return;
5216
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005217 shell_resource = shell->child.desktop_shell;
5218 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005219 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005220}
5221
5222static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005223shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005224{
5225 struct desktop_shell *shell = data;
5226
5227 shell->fade.animation = NULL;
5228
5229 switch (shell->fade.type) {
5230 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005231 weston_surface_destroy(shell->fade.view->surface);
5232 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005233 break;
5234 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005235 lock(shell);
5236 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005237 default:
5238 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005239 }
5240}
5241
Jason Ekstranda7af7042013-10-12 22:38:11 -05005242static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005243shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005244{
5245 struct weston_compositor *compositor = shell->compositor;
5246 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005247 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005248
5249 surface = weston_surface_create(compositor);
5250 if (!surface)
5251 return NULL;
5252
Jason Ekstranda7af7042013-10-12 22:38:11 -05005253 view = weston_view_create(surface);
5254 if (!view) {
5255 weston_surface_destroy(surface);
5256 return NULL;
5257 }
5258
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005259 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005260 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005261 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005262 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5263 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005264 pixman_region32_init(&surface->input);
5265
Jason Ekstranda7af7042013-10-12 22:38:11 -05005266 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005267}
5268
5269static void
5270shell_fade(struct desktop_shell *shell, enum fade_type type)
5271{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005272 float tint;
5273
5274 switch (type) {
5275 case FADE_IN:
5276 tint = 0.0;
5277 break;
5278 case FADE_OUT:
5279 tint = 1.0;
5280 break;
5281 default:
5282 weston_log("shell: invalid fade type\n");
5283 return;
5284 }
5285
5286 shell->fade.type = type;
5287
Jason Ekstranda7af7042013-10-12 22:38:11 -05005288 if (shell->fade.view == NULL) {
5289 shell->fade.view = shell_fade_create_surface(shell);
5290 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005291 return;
5292
Jason Ekstranda7af7042013-10-12 22:38:11 -05005293 shell->fade.view->alpha = 1.0 - tint;
5294 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005295 }
5296
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005297 if (shell->fade.view->output == NULL) {
5298 /* If the black view gets a NULL output, we lost the
5299 * last output and we'll just cancel the fade. This
5300 * happens when you close the last window under the
5301 * X11 or Wayland backends. */
5302 shell->locked = false;
5303 weston_surface_destroy(shell->fade.view->surface);
5304 shell->fade.view = NULL;
5305 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005306 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005307 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005308 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005309 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005310 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005311 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005312 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005313}
5314
5315static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005316do_shell_fade_startup(void *data)
5317{
5318 struct desktop_shell *shell = data;
5319
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005320 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005321 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005322 } else {
5323 weston_log("desktop shell: "
5324 "unexpected fade-in animation type %d\n",
5325 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005326 weston_surface_destroy(shell->fade.view->surface);
5327 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005328 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005329}
5330
5331static void
5332shell_fade_startup(struct desktop_shell *shell)
5333{
5334 struct wl_event_loop *loop;
5335
5336 if (!shell->fade.startup_timer)
5337 return;
5338
5339 wl_event_source_remove(shell->fade.startup_timer);
5340 shell->fade.startup_timer = NULL;
5341
5342 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5343 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5344}
5345
5346static int
5347fade_startup_timeout(void *data)
5348{
5349 struct desktop_shell *shell = data;
5350
5351 shell_fade_startup(shell);
5352 return 0;
5353}
5354
5355static void
5356shell_fade_init(struct desktop_shell *shell)
5357{
5358 /* Make compositor output all black, and wait for the desktop-shell
5359 * client to signal it is ready, then fade in. The timer triggers a
5360 * fade-in, in case the desktop-shell client takes too long.
5361 */
5362
5363 struct wl_event_loop *loop;
5364
Jason Ekstranda7af7042013-10-12 22:38:11 -05005365 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005366 weston_log("%s: warning: fade surface already exists\n",
5367 __func__);
5368 return;
5369 }
5370
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005371 if (shell->startup_animation_type == ANIMATION_NONE)
5372 return;
5373
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 shell->fade.view = shell_fade_create_surface(shell);
5375 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005376 return;
5377
Jason Ekstranda7af7042013-10-12 22:38:11 -05005378 weston_view_update_transform(shell->fade.view);
5379 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005380
5381 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5382 shell->fade.startup_timer =
5383 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5384 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5385}
5386
5387static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005388idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005389{
5390 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005391 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005392 struct weston_seat *seat;
5393
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005394 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005395 struct weston_touch *touch = weston_seat_get_touch(seat);
5396 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5397
5398 if (pointer)
5399 popup_grab_end(pointer);
5400 if (touch)
5401 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005402 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005403
5404 shell_fade(shell, FADE_OUT);
5405 /* lock() is called from shell_fade_done() */
5406}
5407
5408static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005409wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005410{
5411 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005412 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005413
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005414 unlock(shell);
5415}
5416
5417static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005418transform_handler(struct wl_listener *listener, void *data)
5419{
5420 struct weston_surface *surface = data;
5421 struct shell_surface *shsurf = get_shell_surface(surface);
5422 struct weston_view *view;;
5423 int x, y;
5424
5425 if (!shsurf || !shsurf->client->send_position)
5426 return;
5427
5428 view = shsurf->view;
5429 if (!view || !weston_view_is_mapped(view))
5430 return;
5431
5432 x = view->geometry.x;
5433 y = view->geometry.y;
5434
5435 shsurf->client->send_position(surface, x, y);
5436}
5437
5438static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005439center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005440{
Giulio Camuffob8366642013-04-25 13:57:46 +03005441 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005442 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005443
Jason Ekstranda7af7042013-10-12 22:38:11 -05005444 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005445
5446 x = output->x + (output->width - width) / 2 - surf_x / 2;
5447 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005448
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005449 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005450}
5451
5452static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005453weston_view_set_initial_position(struct weston_view *view,
5454 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005455{
5456 struct weston_compositor *compositor = shell->compositor;
5457 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005458 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005459 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005460 struct weston_output *output, *target_output = NULL;
5461 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005462 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005463
5464 /* As a heuristic place the new window on the same output as the
5465 * pointer. Falling back to the output containing 0, 0.
5466 *
5467 * TODO: Do something clever for touch too?
5468 */
5469 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005470 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5471
5472 if (pointer) {
5473 ix = wl_fixed_to_int(pointer->x);
5474 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005475 break;
5476 }
5477 }
5478
5479 wl_list_for_each(output, &compositor->output_list, link) {
5480 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5481 target_output = output;
5482 break;
5483 }
5484 }
5485
5486 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005487 weston_view_set_position(view, 10 + random() % 400,
5488 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005489 return;
5490 }
5491
5492 /* Valid range within output where the surface will still be onscreen.
5493 * If this is negative it means that the surface is bigger than
5494 * output.
5495 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005496 get_output_work_area(shell, target_output, &area);
5497
Derek Foremanf814c5d2015-04-15 12:23:54 -05005498 x = area.x;
5499 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005500 range_x = area.width - view->surface->width;
5501 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005502
Rob Bradford4cb88c72012-08-13 15:18:44 +01005503 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005504 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005505
5506 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005507 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005508
Jason Ekstranda7af7042013-10-12 22:38:11 -05005509 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005510}
5511
5512static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005513set_maximized_position(struct desktop_shell *shell,
5514 struct shell_surface *shsurf)
5515{
5516 int32_t surf_x, surf_y;
5517 pixman_rectangle32_t area;
5518
Jonny Lambd73c6942014-08-20 15:53:20 +02005519 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005520 if (shsurf->has_set_geometry) {
5521 surf_x = shsurf->geometry.x;
5522 surf_y = shsurf->geometry.y;
5523 } else {
5524 surface_subsurfaces_boundingbox(shsurf->surface,
5525 &surf_x, &surf_y, NULL, NULL);
5526 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005527
5528 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005529 area.x - surf_x,
5530 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005531}
5532
5533static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005534map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005535 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005536{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005537 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005538 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005539
Pekka Paalanen77346a62011-11-30 16:26:35 +02005540 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005541 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005542 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005543 if (shsurf->state.fullscreen) {
5544 center_on_output(shsurf->view, shsurf->fullscreen_output);
5545 shell_map_fullscreen(shsurf);
5546 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005547 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005548 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005549 weston_view_set_initial_position(shsurf->view, shell);
5550 }
Juan Zhao96879df2012-02-07 08:45:41 +08005551 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005552 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005553 if (shell_map_popup(shsurf) != 0)
5554 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005555 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005556 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005557 weston_view_set_position(shsurf->view,
5558 shsurf->view->geometry.x + sx,
5559 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005560 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005561 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005562 default:
5563 ;
5564 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005565
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005566 /* Surface stacking order, see also activate(). */
5567 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005568
Jason Ekstranda7af7042013-10-12 22:38:11 -05005569 if (shsurf->type != SHELL_SURFACE_NONE) {
5570 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005571 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005572 shsurf->surface->output = shsurf->output;
5573 shsurf->view->output = shsurf->output;
5574 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005575 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005576
Jason Ekstranda7af7042013-10-12 22:38:11 -05005577 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005578 /* XXX: xwayland's using the same fields for transient type */
5579 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005580 if (shsurf->transient.flags ==
5581 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5582 break;
5583 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005584 if (shsurf->state.relative &&
5585 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5586 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005587 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005588 break;
5589 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005590 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005591 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005592 case SHELL_SURFACE_POPUP:
5593 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005594 default:
5595 break;
5596 }
5597
Rafael Antognolli03b16592013-12-03 15:35:42 -02005598 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5599 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005600 {
5601 switch (shell->win_animation_type) {
5602 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005603 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005604 break;
5605 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005606 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005607 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005608 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005609 default:
5610 break;
5611 }
5612 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005613}
5614
5615static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005616configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005617 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005618{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005619 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005620 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005621
Pekka Paalanen77346a62011-11-30 16:26:35 +02005622 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005623
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005624 assert(shsurf);
5625
Rafael Antognolli03b16592013-12-03 15:35:42 -02005626 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005627 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005628 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005629 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005630 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005631 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005632 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005633
Alex Wu4539b082012-03-01 12:57:46 +08005634 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005635 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005636 wl_list_for_each(view, &surface->views, surface_link)
5637 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005638
Rafael Antognolli03b16592013-12-03 15:35:42 -02005639 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005640 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005641 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005642}
5643
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005644static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005645shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005646{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005647 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005648 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005649 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005650
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005651 assert(shsurf);
5652
5653 shell = shsurf->shell;
5654
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005655 if (!weston_surface_is_mapped(es) &&
5656 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005657 remove_popup_grab(shsurf);
5658 }
5659
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005660 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005661 return;
5662
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005663 if (shsurf->has_next_geometry) {
5664 shsurf->geometry = shsurf->next_geometry;
5665 shsurf->has_next_geometry = false;
5666 shsurf->has_set_geometry = true;
5667 } else if (!shsurf->has_set_geometry) {
5668 surface_subsurfaces_boundingbox(shsurf->surface,
5669 &shsurf->geometry.x,
5670 &shsurf->geometry.y,
5671 &shsurf->geometry.width,
5672 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005673 }
5674
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005675 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005676 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005677 type_changed = 1;
5678 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005679
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005680 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005681 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005682 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005683 shsurf->last_width != es->width ||
5684 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005685 float from_x, from_y;
5686 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005687
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005688 if (shsurf->resize_edges) {
5689 sx = 0;
5690 sy = 0;
5691 }
5692
5693 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5694 sx = shsurf->last_width - es->width;
5695 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5696 sy = shsurf->last_height - es->height;
5697
5698 shsurf->last_width = es->width;
5699 shsurf->last_height = es->height;
5700
Jason Ekstranda7af7042013-10-12 22:38:11 -05005701 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5702 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005703 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005704 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005705 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005706 }
5707}
5708
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005709static bool
5710check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5711{
5712 struct timespec now;
5713
5714 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5715 return false;
5716
5717 /*
5718 * If the shell helper client dies before the session has been
5719 * up for roughly 30 seconds, better just make Weston shut down,
5720 * because the user likely has no way to interact with the desktop
5721 * anyway.
5722 */
5723 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5724 weston_log("Error: %s apparently cannot run at all.\n",
5725 shell->client);
5726 weston_log_continue(STAMP_SPACE "Quitting...");
5727 wl_display_terminate(shell->compositor->wl_display);
5728
5729 return true;
5730 }
5731
5732 return false;
5733}
5734
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005735static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005736
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005737static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005738respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005739{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005740 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005741
5742 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5743 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005744 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005745 shell->child.deathstamp = time;
5746 shell->child.deathcount = 0;
5747 }
5748
5749 shell->child.deathcount++;
5750 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005751 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005752 return;
5753 }
5754
Pekka Paalanen826dc142014-08-27 12:11:53 +03005755 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005756 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005757}
5758
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005759static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005760desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5761{
5762 struct desktop_shell *shell;
5763
5764 shell = container_of(listener, struct desktop_shell,
5765 child.client_destroy_listener);
5766
Pekka Paalanen826dc142014-08-27 12:11:53 +03005767 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005768 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005769 /*
5770 * unbind_desktop_shell() will reset shell->child.desktop_shell
5771 * before the respawned process has a chance to create a new
5772 * desktop_shell object, because we are being called from the
5773 * wl_client destructor which destroys all wl_resources before
5774 * returning.
5775 */
5776
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005777 if (!check_desktop_shell_crash_too_early(shell))
5778 respawn_desktop_shell_process(shell);
5779
Pekka Paalanen826dc142014-08-27 12:11:53 +03005780 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005781}
5782
5783static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005784launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005785{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005786 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005787
Pekka Paalanen826dc142014-08-27 12:11:53 +03005788 shell->child.client = weston_client_start(shell->compositor,
5789 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005790
Arnaud Vrac42631192014-08-25 20:56:46 +02005791 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005792 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005793 return;
5794 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005795
5796 shell->child.client_destroy_listener.notify =
5797 desktop_shell_client_destroy;
5798 wl_client_add_destroy_listener(shell->child.client,
5799 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005800}
5801
5802static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005803handle_shell_client_destroy(struct wl_listener *listener, void *data)
5804{
5805 struct shell_client *sc =
5806 container_of(listener, struct shell_client, destroy_listener);
5807
5808 if (sc->ping_timer)
5809 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005810
5811 /* Since we're about to free shell_client, we remove it from the
5812 * head of the surface list so we don't use that freed list node
5813 * during surface clean up later on.
5814 */
5815 wl_list_remove(&sc->surface_list);
5816
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005817 free(sc);
5818}
5819
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005820static struct shell_client *
5821shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5822 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005823{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005824 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005825
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005826 sc = zalloc(sizeof *sc);
5827 if (sc == NULL) {
5828 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005829 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005830 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005831
5832 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005833 if (sc->resource == NULL) {
5834 free(sc);
5835 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005836 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005837 }
5838
5839 sc->client = client;
5840 sc->shell = shell;
5841 sc->destroy_listener.notify = handle_shell_client_destroy;
5842 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005843 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005844
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005845 return sc;
5846}
5847
5848static void
5849bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5850{
5851 struct desktop_shell *shell = data;
5852 struct shell_client *sc;
5853
5854 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5855 if (sc)
5856 wl_resource_set_implementation(sc->resource,
5857 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005858 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005859}
5860
5861static void
5862bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5863{
5864 struct desktop_shell *shell = data;
5865 struct shell_client *sc;
5866
5867 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5868 if (sc)
5869 wl_resource_set_dispatcher(sc->resource,
5870 xdg_shell_unversioned_dispatch,
5871 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005872}
5873
5874static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005875unbind_desktop_shell(struct wl_resource *resource)
5876{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005877 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005878
5879 if (shell->locked)
5880 resume_desktop(shell);
5881
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005882 shell->child.desktop_shell = NULL;
5883 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005884}
5885
5886static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005887bind_desktop_shell(struct wl_client *client,
5888 void *data, uint32_t version, uint32_t id)
5889{
Tiago Vignattibe143262012-04-16 17:31:41 +03005890 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005891 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005892
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005893 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5894 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005895
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005896 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005897 wl_resource_set_implementation(resource,
5898 &desktop_shell_implementation,
5899 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005900 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005901 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005902 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005903
5904 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5905 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005906}
5907
Kristian Høgsberg07045392012-02-19 18:52:44 -05005908struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005909 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005910 struct weston_surface *current;
5911 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005912 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005913 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005914};
5915
5916static void
5917switcher_next(struct switcher *switcher)
5918{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005919 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005920 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005921 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005922 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005923
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005924 /* temporary re-display minimized surfaces */
5925 struct weston_view *tmp;
5926 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005927 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5928 weston_layer_entry_remove(&view->layer_link);
5929 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005930 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5931 *minimized = view;
5932 }
5933
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005934 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005935 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005936 if (shsurf &&
5937 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5938 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005939 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005940 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005941 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005942 next = view->surface;
5943 prev = view->surface;
5944 view->alpha = 0.25;
5945 weston_view_geometry_dirty(view);
5946 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005947 }
Alex Wu1659daa2012-04-01 20:13:09 +08005948
Jason Ekstranda7af7042013-10-12 22:38:11 -05005949 if (is_black_surface(view->surface, NULL)) {
5950 view->alpha = 0.25;
5951 weston_view_geometry_dirty(view);
5952 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005953 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005954 }
5955
5956 if (next == NULL)
5957 next = first;
5958
Alex Wu07b26062012-03-12 16:06:01 +08005959 if (next == NULL)
5960 return;
5961
Kristian Høgsberg07045392012-02-19 18:52:44 -05005962 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005963 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005964
5965 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005966 wl_list_for_each(view, &next->views, surface_link)
5967 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005968
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005969 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005970 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005971 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005972}
5973
5974static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005975switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005976{
5977 struct switcher *switcher =
5978 container_of(listener, struct switcher, listener);
5979
5980 switcher_next(switcher);
5981}
5982
5983static void
Daniel Stone351eb612012-05-31 15:27:47 -04005984switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005985{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005986 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005987 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005988 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005989
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005990 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005991 if (is_focus_view(view))
5992 continue;
5993
Jason Ekstranda7af7042013-10-12 22:38:11 -05005994 view->alpha = 1.0;
5995 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005996 }
5997
Alex Wu07b26062012-03-12 16:06:01 +08005998 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005999 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006000 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006001 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006002 weston_keyboard_end_grab(keyboard);
6003 if (keyboard->input_method_resource)
6004 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006005
6006 /* re-hide surfaces that were temporary shown during the switch */
6007 struct weston_view **minimized;
6008 wl_array_for_each(minimized, &switcher->minimized_array) {
6009 /* with the exception of the current selected */
6010 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006011 weston_layer_entry_remove(&(*minimized)->layer_link);
6012 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006013 weston_view_damage_below(*minimized);
6014 }
6015 }
6016 wl_array_release(&switcher->minimized_array);
6017
Kristian Høgsberg07045392012-02-19 18:52:44 -05006018 free(switcher);
6019}
6020
6021static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006022switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006023 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006024{
6025 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006026 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006027
Daniel Stonec9785ea2012-05-30 16:31:52 +01006028 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006029 switcher_next(switcher);
6030}
6031
6032static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006033switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006034 uint32_t mods_depressed, uint32_t mods_latched,
6035 uint32_t mods_locked, uint32_t group)
6036{
6037 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006038 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006039
Daniel Stone351eb612012-05-31 15:27:47 -04006040 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6041 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006042}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006043
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006044static void
6045switcher_cancel(struct weston_keyboard_grab *grab)
6046{
6047 struct switcher *switcher = container_of(grab, struct switcher, grab);
6048
6049 switcher_destroy(switcher);
6050}
6051
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006052static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006053 switcher_key,
6054 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006055 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006056};
6057
6058static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006059switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6060 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006061{
Tiago Vignattibe143262012-04-16 17:31:41 +03006062 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006063 struct switcher *switcher;
6064
6065 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006066 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006067 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006068 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006069 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006070 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006071
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006072 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006073 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006074 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006075 weston_keyboard_start_grab(keyboard, &switcher->grab);
6076 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006077 switcher_next(switcher);
6078}
6079
Pekka Paalanen3c647232011-12-22 13:43:43 +02006080static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006081backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6082 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006083{
6084 struct weston_compositor *compositor = data;
6085 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006086 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006087
6088 /* TODO: we're limiting to simple use cases, where we assume just
6089 * control on the primary display. We'd have to extend later if we
6090 * ever get support for setting backlights on random desktop LCD
6091 * panels though */
6092 output = get_default_output(compositor);
6093 if (!output)
6094 return;
6095
6096 if (!output->set_backlight)
6097 return;
6098
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006099 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6100 backlight_new = output->backlight_current - 25;
6101 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6102 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006103
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006104 if (backlight_new < 5)
6105 backlight_new = 5;
6106 if (backlight_new > 255)
6107 backlight_new = 255;
6108
6109 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006110 output->set_backlight(output, output->backlight_current);
6111}
6112
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006113static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006114force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6115 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006116{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006117 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006118 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006119 struct desktop_shell *shell = data;
6120 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006121 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006122
Derek Foreman8ae2db52015-07-15 13:00:36 -05006123 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006124 if (!focus_surface)
6125 return;
6126
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006127 wl_signal_emit(&compositor->kill_signal, focus_surface);
6128
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006129 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006130 wl_client_get_credentials(client, &pid, NULL, NULL);
6131
6132 /* Skip clients that we launched ourselves (the credentials of
6133 * the socketpair is ours) */
6134 if (pid == getpid())
6135 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006136
Daniel Stone325fc2d2012-05-30 16:31:58 +01006137 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006138}
6139
6140static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006141workspace_up_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 = shell->workspaces.current;
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 if (new_index != 0)
6150 new_index--;
6151
6152 change_workspace(shell, new_index);
6153}
6154
6155static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006156workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006157 uint32_t key, void *data)
6158{
6159 struct desktop_shell *shell = data;
6160 unsigned int new_index = shell->workspaces.current;
6161
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006162 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006163 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006164 if (new_index < shell->workspaces.num - 1)
6165 new_index++;
6166
6167 change_workspace(shell, new_index);
6168}
6169
6170static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006171workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006172 uint32_t key, void *data)
6173{
6174 struct desktop_shell *shell = data;
6175 unsigned int new_index;
6176
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006177 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006178 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006179 new_index = key - KEY_F1;
6180 if (new_index >= shell->workspaces.num)
6181 new_index = shell->workspaces.num - 1;
6182
6183 change_workspace(shell, new_index);
6184}
6185
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006186static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006187workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6188 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006189{
6190 struct desktop_shell *shell = data;
6191 unsigned int new_index = shell->workspaces.current;
6192
6193 if (shell->locked)
6194 return;
6195
6196 if (new_index != 0)
6197 new_index--;
6198
Derek Foreman8ae2db52015-07-15 13:00:36 -05006199 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006200}
6201
6202static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006203workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6204 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006205{
6206 struct desktop_shell *shell = data;
6207 unsigned int new_index = shell->workspaces.current;
6208
6209 if (shell->locked)
6210 return;
6211
6212 if (new_index < shell->workspaces.num - 1)
6213 new_index++;
6214
Derek Foreman8ae2db52015-07-15 13:00:36 -05006215 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006216}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006217
6218static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006219shell_reposition_view_on_output_destroy(struct weston_view *view)
6220{
6221 struct weston_output *output, *first_output;
6222 struct weston_compositor *ec = view->surface->compositor;
6223 struct shell_surface *shsurf;
6224 float x, y;
6225 int visible;
6226
6227 x = view->geometry.x;
6228 y = view->geometry.y;
6229
6230 /* At this point the destroyed output is not in the list anymore.
6231 * If the view is still visible somewhere, we leave where it is,
6232 * otherwise, move it to the first output. */
6233 visible = 0;
6234 wl_list_for_each(output, &ec->output_list, link) {
6235 if (pixman_region32_contains_point(&output->region,
6236 x, y, NULL)) {
6237 visible = 1;
6238 break;
6239 }
6240 }
6241
6242 if (!visible) {
6243 first_output = container_of(ec->output_list.next,
6244 struct weston_output, link);
6245
6246 x = first_output->x + first_output->width / 4;
6247 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006248
6249 weston_view_set_position(view, x, y);
6250 } else {
6251 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006252 }
6253
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006254
6255 shsurf = get_shell_surface(view->surface);
6256
6257 if (shsurf) {
6258 shsurf->saved_position_valid = false;
6259 shsurf->next_state.maximized = false;
6260 shsurf->next_state.fullscreen = false;
6261 shsurf->state_changed = true;
6262 }
6263}
6264
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006265void
6266shell_for_each_layer(struct desktop_shell *shell,
6267 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006268{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006269 struct workspace **ws;
6270
6271 func(shell, &shell->fullscreen_layer, data);
6272 func(shell, &shell->panel_layer, data);
6273 func(shell, &shell->background_layer, data);
6274 func(shell, &shell->lock_layer, data);
6275 func(shell, &shell->input_panel_layer, data);
6276
6277 wl_array_for_each(ws, &shell->workspaces.array)
6278 func(shell, &(*ws)->layer, data);
6279}
6280
6281static void
6282shell_output_destroy_move_layer(struct desktop_shell *shell,
6283 struct weston_layer *layer,
6284 void *data)
6285{
6286 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006287 struct weston_view *view;
6288
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006289 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006290 if (view->output != output)
6291 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006292
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006293 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006294 }
6295}
6296
6297static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006298handle_output_destroy(struct wl_listener *listener, void *data)
6299{
6300 struct shell_output *output_listener =
6301 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006302 struct weston_output *output = output_listener->output;
6303 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006304
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006305 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006306
Xiong Zhang6b481422013-10-23 13:58:32 +08006307 wl_list_remove(&output_listener->destroy_listener.link);
6308 wl_list_remove(&output_listener->link);
6309 free(output_listener);
6310}
6311
6312static void
6313create_shell_output(struct desktop_shell *shell,
6314 struct weston_output *output)
6315{
6316 struct shell_output *shell_output;
6317
6318 shell_output = zalloc(sizeof *shell_output);
6319 if (shell_output == NULL)
6320 return;
6321
6322 shell_output->output = output;
6323 shell_output->shell = shell;
6324 shell_output->destroy_listener.notify = handle_output_destroy;
6325 wl_signal_add(&output->destroy_signal,
6326 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006327 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006328}
6329
6330static void
6331handle_output_create(struct wl_listener *listener, void *data)
6332{
6333 struct desktop_shell *shell =
6334 container_of(listener, struct desktop_shell, output_create_listener);
6335 struct weston_output *output = (struct weston_output *)data;
6336
6337 create_shell_output(shell, output);
6338}
6339
6340static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006341handle_output_move_layer(struct desktop_shell *shell,
6342 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006343{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006344 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006345 struct weston_view *view;
6346 float x, y;
6347
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006348 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006349 if (view->output != output)
6350 continue;
6351
6352 x = view->geometry.x + output->move_x;
6353 y = view->geometry.y + output->move_y;
6354 weston_view_set_position(view, x, y);
6355 }
6356}
6357
6358static void
6359handle_output_move(struct wl_listener *listener, void *data)
6360{
6361 struct desktop_shell *shell;
6362
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006363 shell = container_of(listener, struct desktop_shell,
6364 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006365
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006366 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006367}
6368
6369static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006370setup_output_destroy_handler(struct weston_compositor *ec,
6371 struct desktop_shell *shell)
6372{
6373 struct weston_output *output;
6374
6375 wl_list_init(&shell->output_list);
6376 wl_list_for_each(output, &ec->output_list, link)
6377 create_shell_output(shell, output);
6378
6379 shell->output_create_listener.notify = handle_output_create;
6380 wl_signal_add(&ec->output_created_signal,
6381 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006382
6383 shell->output_move_listener.notify = handle_output_move;
6384 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006385}
6386
6387static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006388shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006389{
Tiago Vignattibe143262012-04-16 17:31:41 +03006390 struct desktop_shell *shell =
6391 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006392 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006393 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006394
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006395 /* Force state to unlocked so we don't try to fade */
6396 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006397
6398 if (shell->child.client) {
6399 /* disable respawn */
6400 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006401 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006402 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006403
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006404 wl_list_remove(&shell->idle_listener.link);
6405 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006406 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006407
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006408 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006409 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006410
Xiong Zhang6b481422013-10-23 13:58:32 +08006411 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6412 wl_list_remove(&shell_output->destroy_listener.link);
6413 wl_list_remove(&shell_output->link);
6414 free(shell_output);
6415 }
6416
6417 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006418 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006419
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006420 wl_array_for_each(ws, &shell->workspaces.array)
6421 workspace_destroy(*ws);
6422 wl_array_release(&shell->workspaces.array);
6423
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006424 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006425 free(shell);
6426}
6427
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006428static void
6429shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6430{
6431 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006432 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006433
Bob Ham744e6532016-01-12 10:21:48 +00006434 if (shell->allow_zap)
6435 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6436 MODIFIER_CTRL | MODIFIER_ALT,
6437 terminate_binding, ec);
6438
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006439 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006440 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6441 click_to_activate_binding,
6442 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006443 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6444 click_to_activate_binding,
6445 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006446 weston_compositor_add_touch_binding(ec, 0,
6447 touch_to_activate_binding,
6448 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006449 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6450 MODIFIER_SUPER | MODIFIER_ALT,
6451 surface_opacity_binding, NULL);
6452 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6453 MODIFIER_SUPER, zoom_axis_binding,
6454 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006455
6456 /* configurable bindings */
6457 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006458 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6459 zoom_key_binding, NULL);
6460 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6461 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006462 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6463 maximize_binding, NULL);
6464 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6465 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006466 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6467 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006468 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006469 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006470 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006471 weston_compositor_add_button_binding(ec, BTN_LEFT,
6472 mod | MODIFIER_SHIFT,
6473 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006474
6475 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006476 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006477 rotate_binding, NULL);
6478
Daniel Stone325fc2d2012-05-30 16:31:58 +01006479 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6480 shell);
6481 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6482 ec);
6483 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6484 backlight_binding, ec);
6485 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6486 ec);
6487 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6488 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006489 weston_compositor_add_key_binding(ec, KEY_K, mod,
6490 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006491 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6492 workspace_up_binding, shell);
6493 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6494 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006495 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6496 workspace_move_surface_up_binding,
6497 shell);
6498 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6499 workspace_move_surface_down_binding,
6500 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006501
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006502 if (shell->exposay_modifier)
6503 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6504 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006505
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006506 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6507 if (shell->workspaces.num > 1) {
6508 num_workspace_bindings = shell->workspaces.num;
6509 if (num_workspace_bindings > 6)
6510 num_workspace_bindings = 6;
6511 for (i = 0; i < num_workspace_bindings; i++)
6512 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6513 workspace_f_binding,
6514 shell);
6515 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006516
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006517 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006518}
6519
Jason Ekstrand024177c2014-04-21 19:42:58 -05006520static void
6521handle_seat_created(struct wl_listener *listener, void *data)
6522{
6523 struct weston_seat *seat = data;
6524
6525 create_shell_seat(seat);
6526}
6527
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006528WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006529module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006530 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006531{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006532 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006533 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006534 struct workspace **pws;
6535 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006536 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006537
Peter Huttererf3d62272013-08-08 11:57:05 +10006538 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006539 if (shell == NULL)
6540 return -1;
6541
Kristian Høgsberg75840622011-09-06 13:48:16 -04006542 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006543
6544 shell->destroy_listener.notify = shell_destroy;
6545 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006546 shell->idle_listener.notify = idle_handler;
6547 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6548 shell->wake_listener.notify = wake_handler;
6549 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006550 shell->transform_listener.notify = transform_handler;
6551 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006552
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006553 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006554 ec->shell_interface.create_shell_surface = create_shell_surface;
6555 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006556 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006557 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006558 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006559 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006560 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006561 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006562 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006563 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006564 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006565
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006566 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6567 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006568 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6569 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006570 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006571
6572 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006573 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006574
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006575 if (input_panel_setup(shell) < 0)
6576 return -1;
6577
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006578 shell->text_backend = text_backend_init(ec);
6579 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006580 return -1;
6581
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006582 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006583
Daniel Stonedf8133b2013-11-19 11:37:14 +01006584 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6585 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6586
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006587 for (i = 0; i < shell->workspaces.num; i++) {
6588 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6589 if (pws == NULL)
6590 return -1;
6591
6592 *pws = workspace_create();
6593 if (*pws == NULL)
6594 return -1;
6595 }
6596 activate_workspace(shell, 0);
6597
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006598 weston_layer_init(&shell->minimized_layer, NULL);
6599
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006600 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006601 wl_list_init(&shell->workspaces.animation.link);
6602 shell->workspaces.animation.frame = animate_workspace_change_frame;
6603
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006604 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006605 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006606 return -1;
6607
Rafael Antognollie2a34552013-12-03 15:35:45 -02006608 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6609 shell, bind_xdg_shell) == NULL)
6610 return -1;
6611
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006612 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006613 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006614 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006615 return -1;
6616
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006617 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006618
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006619 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006620
Xiong Zhang6b481422013-10-23 13:58:32 +08006621 setup_output_destroy_handler(ec, shell);
6622
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006623 loop = wl_display_get_event_loop(ec->wl_display);
6624 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006625
Jason Ekstrand024177c2014-04-21 19:42:58 -05006626 wl_list_for_each(seat, &ec->seat_list, link)
6627 handle_seat_created(NULL, seat);
6628 shell->seat_create_listener.notify = handle_seat_created;
6629 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006630
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006631 screenshooter_create(ec);
6632
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006633 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006634
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006635 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006636
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006637 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6638
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006639 return 0;
6640}