blob: 27a6ca3c0ce5ef3d4bd34641faac9acf99b7a447 [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;
Alex Wu4539b082012-03-01 12:57:46 +0800127 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400161
162 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200163
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700164 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200165 bool maximized;
166 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200167 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100168 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500169 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200170 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500172
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700173 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400174 int32_t x, y, width, height;
175 } geometry, next_geometry;
176 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700177
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500178 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500179
180 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200181};
182
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400184 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300185 struct shell_surface *shsurf;
186 struct wl_listener shsurf_destroy_listener;
187};
188
Rusty Lynch1084da52013-08-15 09:10:08 -0700189struct shell_touch_grab {
190 struct weston_touch_grab grab;
191 struct shell_surface *shsurf;
192 struct wl_listener shsurf_destroy_listener;
193 struct weston_touch *touch;
194};
195
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300196struct weston_move_grab {
197 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100198 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500199 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500200};
201
Rusty Lynch1084da52013-08-15 09:10:08 -0700202struct weston_touch_move_grab {
203 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800204 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700205 wl_fixed_t dx, dy;
206};
207
Pekka Paalanen460099f2012-01-20 16:48:25 +0200208struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500210 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200212 float x;
213 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 } center;
215};
216
Giulio Camuffo5085a752013-03-25 21:42:45 +0100217struct shell_seat {
218 struct weston_seat *seat;
219 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500220 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100221
Jason Ekstrand024177c2014-04-21 19:42:58 -0500222 struct wl_listener caps_changed_listener;
223 struct wl_listener pointer_focus_listener;
224 struct wl_listener keyboard_focus_listener;
225
Giulio Camuffo5085a752013-03-25 21:42:45 +0100226 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400227 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200228 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100229 struct wl_list surfaces_list;
230 struct wl_client *client;
231 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200232 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100233 } popup_grab;
234};
235
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800236struct shell_client {
237 struct wl_resource *resource;
238 struct wl_client *client;
239 struct desktop_shell *shell;
240 struct wl_listener destroy_listener;
241 struct wl_event_source *ping_timer;
242 uint32_t ping_serial;
243 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800244 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800245};
246
Alex Wubd3354b2012-04-17 17:20:49 +0800247static struct desktop_shell *
248shell_surface_get_shell(struct shell_surface *shsurf);
249
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500250static void
Derek Foreman74de4692015-07-15 13:00:39 -0500251surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500252
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300253static void
254shell_fade_startup(struct desktop_shell *shell);
255
Philip Withnallbecb77e2013-11-25 18:01:30 +0000256static struct shell_seat *
257get_shell_seat(struct weston_seat *seat);
258
Jonny Lambd73c6942014-08-20 15:53:20 +0200259static void
260get_output_panel_size(struct desktop_shell *shell,
261 struct weston_output *output,
262 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700263
Philip Withnall648a4dd2013-11-25 18:01:44 +0000264static void
265shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
266
Alex Wubd3354b2012-04-17 17:20:49 +0800267static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200268shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
269
270static bool
271shell_surface_is_xdg_surface(struct shell_surface *shsurf);
272
273static bool
274shell_surface_is_xdg_popup(struct shell_surface *shsurf);
275
276static void
277shell_surface_set_parent(struct shell_surface *shsurf,
278 struct weston_surface *parent);
279
Pekka Paalanen8274d902014-08-06 19:36:51 +0300280static int
281shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
282{
283 struct shell_surface *shsurf;
284 const char *typestr[] = {
285 [SHELL_SURFACE_NONE] = "unidentified",
286 [SHELL_SURFACE_TOPLEVEL] = "top-level",
287 [SHELL_SURFACE_POPUP] = "popup",
288 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
289 };
290 const char *t, *c;
291
292 shsurf = get_shell_surface(surface);
293 if (!shsurf)
294 return snprintf(buf, len, "unidentified window");
295
296 t = shsurf->title;
297 c = shsurf->class;
298
299 return snprintf(buf, len, "%s window%s%s%s%s%s",
300 typestr[shsurf->type],
301 t ? " '" : "", t ?: "", t ? "'" : "",
302 c ? " of " : "", c ?: "");
303}
304
Rafael Antognollie2a34552013-12-03 15:35:45 -0200305static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800306shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
307{
308 struct desktop_shell *shell;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200309 struct weston_view *view;
310 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800311
312 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100313
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300314 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800315 return false;
316
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200317 /* Find topmost shsurf on the same fullscreen output on which shsurf
318 * is displaying. We don't care about other outputs.
319 */
320 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
321 layer_link.link) {
322 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
323
324 if (cand_shsurf &&
325 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
326 top_fs_shsurf = cand_shsurf;
327 break;
328 }
329 }
330
331 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800332}
333
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500334static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400335destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336{
337 struct shell_grab *grab;
338
339 grab = container_of(listener, struct shell_grab,
340 shsurf_destroy_listener);
341
342 grab->shsurf = NULL;
343}
344
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800345struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500346get_default_view(struct weston_surface *surface)
347{
348 struct shell_surface *shsurf;
349 struct weston_view *view;
350
351 if (!surface || wl_list_empty(&surface->views))
352 return NULL;
353
354 shsurf = get_shell_surface(surface);
355 if (shsurf)
356 return shsurf->view;
357
358 wl_list_for_each(view, &surface->views, surface_link)
359 if (weston_view_is_mapped(view))
360 return view;
361
362 return container_of(surface->views.next, struct weston_view, surface_link);
363}
364
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300365static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400366popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200367static void
368touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400369
370static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300371shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400372 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300373 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400374 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800375 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300376{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300377 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500378 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300379
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400380 popup_grab_end(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -0500381 if (touch)
382 touch_popup_grab_end(touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400383
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300384 grab->grab.interface = interface;
385 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400386 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500387 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400388 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300389
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700390 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400391 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400392 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800393 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400394 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395 weston_pointer_set_focus(pointer,
396 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400397 wl_fixed_from_int(0),
398 wl_fixed_from_int(0));
399 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300400}
401
Jonny Lambd73c6942014-08-20 15:53:20 +0200402static void
403get_output_panel_size(struct desktop_shell *shell,
404 struct weston_output *output,
405 int *width,
406 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700407{
408 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200409
410 *width = 0;
411 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700412
413 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200414 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700415
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300416 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200417 float x, y;
418
419 if (view->surface->output != output)
420 continue;
421
422 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800423 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
424 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200425 weston_view_to_global_float(view,
426 view->surface->width, 0,
427 &x, &y);
428
Derek Foreman612341f2015-04-15 12:23:55 -0500429 *width = (int)x - output->x;
430 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200431 return;
432
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800433 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
434 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200435 weston_view_to_global_float(view,
436 0, view->surface->height,
437 &x, &y);
438
Derek Foreman612341f2015-04-15 12:23:55 -0500439 *width = view->surface->width + (int)x - output->x;
440 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200441 return;
442
443 default:
444 /* we've already set width and height to
445 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700446 break;
447 }
448 }
449
Jonny Lambd73c6942014-08-20 15:53:20 +0200450 /* the correct view wasn't found */
451}
452
453static void
454get_output_work_area(struct desktop_shell *shell,
455 struct weston_output *output,
456 pixman_rectangle32_t *area)
457{
458 int32_t panel_width = 0, panel_height = 0;
459
Derek Foremanf814c5d2015-04-15 12:23:54 -0500460 area->x = output->x;
461 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200462
463 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200464 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800465 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200466 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500467 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800468 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200469 area->width = output->width;
470 area->height = output->height - panel_height;
471 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800472 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500473 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800474 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200475 area->width = output->width - panel_width;
476 area->height = output->height;
477 break;
478 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700479}
480
Derek Foreman45a7c272015-09-11 14:27:40 -0500481static struct shell_surface *
482find_toplevel_surface(struct shell_surface *in_surface)
483{
484 struct shell_surface *surface = in_surface;
485
Ben Hummon445e44c2015-10-06 11:48:14 -0500486 while (surface) {
487 if (surface->type == SHELL_SURFACE_TOPLEVEL)
488 return surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500489 surface = get_shell_surface(surface->parent);
Ben Hummon445e44c2015-10-06 11:48:14 -0500490 }
Derek Foreman45a7c272015-09-11 14:27:40 -0500491
492 /* If no top level surface was found, just use whatever surface was
493 originally provided. */
Ben Hummon445e44c2015-10-06 11:48:14 -0500494 return in_surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500495}
496
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700497static void
498send_configure_for_surface(struct shell_surface *shsurf)
499{
500 int32_t width, height;
501 struct surface_state *state;
502
503 if (shsurf->state_requested)
504 state = &shsurf->requested_state;
505 else if (shsurf->state_changed)
506 state = &shsurf->next_state;
507 else
508 state = &shsurf->state;
509
510 if (state->fullscreen) {
511 width = shsurf->output->width;
512 height = shsurf->output->height;
513 } else if (state->maximized) {
514 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200515 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700516
517 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200518 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700519
Jonny Lambd73c6942014-08-20 15:53:20 +0200520 width = area.width;
521 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900522 } else if (shsurf->resize_edges) {
523 width = shsurf->geometry.width;
524 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700525 } else {
526 width = 0;
527 height = 0;
528 }
529
530 shsurf->client->send_configure(shsurf->surface, width, height);
531}
532
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300533static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400534shell_surface_state_changed(struct shell_surface *shsurf)
535{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700536 if (shell_surface_is_xdg_surface(shsurf))
537 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400538}
539
540static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300541shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300542{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700543 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400544 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700545 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400546
547 if (grab->shsurf->resize_edges) {
548 grab->shsurf->resize_edges = 0;
549 shell_surface_state_changed(grab->shsurf);
550 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700551 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300552
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700553 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300554}
555
556static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700557shell_touch_grab_start(struct shell_touch_grab *grab,
558 const struct weston_touch_grab_interface *interface,
559 struct shell_surface *shsurf,
560 struct weston_touch *touch)
561{
562 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500563 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800564
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200565 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500566 if (pointer)
567 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700568
Rusty Lynch1084da52013-08-15 09:10:08 -0700569 grab->grab.interface = interface;
570 grab->shsurf = shsurf;
571 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
572 wl_signal_add(&shsurf->destroy_signal,
573 &grab->shsurf_destroy_listener);
574
575 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700576 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700577
578 weston_touch_start_grab(touch, &grab->grab);
579 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500580 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500581 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700582}
583
584static void
585shell_touch_grab_end(struct shell_touch_grab *grab)
586{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700587 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700588 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700589 grab->shsurf->grabbed = 0;
590 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700591
592 weston_touch_end_grab(grab->touch);
593}
594
595static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500596center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800597 struct weston_output *output);
598
Daniel Stone496ca172012-05-30 16:31:42 +0100599static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300600get_modifier(char *modifier)
601{
602 if (!modifier)
603 return MODIFIER_SUPER;
604
605 if (!strcmp("ctrl", modifier))
606 return MODIFIER_CTRL;
607 else if (!strcmp("alt", modifier))
608 return MODIFIER_ALT;
609 else if (!strcmp("super", modifier))
610 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000611 else if (!strcmp("none", modifier))
612 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300613 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");
Bob Ham553d1242016-01-12 10:21:49 +0000663 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800664 free(s);
665
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400666 weston_config_section_get_string(section, "animation", &s, "none");
667 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200668 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200669 weston_config_section_get_string(section, "close-animation", &s, "fade");
670 shell->win_close_animation_type = get_animation_type(s);
671 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700672 weston_config_section_get_string(section,
673 "startup-animation", &s, "fade");
674 shell->startup_animation_type = get_animation_type(s);
675 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700676 if (shell->startup_animation_type == ANIMATION_ZOOM)
677 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100678 weston_config_section_get_string(section, "focus-animation", &s, "none");
679 shell->focus_animation_type = get_animation_type(s);
680 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400681 weston_config_section_get_uint(section, "num-workspaces",
682 &shell->workspaces.num,
683 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200684}
685
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800686struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100687get_default_output(struct weston_compositor *compositor)
688{
689 return container_of(compositor->output_list.next,
690 struct weston_output, link);
691}
692
Pekka Paalanen8274d902014-08-06 19:36:51 +0300693static int
694focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
695{
696 return snprintf(buf, len, "focus highlight effect for output %s",
697 surface->output->name);
698}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100699
700/* no-op func for checking focus surface */
701static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600702focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100703{
704}
705
706static struct focus_surface *
707get_focus_surface(struct weston_surface *surface)
708{
709 if (surface->configure == focus_surface_configure)
710 return surface->configure_private;
711 else
712 return NULL;
713}
714
715static bool
716is_focus_surface (struct weston_surface *es)
717{
718 return (es->configure == focus_surface_configure);
719}
720
721static bool
722is_focus_view (struct weston_view *view)
723{
724 return is_focus_surface (view->surface);
725}
726
727static struct focus_surface *
728create_focus_surface(struct weston_compositor *ec,
729 struct weston_output *output)
730{
731 struct focus_surface *fsurf = NULL;
732 struct weston_surface *surface = NULL;
733
734 fsurf = malloc(sizeof *fsurf);
735 if (!fsurf)
736 return NULL;
737
738 fsurf->surface = weston_surface_create(ec);
739 surface = fsurf->surface;
740 if (surface == NULL) {
741 free(fsurf);
742 return NULL;
743 }
744
745 surface->configure = focus_surface_configure;
746 surface->output = output;
747 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300748 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100749
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800750 fsurf->view = weston_view_create(surface);
751 if (fsurf->view == NULL) {
752 weston_surface_destroy(surface);
753 free(fsurf);
754 return NULL;
755 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100756 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600758 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600759 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100760 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
761 pixman_region32_fini(&surface->opaque);
762 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
763 output->width, output->height);
764 pixman_region32_fini(&surface->input);
765 pixman_region32_init(&surface->input);
766
767 wl_list_init(&fsurf->workspace_transform.link);
768
769 return fsurf;
770}
771
772static void
773focus_surface_destroy(struct focus_surface *fsurf)
774{
775 weston_surface_destroy(fsurf->surface);
776 free(fsurf);
777}
778
779static void
780focus_animation_done(struct weston_view_animation *animation, void *data)
781{
782 struct workspace *ws = data;
783
784 ws->focus_animation = NULL;
785}
786
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200787static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200788focus_state_destroy(struct focus_state *state)
789{
790 wl_list_remove(&state->seat_destroy_listener.link);
791 wl_list_remove(&state->surface_destroy_listener.link);
792 free(state);
793}
794
795static void
796focus_state_seat_destroy(struct wl_listener *listener, void *data)
797{
798 struct focus_state *state = container_of(listener,
799 struct focus_state,
800 seat_destroy_listener);
801
802 wl_list_remove(&state->link);
803 focus_state_destroy(state);
804}
805
806static void
807focus_state_surface_destroy(struct wl_listener *listener, void *data)
808{
809 struct focus_state *state = container_of(listener,
810 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400811 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400812 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500813 struct weston_surface *main_surface, *next;
814 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200815
Pekka Paalanen01388e22013-04-25 13:57:44 +0300816 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
817
Kristian Høgsberge3778222012-07-31 17:29:30 -0400818 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300819 wl_list_for_each(view,
820 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500821 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400822 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100823 if (is_focus_view(view))
824 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400825
Jason Ekstranda7af7042013-10-12 22:38:11 -0500826 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400827 break;
828 }
829
Pekka Paalanen01388e22013-04-25 13:57:44 +0300830 /* if the focus was a sub-surface, activate its main surface */
831 if (main_surface != state->keyboard_focus)
832 next = main_surface;
833
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400835 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100837 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400838 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
840 if (state->ws->focus_animation)
841 weston_view_animation_destroy(state->ws->focus_animation);
842
843 state->ws->focus_animation = weston_fade_run(
844 state->ws->fsurf_front->view,
845 state->ws->fsurf_front->view->alpha, 0.0, 300,
846 focus_animation_done, state->ws);
847 }
848
Kristian Høgsberge3778222012-07-31 17:29:30 -0400849 wl_list_remove(&state->link);
850 focus_state_destroy(state);
851 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200852}
853
854static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400855focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200856{
Jonas Ådahl04769742012-06-13 00:01:24 +0200857 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200858
859 state = malloc(sizeof *state);
860 if (state == NULL)
861 return NULL;
862
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400864 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200865 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400866 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200867
868 state->seat_destroy_listener.notify = focus_state_seat_destroy;
869 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400870 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200871 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400872 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200873
874 return state;
875}
876
Jonas Ådahl8538b222012-08-29 22:13:03 +0200877static struct focus_state *
878ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
879{
880 struct workspace *ws = get_current_workspace(shell);
881 struct focus_state *state;
882
883 wl_list_for_each(state, &ws->focus_list, link)
884 if (state->seat == seat)
885 break;
886
887 if (&state->link == &ws->focus_list)
888 state = focus_state_create(seat, ws);
889
890 return state;
891}
892
Jonas Ådahl04769742012-06-13 00:01:24 +0200893static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800894focus_state_set_focus(struct focus_state *state,
895 struct weston_surface *surface)
896{
897 if (state->keyboard_focus) {
898 wl_list_remove(&state->surface_destroy_listener.link);
899 wl_list_init(&state->surface_destroy_listener.link);
900 }
901
902 state->keyboard_focus = surface;
903 if (surface)
904 wl_signal_add(&surface->destroy_signal,
905 &state->surface_destroy_listener);
906}
907
908static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400909restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200910{
911 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400912 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100913 struct wl_list pending_seat_list;
914 struct weston_seat *seat, *next_seat;
915
916 /* Temporarily steal the list of seats so that we can keep
917 * track of the seats we've already processed */
918 wl_list_init(&pending_seat_list);
919 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
920 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200921
922 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500923 struct weston_keyboard *keyboard =
924 weston_seat_get_keyboard(state->seat);
925
Neil Roberts4237f502014-04-09 16:33:31 +0100926 wl_list_remove(&state->seat->link);
927 wl_list_insert(&shell->compositor->seat_list,
928 &state->seat->link);
929
Derek Foreman1281a362015-07-31 16:55:32 -0500930 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800931 continue;
932
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400933 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200934
Derek Foreman1281a362015-07-31 16:55:32 -0500935 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200936 }
Neil Roberts4237f502014-04-09 16:33:31 +0100937
938 /* For any remaining seats that we don't have a focus state
939 * for we'll reset the keyboard focus to NULL */
940 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500941 struct weston_keyboard *keyboard =
942 weston_seat_get_keyboard(seat);
943
Neil Roberts4237f502014-04-09 16:33:31 +0100944 wl_list_insert(&shell->compositor->seat_list, &seat->link);
945
Derek Foreman1281a362015-07-31 16:55:32 -0500946 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100947 continue;
948
Derek Foreman1281a362015-07-31 16:55:32 -0500949 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100950 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200951}
952
953static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200954replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
955 struct weston_seat *seat)
956{
Derek Foreman1281a362015-07-31 16:55:32 -0500957 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200958 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959
960 wl_list_for_each(state, &ws->focus_list, link) {
961 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500962 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200963 return;
964 }
965 }
966}
967
968static void
969drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
970 struct weston_surface *surface)
971{
972 struct focus_state *state;
973
974 wl_list_for_each(state, &ws->focus_list, link)
975 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800976 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200977}
978
979static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100980animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
981 struct weston_view *from, struct weston_view *to)
982{
983 struct weston_output *output;
984 bool focus_surface_created = false;
985
986 /* FIXME: Only support dim animation using two layers */
987 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
988 return;
989
990 output = get_default_output(shell->compositor);
991 if (ws->fsurf_front == NULL && (from || to)) {
992 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800993 if (ws->fsurf_front == NULL)
994 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100995 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800996
997 ws->fsurf_back = create_focus_surface(shell->compositor, output);
998 if (ws->fsurf_back == NULL) {
999 focus_surface_destroy(ws->fsurf_front);
1000 return;
1001 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001003
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 focus_surface_created = true;
1005 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001006 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1007 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001008 }
1009
1010 if (ws->focus_animation) {
1011 weston_view_animation_destroy(ws->focus_animation);
1012 ws->focus_animation = NULL;
1013 }
1014
1015 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001016 weston_layer_entry_insert(&to->layer_link,
1017 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001018 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001019 weston_layer_entry_insert(&ws->layer.view_list,
1020 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001021
1022 if (focus_surface_created) {
1023 ws->focus_animation = weston_fade_run(
1024 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001025 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026 focus_animation_done, ws);
1027 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001028 weston_layer_entry_insert(&from->layer_link,
1029 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001030 ws->focus_animation = weston_stable_fade_run(
1031 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001032 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001033 focus_animation_done, ws);
1034 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001035 weston_layer_entry_insert(&ws->layer.view_list,
1036 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 ws->focus_animation = weston_stable_fade_run(
1038 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001039 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001040 focus_animation_done, ws);
1041 }
1042}
1043
1044static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001045workspace_destroy(struct workspace *ws)
1046{
Jonas Ådahl04769742012-06-13 00:01:24 +02001047 struct focus_state *state, *next;
1048
1049 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1050 focus_state_destroy(state);
1051
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001052 if (ws->fsurf_front)
1053 focus_surface_destroy(ws->fsurf_front);
1054 if (ws->fsurf_back)
1055 focus_surface_destroy(ws->fsurf_back);
1056
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001057 free(ws);
1058}
1059
Jonas Ådahl04769742012-06-13 00:01:24 +02001060static void
1061seat_destroyed(struct wl_listener *listener, void *data)
1062{
1063 struct weston_seat *seat = data;
1064 struct focus_state *state, *next;
1065 struct workspace *ws = container_of(listener,
1066 struct workspace,
1067 seat_destroyed_listener);
1068
1069 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1070 if (state->seat == seat)
1071 wl_list_remove(&state->link);
1072}
1073
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001074static struct workspace *
1075workspace_create(void)
1076{
1077 struct workspace *ws = malloc(sizeof *ws);
1078 if (ws == NULL)
1079 return NULL;
1080
1081 weston_layer_init(&ws->layer, NULL);
1082
Jonas Ådahl04769742012-06-13 00:01:24 +02001083 wl_list_init(&ws->focus_list);
1084 wl_list_init(&ws->seat_destroyed_listener.link);
1085 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001086 ws->fsurf_front = NULL;
1087 ws->fsurf_back = NULL;
1088 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001089
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001090 return ws;
1091}
1092
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001093static int
1094workspace_is_empty(struct workspace *ws)
1095{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001096 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097}
1098
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001099static struct workspace *
1100get_workspace(struct desktop_shell *shell, unsigned int index)
1101{
1102 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001103 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001104 pws += index;
1105 return *pws;
1106}
1107
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001108struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001109get_current_workspace(struct desktop_shell *shell)
1110{
1111 return get_workspace(shell, shell->workspaces.current);
1112}
1113
1114static void
1115activate_workspace(struct desktop_shell *shell, unsigned int index)
1116{
1117 struct workspace *ws;
1118
1119 ws = get_workspace(shell, index);
1120 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1121
1122 shell->workspaces.current = index;
1123}
1124
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125static unsigned int
1126get_output_height(struct weston_output *output)
1127{
1128 return abs(output->region.extents.y1 - output->region.extents.y2);
1129}
1130
1131static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001132view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134 struct weston_transform *transform;
1135
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001136 if (is_focus_view(view)) {
1137 struct focus_surface *fsurf = get_focus_surface(view->surface);
1138 transform = &fsurf->workspace_transform;
1139 } else {
1140 struct shell_surface *shsurf = get_shell_surface(view->surface);
1141 transform = &shsurf->workspace_transform;
1142 }
1143
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001146 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001148 weston_matrix_init(&transform->matrix);
1149 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152}
1153
1154static void
1155workspace_translate_out(struct workspace *ws, double fraction)
1156{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001158 unsigned int height;
1159 double d;
1160
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001161 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163 d = height * fraction;
1164
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001165 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166 }
1167}
1168
1169static void
1170workspace_translate_in(struct workspace *ws, double fraction)
1171{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173 unsigned int height;
1174 double d;
1175
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001176 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001178
1179 if (fraction > 0)
1180 d = -(height - height * fraction);
1181 else
1182 d = height + height * fraction;
1183
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001184 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185 }
1186}
1187
1188static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001189reverse_workspace_change_animation(struct desktop_shell *shell,
1190 unsigned int index,
1191 struct workspace *from,
1192 struct workspace *to)
1193{
1194 shell->workspaces.current = index;
1195
1196 shell->workspaces.anim_to = to;
1197 shell->workspaces.anim_from = from;
1198 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1199 shell->workspaces.anim_timestamp = 0;
1200
Scott Moreau4272e632012-08-13 09:58:41 -06001201 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202}
1203
1204static void
1205workspace_deactivate_transforms(struct workspace *ws)
1206{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001208 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001209
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001210 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001211 if (is_focus_view(view)) {
1212 struct focus_surface *fsurf = get_focus_surface(view->surface);
1213 transform = &fsurf->workspace_transform;
1214 } else {
1215 struct shell_surface *shsurf = get_shell_surface(view->surface);
1216 transform = &shsurf->workspace_transform;
1217 }
1218
1219 if (!wl_list_empty(&transform->link)) {
1220 wl_list_remove(&transform->link);
1221 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001222 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001224 }
1225}
1226
1227static void
1228finish_workspace_change_animation(struct desktop_shell *shell,
1229 struct workspace *from,
1230 struct workspace *to)
1231{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001232 struct weston_view *view;
1233
Scott Moreau4272e632012-08-13 09:58:41 -06001234 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001235
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001236 /* Views that extend past the bottom of the output are still
1237 * visible after the workspace animation ends but before its layer
1238 * is hidden. In that case, we need to damage below those views so
1239 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001240 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001241 weston_view_damage_below(view);
1242
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001243 wl_list_remove(&shell->workspaces.animation.link);
1244 workspace_deactivate_transforms(from);
1245 workspace_deactivate_transforms(to);
1246 shell->workspaces.anim_to = NULL;
1247
1248 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1249}
1250
1251static void
1252animate_workspace_change_frame(struct weston_animation *animation,
1253 struct weston_output *output, uint32_t msecs)
1254{
1255 struct desktop_shell *shell =
1256 container_of(animation, struct desktop_shell,
1257 workspaces.animation);
1258 struct workspace *from = shell->workspaces.anim_from;
1259 struct workspace *to = shell->workspaces.anim_to;
1260 uint32_t t;
1261 double x, y;
1262
1263 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1264 finish_workspace_change_animation(shell, from, to);
1265 return;
1266 }
1267
1268 if (shell->workspaces.anim_timestamp == 0) {
1269 if (shell->workspaces.anim_current == 0.0)
1270 shell->workspaces.anim_timestamp = msecs;
1271 else
1272 shell->workspaces.anim_timestamp =
1273 msecs -
1274 /* Invers of movement function 'y' below. */
1275 (asin(1.0 - shell->workspaces.anim_current) *
1276 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1277 M_2_PI);
1278 }
1279
1280 t = msecs - shell->workspaces.anim_timestamp;
1281
1282 /*
1283 * x = [0, π/2]
1284 * y(x) = sin(x)
1285 */
1286 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1287 y = sin(x);
1288
1289 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001290 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001291
1292 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1293 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1294 shell->workspaces.anim_current = y;
1295
Scott Moreau4272e632012-08-13 09:58:41 -06001296 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001297 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001298 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001299 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001300}
1301
1302static void
1303animate_workspace_change(struct desktop_shell *shell,
1304 unsigned int index,
1305 struct workspace *from,
1306 struct workspace *to)
1307{
1308 struct weston_output *output;
1309
1310 int dir;
1311
1312 if (index > shell->workspaces.current)
1313 dir = -1;
1314 else
1315 dir = 1;
1316
1317 shell->workspaces.current = index;
1318
1319 shell->workspaces.anim_dir = dir;
1320 shell->workspaces.anim_from = from;
1321 shell->workspaces.anim_to = to;
1322 shell->workspaces.anim_current = 0.0;
1323 shell->workspaces.anim_timestamp = 0;
1324
1325 output = container_of(shell->compositor->output_list.next,
1326 struct weston_output, link);
1327 wl_list_insert(&output->animation_list,
1328 &shell->workspaces.animation.link);
1329
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001330 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001331
1332 workspace_translate_in(to, 0);
1333
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001334 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001335
Scott Moreau4272e632012-08-13 09:58:41 -06001336 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001337}
1338
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001339static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001340update_workspace(struct desktop_shell *shell, unsigned int index,
1341 struct workspace *from, struct workspace *to)
1342{
1343 shell->workspaces.current = index;
1344 wl_list_insert(&from->layer.link, &to->layer.link);
1345 wl_list_remove(&from->layer.link);
1346}
1347
1348static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001349change_workspace(struct desktop_shell *shell, unsigned int index)
1350{
1351 struct workspace *from;
1352 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001353 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001354
1355 if (index == shell->workspaces.current)
1356 return;
1357
1358 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001359 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001360 return;
1361
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001362 from = get_current_workspace(shell);
1363 to = get_workspace(shell, index);
1364
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001365 if (shell->workspaces.anim_from == to &&
1366 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001367 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001368 reverse_workspace_change_animation(shell, index, from, to);
1369 return;
1370 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001371
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001372 if (shell->workspaces.anim_to != NULL)
1373 finish_workspace_change_animation(shell,
1374 shell->workspaces.anim_from,
1375 shell->workspaces.anim_to);
1376
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001377 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001378
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001379 if (shell->focus_animation_type != ANIMATION_NONE) {
1380 wl_list_for_each(state, &from->focus_list, link)
1381 if (state->keyboard_focus)
1382 animate_focus_change(shell, from,
1383 get_default_view(state->keyboard_focus), NULL);
1384
1385 wl_list_for_each(state, &to->focus_list, link)
1386 if (state->keyboard_focus)
1387 animate_focus_change(shell, to,
1388 NULL, get_default_view(state->keyboard_focus));
1389 }
1390
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001391 if (workspace_is_empty(to) && workspace_is_empty(from))
1392 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001393 else
1394 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001395}
1396
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001397static bool
1398workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1399{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001400 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001401 struct wl_list *e;
1402
1403 if (wl_list_empty(list))
1404 return false;
1405
1406 e = list->next;
1407
1408 if (e->next != list)
1409 return false;
1410
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001411 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001412}
1413
1414static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001415surface_keyboard_focus_lost(struct weston_surface *surface)
1416{
1417 struct weston_compositor *compositor = surface->compositor;
1418 struct weston_seat *seat;
1419 struct weston_surface *focus;
1420
1421 wl_list_for_each(seat, &compositor->seat_list, link) {
1422 struct weston_keyboard *keyboard =
1423 weston_seat_get_keyboard(seat);
1424
1425 if (!keyboard)
1426 continue;
1427
1428 focus = weston_surface_get_main_surface(keyboard->focus);
1429 if (focus == surface)
1430 weston_keyboard_set_focus(keyboard, NULL);
1431 }
1432}
1433
1434static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001435take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001436 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001437 unsigned int index)
1438{
Derek Foreman1281a362015-07-31 16:55:32 -05001439 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001440 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001442 struct shell_surface *shsurf;
1443 struct workspace *from;
1444 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001445 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446
Derek Foreman1281a362015-07-31 16:55:32 -05001447 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 view = get_default_view(surface);
1449 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001450 index == shell->workspaces.current ||
1451 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001452 return;
1453
1454 from = get_current_workspace(shell);
1455 to = get_workspace(shell, index);
1456
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001457 weston_layer_entry_remove(&view->layer_link);
1458 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001459
Philip Withnall659163d2013-11-25 18:01:36 +00001460 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001461 if (shsurf != NULL)
1462 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001463
Jonas Ådahle9d22502012-08-29 22:13:01 +02001464 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001465 drop_focus_state(shell, from, surface);
1466
1467 if (shell->workspaces.anim_from == to &&
1468 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001469 wl_list_remove(&to->layer.link);
1470 wl_list_insert(from->layer.link.prev, &to->layer.link);
1471
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001472 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001473
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001474 return;
1475 }
1476
1477 if (shell->workspaces.anim_to != NULL)
1478 finish_workspace_change_animation(shell,
1479 shell->workspaces.anim_from,
1480 shell->workspaces.anim_to);
1481
1482 if (workspace_is_empty(from) &&
1483 workspace_has_only(to, surface))
1484 update_workspace(shell, index, from, to);
1485 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001486 if (shsurf != NULL &&
1487 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001488 wl_list_insert(&shell->workspaces.anim_sticky_list,
1489 &shsurf->workspace_transform.link);
1490
1491 animate_workspace_change(shell, index, from, to);
1492 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001493
Jonas Ådahl8538b222012-08-29 22:13:03 +02001494 state = ensure_focus_state(shell, seat);
1495 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001496 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001497}
1498
1499static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001500touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001501 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001502{
1503}
1504
1505static void
1506touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1507{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001508 struct weston_touch_move_grab *move =
1509 (struct weston_touch_move_grab *) container_of(
1510 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001511
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001512 if (touch_id == 0)
1513 move->active = 0;
1514
Jonas Ådahl9484b692013-12-02 22:05:03 +01001515 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001516 shell_touch_grab_end(&move->base);
1517 free(move);
1518 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001519}
1520
1521static void
1522touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001523 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001524{
1525 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1526 struct shell_surface *shsurf = move->base.shsurf;
1527 struct weston_surface *es;
1528 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1529 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1530
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001531 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001532 return;
1533
1534 es = shsurf->surface;
1535
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001536 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001537
1538 weston_compositor_schedule_repaint(es->compositor);
1539}
1540
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001541static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001542touch_move_grab_frame(struct weston_touch_grab *grab)
1543{
1544}
1545
1546static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001547touch_move_grab_cancel(struct weston_touch_grab *grab)
1548{
1549 struct weston_touch_move_grab *move =
1550 (struct weston_touch_move_grab *) container_of(
1551 grab, struct shell_touch_grab, grab);
1552
1553 shell_touch_grab_end(&move->base);
1554 free(move);
1555}
1556
Rusty Lynch1084da52013-08-15 09:10:08 -07001557static const struct weston_touch_grab_interface touch_move_grab_interface = {
1558 touch_move_grab_down,
1559 touch_move_grab_up,
1560 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001561 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001562 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001563};
1564
1565static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001566surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001567{
1568 struct weston_touch_move_grab *move;
1569
1570 if (!shsurf)
1571 return -1;
1572
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001573 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001574 return 0;
1575
1576 move = malloc(sizeof *move);
1577 if (!move)
1578 return -1;
1579
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001580 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001582 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001584 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001585
1586 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001587 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001588
1589 return 0;
1590}
1591
1592static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001593noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001594{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001595}
1596
1597static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001598noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001599 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001600{
1601}
1602
1603static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001604noop_grab_axis_source(struct weston_pointer_grab *grab,
1605 uint32_t source)
1606{
1607}
1608
1609static void
1610noop_grab_frame(struct weston_pointer_grab *grab)
1611{
1612}
1613
1614static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001615constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1616{
1617 struct shell_surface *shsurf = move->base.shsurf;
1618 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001619 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001620 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001621 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001622
1623 x = wl_fixed_to_int(pointer->x + move->dx);
1624 y = wl_fixed_to_int(pointer->y + move->dy);
1625
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001626 if (shsurf->shell->panel_position ==
1627 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001628 get_output_work_area(shsurf->shell,
1629 shsurf->surface->output,
1630 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001631
Derek Foreman6feb0f92015-04-15 12:23:56 -05001632 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1633 if (bottom - safety < area.y)
1634 y = area.y + safety - shsurf->geometry.height
1635 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001636
1637 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001638 y + shsurf->geometry.y < area.y)
1639 y = area.y - shsurf->geometry.y;
1640
Jonny Lambd73c6942014-08-20 15:53:20 +02001641 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001642
1643 *cx = x;
1644 *cy = y;
1645}
1646
1647static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001648move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001649 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001650{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001651 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001652 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001653 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001654 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001655
Jonas Ådahld2510102014-10-05 21:39:14 +02001656 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001657 if (!shsurf)
1658 return;
1659
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001660 constrain_position(move, &cx, &cy);
1661
1662 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001665}
1666
1667static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001668move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001669 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001670{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001671 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1672 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001673 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001674 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001675
Daniel Stone4dbadb12012-05-30 16:31:51 +01001676 if (pointer->button_count == 0 &&
1677 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001678 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001679 free(grab);
1680 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001681}
1682
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001683static void
1684move_grab_cancel(struct weston_pointer_grab *grab)
1685{
1686 struct shell_grab *shell_grab =
1687 container_of(grab, struct shell_grab, grab);
1688
1689 shell_grab_end(shell_grab);
1690 free(grab);
1691}
1692
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001693static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001694 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001695 move_grab_motion,
1696 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001697 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001698 noop_grab_axis_source,
1699 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001700 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001701};
1702
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001704surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001705 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706{
1707 struct weston_move_grab *move;
1708
1709 if (!shsurf)
1710 return -1;
1711
Derek Foreman45a7c272015-09-11 14:27:40 -05001712 shsurf = find_toplevel_surface(shsurf);
1713
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001714 if (shsurf->grabbed ||
1715 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716 return 0;
1717
1718 move = malloc(sizeof *move);
1719 if (!move)
1720 return -1;
1721
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001723 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001725 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001726 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
1728 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001729 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730
1731 return 0;
1732}
1733
1734static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001735common_surface_move(struct wl_resource *resource,
1736 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001738 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001739 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1740 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001741 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001742 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743
Derek Foreman1281a362015-07-31 16:55:32 -05001744 if (pointer &&
1745 pointer->focus &&
1746 pointer->button_count > 0 &&
1747 pointer->grab_serial == serial) {
1748 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001749 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001750 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001751 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001752 } else if (touch &&
1753 touch->focus &&
1754 touch->grab_serial == serial) {
1755 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001756 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001757 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001758 wl_resource_post_no_memory(resource);
1759 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760}
1761
Rafael Antognollie2a34552013-12-03 15:35:45 -02001762static void
1763shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1764 struct wl_resource *seat_resource, uint32_t serial)
1765{
1766 common_surface_move(resource, seat_resource, serial);
1767}
1768
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001769struct weston_resize_grab {
1770 struct shell_grab base;
1771 uint32_t edges;
1772 int32_t width, height;
1773};
1774
1775static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001776resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001777 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778{
1779 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001780 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001781 struct shell_surface *shsurf = resize->base.shsurf;
1782 int32_t width, height;
1783 wl_fixed_t from_x, from_y;
1784 wl_fixed_t to_x, to_y;
1785
Jonas Ådahld2510102014-10-05 21:39:14 +02001786 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001787
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001788 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001789 return;
1790
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 weston_view_from_global_fixed(shsurf->view,
1792 pointer->grab_x, pointer->grab_y,
1793 &from_x, &from_y);
1794 weston_view_from_global_fixed(shsurf->view,
1795 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001796
1797 width = resize->width;
1798 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1799 width += wl_fixed_to_int(from_x - to_x);
1800 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1801 width += wl_fixed_to_int(to_x - from_x);
1802 }
1803
1804 height = resize->height;
1805 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1806 height += wl_fixed_to_int(from_y - to_y);
1807 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1808 height += wl_fixed_to_int(to_y - from_y);
1809 }
1810
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001811 if (width < 1)
1812 width = 1;
1813 if (height < 1)
1814 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001815 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001816}
1817
1818static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001819send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820{
1821 struct shell_surface *shsurf = get_shell_surface(surface);
1822
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001823 assert(shsurf);
1824
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001825 if (shsurf->resource)
1826 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001827 shsurf->resize_edges,
1828 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001829}
1830
1831static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001832 send_configure,
1833 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001834};
1835
1836static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001837resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001838 uint32_t time, uint32_t button, uint32_t state_w)
1839{
1840 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001841 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001842 enum wl_pointer_button_state state = state_w;
1843
1844 if (pointer->button_count == 0 &&
1845 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1846 shell_grab_end(&resize->base);
1847 free(grab);
1848 }
1849}
1850
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001851static void
1852resize_grab_cancel(struct weston_pointer_grab *grab)
1853{
1854 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1855
1856 shell_grab_end(&resize->base);
1857 free(grab);
1858}
1859
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001860static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001861 noop_grab_focus,
1862 resize_grab_motion,
1863 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001864 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001865 noop_grab_axis_source,
1866 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001867 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001868};
1869
Giulio Camuffob8366642013-04-25 13:57:46 +03001870/*
1871 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001872 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001873static void
1874surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1875 int32_t *y, int32_t *w, int32_t *h) {
1876 pixman_region32_t region;
1877 pixman_box32_t *box;
1878 struct weston_subsurface *subsurface;
1879
1880 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001881 surface->width,
1882 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001883
1884 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1885 pixman_region32_union_rect(&region, &region,
1886 subsurface->position.x,
1887 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 subsurface->surface->width,
1889 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001890 }
1891
1892 box = pixman_region32_extents(&region);
1893 if (x)
1894 *x = box->x1;
1895 if (y)
1896 *y = box->y1;
1897 if (w)
1898 *w = box->x2 - box->x1;
1899 if (h)
1900 *h = box->y2 - box->y1;
1901
1902 pixman_region32_fini(&region);
1903}
1904
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001905static int
1906surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001907 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001908{
1909 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001910 const unsigned resize_topbottom =
1911 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1912 const unsigned resize_leftright =
1913 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1914 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001915
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001916 if (shsurf->grabbed ||
1917 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001918 return 0;
1919
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001920 /* Check for invalid edge combinations. */
1921 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1922 (edges & resize_topbottom) == resize_topbottom ||
1923 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001924 return 0;
1925
1926 resize = malloc(sizeof *resize);
1927 if (!resize)
1928 return -1;
1929
1930 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001931
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001932 resize->width = shsurf->geometry.width;
1933 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001934
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001935 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001936 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001937 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001938 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001939
1940 return 0;
1941}
1942
1943static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001944common_surface_resize(struct wl_resource *resource,
1945 struct wl_resource *seat_resource, uint32_t serial,
1946 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001947{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001948 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001949 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001950 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001951 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001952
Derek Foreman1281a362015-07-31 16:55:32 -05001953 if (!pointer ||
1954 pointer->button_count == 0 ||
1955 pointer->grab_serial != serial ||
1956 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001957 return;
1958
Derek Foreman1281a362015-07-31 16:55:32 -05001959 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001960 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001961 return;
1962
Derek Foreman1281a362015-07-31 16:55:32 -05001963 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001964 wl_resource_post_no_memory(resource);
1965}
1966
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001967static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001968shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1969 struct wl_resource *seat_resource, uint32_t serial,
1970 uint32_t edges)
1971{
1972 common_surface_resize(resource, seat_resource, serial, edges);
1973}
1974
1975static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001976busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001977{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001978 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001979 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001981 wl_fixed_t sx, sy;
1982
Jason Ekstranda7af7042013-10-12 22:38:11 -05001983 view = weston_compositor_pick_view(pointer->seat->compositor,
1984 pointer->x, pointer->y,
1985 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001986
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001987 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1988 shell_grab_end(grab);
1989 free(grab);
1990 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001991}
1992
1993static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001994busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001995 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001996{
Jonas Ådahld2510102014-10-05 21:39:14 +02001997 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001998}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001999
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002000static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002001busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002002 uint32_t time, uint32_t button, uint32_t state)
2003{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002004 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002005 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002006 struct weston_pointer *pointer = grab->grab.pointer;
2007 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002008
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002009 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002010 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002011 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002012 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002013 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002014 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002015 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002016}
2017
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002018static void
2019busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2020{
2021 struct shell_grab *grab = (struct shell_grab *) base;
2022
2023 shell_grab_end(grab);
2024 free(grab);
2025}
2026
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002027static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002028 busy_cursor_grab_focus,
2029 busy_cursor_grab_motion,
2030 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002031 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002032 noop_grab_axis_source,
2033 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002034 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002035};
2036
2037static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002038set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002039{
2040 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002042 if (pointer->grab->interface == &busy_cursor_grab_interface)
2043 return;
2044
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002045 grab = malloc(sizeof *grab);
2046 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002047 return;
2048
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002049 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002050 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002051 /* Mark the shsurf as ungrabbed so that button binding is able
2052 * to move it. */
2053 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002054}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002055
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002057end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002058{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002059 struct shell_grab *grab;
2060 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002061
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002062 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002063 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2064
2065 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002066 continue;
2067
Derek Foreman1281a362015-07-31 16:55:32 -05002068 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002069 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002070 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002071 wl_resource_get_client(grab->shsurf->resource) == client) {
2072 shell_grab_end(grab);
2073 free(grab);
2074 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002075 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002076}
2077
Scott Moreau9521d5e2012-04-19 13:06:17 -06002078static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002079handle_shell_client_destroy(struct wl_listener *listener, void *data);
2080
2081static int
2082xdg_ping_timeout_handler(void *data)
2083{
2084 struct shell_client *sc = data;
2085 struct weston_seat *seat;
2086 struct shell_surface *shsurf;
2087
2088 /* Client is not responding */
2089 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002090 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002091 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2092
2093 if (!pointer ||
2094 !pointer->focus ||
2095 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002096 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002097
Derek Foreman1281a362015-07-31 16:55:32 -05002098 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002099 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002100 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002101 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002102 }
2103
2104 return 1;
2105}
2106
2107static void
2108handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2109{
2110 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002111 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002112 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002113 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002114
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002115 if (sc->unresponsive) {
2116 xdg_ping_timeout_handler(sc);
2117 return;
2118 }
2119
2120 sc->ping_serial = serial;
2121 loop = wl_display_get_event_loop(compositor->wl_display);
2122 if (sc->ping_timer == NULL)
2123 sc->ping_timer =
2124 wl_event_loop_add_timer(loop,
2125 xdg_ping_timeout_handler, sc);
2126 if (sc->ping_timer == NULL)
2127 return;
2128
2129 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2130
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002131 if (shell_surface_is_xdg_surface(shsurf) ||
2132 shell_surface_is_xdg_popup(shsurf))
2133 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002134 else if (shell_surface_is_wl_shell_surface(shsurf)
2135 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002136 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002137}
2138
Scott Moreauff1db4a2012-04-17 19:06:18 -06002139static void
2140ping_handler(struct weston_surface *surface, uint32_t serial)
2141{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002142 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002143
2144 if (!shsurf)
2145 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002146 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002147 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002148 if (shsurf->surface == shsurf->shell->grab_surface)
2149 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002150 if (!shsurf->owner)
2151 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002152
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002153 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002154}
2155
2156static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002157handle_pointer_focus(struct wl_listener *listener, void *data)
2158{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002159 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002161 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002162 uint32_t serial;
2163
Jason Ekstranda7af7042013-10-12 22:38:11 -05002164 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002165 return;
2166
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002168 serial = wl_display_next_serial(compositor->wl_display);
2169 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002170}
2171
2172static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002173shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2174{
2175 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002176 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002177}
2178
2179static void
2180shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2181{
2182 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002183 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002184}
2185
2186static void
2187handle_keyboard_focus(struct wl_listener *listener, void *data)
2188{
2189 struct weston_keyboard *keyboard = data;
2190 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2191
2192 if (seat->focused_surface) {
2193 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2194 if (shsurf)
2195 shell_surface_lose_keyboard_focus(shsurf);
2196 }
2197
2198 seat->focused_surface = keyboard->focus;
2199
2200 if (seat->focused_surface) {
2201 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2202 if (shsurf)
2203 shell_surface_gain_keyboard_focus(shsurf);
2204 }
2205}
2206
2207static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002208shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002209{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002210 if (sc->ping_serial != serial)
2211 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002212
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002213 sc->unresponsive = 0;
2214 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002215
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002216 if (sc->ping_timer) {
2217 wl_event_source_remove(sc->ping_timer);
2218 sc->ping_timer = NULL;
2219 }
2220
2221}
2222
2223static void
2224shell_surface_pong(struct wl_client *client,
2225 struct wl_resource *resource, uint32_t serial)
2226{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002227 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2228 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002229
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002230 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002231}
2232
2233static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002234set_title(struct shell_surface *shsurf, const char *title)
2235{
2236 free(shsurf->title);
2237 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002238 shsurf->surface->timeline.force_refresh = 1;
2239}
2240
2241static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002242set_pid(struct shell_surface *shsurf, pid_t pid)
2243{
2244 /* We have no use for it */
2245}
2246
2247static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002248set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2249{
2250 shsurf->type = t;
2251 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002252}
2253
2254static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002255set_window_geometry(struct shell_surface *shsurf,
2256 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002257{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002258 shsurf->next_geometry.x = x;
2259 shsurf->next_geometry.y = y;
2260 shsurf->next_geometry.width = width;
2261 shsurf->next_geometry.height = height;
2262 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002263}
2264
2265static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002266shell_surface_set_title(struct wl_client *client,
2267 struct wl_resource *resource, const char *title)
2268{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002269 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002270
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002271 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002272}
2273
2274static void
2275shell_surface_set_class(struct wl_client *client,
2276 struct wl_resource *resource, const char *class)
2277{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002278 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002279
2280 free(shsurf->class);
2281 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002282 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002283}
2284
Alex Wu4539b082012-03-01 12:57:46 +08002285static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002286restore_output_mode(struct weston_output *output)
2287{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002288 if (output->original_mode)
2289 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002290}
2291
2292static void
2293restore_all_output_modes(struct weston_compositor *compositor)
2294{
2295 struct weston_output *output;
2296
2297 wl_list_for_each(output, &compositor->output_list, link)
2298 restore_output_mode(output);
2299}
2300
Philip Withnall07926d92013-11-25 18:01:40 +00002301/* The surface will be inserted into the list immediately after the link
2302 * returned by this function (i.e. will be stacked immediately above the
2303 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002304static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002305shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2306{
2307 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002308 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002309
2310 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002311 case SHELL_SURFACE_XWAYLAND:
2312 return &shsurf->shell->fullscreen_layer.view_list;
2313
2314 case SHELL_SURFACE_NONE:
2315 return NULL;
2316
Kristian Høgsbergead52422014-01-01 13:51:52 -08002317 case SHELL_SURFACE_POPUP:
2318 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002319 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002320 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002321 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002322 /* Move the surface to its parent layer so
2323 * that surfaces which are transient for
2324 * fullscreen surfaces don't get hidden by the
2325 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002326
2327 /* TODO: Handle a parent with multiple views */
2328 parent = get_default_view(shsurf->parent);
2329 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002330 return container_of(parent->layer_link.link.prev,
2331 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002332 }
Philip Withnall07926d92013-11-25 18:01:40 +00002333
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002334 /* Move the surface to a normal workspace layer so that surfaces
2335 * which were previously fullscreen or transient are no longer
2336 * rendered on top. */
2337 ws = get_current_workspace(shsurf->shell);
2338 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002339 }
2340
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002341 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002342}
2343
Philip Withnall648a4dd2013-11-25 18:01:44 +00002344static void
2345shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2346{
2347 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002348 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002349
2350 /* Move the child layers to the same workspace as shsurf. They will be
2351 * stacked above shsurf. */
2352 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002353 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002354 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002355 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002356 prev = container_of(shsurf->view->layer_link.link.prev,
2357 struct weston_layer_entry, link);
2358 weston_layer_entry_remove(&child->view->layer_link);
2359 weston_layer_entry_insert(prev,
2360 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002361 weston_view_geometry_dirty(child->view);
2362 weston_surface_damage(child->surface);
2363
2364 /* Recurse. We don’t expect this to recurse very far (if
2365 * at all) because that would imply we have transient
2366 * (or popup) children of transient surfaces, which
2367 * would be unusual. */
2368 shell_surface_update_child_surface_layers(child);
2369 }
2370 }
2371}
2372
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002373/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002374 * geometry to ensure the changes are redrawn.
2375 *
2376 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2377 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002378static void
2379shell_surface_update_layer(struct shell_surface *shsurf)
2380{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002381 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002382
2383 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2384
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002385 if (new_layer_link == NULL)
2386 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002387 if (new_layer_link == &shsurf->view->layer_link)
2388 return;
2389
2390 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002391 weston_layer_entry_remove(&shsurf->view->layer_link);
2392 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002393 weston_view_geometry_dirty(shsurf->view);
2394 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002395
2396 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002397}
2398
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002399static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002400shell_surface_set_parent(struct shell_surface *shsurf,
2401 struct weston_surface *parent)
2402{
2403 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002404
2405 wl_list_remove(&shsurf->children_link);
2406 wl_list_init(&shsurf->children_link);
2407
2408 /* Insert into the parent surface’s child list. */
2409 if (parent != NULL) {
2410 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2411 if (parent_shsurf != NULL)
2412 wl_list_insert(&parent_shsurf->children_list,
2413 &shsurf->children_link);
2414 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002415}
2416
2417static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002418shell_surface_set_output(struct shell_surface *shsurf,
2419 struct weston_output *output)
2420{
2421 struct weston_surface *es = shsurf->surface;
2422
2423 /* get the default output, if the client set it as NULL
2424 check whether the ouput is available */
2425 if (output)
2426 shsurf->output = output;
2427 else if (es->output)
2428 shsurf->output = es->output;
2429 else
2430 shsurf->output = get_default_output(es->compositor);
2431}
2432
2433static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002434surface_clear_next_states(struct shell_surface *shsurf)
2435{
2436 shsurf->next_state.maximized = false;
2437 shsurf->next_state.fullscreen = false;
2438
2439 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2440 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2441 shsurf->state_changed = true;
2442}
2443
2444static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002445set_toplevel(struct shell_surface *shsurf)
2446{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002447 shell_surface_set_parent(shsurf, NULL);
2448 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002449 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002450
2451 /* The layer_link is updated in set_surface_type(),
2452 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002453}
2454
2455static void
2456shell_surface_set_toplevel(struct wl_client *client,
2457 struct wl_resource *resource)
2458{
2459 struct shell_surface *surface = wl_resource_get_user_data(resource);
2460
2461 set_toplevel(surface);
2462}
2463
2464static void
2465set_transient(struct shell_surface *shsurf,
2466 struct weston_surface *parent, int x, int y, uint32_t flags)
2467{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002468 assert(parent != NULL);
2469
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002470 shell_surface_set_parent(shsurf, parent);
2471
2472 surface_clear_next_states(shsurf);
2473
Philip Withnallbecb77e2013-11-25 18:01:30 +00002474 shsurf->transient.x = x;
2475 shsurf->transient.y = y;
2476 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002477
Rafael Antognollied207b42013-12-03 15:35:43 -02002478 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002479 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002480 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002481
2482 /* The layer_link is updated in set_surface_type(),
2483 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002484}
2485
2486static void
2487shell_surface_set_transient(struct wl_client *client,
2488 struct wl_resource *resource,
2489 struct wl_resource *parent_resource,
2490 int x, int y, uint32_t flags)
2491{
2492 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2493 struct weston_surface *parent =
2494 wl_resource_get_user_data(parent_resource);
2495
2496 set_transient(shsurf, parent, x, y, flags);
2497}
2498
2499static void
2500set_fullscreen(struct shell_surface *shsurf,
2501 uint32_t method,
2502 uint32_t framerate,
2503 struct weston_output *output)
2504{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002505 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002506 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002507
2508 shsurf->fullscreen_output = shsurf->output;
2509 shsurf->fullscreen.type = method;
2510 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002511
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002512 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002513}
2514
2515static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002516weston_view_set_initial_position(struct weston_view *view,
2517 struct desktop_shell *shell);
2518
2519static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002520unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002521{
Philip Withnallf85fe842013-11-25 18:01:37 +00002522 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002523 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2524 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002525 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002526 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002527
Alex Wu4539b082012-03-01 12:57:46 +08002528 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2529 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002530
Alex Wu4539b082012-03-01 12:57:46 +08002531 wl_list_remove(&shsurf->fullscreen.transform.link);
2532 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002533
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534 if (shsurf->fullscreen.black_view)
2535 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2536 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002537
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002538 if (shsurf->saved_position_valid)
2539 weston_view_set_position(shsurf->view,
2540 shsurf->saved_x, shsurf->saved_y);
2541 else
2542 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2543
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002544 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002546 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002547 shsurf->saved_rotation_valid = false;
2548 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002549
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002550 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002551}
2552
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002553static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002554shell_surface_set_fullscreen(struct wl_client *client,
2555 struct wl_resource *resource,
2556 uint32_t method,
2557 uint32_t framerate,
2558 struct wl_resource *output_resource)
2559{
2560 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2561 struct weston_output *output;
2562
2563 if (output_resource)
2564 output = wl_resource_get_user_data(output_resource);
2565 else
2566 output = NULL;
2567
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002568 shell_surface_set_parent(shsurf, NULL);
2569
Rafael Antognolli03b16592013-12-03 15:35:42 -02002570 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002571 shsurf->next_state.fullscreen = true;
2572 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002573 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574}
2575
2576static void
2577set_popup(struct shell_surface *shsurf,
2578 struct weston_surface *parent,
2579 struct weston_seat *seat,
2580 uint32_t serial,
2581 int32_t x,
2582 int32_t y)
2583{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002584 assert(parent != NULL);
2585
Philip Withnallbecb77e2013-11-25 18:01:30 +00002586 shsurf->popup.shseat = get_shell_seat(seat);
2587 shsurf->popup.serial = serial;
2588 shsurf->popup.x = x;
2589 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002590
Pekka Paalanenb5026542014-11-12 15:09:24 +02002591 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592}
2593
2594static void
2595shell_surface_set_popup(struct wl_client *client,
2596 struct wl_resource *resource,
2597 struct wl_resource *seat_resource,
2598 uint32_t serial,
2599 struct wl_resource *parent_resource,
2600 int32_t x, int32_t y, uint32_t flags)
2601{
2602 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002603 struct weston_surface *parent =
2604 wl_resource_get_user_data(parent_resource);
2605
2606 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002607
Rafael Antognolli03b16592013-12-03 15:35:42 -02002608 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002609 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002610 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002611 wl_resource_get_user_data(seat_resource),
2612 serial, x, y);
2613}
2614
2615static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002616unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002617{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002618 /* undo all maximized things here */
2619 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002620
2621 if (shsurf->saved_position_valid)
2622 weston_view_set_position(shsurf->view,
2623 shsurf->saved_x, shsurf->saved_y);
2624 else
2625 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002626
2627 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 wl_list_insert(&shsurf->view->geometry.transformation_list,
2629 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002630 shsurf->saved_rotation_valid = false;
2631 }
2632
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002633 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002634}
2635
Philip Withnallbecb77e2013-11-25 18:01:30 +00002636static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002637set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002638{
2639 struct shell_surface *shsurf;
2640 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002641 struct weston_view *view;
2642
2643 view = get_default_view(surface);
2644 if (!view)
2645 return;
2646
2647 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2648
2649 shsurf = get_shell_surface(surface);
2650 current_ws = get_current_workspace(shsurf->shell);
2651
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002652 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002653 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002654
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002655 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002656 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002657
2658 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002659 weston_view_damage_below(view);
2660}
2661
2662static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002663shell_surface_set_maximized(struct wl_client *client,
2664 struct wl_resource *resource,
2665 struct wl_resource *output_resource)
2666{
2667 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2668 struct weston_output *output;
2669
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002670 surface_clear_next_states(shsurf);
2671 shsurf->next_state.maximized = true;
2672 shsurf->state_changed = true;
2673
Pekka Paalanenb5026542014-11-12 15:09:24 +02002674 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002675 shell_surface_set_parent(shsurf, NULL);
2676
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677 if (output_resource)
2678 output = wl_resource_get_user_data(output_resource);
2679 else
2680 output = NULL;
2681
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002682 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002683
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002684 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002685}
2686
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002687/* This is only ever called from set_surface_type(), so there’s no need to
2688 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002689static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002690reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002691{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002692 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002693 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002694 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002695 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002696
Pekka Paalanen98262232011-12-01 10:42:22 +02002697 return 0;
2698}
2699
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002700static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002701set_full_output(struct shell_surface *shsurf)
2702{
2703 shsurf->saved_x = shsurf->view->geometry.x;
2704 shsurf->saved_y = shsurf->view->geometry.y;
2705 shsurf->saved_position_valid = true;
2706
2707 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2708 wl_list_remove(&shsurf->rotation.transform.link);
2709 wl_list_init(&shsurf->rotation.transform.link);
2710 weston_view_geometry_dirty(shsurf->view);
2711 shsurf->saved_rotation_valid = true;
2712 }
2713}
2714
2715static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002716set_surface_type(struct shell_surface *shsurf)
2717{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002718 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002719 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002720
Philip Withnallbecb77e2013-11-25 18:01:30 +00002721 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002722
Rafael Antognolli03b16592013-12-03 15:35:42 -02002723 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002724 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002725
2726 switch (shsurf->type) {
2727 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002728 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002729 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002730 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 weston_view_set_position(shsurf->view,
2732 pev->geometry.x + shsurf->transient.x,
2733 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002734 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002735 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002736
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002737 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738 weston_view_set_position(shsurf->view, shsurf->transient.x,
2739 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002740 break;
2741
Philip Withnall0f640e22013-11-25 18:01:31 +00002742 case SHELL_SURFACE_POPUP:
2743 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002744 default:
2745 break;
2746 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002747
2748 /* Update the surface’s layer. */
2749 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002750}
2751
Tiago Vignattibe143262012-04-16 17:31:41 +03002752static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002753shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002754{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002755 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002756}
2757
Pekka Paalanen8274d902014-08-06 19:36:51 +03002758static int
2759black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2760{
2761 struct weston_surface *fs_surface = surface->configure_private;
2762 int n;
2763 int rem;
2764 int ret;
2765
2766 n = snprintf(buf, len, "black background surface for ");
2767 if (n < 0)
2768 return n;
2769
2770 rem = (int)len - n;
2771 if (rem < 0)
2772 rem = 0;
2773
2774 if (fs_surface->get_label)
2775 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2776 else
2777 ret = snprintf(buf + n, rem, "<unknown>");
2778
2779 if (ret < 0)
2780 return n;
2781
2782 return n + ret;
2783}
2784
Alex Wu21858432012-04-01 20:13:08 +08002785static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002786black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002787
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002789create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002790 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002791 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002792{
2793 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002794 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002795
2796 surface = weston_surface_create(ec);
2797 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002798 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002799 return NULL;
2800 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002801 view = weston_view_create(surface);
2802 if (surface == NULL) {
2803 weston_log("no memory\n");
2804 weston_surface_destroy(surface);
2805 return NULL;
2806 }
Alex Wu4539b082012-03-01 12:57:46 +08002807
Alex Wu21858432012-04-01 20:13:08 +08002808 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002809 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002810 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002811 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002812 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002813 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002814 pixman_region32_fini(&surface->input);
2815 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002816
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002817 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002818 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002819
2820 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002821}
2822
Philip Withnalled8f1a92013-11-25 18:01:43 +00002823static void
2824shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2825{
2826 struct weston_output *output = shsurf->fullscreen_output;
2827
Rafael Antognolli03b16592013-12-03 15:35:42 -02002828 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002829
2830 if (!shsurf->fullscreen.black_view)
2831 shsurf->fullscreen.black_view =
2832 create_black_surface(shsurf->surface->compositor,
2833 shsurf->surface,
2834 output->x, output->y,
2835 output->width,
2836 output->height);
2837
2838 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002839 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2840 weston_layer_entry_insert(&shsurf->view->layer_link,
2841 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002842 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2843 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002844
2845 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002846}
2847
Alex Wu4539b082012-03-01 12:57:46 +08002848/* Create black surface and append it to the associated fullscreen surface.
2849 * Handle size dismatch and positioning according to the method. */
2850static void
2851shell_configure_fullscreen(struct shell_surface *shsurf)
2852{
2853 struct weston_output *output = shsurf->fullscreen_output;
2854 struct weston_surface *surface = shsurf->surface;
2855 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002856 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002857 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002858
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002859 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2860 restore_output_mode(output);
2861
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002862 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002863 weston_layer_entry_remove(&shsurf->view->layer_link);
2864 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002865
Philip Withnalled8f1a92013-11-25 18:01:43 +00002866 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002867
Jason Ekstranda7af7042013-10-12 22:38:11 -05002868 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002869 &surf_width, &surf_height);
2870
Alex Wu4539b082012-03-01 12:57:46 +08002871 switch (shsurf->fullscreen.type) {
2872 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002873 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002874 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002875 break;
2876 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002877 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002878 if (output->width == surf_width &&
2879 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002880 weston_view_set_position(shsurf->view,
2881 output->x - surf_x,
2882 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002883 break;
2884 }
2885
Alex Wu4539b082012-03-01 12:57:46 +08002886 matrix = &shsurf->fullscreen.transform.matrix;
2887 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002888
Scott Moreau1bad5db2012-08-18 01:04:05 -06002889 output_aspect = (float) output->width /
2890 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 /* XXX: Use surf_width and surf_height here? */
2892 surface_aspect = (float) surface->width /
2893 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002894 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002895 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002896 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002897 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002898 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002899 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002900
Alex Wu4539b082012-03-01 12:57:46 +08002901 weston_matrix_scale(matrix, scale, scale, 1);
2902 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002904 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002905 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2906 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002908
Alex Wu4539b082012-03-01 12:57:46 +08002909 break;
2910 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002911 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002912 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002913 surf_width * surface->buffer_viewport.buffer.scale,
2914 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002915 shsurf->fullscreen.framerate};
2916
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002917 if (weston_output_mode_switch_to_temporary(output, &mode,
2918 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002919 weston_view_set_position(shsurf->view,
2920 output->x - surf_x,
2921 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002922 shsurf->fullscreen.black_view->surface->width = output->width;
2923 shsurf->fullscreen.black_view->surface->height = output->height;
2924 weston_view_set_position(shsurf->fullscreen.black_view,
2925 output->x - surf_x,
2926 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002927 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002928 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002929 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002930 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002931 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002932 }
Alex Wubd3354b2012-04-17 17:20:49 +08002933 }
Alex Wu4539b082012-03-01 12:57:46 +08002934 break;
2935 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002937 break;
2938 default:
2939 break;
2940 }
2941}
2942
Alex Wu4539b082012-03-01 12:57:46 +08002943static void
2944shell_map_fullscreen(struct shell_surface *shsurf)
2945{
Alex Wubd3354b2012-04-17 17:20:49 +08002946 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002947}
2948
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002949static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002950set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2951{
2952 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002953 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002954 shsurf->transient.x = x;
2955 shsurf->transient.y = y;
2956 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002957
2958 shell_surface_set_parent(shsurf, NULL);
2959
Pekka Paalanenb5026542014-11-12 15:09:24 +02002960 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002961 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002962}
2963
Kristian Høgsberg47792412014-05-04 13:47:06 -07002964static void
2965shell_interface_set_fullscreen(struct shell_surface *shsurf,
2966 uint32_t method,
2967 uint32_t framerate,
2968 struct weston_output *output)
2969{
2970 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002971 shsurf->next_state.fullscreen = true;
2972 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002973
2974 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002975}
2976
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002977static struct weston_output *
2978get_focused_output(struct weston_compositor *compositor)
2979{
2980 struct weston_seat *seat;
2981 struct weston_output *output = NULL;
2982
2983 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002984 struct weston_touch *touch = weston_seat_get_touch(seat);
2985 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2986 struct weston_keyboard *keyboard =
2987 weston_seat_get_keyboard(seat);
2988
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002989 /* Priority has touch focus, then pointer and
2990 * then keyboard focus. We should probably have
2991 * three for loops and check frist for touch,
2992 * then for pointer, etc. but unless somebody has some
2993 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002994 if (touch && touch->focus)
2995 output = touch->focus->output;
2996 else if (pointer && pointer->focus)
2997 output = pointer->focus->output;
2998 else if (keyboard && keyboard->focus)
2999 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003000
3001 if (output)
3002 break;
3003 }
3004
3005 return output;
3006}
3007
3008static void
3009shell_interface_set_maximized(struct shell_surface *shsurf)
3010{
3011 struct weston_output *output;
3012
3013 surface_clear_next_states(shsurf);
3014 shsurf->next_state.maximized = true;
3015 shsurf->state_changed = true;
3016 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3017
3018 if (!weston_surface_is_mapped(shsurf->surface))
3019 output = get_focused_output(shsurf->surface->compositor);
3020 else
3021 output = shsurf->surface->output;
3022
3023 shell_surface_set_output(shsurf, output);
3024 send_configure_for_surface(shsurf);
3025}
3026
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003027static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003028shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003029{
Derek Foremane4d6c832015-08-05 14:48:11 -05003030 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003031}
3032
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003033static int
3034shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003035 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003036 uint32_t edges)
3037{
Derek Foremane4d6c832015-08-05 14:48:11 -05003038 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003039}
3040
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003041static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003042
3043static void
3044destroy_shell_seat(struct wl_listener *listener, void *data)
3045{
3046 struct shell_seat *shseat =
3047 container_of(listener,
3048 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003049 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003050
3051 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003052 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003053 shseat->popup_grab.client = NULL;
3054
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003055 wl_list_for_each_safe(shsurf, next,
3056 &shseat->popup_grab.surfaces_list,
3057 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003058 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003059 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003060 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061 }
3062
3063 wl_list_remove(&shseat->seat_destroy_listener.link);
3064 free(shseat);
3065}
3066
Jason Ekstrand024177c2014-04-21 19:42:58 -05003067static void
3068shell_seat_caps_changed(struct wl_listener *listener, void *data)
3069{
Derek Foreman1281a362015-07-31 16:55:32 -05003070 struct weston_keyboard *keyboard;
3071 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003072 struct shell_seat *seat;
3073
3074 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003075 keyboard = weston_seat_get_keyboard(seat->seat);
3076 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003077
Derek Foreman1281a362015-07-31 16:55:32 -05003078 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003079 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003080 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003081 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003082 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003083 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003084 wl_list_init(&seat->keyboard_focus_listener.link);
3085 }
3086
Derek Foreman1281a362015-07-31 16:55:32 -05003087 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003088 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003089 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003090 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003091 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003092 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003093 wl_list_init(&seat->pointer_focus_listener.link);
3094 }
3095}
3096
Giulio Camuffo5085a752013-03-25 21:42:45 +01003097static struct shell_seat *
3098create_shell_seat(struct weston_seat *seat)
3099{
3100 struct shell_seat *shseat;
3101
3102 shseat = calloc(1, sizeof *shseat);
3103 if (!shseat) {
3104 weston_log("no memory to allocate shell seat\n");
3105 return NULL;
3106 }
3107
3108 shseat->seat = seat;
3109 wl_list_init(&shseat->popup_grab.surfaces_list);
3110
3111 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3112 wl_signal_add(&seat->destroy_signal,
3113 &shseat->seat_destroy_listener);
3114
Jason Ekstrand024177c2014-04-21 19:42:58 -05003115 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3116 wl_list_init(&shseat->keyboard_focus_listener.link);
3117
3118 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3119 wl_list_init(&shseat->pointer_focus_listener.link);
3120
3121 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3122 wl_signal_add(&seat->updated_caps_signal,
3123 &shseat->caps_changed_listener);
3124 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3125
Giulio Camuffo5085a752013-03-25 21:42:45 +01003126 return shseat;
3127}
3128
3129static struct shell_seat *
3130get_shell_seat(struct weston_seat *seat)
3131{
3132 struct wl_listener *listener;
3133
3134 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003135 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003136
3137 return container_of(listener,
3138 struct shell_seat, seat_destroy_listener);
3139}
3140
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003141static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003142popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003143{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003144 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003145 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003146 struct shell_seat *shseat =
3147 container_of(grab, struct shell_seat, popup_grab.grab);
3148 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003149 wl_fixed_t sx, sy;
3150
Jason Ekstranda7af7042013-10-12 22:38:11 -05003151 view = weston_compositor_pick_view(pointer->seat->compositor,
3152 pointer->x, pointer->y,
3153 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003154
Jason Ekstranda7af7042013-10-12 22:38:11 -05003155 if (view && view->surface->resource &&
3156 wl_resource_get_client(view->surface->resource) == client) {
3157 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003158 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003159 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003160 }
3161}
3162
3163static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003164popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003165 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003166{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003167 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003168 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003169 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003170 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003171 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003172
Jonas Ådahl61917c82014-08-11 22:44:02 +02003173 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003174 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003175 weston_view_from_global_fixed(pointer->focus, x, y,
3176 &pointer->sx, &pointer->sy);
3177 }
3178
Jonas Ådahld2510102014-10-05 21:39:14 +02003179 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003180
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003181 if (!pointer->focus_client)
3182 return;
3183
3184 resource_list = &pointer->focus_client->pointer_resources;
3185 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003186 weston_view_from_global_fixed(pointer->focus,
3187 pointer->x, pointer->y,
3188 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003189 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003190 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003191}
3192
3193static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003194popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003195 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003196{
3197 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003198 struct shell_seat *shseat =
3199 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003200 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003201 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003202 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003203 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003205 if (grab->pointer->focus_client)
3206 resource_list = &grab->pointer->focus_client->pointer_resources;
3207 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003208 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003209 wl_resource_for_each(resource, resource_list) {
3210 wl_pointer_send_button(resource, serial,
3211 time, button, state);
3212 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003213 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003214 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003215 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003216 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003217 }
3218
Daniel Stone4dbadb12012-05-30 16:31:51 +01003219 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003220 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003221}
3222
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003223static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003224popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003225 uint32_t time,
3226 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003227{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003228 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003229}
3230
3231static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003232popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3233{
3234 weston_pointer_send_axis_source(grab->pointer, source);
3235}
3236
3237static void
3238popup_grab_frame(struct weston_pointer_grab *grab)
3239{
3240 weston_pointer_send_frame(grab->pointer);
3241}
3242
3243static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003244popup_grab_cancel(struct weston_pointer_grab *grab)
3245{
3246 popup_grab_end(grab->pointer);
3247}
3248
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003249static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003250 popup_grab_focus,
3251 popup_grab_motion,
3252 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003253 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003254 popup_grab_axis_source,
3255 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003256 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003257};
3258
3259static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003260touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003261 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003262{
3263 struct wl_resource *resource;
3264 struct shell_seat *shseat =
3265 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3266 struct wl_display *display = shseat->seat->compositor->wl_display;
3267 uint32_t serial;
3268 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003269 wl_fixed_t sx, sy;
3270
3271 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003272
3273 resource_list = &grab->touch->focus_resource_list;
3274 if (!wl_list_empty(resource_list)) {
3275 serial = wl_display_get_serial(display);
3276 wl_resource_for_each(resource, resource_list) {
3277 wl_touch_send_down(resource, serial, time,
3278 grab->touch->focus->surface->resource,
3279 touch_id, sx, sy);
3280 }
3281 }
3282}
3283
3284static void
3285touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3286{
3287 struct wl_resource *resource;
3288 struct shell_seat *shseat =
3289 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3290 struct wl_display *display = shseat->seat->compositor->wl_display;
3291 uint32_t serial;
3292 struct wl_list *resource_list;
3293
3294 resource_list = &grab->touch->focus_resource_list;
3295 if (!wl_list_empty(resource_list)) {
3296 serial = wl_display_get_serial(display);
3297 wl_resource_for_each(resource, resource_list) {
3298 wl_touch_send_up(resource, serial, time, touch_id);
3299 }
3300 }
3301}
3302
3303static void
3304touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003305 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003306{
3307 struct wl_resource *resource;
3308 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003309 wl_fixed_t sx, sy;
3310
3311 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003312
3313 resource_list = &grab->touch->focus_resource_list;
3314 if (!wl_list_empty(resource_list)) {
3315 wl_resource_for_each(resource, resource_list) {
3316 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3317 }
3318 }
3319}
3320
3321static void
3322touch_popup_grab_frame(struct weston_touch_grab *grab)
3323{
3324}
3325
3326static void
3327touch_popup_grab_cancel(struct weston_touch_grab *grab)
3328{
3329 touch_popup_grab_end(grab->touch);
3330}
3331
3332static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3333 touch_popup_grab_down,
3334 touch_popup_grab_up,
3335 touch_popup_grab_motion,
3336 touch_popup_grab_frame,
3337 touch_popup_grab_cancel,
3338};
3339
3340static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003341shell_surface_send_popup_done(struct shell_surface *shsurf)
3342{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003343 if (shsurf->resource == NULL)
3344 return;
3345 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003346 wl_shell_surface_send_popup_done(shsurf->resource);
3347 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003348 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003349}
3350
3351static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003352popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003353{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003354 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003355 struct shell_seat *shseat =
3356 container_of(grab, struct shell_seat, popup_grab.grab);
3357 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003358 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003359
3360 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003361 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003362 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003363 shseat->popup_grab.grab.interface = NULL;
3364 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003365 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003366 wl_list_for_each_safe(shsurf, next,
3367 &shseat->popup_grab.surfaces_list,
3368 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003369 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003370 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003371 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003372 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003373 wl_list_init(&shseat->popup_grab.surfaces_list);
3374 }
3375}
3376
3377static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003378touch_popup_grab_end(struct weston_touch *touch)
3379{
3380 struct weston_touch_grab *grab = touch->grab;
3381 struct shell_seat *shseat =
3382 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3383 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003384 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003385
3386 if (touch->grab->interface == &touch_popup_grab_interface) {
3387 weston_touch_end_grab(grab->touch);
3388 shseat->popup_grab.client = NULL;
3389 shseat->popup_grab.touch_grab.interface = NULL;
3390 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3391 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003392 wl_list_for_each_safe(shsurf, next,
3393 &shseat->popup_grab.surfaces_list,
3394 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003395 shell_surface_send_popup_done(shsurf);
3396 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003397 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003398 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003399 wl_list_init(&shseat->popup_grab.surfaces_list);
3400 }
3401}
3402
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003403static struct shell_surface *
3404get_top_popup(struct shell_seat *shseat)
3405{
3406 struct shell_surface *shsurf;
3407
3408 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3409 return NULL;
3410 } else {
3411 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3412 struct shell_surface,
3413 popup.grab_link);
3414 return shsurf;
3415 }
3416}
3417
3418static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003419add_popup_grab(struct shell_surface *shsurf,
3420 struct shell_seat *shseat,
3421 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003422{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003423 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003424 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003425 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3426 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003427
3428 parent = get_shell_surface(shsurf->parent);
3429 top_surface = get_top_popup(shseat);
3430 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003431 (!parent ||
3432 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003433 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003434 wl_resource_post_error(shsurf->owner_resource,
3435 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003436 "xdg_popup was not created on the "
3437 "topmost popup");
3438 return -1;
3439 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003440
3441 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003442 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003443 shseat->popup_grab.client =
3444 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003445
3446 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003447 shseat->popup_grab.grab.interface =
3448 &popup_grab_interface;
3449
3450 /* We must make sure here that this popup was opened
3451 * after a mouse press, and not just by moving around
3452 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003453 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003454 shseat->popup_grab.initial_up = 0;
3455 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003456 shseat->popup_grab.touch_grab.interface =
3457 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003458 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003459
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003460 wl_list_insert(&shseat->popup_grab.surfaces_list,
3461 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003462
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003463 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003464 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003465 &shseat->popup_grab.grab);
3466 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003467 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003468 &shseat->popup_grab.touch_grab);
3469 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003470 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003471 wl_list_insert(&shseat->popup_grab.surfaces_list,
3472 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003473 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003474
3475 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003476}
3477
3478static void
3479remove_popup_grab(struct shell_surface *shsurf)
3480{
3481 struct shell_seat *shseat = shsurf->popup.shseat;
3482
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003483 if (shell_surface_is_xdg_popup(shsurf) &&
3484 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003485 wl_resource_post_error(shsurf->owner_resource,
3486 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003487 "xdg_popup was destroyed while it was "
3488 "not the topmost popup.");
3489 return;
3490 }
3491
Giulio Camuffo5085a752013-03-25 21:42:45 +01003492 wl_list_remove(&shsurf->popup.grab_link);
3493 wl_list_init(&shsurf->popup.grab_link);
3494 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003495 if (shseat->popup_grab.type == POINTER) {
3496 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3497 shseat->popup_grab.grab.interface = NULL;
3498 } else if (shseat->popup_grab.type == TOUCH) {
3499 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3500 shseat->popup_grab.touch_grab.interface = NULL;
3501 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003502 }
3503}
3504
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003505static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003506shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003507{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003508 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003509 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003510 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3511 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003512
Jason Ekstranda7af7042013-10-12 22:38:11 -05003513 shsurf->surface->output = parent_view->output;
3514 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003515
Jason Ekstranda7af7042013-10-12 22:38:11 -05003516 weston_view_set_transform_parent(shsurf->view, parent_view);
3517 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3518 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003519
Derek Foreman1281a362015-07-31 16:55:32 -05003520 if (pointer &&
3521 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003522 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3523 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003524 } else if (touch &&
3525 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003526 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3527 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003528 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003529 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003530 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003531 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003532
3533 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003534}
3535
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003536static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003537 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003538 shell_surface_move,
3539 shell_surface_resize,
3540 shell_surface_set_toplevel,
3541 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003542 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003543 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003544 shell_surface_set_maximized,
3545 shell_surface_set_title,
3546 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003547};
3548
3549static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003550destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003551{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003552 struct shell_surface *child, *next;
3553
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003554 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3555
Giulio Camuffo5085a752013-03-25 21:42:45 +01003556 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3557 remove_popup_grab(shsurf);
3558 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003559
Alex Wubd3354b2012-04-17 17:20:49 +08003560 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003561 shell_surface_is_top_fullscreen(shsurf))
3562 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003563
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564 if (shsurf->fullscreen.black_view)
3565 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003566
Alex Wubd3354b2012-04-17 17:20:49 +08003567 /* As destroy_resource() use wl_list_for_each_safe(),
3568 * we can always remove the listener.
3569 */
3570 wl_list_remove(&shsurf->surface_destroy_listener.link);
3571 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003572 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003573 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003574
Jason Ekstranda7af7042013-10-12 22:38:11 -05003575 weston_view_destroy(shsurf->view);
3576
Philip Withnall648a4dd2013-11-25 18:01:44 +00003577 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003578 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3579 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003580
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003581 wl_list_remove(&shsurf->link);
3582 free(shsurf);
3583}
3584
3585static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003586shell_destroy_shell_surface(struct wl_resource *resource)
3587{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003588 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003589
Bryan Caina46b9462014-04-04 17:41:24 -05003590 if (!wl_list_empty(&shsurf->popup.grab_link))
3591 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003592 if (shsurf->resource)
3593 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003594 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003595}
3596
3597static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003598shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003599{
3600 struct shell_surface *shsurf = container_of(listener,
3601 struct shell_surface,
3602 surface_destroy_listener);
3603
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003604 if (shsurf->resource)
3605 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003606
3607 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003608}
3609
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003610static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003611fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003612{
3613 struct shell_surface *shsurf = data;
3614
3615 weston_surface_destroy(shsurf->surface);
3616}
3617
3618static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003619fade_out_done(struct weston_view_animation *animation, void *data)
3620{
3621 struct shell_surface *shsurf = data;
3622 struct wl_event_loop *loop;
3623
3624 loop = wl_display_get_event_loop(
3625 shsurf->surface->compositor->wl_display);
3626
3627 if (!shsurf->destroying) {
3628 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3629 shsurf->destroying = true;
3630 }
3631}
3632
3633static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003634handle_resource_destroy(struct wl_listener *listener, void *data)
3635{
3636 struct shell_surface *shsurf =
3637 container_of(listener, struct shell_surface,
3638 resource_destroy_listener);
3639
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003640 if (!weston_surface_is_mapped(shsurf->surface))
3641 return;
3642
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003643 shsurf->surface->ref_count++;
3644
3645 pixman_region32_fini(&shsurf->surface->pending.input);
3646 pixman_region32_init(&shsurf->surface->pending.input);
3647 pixman_region32_fini(&shsurf->surface->input);
3648 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003649 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3650 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3651 fade_out_done, shsurf);
3652 } else {
3653 weston_surface_destroy(shsurf->surface);
3654 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003655}
3656
3657static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003658shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003659
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003660struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003661get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003662{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003663 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003664 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003665 else
3666 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003667}
3668
Rafael Antognollie2a34552013-12-03 15:35:45 -02003669static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003670create_common_surface(struct shell_client *owner, void *shell,
3671 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003672 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003673{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003674 struct shell_surface *shsurf;
3675
Pekka Paalanen50b67472014-10-01 15:02:41 +03003676 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003677
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003678 shsurf = calloc(1, sizeof *shsurf);
3679 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003680 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003681 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003682 }
3683
Jason Ekstranda7af7042013-10-12 22:38:11 -05003684 shsurf->view = weston_view_create(surface);
3685 if (!shsurf->view) {
3686 weston_log("no memory to allocate shell surface\n");
3687 free(shsurf);
3688 return NULL;
3689 }
3690
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003691 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003692 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003693 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003694
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003695 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3696 wl_resource_add_destroy_listener(surface->resource,
3697 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003698 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003699
Tiago Vignattibc052c92012-04-19 16:18:18 +03003700 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003701 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003702 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003703 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003704 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003705 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3706 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003707 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003708 wl_list_init(&shsurf->fullscreen.transform.link);
3709
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003710 shsurf->output = get_default_output(shsurf->shell->compositor);
3711
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003712 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003713 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003714 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003715 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003716
3717 /* init link so its safe to always remove it in destroy_shell_surface */
3718 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003719 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003720
Pekka Paalanen460099f2012-01-20 16:48:25 +02003721 /* empty when not in use */
3722 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003723 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003724
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003725 wl_list_init(&shsurf->workspace_transform.link);
3726
Philip Withnall648a4dd2013-11-25 18:01:44 +00003727 wl_list_init(&shsurf->children_link);
3728 wl_list_init(&shsurf->children_list);
3729 shsurf->parent = NULL;
3730
Pekka Paalanenb5026542014-11-12 15:09:24 +02003731 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003732
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003733 shsurf->client = client;
3734
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003735 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003736}
3737
Rafael Antognollie2a34552013-12-03 15:35:45 -02003738static struct shell_surface *
3739create_shell_surface(void *shell, struct weston_surface *surface,
3740 const struct weston_shell_client *client)
3741{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003742 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003743}
3744
Tiago Vignattibc052c92012-04-19 16:18:18 +03003745static void
3746shell_get_shell_surface(struct wl_client *client,
3747 struct wl_resource *resource,
3748 uint32_t id,
3749 struct wl_resource *surface_resource)
3750{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003751 struct weston_surface *surface =
3752 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003753 struct shell_client *sc = wl_resource_get_user_data(resource);
3754 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003755 struct shell_surface *shsurf;
3756
Pekka Paalanen50b67472014-10-01 15:02:41 +03003757 if (weston_surface_set_role(surface, "wl_shell_surface",
3758 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003759 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003760
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003761 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003762 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003763 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003764 return;
3765 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003766
Jason Ekstranda85118c2013-06-27 20:17:02 -05003767 shsurf->resource =
3768 wl_resource_create(client,
3769 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003770 if (!shsurf->resource) {
3771 wl_resource_post_no_memory(surface_resource);
3772 return;
3773 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003774 wl_resource_set_implementation(shsurf->resource,
3775 &shell_surface_implementation,
3776 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003777 wl_list_insert(&sc->surface_list,
3778 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003779}
3780
Rafael Antognollie2a34552013-12-03 15:35:45 -02003781static bool
3782shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3783{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003784 /* A shell surface without a resource is created from xwayland
3785 * and is considered a wl_shell surface for now. */
3786
3787 return shsurf->resource == NULL ||
3788 wl_resource_instance_of(shsurf->resource,
3789 &wl_shell_surface_interface,
3790 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003791}
3792
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003793static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003794 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003795};
3796
Rafael Antognollie2a34552013-12-03 15:35:45 -02003797/****************************
3798 * xdg-shell implementation */
3799
3800static void
3801xdg_surface_destroy(struct wl_client *client,
3802 struct wl_resource *resource)
3803{
3804 wl_resource_destroy(resource);
3805}
3806
3807static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003808xdg_surface_set_parent(struct wl_client *client,
3809 struct wl_resource *resource,
3810 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003811{
3812 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003813 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003814
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003815 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003816 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003817 shell_surface_set_parent(shsurf, parent->surface);
3818 } else {
3819 shell_surface_set_parent(shsurf, NULL);
3820 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003821}
3822
3823static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003824xdg_surface_set_app_id(struct wl_client *client,
3825 struct wl_resource *resource,
3826 const char *app_id)
3827{
3828 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3829
3830 free(shsurf->class);
3831 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003832 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003833}
3834
3835static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003836xdg_surface_show_window_menu(struct wl_client *client,
3837 struct wl_resource *surface_resource,
3838 struct wl_resource *seat_resource,
3839 uint32_t serial,
3840 int32_t x,
3841 int32_t y)
3842{
3843 /* TODO */
3844}
3845
3846static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003847xdg_surface_set_title(struct wl_client *client,
3848 struct wl_resource *resource, const char *title)
3849{
3850 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3851
3852 set_title(shsurf, title);
3853}
3854
3855static void
3856xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3857 struct wl_resource *seat_resource, uint32_t serial)
3858{
3859 common_surface_move(resource, seat_resource, serial);
3860}
3861
3862static void
3863xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3864 struct wl_resource *seat_resource, uint32_t serial,
3865 uint32_t edges)
3866{
3867 common_surface_resize(resource, seat_resource, serial, edges);
3868}
3869
3870static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003871xdg_surface_ack_configure(struct wl_client *client,
3872 struct wl_resource *resource,
3873 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003874{
3875 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3876
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003877 if (shsurf->state_requested) {
3878 shsurf->next_state = shsurf->requested_state;
3879 shsurf->state_changed = true;
3880 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003881 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003882}
3883
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003884static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003885xdg_surface_set_window_geometry(struct wl_client *client,
3886 struct wl_resource *resource,
3887 int32_t x,
3888 int32_t y,
3889 int32_t width,
3890 int32_t height)
3891{
3892 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3893
3894 set_window_geometry(shsurf, x, y, width, height);
3895}
3896
3897static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003898xdg_surface_set_maximized(struct wl_client *client,
3899 struct wl_resource *resource)
3900{
3901 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003902 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003903
3904 shsurf->state_requested = true;
3905 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003906
3907 if (!weston_surface_is_mapped(shsurf->surface))
3908 output = get_focused_output(shsurf->surface->compositor);
3909 else
3910 output = shsurf->surface->output;
3911
3912 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003913 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003914}
3915
3916static void
3917xdg_surface_unset_maximized(struct wl_client *client,
3918 struct wl_resource *resource)
3919{
3920 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3921
3922 shsurf->state_requested = true;
3923 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003924 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003925}
3926
3927static void
3928xdg_surface_set_fullscreen(struct wl_client *client,
3929 struct wl_resource *resource,
3930 struct wl_resource *output_resource)
3931{
3932 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3933 struct weston_output *output;
3934
3935 shsurf->state_requested = true;
3936 shsurf->requested_state.fullscreen = true;
3937
3938 if (output_resource)
3939 output = wl_resource_get_user_data(output_resource);
3940 else
3941 output = NULL;
3942
Marek Chalupa052917a2014-09-02 11:35:12 +02003943 /* handle clients launching in fullscreen */
3944 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3945 /* Set the output to the one that has focus currently. */
3946 assert(shsurf->surface);
3947 output = get_focused_output(shsurf->surface->compositor);
3948 }
3949
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003950 shell_surface_set_output(shsurf, output);
3951 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003952
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003953 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003954}
3955
3956static void
3957xdg_surface_unset_fullscreen(struct wl_client *client,
3958 struct wl_resource *resource)
3959{
3960 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3961
3962 shsurf->state_requested = true;
3963 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003964 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003965}
3966
3967static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003968xdg_surface_set_minimized(struct wl_client *client,
3969 struct wl_resource *resource)
3970{
3971 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3972
3973 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3974 return;
3975
3976 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003977 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003978}
3979
Rafael Antognollie2a34552013-12-03 15:35:45 -02003980static const struct xdg_surface_interface xdg_surface_implementation = {
3981 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003982 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003983 xdg_surface_set_title,
3984 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003985 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003986 xdg_surface_move,
3987 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003988 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003989 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003990 xdg_surface_set_maximized,
3991 xdg_surface_unset_maximized,
3992 xdg_surface_set_fullscreen,
3993 xdg_surface_unset_fullscreen,
3994 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003995};
3996
3997static void
3998xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003999 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004000{
4001 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004002 uint32_t *s;
4003 struct wl_array states;
4004 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004005
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004006 assert(shsurf);
4007
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004008 if (!shsurf->resource)
4009 return;
4010
4011 wl_array_init(&states);
4012 if (shsurf->requested_state.fullscreen) {
4013 s = wl_array_add(&states, sizeof *s);
4014 *s = XDG_SURFACE_STATE_FULLSCREEN;
4015 } else if (shsurf->requested_state.maximized) {
4016 s = wl_array_add(&states, sizeof *s);
4017 *s = XDG_SURFACE_STATE_MAXIMIZED;
4018 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004019 if (shsurf->resize_edges != 0) {
4020 s = wl_array_add(&states, sizeof *s);
4021 *s = XDG_SURFACE_STATE_RESIZING;
4022 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004023 if (shsurf->focus_count > 0) {
4024 s = wl_array_add(&states, sizeof *s);
4025 *s = XDG_SURFACE_STATE_ACTIVATED;
4026 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004027
4028 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4029 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4030
4031 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004032}
4033
4034static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004035 xdg_send_configure,
4036 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004037};
4038
4039static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004040xdg_shell_destroy(struct wl_client *client,
4041 struct wl_resource *resource)
4042{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004043 struct shell_client *sc = wl_resource_get_user_data(resource);
4044 struct wl_resource *shsurf_resource;
4045 struct shell_surface *shsurf;
4046
4047 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4048 shsurf = wl_resource_get_user_data(shsurf_resource);
4049 if (shsurf->owner_resource == resource) {
4050 wl_resource_post_error(
4051 resource,
4052 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4053 "not all child surface objects destroyed");
4054 return;
4055 }
4056 }
4057
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004058 wl_resource_destroy(resource);
4059}
4060
4061static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004062xdg_use_unstable_version(struct wl_client *client,
4063 struct wl_resource *resource,
4064 int32_t version)
4065{
4066 if (version > 1) {
4067 wl_resource_post_error(resource,
4068 1,
4069 "xdg-shell:: version not implemented yet.");
4070 return;
4071 }
4072}
4073
4074static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004075create_xdg_surface(struct shell_client *owner, void *shell,
4076 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004077 const struct weston_shell_client *client)
4078{
4079 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004080
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004081 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004082 if (!shsurf)
4083 return NULL;
4084
Pekka Paalanenb5026542014-11-12 15:09:24 +02004085 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004086
4087 return shsurf;
4088}
4089
4090static void
4091xdg_get_xdg_surface(struct wl_client *client,
4092 struct wl_resource *resource,
4093 uint32_t id,
4094 struct wl_resource *surface_resource)
4095{
4096 struct weston_surface *surface =
4097 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004098 struct shell_client *sc = wl_resource_get_user_data(resource);
4099 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004100 struct shell_surface *shsurf;
4101
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004102 shsurf = get_shell_surface(surface);
4103 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4104 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4105 "This wl_surface is already an "
4106 "xdg_surface");
4107 return;
4108 }
4109
Pekka Paalanen50b67472014-10-01 15:02:41 +03004110 if (weston_surface_set_role(surface, "xdg_surface",
4111 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004112 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004113
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004114 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004115 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004116 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004117 return;
4118 }
4119
4120 shsurf->resource =
4121 wl_resource_create(client,
4122 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004123 if (!shsurf->resource) {
4124 wl_resource_post_no_memory(surface_resource);
4125 return;
4126 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004127 wl_resource_set_implementation(shsurf->resource,
4128 &xdg_surface_implementation,
4129 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004130 shsurf->owner_resource = resource;
4131 wl_list_insert(&sc->surface_list,
4132 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004133}
4134
4135static bool
4136shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4137{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004138 return shsurf->resource &&
4139 wl_resource_instance_of(shsurf->resource,
4140 &xdg_surface_interface,
4141 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004142}
4143
4144/* xdg-popup implementation */
4145
4146static void
4147xdg_popup_destroy(struct wl_client *client,
4148 struct wl_resource *resource)
4149{
4150 wl_resource_destroy(resource);
4151}
4152
Rafael Antognollie2a34552013-12-03 15:35:45 -02004153static const struct xdg_popup_interface xdg_popup_implementation = {
4154 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004155};
4156
4157static void
4158xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004159 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004160{
4161}
4162
4163static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004164 xdg_popup_send_configure,
4165 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004166};
4167
4168static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004169create_xdg_popup(struct shell_client *owner, void *shell,
4170 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004171 const struct weston_shell_client *client,
4172 struct weston_surface *parent,
4173 struct shell_seat *seat,
4174 uint32_t serial,
4175 int32_t x, int32_t y)
4176{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004177 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004178
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004179 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004180 if (!shsurf)
4181 return NULL;
4182
Pekka Paalanenb5026542014-11-12 15:09:24 +02004183 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004184 shsurf->popup.shseat = seat;
4185 shsurf->popup.serial = serial;
4186 shsurf->popup.x = x;
4187 shsurf->popup.y = y;
4188 shell_surface_set_parent(shsurf, parent);
4189
4190 return shsurf;
4191}
4192
4193static void
4194xdg_get_xdg_popup(struct wl_client *client,
4195 struct wl_resource *resource,
4196 uint32_t id,
4197 struct wl_resource *surface_resource,
4198 struct wl_resource *parent_resource,
4199 struct wl_resource *seat_resource,
4200 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004201 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004202{
4203 struct weston_surface *surface =
4204 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004205 struct shell_client *sc = wl_resource_get_user_data(resource);
4206 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004207 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004208 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004209 struct weston_surface *parent;
4210 struct shell_seat *seat;
4211
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004212 shsurf = get_shell_surface(surface);
4213 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4214 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4215 "This wl_surface is already an "
4216 "xdg_popup");
4217 return;
4218 }
4219
Pekka Paalanen50b67472014-10-01 15:02:41 +03004220 if (weston_surface_set_role(surface, "xdg_popup",
4221 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004222 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004223
4224 if (!parent_resource) {
4225 wl_resource_post_error(surface_resource,
4226 WL_DISPLAY_ERROR_INVALID_OBJECT,
4227 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004228 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004229 }
4230
4231 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004232 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004233
Jonas Ådahlee45a552015-03-18 16:37:47 +08004234 /* Verify that we are creating the top most popup when mapping,
4235 * as it's not until then we know whether it was mapped as most
4236 * top level or not. */
4237
4238 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004239 if (!parent_shsurf ||
4240 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4241 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004242 wl_resource_post_error(resource,
4243 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4244 "xdg_popup parent was invalid");
4245 return;
4246 }
4247
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004248 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004249 parent, seat, serial, x, y);
4250 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004251 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004252 return;
4253 }
4254
4255 shsurf->resource =
4256 wl_resource_create(client,
4257 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004258 if (!shsurf->resource) {
4259 wl_resource_post_no_memory(surface_resource);
4260 return;
4261 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004262 wl_resource_set_implementation(shsurf->resource,
4263 &xdg_popup_implementation,
4264 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004265 shsurf->owner_resource = resource;
4266 wl_list_insert(&sc->surface_list,
4267 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004268}
4269
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004270static void
4271xdg_pong(struct wl_client *client,
4272 struct wl_resource *resource, uint32_t serial)
4273{
4274 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004275
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004276 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004277}
4278
Rafael Antognollie2a34552013-12-03 15:35:45 -02004279static bool
4280shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4281{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004282 return (shsurf->resource &&
4283 wl_resource_instance_of(shsurf->resource,
4284 &xdg_popup_interface,
4285 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004286}
4287
4288static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004289 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004290 xdg_use_unstable_version,
4291 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004292 xdg_get_xdg_popup,
4293 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004294};
4295
4296static int
4297xdg_shell_unversioned_dispatch(const void *implementation,
4298 void *_target, uint32_t opcode,
4299 const struct wl_message *message,
4300 union wl_argument *args)
4301{
4302 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004303 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004304
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004305 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004306 wl_resource_post_error(resource,
4307 WL_DISPLAY_ERROR_INVALID_OBJECT,
4308 "must call use_unstable_version first");
4309 return 0;
4310 }
4311
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004312#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004313
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004314 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4315 "shell implementation doesn't match protocol version");
4316
Rafael Antognollie2a34552013-12-03 15:35:45 -02004317 if (args[0].i != XDG_SERVER_VERSION) {
4318 wl_resource_post_error(resource,
4319 WL_DISPLAY_ERROR_INVALID_OBJECT,
4320 "incompatible version, server is %d "
4321 "client wants %d",
4322 XDG_SERVER_VERSION, args[0].i);
4323 return 0;
4324 }
4325
4326 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004327 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004328
4329 return 1;
4330}
4331
4332/* end of xdg-shell implementation */
4333/***********************************/
4334
Kristian Høgsberg07937562011-04-12 17:25:42 -04004335static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004336shell_fade(struct desktop_shell *shell, enum fade_type type);
4337
Pekka Paalanen77346a62011-11-30 16:26:35 +02004338static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004339configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004340{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004341 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004342
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004343 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344 if (v->output == ev->output && v != ev) {
4345 weston_view_unmap(v);
4346 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004347 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004348 }
4349 }
4350
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004351 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004352
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004353 if (wl_list_empty(&ev->layer_link.link)) {
4354 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004355 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004356 }
4357}
4358
Pekka Paalanen8274d902014-08-06 19:36:51 +03004359static int
4360background_get_label(struct weston_surface *surface, char *buf, size_t len)
4361{
4362 return snprintf(buf, len, "background for output %s",
4363 surface->output->name);
4364}
4365
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004366static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004367background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004368{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004369 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004371
Jason Ekstranda7af7042013-10-12 22:38:11 -05004372 view = container_of(es->views.next, struct weston_view, surface_link);
4373
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004374 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004375}
4376
4377static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004378desktop_shell_set_background(struct wl_client *client,
4379 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004380 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004381 struct wl_resource *surface_resource)
4382{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004383 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004384 struct weston_surface *surface =
4385 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004386 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004387
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004388 if (surface->configure) {
4389 wl_resource_post_error(surface_resource,
4390 WL_DISPLAY_ERROR_INVALID_OBJECT,
4391 "surface role already assigned");
4392 return;
4393 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004394
Jason Ekstranda7af7042013-10-12 22:38:11 -05004395 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4396 weston_view_destroy(view);
4397 view = weston_view_create(surface);
4398
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004399 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004400 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004401 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004402 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004404 weston_desktop_shell_send_configure(resource, 0,
4405 surface_resource,
4406 surface->output->width,
4407 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004408}
4409
Pekka Paalanen8274d902014-08-06 19:36:51 +03004410static int
4411panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4412{
4413 return snprintf(buf, len, "panel for output %s",
4414 surface->output->name);
4415}
4416
Kristian Høgsberg75840622011-09-06 13:48:16 -04004417static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004418panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004419{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004420 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004421 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004422
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 view = container_of(es->views.next, struct weston_view, surface_link);
4424
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004425 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004426}
4427
4428static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004429desktop_shell_set_panel(struct wl_client *client,
4430 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004431 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004432 struct wl_resource *surface_resource)
4433{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004434 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004435 struct weston_surface *surface =
4436 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004437 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004438
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004439 if (surface->configure) {
4440 wl_resource_post_error(surface_resource,
4441 WL_DISPLAY_ERROR_INVALID_OBJECT,
4442 "surface role already assigned");
4443 return;
4444 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004445
Jason Ekstranda7af7042013-10-12 22:38:11 -05004446 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4447 weston_view_destroy(view);
4448 view = weston_view_create(surface);
4449
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004450 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004451 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004452 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004453 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004454 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004455 weston_desktop_shell_send_configure(resource, 0,
4456 surface_resource,
4457 surface->output->width,
4458 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004459}
4460
Pekka Paalanen8274d902014-08-06 19:36:51 +03004461static int
4462lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4463{
4464 return snprintf(buf, len, "lock window");
4465}
4466
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004467static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004468lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004469{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004470 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004471 struct weston_view *view;
4472
4473 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004474
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004475 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004476 return;
4477
Jason Ekstranda7af7042013-10-12 22:38:11 -05004478 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004479
4480 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004481 weston_layer_entry_insert(&shell->lock_layer.view_list,
4482 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004483 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004484 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004485 }
4486}
4487
4488static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004489handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004490{
Tiago Vignattibe143262012-04-16 17:31:41 +03004491 struct desktop_shell *shell =
4492 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004493
Martin Minarik6d118362012-06-07 18:01:59 +02004494 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004495 shell->lock_surface = NULL;
4496}
4497
4498static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004499desktop_shell_set_lock_surface(struct wl_client *client,
4500 struct wl_resource *resource,
4501 struct wl_resource *surface_resource)
4502{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004503 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004504 struct weston_surface *surface =
4505 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004506
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004507 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004508
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004509 if (!shell->locked)
4510 return;
4511
Pekka Paalanen98262232011-12-01 10:42:22 +02004512 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004513
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004514 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004515 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004516 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004517
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004518 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004519 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004520 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004521 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004522}
4523
4524static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004525resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004526{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004527 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004528
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004529 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004530 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004531 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004532 &shell->input_panel_layer.link);
4533 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004534 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004535 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004536 wl_list_insert(&shell->compositor->cursor_layer.link,
4537 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004538 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004539 wl_list_insert(&shell->fullscreen_layer.link,
4540 &shell->panel_layer.link);
4541 wl_list_insert(&shell->panel_layer.link,
4542 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004543
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004544 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004545
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004546 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004547 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004548 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004549}
4550
4551static void
4552desktop_shell_unlock(struct wl_client *client,
4553 struct wl_resource *resource)
4554{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004555 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004556
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004557 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004558
4559 if (shell->locked)
4560 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004561}
4562
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004563static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004564desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004565 struct wl_resource *resource,
4566 struct wl_resource *surface_resource)
4567{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004568 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004569
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004570 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004571 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004572}
4573
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004574static void
4575desktop_shell_desktop_ready(struct wl_client *client,
4576 struct wl_resource *resource)
4577{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004578 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004579
4580 shell_fade_startup(shell);
4581}
4582
Jonny Lamb765760d2014-08-20 15:53:19 +02004583static void
4584desktop_shell_set_panel_position(struct wl_client *client,
4585 struct wl_resource *resource,
4586 uint32_t position)
4587{
4588 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4589
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004590 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4591 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4592 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4593 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004594 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004595 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004596 "bad position argument");
4597 return;
4598 }
4599
4600 shell->panel_position = position;
4601}
4602
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004603static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004604 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004605 desktop_shell_set_panel,
4606 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004607 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004608 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004609 desktop_shell_desktop_ready,
4610 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004611};
4612
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004613static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004614get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004615{
4616 struct shell_surface *shsurf;
4617
4618 shsurf = get_shell_surface(surface);
4619 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004620 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004621 return shsurf->type;
4622}
4623
Kristian Høgsberg75840622011-09-06 13:48:16 -04004624static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004625move_binding(struct weston_pointer *pointer, uint32_t time,
4626 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004627{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004628 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004629 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004630 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004631
Derek Foreman8ae2db52015-07-15 13:00:36 -05004632 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004633 return;
4634
Derek Foreman8ae2db52015-07-15 13:00:36 -05004635 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004636
Pekka Paalanen01388e22013-04-25 13:57:44 +03004637 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004638 if (surface == NULL)
4639 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004640
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004641 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004642 if (shsurf == NULL || shsurf->state.fullscreen ||
4643 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004644 return;
4645
Derek Foreman794fa0e2015-07-15 13:00:38 -05004646 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004647}
4648
4649static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004650maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4651 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004652{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004653 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004654 struct weston_surface *surface;
4655 struct shell_surface *shsurf;
4656
4657 surface = weston_surface_get_main_surface(focus);
4658 if (surface == NULL)
4659 return;
4660
4661 shsurf = get_shell_surface(surface);
4662 if (shsurf == NULL)
4663 return;
4664
4665 if (!shell_surface_is_xdg_surface(shsurf))
4666 return;
4667
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004668 shsurf->state_requested = true;
4669 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004670 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004671}
4672
4673static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004674fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4675 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004676{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004677 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004678 struct weston_surface *surface;
4679 struct shell_surface *shsurf;
4680
4681 surface = weston_surface_get_main_surface(focus);
4682 if (surface == NULL)
4683 return;
4684
4685 shsurf = get_shell_surface(surface);
4686 if (shsurf == NULL)
4687 return;
4688
4689 if (!shell_surface_is_xdg_surface(shsurf))
4690 return;
4691
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004692 shsurf->state_requested = true;
4693 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004694 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004695 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004696}
4697
4698static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004699touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004700{
Derek Foreman362656b2014-09-04 10:23:05 -05004701 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004702 struct weston_surface *surface;
4703 struct shell_surface *shsurf;
4704
Derek Foreman8ae2db52015-07-15 13:00:36 -05004705 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004706 return;
4707
Derek Foreman8ae2db52015-07-15 13:00:36 -05004708 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004709 surface = weston_surface_get_main_surface(focus);
4710 if (surface == NULL)
4711 return;
4712
4713 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004714 if (shsurf == NULL || shsurf->state.fullscreen ||
4715 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004716 return;
4717
Derek Foremanb7674ae2015-07-15 13:00:37 -05004718 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004719}
4720
4721static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004722resize_binding(struct weston_pointer *pointer, uint32_t time,
4723 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004724{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004725 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004726 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004727 uint32_t edges = 0;
4728 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004729 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004730
Derek Foreman8ae2db52015-07-15 13:00:36 -05004731 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004732 return;
4733
Derek Foreman8ae2db52015-07-15 13:00:36 -05004734 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004735
Pekka Paalanen01388e22013-04-25 13:57:44 +03004736 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004737 if (surface == NULL)
4738 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004739
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004740 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004741 if (shsurf == NULL || shsurf->state.fullscreen ||
4742 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004743 return;
4744
Jason Ekstranda7af7042013-10-12 22:38:11 -05004745 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004746 wl_fixed_to_int(pointer->grab_x),
4747 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004748 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004749
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004750 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004751 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004752 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004753 edges |= 0;
4754 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004755 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004756
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004757 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004758 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004759 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004760 edges |= 0;
4761 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004762 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004763
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004764 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004765}
4766
4767static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004768surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004769 struct weston_pointer_axis_event *event,
4770 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004771{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004772 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004773 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004774 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004775 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004776
Pekka Paalanen01388e22013-04-25 13:57:44 +03004777 /* XXX: broken for windows containing sub-surfaces */
4778 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004779 if (surface == NULL)
4780 return;
4781
4782 shsurf = get_shell_surface(surface);
4783 if (!shsurf)
4784 return;
4785
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004786 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004787
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 if (shsurf->view->alpha > 1.0)
4789 shsurf->view->alpha = 1.0;
4790 if (shsurf->view->alpha < step)
4791 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004792
Jason Ekstranda7af7042013-10-12 22:38:11 -05004793 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004794 weston_surface_damage(surface);
4795}
4796
4797static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004798do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004799 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004800{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004801 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004802 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004803 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004804 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004805
Derek Foreman1281a362015-07-31 16:55:32 -05004806 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004807 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4808 return;
4809 }
4810
Scott Moreauccbf29d2012-02-22 14:21:41 -07004811 wl_list_for_each(output, &compositor->output_list, link) {
4812 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004813 wl_fixed_to_double(pointer->x),
4814 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004815 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004816 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004817 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004818 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004819 increment = -output->zoom.increment;
4820 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004821 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004822 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004823 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004824 else
4825 increment = 0;
4826
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004827 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004828
Scott Moreaue6603982012-06-11 13:07:51 -06004829 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004830 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004831 else if (output->zoom.level > output->zoom.max_level)
4832 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004833
4834 if (!output->zoom.active) {
4835 if (output->zoom.level <= 0.0)
4836 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004837 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004838 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004839
Scott Moreaue6603982012-06-11 13:07:51 -06004840 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004841
Jason Ekstranda7af7042013-10-12 22:38:11 -05004842 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004843 }
4844 }
4845}
4846
Scott Moreauccbf29d2012-02-22 14:21:41 -07004847static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004848zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004849 struct weston_pointer_axis_event *event,
4850 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004851{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004852 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004853}
4854
4855static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004856zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4857 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004858{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004859 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004860}
4861
4862static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004863terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4864 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004865{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004866 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004867
Daniel Stone325fc2d2012-05-30 16:31:58 +01004868 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004869}
4870
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004871static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004872rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004873 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004874{
4875 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004876 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004877 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004878 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004879 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004880
Jonas Ådahld2510102014-10-05 21:39:14 +02004881 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004882
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004883 if (!shsurf)
4884 return;
4885
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004886 cx = 0.5f * shsurf->surface->width;
4887 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004888
Daniel Stone37816df2012-05-16 18:45:18 +01004889 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4890 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004891 r = sqrtf(dx * dx + dy * dy);
4892
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004893 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004895
4896 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004897 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004898 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004899
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004900 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004901 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004902
4903 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004904 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004905 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004906 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004907 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004908
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004909 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004910 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004911 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004912 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004913 wl_list_init(&shsurf->rotation.transform.link);
4914 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004915 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004916 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004917
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004918 /* We need to adjust the position of the surface
4919 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004920 cposx = shsurf->view->geometry.x + cx;
4921 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004922 dposx = rotate->center.x - cposx;
4923 dposy = rotate->center.y - cposy;
4924 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004925 weston_view_set_position(shsurf->view,
4926 shsurf->view->geometry.x + dposx,
4927 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004928 }
4929
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004930 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004931 * lazily applies the damage due to rotation update.
4932 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004933 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004934}
4935
4936static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004937rotate_grab_button(struct weston_pointer_grab *grab,
4938 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004939{
4940 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004941 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004942 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004943 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004944 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004945
Daniel Stone4dbadb12012-05-30 16:31:51 +01004946 if (pointer->button_count == 0 &&
4947 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004948 if (shsurf)
4949 weston_matrix_multiply(&shsurf->rotation.rotation,
4950 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004951 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004952 free(rotate);
4953 }
4954}
4955
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004956static void
4957rotate_grab_cancel(struct weston_pointer_grab *grab)
4958{
4959 struct rotate_grab *rotate =
4960 container_of(grab, struct rotate_grab, base.grab);
4961
4962 shell_grab_end(&rotate->base);
4963 free(rotate);
4964}
4965
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004966static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004967 noop_grab_focus,
4968 rotate_grab_motion,
4969 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02004970 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10004971 noop_grab_axis_source,
4972 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004973 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004974};
4975
4976static void
Derek Foreman74de4692015-07-15 13:00:39 -05004977surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004978{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004979 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004980 float dx, dy;
4981 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004982
Derek Foreman45a7c272015-09-11 14:27:40 -05004983 surface = find_toplevel_surface(surface);
4984
Pekka Paalanen460099f2012-01-20 16:48:25 +02004985 rotate = malloc(sizeof *rotate);
4986 if (!rotate)
4987 return;
4988
Jason Ekstranda7af7042013-10-12 22:38:11 -05004989 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004990 surface->surface->width * 0.5f,
4991 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004992 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004993
Derek Foreman74de4692015-07-15 13:00:39 -05004994 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4995 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004996 r = sqrtf(dx * dx + dy * dy);
4997 if (r > 20.0f) {
4998 struct weston_matrix inverse;
4999
5000 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005001 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005002 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005003
5004 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005005 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005006 } else {
5007 weston_matrix_init(&surface->rotation.rotation);
5008 weston_matrix_init(&rotate->rotation);
5009 }
5010
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005011 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005012 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005013}
5014
5015static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005016rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005017 void *data)
5018{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005019 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005020 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005021 struct shell_surface *surface;
5022
Derek Foreman8ae2db52015-07-15 13:00:36 -05005023 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005024 return;
5025
Derek Foreman8ae2db52015-07-15 13:00:36 -05005026 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005027
Pekka Paalanen01388e22013-04-25 13:57:44 +03005028 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005029 if (base_surface == NULL)
5030 return;
5031
5032 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005033 if (surface == NULL || surface->state.fullscreen ||
5034 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005035 return;
5036
Derek Foreman74de4692015-07-15 13:00:39 -05005037 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005038}
5039
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005040/* Move all fullscreen layers down to the current workspace and hide their
5041 * black views. The surfaces' state is set to both fullscreen and lowered,
5042 * and this is reversed when such a surface is re-configured, see
5043 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5044 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005045 * lowering_output = NULL - Lower on all outputs, else only lower on the
5046 * specified output.
5047 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005048 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005049 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005050void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005051lower_fullscreen_layer(struct desktop_shell *shell,
5052 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005053{
5054 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005055 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005056
5057 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005058 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005059 &shell->fullscreen_layer.view_list.link,
5060 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005061 struct shell_surface *shsurf = get_shell_surface(view->surface);
5062
5063 if (!shsurf)
5064 continue;
5065
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005066 /* Only lower surfaces which have lowering_output as their fullscreen
5067 * output, unless a NULL output asks for lowering on all outputs.
5068 */
5069 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5070 continue;
5071
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005072 /* We can have a non-fullscreen popup for a fullscreen surface
5073 * in the fullscreen layer. */
5074 if (shsurf->state.fullscreen) {
5075 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005076 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5077 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005078 weston_view_damage_below(shsurf->fullscreen.black_view);
5079
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005080 }
5081
5082 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005083 weston_layer_entry_remove(&view->layer_link);
5084 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005085 weston_view_damage_below(view);
5086 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005087
5088 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005089 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005090}
5091
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005092void
Tiago Vignattibe143262012-04-16 17:31:41 +03005093activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005094 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005095{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005096 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005097 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005098 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005099 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005100 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005101
Pekka Paalanen01388e22013-04-25 13:57:44 +03005102 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005103 shsurf = get_shell_surface(main_surface);
5104 assert(shsurf);
5105
5106 /* Only demote fullscreen surfaces on the output of activated shsurf.
5107 * Leave fullscreen surfaces on unrelated outputs alone. */
5108 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005109
Daniel Stone37816df2012-05-16 18:45:18 +01005110 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005111
Jonas Ådahl8538b222012-08-29 22:13:03 +02005112 state = ensure_focus_state(shell, seat);
5113 if (state == NULL)
5114 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005115
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005116 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005117 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005118
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005119 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005120 shell_configure_fullscreen(shsurf);
5121 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005122 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005123
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005124 /* Update the surface’s layer. This brings it to the top of the stacking
5125 * order as appropriate. */
5126 shell_surface_update_layer(shsurf);
5127
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005128 if (shell->focus_animation_type != ANIMATION_NONE) {
5129 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005130 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005131 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005132}
5133
Alex Wu21858432012-04-01 20:13:08 +08005134/* no-op func for checking black surface */
5135static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005136black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005137{
5138}
5139
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005140static bool
Alex Wu21858432012-04-01 20:13:08 +08005141is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5142{
5143 if (es->configure == black_surface_configure) {
5144 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005145 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005146 return true;
5147 }
5148 return false;
5149}
5150
Kristian Høgsberg75840622011-09-06 13:48:16 -04005151static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005152activate_binding(struct weston_seat *seat,
5153 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005154 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005155{
Derek Foremanbbde6432015-12-10 13:08:01 -06005156 struct focus_state *state;
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005157 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005158 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005159
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005160 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005161
Pekka Paalanen01388e22013-04-25 13:57:44 +03005162 if (is_black_surface(focus, &main_surface))
5163 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005164
Pekka Paalanen01388e22013-04-25 13:57:44 +03005165 main_surface = weston_surface_get_main_surface(focus);
5166 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005167 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005168
Derek Foremanbbde6432015-12-10 13:08:01 -06005169 state = ensure_focus_state(shell, seat);
5170 if (state == NULL)
5171 return;
5172
5173 if (state->keyboard_focus == focus)
5174 return;
5175
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005176 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005177}
5178
5179static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005180click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5181 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005182{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005183 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005184 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005185 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005186 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005187
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005188 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005189}
5190
5191static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005192touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5193 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005194{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005195 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005196 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005197 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005198 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005199
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005200 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005201}
5202
5203static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005204unfocus_all_seats(struct desktop_shell *shell)
5205{
5206 struct weston_seat *seat, *next;
5207
5208 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005209 struct weston_keyboard *keyboard =
5210 weston_seat_get_keyboard(seat);
5211
5212 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005213 continue;
5214
Derek Foreman1281a362015-07-31 16:55:32 -05005215 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005216 }
5217}
5218
5219static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005220lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005221{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005222 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005223
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005224 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005225 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005226 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005227 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005228
5229 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005230
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005231 /* Hide all surfaces by removing the fullscreen, panel and
5232 * toplevel layers. This way nothing else can show or receive
5233 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005234
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005235 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005236 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005237 if (shell->showing_input_panels)
5238 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005239 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005240 wl_list_insert(&shell->compositor->cursor_layer.link,
5241 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005242
Derek Foreman004b4a12015-07-20 16:28:13 -05005243 weston_compositor_sleep(shell->compositor);
5244
Neil Robertsb4a91702014-04-09 16:33:32 +01005245 /* Remove the keyboard focus on all seats. This will be
5246 * restored to the workspace's saved state via
5247 * restore_focus_state when the compositor is unlocked */
5248 unfocus_all_seats(shell);
5249
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005250 /* TODO: disable bindings that should not work while locked. */
5251
5252 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005253}
5254
5255static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005256unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005257{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005258 struct wl_resource *shell_resource;
5259
Pekka Paalanend81c2162011-11-16 13:47:34 +02005260 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005261 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005262 return;
5263 }
5264
5265 /* If desktop-shell client has gone away, unlock immediately. */
5266 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005267 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005268 return;
5269 }
5270
5271 if (shell->prepare_event_sent)
5272 return;
5273
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005274 shell_resource = shell->child.desktop_shell;
5275 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005276 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005277}
5278
5279static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005280shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005281{
5282 struct desktop_shell *shell = data;
5283
5284 shell->fade.animation = NULL;
5285
5286 switch (shell->fade.type) {
5287 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005288 weston_surface_destroy(shell->fade.view->surface);
5289 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005290 break;
5291 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005292 lock(shell);
5293 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005294 default:
5295 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005296 }
5297}
5298
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005300shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005301{
5302 struct weston_compositor *compositor = shell->compositor;
5303 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005304 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005305
5306 surface = weston_surface_create(compositor);
5307 if (!surface)
5308 return NULL;
5309
Jason Ekstranda7af7042013-10-12 22:38:11 -05005310 view = weston_view_create(surface);
5311 if (!view) {
5312 weston_surface_destroy(surface);
5313 return NULL;
5314 }
5315
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005316 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005317 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005318 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005319 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5320 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005321 pixman_region32_init(&surface->input);
5322
Jason Ekstranda7af7042013-10-12 22:38:11 -05005323 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005324}
5325
5326static void
5327shell_fade(struct desktop_shell *shell, enum fade_type type)
5328{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005329 float tint;
5330
5331 switch (type) {
5332 case FADE_IN:
5333 tint = 0.0;
5334 break;
5335 case FADE_OUT:
5336 tint = 1.0;
5337 break;
5338 default:
5339 weston_log("shell: invalid fade type\n");
5340 return;
5341 }
5342
5343 shell->fade.type = type;
5344
Jason Ekstranda7af7042013-10-12 22:38:11 -05005345 if (shell->fade.view == NULL) {
5346 shell->fade.view = shell_fade_create_surface(shell);
5347 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005348 return;
5349
Jason Ekstranda7af7042013-10-12 22:38:11 -05005350 shell->fade.view->alpha = 1.0 - tint;
5351 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005352 }
5353
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005354 if (shell->fade.view->output == NULL) {
5355 /* If the black view gets a NULL output, we lost the
5356 * last output and we'll just cancel the fade. This
5357 * happens when you close the last window under the
5358 * X11 or Wayland backends. */
5359 shell->locked = false;
5360 weston_surface_destroy(shell->fade.view->surface);
5361 shell->fade.view = NULL;
5362 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005363 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005364 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005365 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005366 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005367 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005368 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005369 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005370}
5371
5372static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005373do_shell_fade_startup(void *data)
5374{
5375 struct desktop_shell *shell = data;
5376
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005377 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005378 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005379 } else {
5380 weston_log("desktop shell: "
5381 "unexpected fade-in animation type %d\n",
5382 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005383 weston_surface_destroy(shell->fade.view->surface);
5384 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005385 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005386}
5387
5388static void
5389shell_fade_startup(struct desktop_shell *shell)
5390{
5391 struct wl_event_loop *loop;
5392
5393 if (!shell->fade.startup_timer)
5394 return;
5395
5396 wl_event_source_remove(shell->fade.startup_timer);
5397 shell->fade.startup_timer = NULL;
5398
5399 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5400 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5401}
5402
5403static int
5404fade_startup_timeout(void *data)
5405{
5406 struct desktop_shell *shell = data;
5407
5408 shell_fade_startup(shell);
5409 return 0;
5410}
5411
5412static void
5413shell_fade_init(struct desktop_shell *shell)
5414{
5415 /* Make compositor output all black, and wait for the desktop-shell
5416 * client to signal it is ready, then fade in. The timer triggers a
5417 * fade-in, in case the desktop-shell client takes too long.
5418 */
5419
5420 struct wl_event_loop *loop;
5421
Jason Ekstranda7af7042013-10-12 22:38:11 -05005422 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005423 weston_log("%s: warning: fade surface already exists\n",
5424 __func__);
5425 return;
5426 }
5427
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005428 if (shell->startup_animation_type == ANIMATION_NONE)
5429 return;
5430
Jason Ekstranda7af7042013-10-12 22:38:11 -05005431 shell->fade.view = shell_fade_create_surface(shell);
5432 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005433 return;
5434
Jason Ekstranda7af7042013-10-12 22:38:11 -05005435 weston_view_update_transform(shell->fade.view);
5436 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005437
5438 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5439 shell->fade.startup_timer =
5440 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5441 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5442}
5443
5444static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005445idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005446{
5447 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005448 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005449 struct weston_seat *seat;
5450
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005451 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005452 struct weston_touch *touch = weston_seat_get_touch(seat);
5453 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5454
5455 if (pointer)
5456 popup_grab_end(pointer);
5457 if (touch)
5458 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005459 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005460
5461 shell_fade(shell, FADE_OUT);
5462 /* lock() is called from shell_fade_done() */
5463}
5464
5465static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005466wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005467{
5468 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005469 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005470
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005471 unlock(shell);
5472}
5473
5474static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005475transform_handler(struct wl_listener *listener, void *data)
5476{
5477 struct weston_surface *surface = data;
5478 struct shell_surface *shsurf = get_shell_surface(surface);
5479 struct weston_view *view;;
5480 int x, y;
5481
5482 if (!shsurf || !shsurf->client->send_position)
5483 return;
5484
5485 view = shsurf->view;
5486 if (!view || !weston_view_is_mapped(view))
5487 return;
5488
5489 x = view->geometry.x;
5490 y = view->geometry.y;
5491
5492 shsurf->client->send_position(surface, x, y);
5493}
5494
5495static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005496center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005497{
Giulio Camuffob8366642013-04-25 13:57:46 +03005498 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005499 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005500
Jason Ekstranda7af7042013-10-12 22:38:11 -05005501 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005502
5503 x = output->x + (output->width - width) / 2 - surf_x / 2;
5504 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005505
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005506 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005507}
5508
5509static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005510weston_view_set_initial_position(struct weston_view *view,
5511 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005512{
5513 struct weston_compositor *compositor = shell->compositor;
5514 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005515 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005516 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005517 struct weston_output *output, *target_output = NULL;
5518 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005519 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005520
5521 /* As a heuristic place the new window on the same output as the
5522 * pointer. Falling back to the output containing 0, 0.
5523 *
5524 * TODO: Do something clever for touch too?
5525 */
5526 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005527 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5528
5529 if (pointer) {
5530 ix = wl_fixed_to_int(pointer->x);
5531 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005532 break;
5533 }
5534 }
5535
5536 wl_list_for_each(output, &compositor->output_list, link) {
5537 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5538 target_output = output;
5539 break;
5540 }
5541 }
5542
5543 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005544 weston_view_set_position(view, 10 + random() % 400,
5545 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005546 return;
5547 }
5548
5549 /* Valid range within output where the surface will still be onscreen.
5550 * If this is negative it means that the surface is bigger than
5551 * output.
5552 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005553 get_output_work_area(shell, target_output, &area);
5554
Derek Foremanf814c5d2015-04-15 12:23:54 -05005555 x = area.x;
5556 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005557 range_x = area.width - view->surface->width;
5558 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005559
Rob Bradford4cb88c72012-08-13 15:18:44 +01005560 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005561 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005562
5563 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005564 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005565
Jason Ekstranda7af7042013-10-12 22:38:11 -05005566 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005567}
5568
5569static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005570set_maximized_position(struct desktop_shell *shell,
5571 struct shell_surface *shsurf)
5572{
5573 int32_t surf_x, surf_y;
5574 pixman_rectangle32_t area;
5575
Jonny Lambd73c6942014-08-20 15:53:20 +02005576 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005577 if (shsurf->has_set_geometry) {
5578 surf_x = shsurf->geometry.x;
5579 surf_y = shsurf->geometry.y;
5580 } else {
5581 surface_subsurfaces_boundingbox(shsurf->surface,
5582 &surf_x, &surf_y, NULL, NULL);
5583 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005584
5585 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005586 area.x - surf_x,
5587 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005588}
5589
5590static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005591map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005592 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005593{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005594 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005595 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005596
Pekka Paalanen77346a62011-11-30 16:26:35 +02005597 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005598 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005599 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005600 if (shsurf->state.fullscreen) {
5601 center_on_output(shsurf->view, shsurf->fullscreen_output);
5602 shell_map_fullscreen(shsurf);
5603 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005604 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005605 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005606 weston_view_set_initial_position(shsurf->view, shell);
5607 }
Juan Zhao96879df2012-02-07 08:45:41 +08005608 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005609 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005610 if (shell_map_popup(shsurf) != 0)
5611 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005612 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005613 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005614 weston_view_set_position(shsurf->view,
5615 shsurf->view->geometry.x + sx,
5616 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005617 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005618 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005619 default:
5620 ;
5621 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005622
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005623 /* Surface stacking order, see also activate(). */
5624 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005625
Jason Ekstranda7af7042013-10-12 22:38:11 -05005626 if (shsurf->type != SHELL_SURFACE_NONE) {
5627 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005628 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005629 shsurf->surface->output = shsurf->output;
5630 shsurf->view->output = shsurf->output;
5631 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005632 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005633
Jason Ekstranda7af7042013-10-12 22:38:11 -05005634 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005635 /* XXX: xwayland's using the same fields for transient type */
5636 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005637 if (shsurf->transient.flags ==
5638 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5639 break;
5640 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005641 if (shsurf->state.relative &&
5642 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5643 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005644 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005645 break;
5646 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005647 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005648 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005649 case SHELL_SURFACE_POPUP:
5650 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005651 default:
5652 break;
5653 }
5654
Rafael Antognolli03b16592013-12-03 15:35:42 -02005655 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5656 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005657 {
5658 switch (shell->win_animation_type) {
5659 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005660 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005661 break;
5662 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005663 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005664 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005665 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005666 default:
5667 break;
5668 }
5669 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005670}
5671
5672static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005673configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005674 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005675{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005676 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005677 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005678
Pekka Paalanen77346a62011-11-30 16:26:35 +02005679 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005680
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005681 assert(shsurf);
5682
Rafael Antognolli03b16592013-12-03 15:35:42 -02005683 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005684 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005685 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005686 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005687 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005688 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005689 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005690
Alex Wu4539b082012-03-01 12:57:46 +08005691 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005692 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005693 wl_list_for_each(view, &surface->views, surface_link)
5694 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005695
Rafael Antognolli03b16592013-12-03 15:35:42 -02005696 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005697 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005698 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005699}
5700
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005701static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005702shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005703{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005704 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005705 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005706 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005707
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005708 assert(shsurf);
5709
5710 shell = shsurf->shell;
5711
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005712 if (!weston_surface_is_mapped(es) &&
5713 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005714 remove_popup_grab(shsurf);
5715 }
5716
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005717 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005718 return;
5719
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005720 if (shsurf->has_next_geometry) {
5721 shsurf->geometry = shsurf->next_geometry;
5722 shsurf->has_next_geometry = false;
5723 shsurf->has_set_geometry = true;
5724 } else if (!shsurf->has_set_geometry) {
5725 surface_subsurfaces_boundingbox(shsurf->surface,
5726 &shsurf->geometry.x,
5727 &shsurf->geometry.y,
5728 &shsurf->geometry.width,
5729 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005730 }
5731
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005732 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005733 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005734 type_changed = 1;
5735 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005736
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005737 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005738 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005739 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005740 shsurf->last_width != es->width ||
5741 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005742 float from_x, from_y;
5743 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005744
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005745 if (shsurf->resize_edges) {
5746 sx = 0;
5747 sy = 0;
5748 }
5749
5750 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5751 sx = shsurf->last_width - es->width;
5752 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5753 sy = shsurf->last_height - es->height;
5754
5755 shsurf->last_width = es->width;
5756 shsurf->last_height = es->height;
5757
Jason Ekstranda7af7042013-10-12 22:38:11 -05005758 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5759 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005760 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005761 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005762 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005763 }
5764}
5765
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005766static bool
5767check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5768{
5769 struct timespec now;
5770
5771 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5772 return false;
5773
5774 /*
5775 * If the shell helper client dies before the session has been
5776 * up for roughly 30 seconds, better just make Weston shut down,
5777 * because the user likely has no way to interact with the desktop
5778 * anyway.
5779 */
5780 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5781 weston_log("Error: %s apparently cannot run at all.\n",
5782 shell->client);
5783 weston_log_continue(STAMP_SPACE "Quitting...");
5784 wl_display_terminate(shell->compositor->wl_display);
5785
5786 return true;
5787 }
5788
5789 return false;
5790}
5791
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005792static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005793
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005794static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005795respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005796{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005797 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005798
5799 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5800 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005801 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005802 shell->child.deathstamp = time;
5803 shell->child.deathcount = 0;
5804 }
5805
5806 shell->child.deathcount++;
5807 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005808 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005809 return;
5810 }
5811
Pekka Paalanen826dc142014-08-27 12:11:53 +03005812 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005813 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005814}
5815
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005816static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005817desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5818{
5819 struct desktop_shell *shell;
5820
5821 shell = container_of(listener, struct desktop_shell,
5822 child.client_destroy_listener);
5823
Pekka Paalanen826dc142014-08-27 12:11:53 +03005824 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005825 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005826 /*
5827 * unbind_desktop_shell() will reset shell->child.desktop_shell
5828 * before the respawned process has a chance to create a new
5829 * desktop_shell object, because we are being called from the
5830 * wl_client destructor which destroys all wl_resources before
5831 * returning.
5832 */
5833
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005834 if (!check_desktop_shell_crash_too_early(shell))
5835 respawn_desktop_shell_process(shell);
5836
Pekka Paalanen826dc142014-08-27 12:11:53 +03005837 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005838}
5839
5840static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005841launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005842{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005843 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005844
Pekka Paalanen826dc142014-08-27 12:11:53 +03005845 shell->child.client = weston_client_start(shell->compositor,
5846 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005847
Arnaud Vrac42631192014-08-25 20:56:46 +02005848 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005849 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005850 return;
5851 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005852
5853 shell->child.client_destroy_listener.notify =
5854 desktop_shell_client_destroy;
5855 wl_client_add_destroy_listener(shell->child.client,
5856 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005857}
5858
5859static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005860handle_shell_client_destroy(struct wl_listener *listener, void *data)
5861{
5862 struct shell_client *sc =
5863 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005864 struct wl_resource *shsurf_resource;
5865 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005866
5867 if (sc->ping_timer)
5868 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005869
5870 /* Since we're about to free shell_client, we remove it from the
5871 * head of the surface list so we don't use that freed list node
5872 * during surface clean up later on.
5873 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005874 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5875 shsurf = wl_resource_get_user_data(shsurf_resource);
5876 shsurf->owner = NULL;
5877 }
Derek Foremane42d7542015-05-29 10:46:44 -05005878 wl_list_remove(&sc->surface_list);
5879
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005880 free(sc);
5881}
5882
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005883static struct shell_client *
5884shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5885 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005886{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005887 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005888
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005889 sc = zalloc(sizeof *sc);
5890 if (sc == NULL) {
5891 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005892 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005893 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005894
5895 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005896 if (sc->resource == NULL) {
5897 free(sc);
5898 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005899 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005900 }
5901
5902 sc->client = client;
5903 sc->shell = shell;
5904 sc->destroy_listener.notify = handle_shell_client_destroy;
5905 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005906 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005907
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005908 return sc;
5909}
5910
5911static void
5912bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5913{
5914 struct desktop_shell *shell = data;
5915 struct shell_client *sc;
5916
5917 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5918 if (sc)
5919 wl_resource_set_implementation(sc->resource,
5920 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005921 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005922}
5923
5924static void
5925bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5926{
5927 struct desktop_shell *shell = data;
5928 struct shell_client *sc;
5929
5930 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5931 if (sc)
5932 wl_resource_set_dispatcher(sc->resource,
5933 xdg_shell_unversioned_dispatch,
5934 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005935}
5936
5937static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005938unbind_desktop_shell(struct wl_resource *resource)
5939{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005940 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005941
5942 if (shell->locked)
5943 resume_desktop(shell);
5944
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005945 shell->child.desktop_shell = NULL;
5946 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005947}
5948
5949static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005950bind_desktop_shell(struct wl_client *client,
5951 void *data, uint32_t version, uint32_t id)
5952{
Tiago Vignattibe143262012-04-16 17:31:41 +03005953 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005954 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005955
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005956 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5957 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005958
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005959 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005960 wl_resource_set_implementation(resource,
5961 &desktop_shell_implementation,
5962 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005963 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005964 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005965 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005966
5967 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5968 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005969}
5970
Kristian Høgsberg07045392012-02-19 18:52:44 -05005971struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005972 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005973 struct weston_surface *current;
5974 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005975 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005976 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005977};
5978
5979static void
5980switcher_next(struct switcher *switcher)
5981{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005982 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005983 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005984 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005985 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005986
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005987 /* temporary re-display minimized surfaces */
5988 struct weston_view *tmp;
5989 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005990 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5991 weston_layer_entry_remove(&view->layer_link);
5992 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005993 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5994 *minimized = view;
5995 }
5996
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005997 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005998 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005999 if (shsurf &&
6000 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6001 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006002 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006003 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006004 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006005 next = view->surface;
6006 prev = view->surface;
6007 view->alpha = 0.25;
6008 weston_view_geometry_dirty(view);
6009 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006010 }
Alex Wu1659daa2012-04-01 20:13:09 +08006011
Jason Ekstranda7af7042013-10-12 22:38:11 -05006012 if (is_black_surface(view->surface, NULL)) {
6013 view->alpha = 0.25;
6014 weston_view_geometry_dirty(view);
6015 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006016 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006017 }
6018
6019 if (next == NULL)
6020 next = first;
6021
Alex Wu07b26062012-03-12 16:06:01 +08006022 if (next == NULL)
6023 return;
6024
Kristian Høgsberg07045392012-02-19 18:52:44 -05006025 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006026 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006027
6028 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006029 wl_list_for_each(view, &next->views, surface_link)
6030 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006031
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006032 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006033 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006034 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006035}
6036
6037static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006038switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006039{
6040 struct switcher *switcher =
6041 container_of(listener, struct switcher, listener);
6042
6043 switcher_next(switcher);
6044}
6045
6046static void
Daniel Stone351eb612012-05-31 15:27:47 -04006047switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006048{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006049 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006050 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006051 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006052
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006053 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006054 if (is_focus_view(view))
6055 continue;
6056
Jason Ekstranda7af7042013-10-12 22:38:11 -05006057 view->alpha = 1.0;
6058 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006059 }
6060
Alex Wu07b26062012-03-12 16:06:01 +08006061 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006062 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006063 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006064 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006065 weston_keyboard_end_grab(keyboard);
6066 if (keyboard->input_method_resource)
6067 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006068
6069 /* re-hide surfaces that were temporary shown during the switch */
6070 struct weston_view **minimized;
6071 wl_array_for_each(minimized, &switcher->minimized_array) {
6072 /* with the exception of the current selected */
6073 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006074 weston_layer_entry_remove(&(*minimized)->layer_link);
6075 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006076 weston_view_damage_below(*minimized);
6077 }
6078 }
6079 wl_array_release(&switcher->minimized_array);
6080
Kristian Høgsberg07045392012-02-19 18:52:44 -05006081 free(switcher);
6082}
6083
6084static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006085switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006086 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006087{
6088 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006089 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006090
Daniel Stonec9785ea2012-05-30 16:31:52 +01006091 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006092 switcher_next(switcher);
6093}
6094
6095static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006096switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006097 uint32_t mods_depressed, uint32_t mods_latched,
6098 uint32_t mods_locked, uint32_t group)
6099{
6100 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006101 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006102
Daniel Stone351eb612012-05-31 15:27:47 -04006103 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6104 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006105}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006106
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006107static void
6108switcher_cancel(struct weston_keyboard_grab *grab)
6109{
6110 struct switcher *switcher = container_of(grab, struct switcher, grab);
6111
6112 switcher_destroy(switcher);
6113}
6114
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006115static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006116 switcher_key,
6117 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006118 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006119};
6120
6121static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006122switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6123 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006124{
Tiago Vignattibe143262012-04-16 17:31:41 +03006125 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006126 struct switcher *switcher;
6127
6128 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006129 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006130 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006131 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006132 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006133 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006134
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006135 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006136 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006137 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006138 weston_keyboard_start_grab(keyboard, &switcher->grab);
6139 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006140 switcher_next(switcher);
6141}
6142
Pekka Paalanen3c647232011-12-22 13:43:43 +02006143static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006144backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6145 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006146{
6147 struct weston_compositor *compositor = data;
6148 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006149 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006150
6151 /* TODO: we're limiting to simple use cases, where we assume just
6152 * control on the primary display. We'd have to extend later if we
6153 * ever get support for setting backlights on random desktop LCD
6154 * panels though */
6155 output = get_default_output(compositor);
6156 if (!output)
6157 return;
6158
6159 if (!output->set_backlight)
6160 return;
6161
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006162 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6163 backlight_new = output->backlight_current - 25;
6164 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6165 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006166
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006167 if (backlight_new < 5)
6168 backlight_new = 5;
6169 if (backlight_new > 255)
6170 backlight_new = 255;
6171
6172 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006173 output->set_backlight(output, output->backlight_current);
6174}
6175
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006176static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006177force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6178 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006179{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006180 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006181 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006182 struct desktop_shell *shell = data;
6183 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006184 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006185
Derek Foreman8ae2db52015-07-15 13:00:36 -05006186 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006187 if (!focus_surface)
6188 return;
6189
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006190 wl_signal_emit(&compositor->kill_signal, focus_surface);
6191
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006192 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006193 wl_client_get_credentials(client, &pid, NULL, NULL);
6194
6195 /* Skip clients that we launched ourselves (the credentials of
6196 * the socketpair is ours) */
6197 if (pid == getpid())
6198 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006199
Daniel Stone325fc2d2012-05-30 16:31:58 +01006200 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006201}
6202
6203static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006204workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006205 uint32_t key, void *data)
6206{
6207 struct desktop_shell *shell = data;
6208 unsigned int new_index = shell->workspaces.current;
6209
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006210 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006211 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006212 if (new_index != 0)
6213 new_index--;
6214
6215 change_workspace(shell, new_index);
6216}
6217
6218static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006219workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006220 uint32_t key, void *data)
6221{
6222 struct desktop_shell *shell = data;
6223 unsigned int new_index = shell->workspaces.current;
6224
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006225 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006226 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006227 if (new_index < shell->workspaces.num - 1)
6228 new_index++;
6229
6230 change_workspace(shell, new_index);
6231}
6232
6233static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006234workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006235 uint32_t key, void *data)
6236{
6237 struct desktop_shell *shell = data;
6238 unsigned int new_index;
6239
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006240 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006241 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006242 new_index = key - KEY_F1;
6243 if (new_index >= shell->workspaces.num)
6244 new_index = shell->workspaces.num - 1;
6245
6246 change_workspace(shell, new_index);
6247}
6248
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006249static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006250workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6251 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006252{
6253 struct desktop_shell *shell = data;
6254 unsigned int new_index = shell->workspaces.current;
6255
6256 if (shell->locked)
6257 return;
6258
6259 if (new_index != 0)
6260 new_index--;
6261
Derek Foreman8ae2db52015-07-15 13:00:36 -05006262 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006263}
6264
6265static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006266workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6267 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006268{
6269 struct desktop_shell *shell = data;
6270 unsigned int new_index = shell->workspaces.current;
6271
6272 if (shell->locked)
6273 return;
6274
6275 if (new_index < shell->workspaces.num - 1)
6276 new_index++;
6277
Derek Foreman8ae2db52015-07-15 13:00:36 -05006278 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006279}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006280
6281static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006282shell_reposition_view_on_output_destroy(struct weston_view *view)
6283{
6284 struct weston_output *output, *first_output;
6285 struct weston_compositor *ec = view->surface->compositor;
6286 struct shell_surface *shsurf;
6287 float x, y;
6288 int visible;
6289
6290 x = view->geometry.x;
6291 y = view->geometry.y;
6292
6293 /* At this point the destroyed output is not in the list anymore.
6294 * If the view is still visible somewhere, we leave where it is,
6295 * otherwise, move it to the first output. */
6296 visible = 0;
6297 wl_list_for_each(output, &ec->output_list, link) {
6298 if (pixman_region32_contains_point(&output->region,
6299 x, y, NULL)) {
6300 visible = 1;
6301 break;
6302 }
6303 }
6304
6305 if (!visible) {
6306 first_output = container_of(ec->output_list.next,
6307 struct weston_output, link);
6308
6309 x = first_output->x + first_output->width / 4;
6310 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006311
6312 weston_view_set_position(view, x, y);
6313 } else {
6314 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006315 }
6316
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006317
6318 shsurf = get_shell_surface(view->surface);
6319
6320 if (shsurf) {
6321 shsurf->saved_position_valid = false;
6322 shsurf->next_state.maximized = false;
6323 shsurf->next_state.fullscreen = false;
6324 shsurf->state_changed = true;
6325 }
6326}
6327
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006328void
6329shell_for_each_layer(struct desktop_shell *shell,
6330 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006331{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006332 struct workspace **ws;
6333
6334 func(shell, &shell->fullscreen_layer, data);
6335 func(shell, &shell->panel_layer, data);
6336 func(shell, &shell->background_layer, data);
6337 func(shell, &shell->lock_layer, data);
6338 func(shell, &shell->input_panel_layer, data);
6339
6340 wl_array_for_each(ws, &shell->workspaces.array)
6341 func(shell, &(*ws)->layer, data);
6342}
6343
6344static void
6345shell_output_destroy_move_layer(struct desktop_shell *shell,
6346 struct weston_layer *layer,
6347 void *data)
6348{
6349 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006350 struct weston_view *view;
6351
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006352 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006353 if (view->output != output)
6354 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006355
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006356 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006357 }
6358}
6359
6360static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006361handle_output_destroy(struct wl_listener *listener, void *data)
6362{
6363 struct shell_output *output_listener =
6364 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006365 struct weston_output *output = output_listener->output;
6366 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006367
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006368 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006369
Xiong Zhang6b481422013-10-23 13:58:32 +08006370 wl_list_remove(&output_listener->destroy_listener.link);
6371 wl_list_remove(&output_listener->link);
6372 free(output_listener);
6373}
6374
6375static void
6376create_shell_output(struct desktop_shell *shell,
6377 struct weston_output *output)
6378{
6379 struct shell_output *shell_output;
6380
6381 shell_output = zalloc(sizeof *shell_output);
6382 if (shell_output == NULL)
6383 return;
6384
6385 shell_output->output = output;
6386 shell_output->shell = shell;
6387 shell_output->destroy_listener.notify = handle_output_destroy;
6388 wl_signal_add(&output->destroy_signal,
6389 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006390 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006391}
6392
6393static void
6394handle_output_create(struct wl_listener *listener, void *data)
6395{
6396 struct desktop_shell *shell =
6397 container_of(listener, struct desktop_shell, output_create_listener);
6398 struct weston_output *output = (struct weston_output *)data;
6399
6400 create_shell_output(shell, output);
6401}
6402
6403static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006404handle_output_move_layer(struct desktop_shell *shell,
6405 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006406{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006407 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006408 struct weston_view *view;
6409 float x, y;
6410
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006411 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006412 if (view->output != output)
6413 continue;
6414
6415 x = view->geometry.x + output->move_x;
6416 y = view->geometry.y + output->move_y;
6417 weston_view_set_position(view, x, y);
6418 }
6419}
6420
6421static void
6422handle_output_move(struct wl_listener *listener, void *data)
6423{
6424 struct desktop_shell *shell;
6425
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006426 shell = container_of(listener, struct desktop_shell,
6427 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006428
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006429 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006430}
6431
6432static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006433setup_output_destroy_handler(struct weston_compositor *ec,
6434 struct desktop_shell *shell)
6435{
6436 struct weston_output *output;
6437
6438 wl_list_init(&shell->output_list);
6439 wl_list_for_each(output, &ec->output_list, link)
6440 create_shell_output(shell, output);
6441
6442 shell->output_create_listener.notify = handle_output_create;
6443 wl_signal_add(&ec->output_created_signal,
6444 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006445
6446 shell->output_move_listener.notify = handle_output_move;
6447 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006448}
6449
6450static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006451shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006452{
Tiago Vignattibe143262012-04-16 17:31:41 +03006453 struct desktop_shell *shell =
6454 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006455 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006456 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006457
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006458 /* Force state to unlocked so we don't try to fade */
6459 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006460
6461 if (shell->child.client) {
6462 /* disable respawn */
6463 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006464 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006465 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006466
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006467 wl_list_remove(&shell->idle_listener.link);
6468 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006469 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006470
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006471 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006472 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006473
Xiong Zhang6b481422013-10-23 13:58:32 +08006474 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6475 wl_list_remove(&shell_output->destroy_listener.link);
6476 wl_list_remove(&shell_output->link);
6477 free(shell_output);
6478 }
6479
6480 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006481 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006482
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006483 wl_array_for_each(ws, &shell->workspaces.array)
6484 workspace_destroy(*ws);
6485 wl_array_release(&shell->workspaces.array);
6486
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006487 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006488 free(shell);
6489}
6490
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006491static void
6492shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6493{
6494 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006495 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006496
Bob Ham744e6532016-01-12 10:21:48 +00006497 if (shell->allow_zap)
6498 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6499 MODIFIER_CTRL | MODIFIER_ALT,
6500 terminate_binding, ec);
6501
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006502 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006503 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6504 click_to_activate_binding,
6505 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006506 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6507 click_to_activate_binding,
6508 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006509 weston_compositor_add_touch_binding(ec, 0,
6510 touch_to_activate_binding,
6511 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006512 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6513 MODIFIER_SUPER | MODIFIER_ALT,
6514 surface_opacity_binding, NULL);
6515 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6516 MODIFIER_SUPER, zoom_axis_binding,
6517 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006518 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6519 backlight_binding, ec);
6520 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6521 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006522
6523 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006524 if (shell->exposay_modifier)
6525 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6526 exposay_binding, shell);
6527
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006528 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006529 if (!mod)
6530 return;
6531
Daniel Stone325fc2d2012-05-30 16:31:58 +01006532 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6533 zoom_key_binding, NULL);
6534 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6535 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006536 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6537 maximize_binding, NULL);
6538 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6539 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006540 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6541 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006542 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006543 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006544 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006545 weston_compositor_add_button_binding(ec, BTN_LEFT,
6546 mod | MODIFIER_SHIFT,
6547 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006548
6549 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006550 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006551 rotate_binding, NULL);
6552
Daniel Stone325fc2d2012-05-30 16:31:58 +01006553 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6554 shell);
6555 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6556 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006557 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6558 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006559 weston_compositor_add_key_binding(ec, KEY_K, mod,
6560 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006561 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6562 workspace_up_binding, shell);
6563 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6564 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006565 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6566 workspace_move_surface_up_binding,
6567 shell);
6568 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6569 workspace_move_surface_down_binding,
6570 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006571
6572 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6573 if (shell->workspaces.num > 1) {
6574 num_workspace_bindings = shell->workspaces.num;
6575 if (num_workspace_bindings > 6)
6576 num_workspace_bindings = 6;
6577 for (i = 0; i < num_workspace_bindings; i++)
6578 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6579 workspace_f_binding,
6580 shell);
6581 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006582
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006583 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006584}
6585
Jason Ekstrand024177c2014-04-21 19:42:58 -05006586static void
6587handle_seat_created(struct wl_listener *listener, void *data)
6588{
6589 struct weston_seat *seat = data;
6590
6591 create_shell_seat(seat);
6592}
6593
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006594WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006595module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006596 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006597{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006598 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006599 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006600 struct workspace **pws;
6601 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006602 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006603
Peter Huttererf3d62272013-08-08 11:57:05 +10006604 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006605 if (shell == NULL)
6606 return -1;
6607
Kristian Høgsberg75840622011-09-06 13:48:16 -04006608 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006609
6610 shell->destroy_listener.notify = shell_destroy;
6611 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006612 shell->idle_listener.notify = idle_handler;
6613 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6614 shell->wake_listener.notify = wake_handler;
6615 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006616 shell->transform_listener.notify = transform_handler;
6617 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006618
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006619 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006620 ec->shell_interface.create_shell_surface = create_shell_surface;
6621 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006622 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006623 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006624 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006625 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006626 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006627 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006628 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006629 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006630 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006631
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006632 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6633 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006634 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6635 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006636 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006637
6638 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006639 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006640
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006641 if (input_panel_setup(shell) < 0)
6642 return -1;
6643
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006644 shell->text_backend = text_backend_init(ec);
6645 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006646 return -1;
6647
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006648 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006649
Daniel Stonedf8133b2013-11-19 11:37:14 +01006650 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6651 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6652
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006653 for (i = 0; i < shell->workspaces.num; i++) {
6654 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6655 if (pws == NULL)
6656 return -1;
6657
6658 *pws = workspace_create();
6659 if (*pws == NULL)
6660 return -1;
6661 }
6662 activate_workspace(shell, 0);
6663
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006664 weston_layer_init(&shell->minimized_layer, NULL);
6665
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006666 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006667 wl_list_init(&shell->workspaces.animation.link);
6668 shell->workspaces.animation.frame = animate_workspace_change_frame;
6669
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006670 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006671 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006672 return -1;
6673
Rafael Antognollie2a34552013-12-03 15:35:45 -02006674 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6675 shell, bind_xdg_shell) == NULL)
6676 return -1;
6677
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006678 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006679 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006680 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006681 return -1;
6682
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006683 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006684
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006685 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006686
Xiong Zhang6b481422013-10-23 13:58:32 +08006687 setup_output_destroy_handler(ec, shell);
6688
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006689 loop = wl_display_get_event_loop(ec->wl_display);
6690 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006691
Jason Ekstrand024177c2014-04-21 19:42:58 -05006692 wl_list_for_each(seat, &ec->seat_list, link)
6693 handle_seat_created(NULL, seat);
6694 shell->seat_create_listener.notify = handle_seat_created;
6695 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006696
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006697 screenshooter_create(ec);
6698
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006699 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006700
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006701 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006702
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006703 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6704
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006705 return 0;
6706}