blob: 7d5bca9547f9448ca23637f4b2edd89c0a845783 [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
Quentin Glidic5c201952016-03-23 13:50:49 +0100454get_output_work_area(void *data,
Jonny Lambd73c6942014-08-20 15:53:20 +0200455 struct weston_output *output,
456 pixman_rectangle32_t *area)
457{
Quentin Glidic5c201952016-03-23 13:50:49 +0100458 struct desktop_shell *shell = data;
Jonny Lambd73c6942014-08-20 15:53:20 +0200459 int32_t panel_width = 0, panel_height = 0;
460
Derek Foremanf814c5d2015-04-15 12:23:54 -0500461 area->x = output->x;
462 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200463
464 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200465 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800466 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200467 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500468 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800469 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200470 area->width = output->width;
471 area->height = output->height - panel_height;
472 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800473 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500474 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800475 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200476 area->width = output->width - panel_width;
477 area->height = output->height;
478 break;
479 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700480}
481
Derek Foreman45a7c272015-09-11 14:27:40 -0500482static struct shell_surface *
483find_toplevel_surface(struct shell_surface *in_surface)
484{
485 struct shell_surface *surface = in_surface;
486
Ben Hummon445e44c2015-10-06 11:48:14 -0500487 while (surface) {
488 if (surface->type == SHELL_SURFACE_TOPLEVEL)
489 return surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500490 surface = get_shell_surface(surface->parent);
Ben Hummon445e44c2015-10-06 11:48:14 -0500491 }
Derek Foreman45a7c272015-09-11 14:27:40 -0500492
493 /* If no top level surface was found, just use whatever surface was
494 originally provided. */
Ben Hummon445e44c2015-10-06 11:48:14 -0500495 return in_surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500496}
497
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700498static void
499send_configure_for_surface(struct shell_surface *shsurf)
500{
501 int32_t width, height;
502 struct surface_state *state;
503
504 if (shsurf->state_requested)
505 state = &shsurf->requested_state;
506 else if (shsurf->state_changed)
507 state = &shsurf->next_state;
508 else
509 state = &shsurf->state;
510
511 if (state->fullscreen) {
512 width = shsurf->output->width;
513 height = shsurf->output->height;
514 } else if (state->maximized) {
515 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200516 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700517
518 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200519 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700520
Jonny Lambd73c6942014-08-20 15:53:20 +0200521 width = area.width;
522 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900523 } else if (shsurf->resize_edges) {
524 width = shsurf->geometry.width;
525 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700526 } else {
527 width = 0;
528 height = 0;
529 }
530
531 shsurf->client->send_configure(shsurf->surface, width, height);
532}
533
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300534static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400535shell_surface_state_changed(struct shell_surface *shsurf)
536{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700537 if (shell_surface_is_xdg_surface(shsurf))
538 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400539}
540
541static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300542shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300543{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700544 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400545 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700546 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400547
548 if (grab->shsurf->resize_edges) {
549 grab->shsurf->resize_edges = 0;
550 shell_surface_state_changed(grab->shsurf);
551 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700552 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300553
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700554 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300555}
556
557static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700558shell_touch_grab_start(struct shell_touch_grab *grab,
559 const struct weston_touch_grab_interface *interface,
560 struct shell_surface *shsurf,
561 struct weston_touch *touch)
562{
563 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500564 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800565
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200566 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500567 if (pointer)
568 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700569
Rusty Lynch1084da52013-08-15 09:10:08 -0700570 grab->grab.interface = interface;
571 grab->shsurf = shsurf;
572 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
573 wl_signal_add(&shsurf->destroy_signal,
574 &grab->shsurf_destroy_listener);
575
576 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700577 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700578
579 weston_touch_start_grab(touch, &grab->grab);
580 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500581 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500582 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700583}
584
585static void
586shell_touch_grab_end(struct shell_touch_grab *grab)
587{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700588 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700589 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700590 grab->shsurf->grabbed = 0;
591 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700592
593 weston_touch_end_grab(grab->touch);
594}
595
596static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800598 struct weston_output *output);
599
Daniel Stone496ca172012-05-30 16:31:42 +0100600static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300601get_modifier(char *modifier)
602{
603 if (!modifier)
604 return MODIFIER_SUPER;
605
606 if (!strcmp("ctrl", modifier))
607 return MODIFIER_CTRL;
608 else if (!strcmp("alt", modifier))
609 return MODIFIER_ALT;
610 else if (!strcmp("super", modifier))
611 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000612 else if (!strcmp("none", modifier))
613 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300614 else
615 return MODIFIER_SUPER;
616}
617
Juan Zhaoe10d2792012-04-25 19:09:52 +0800618static enum animation_type
619get_animation_type(char *animation)
620{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800621 if (!animation)
622 return ANIMATION_NONE;
623
Juan Zhaoe10d2792012-04-25 19:09:52 +0800624 if (!strcmp("zoom", animation))
625 return ANIMATION_ZOOM;
626 else if (!strcmp("fade", animation))
627 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628 else if (!strcmp("dim-layer", animation))
629 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800630 else
631 return ANIMATION_NONE;
632}
633
Alex Wu4539b082012-03-01 12:57:46 +0800634static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400635shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200636{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400637 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500638 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300639 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000640 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200641
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400642 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400643 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300644 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
645 WESTON_SHELL_CLIENT);
646 if (ret < 0)
647 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400648 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500649 "client", &s, client);
650 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100651 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000652
653 weston_config_section_get_bool(section,
654 "allow-zap", &allow_zap, true);
655 shell->allow_zap = allow_zap;
656
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100657 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400658 "binding-modifier", &s, "super");
659 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200660 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800661
662 weston_config_section_get_string(section,
663 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000664 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800665 free(s);
666
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400667 weston_config_section_get_string(section, "animation", &s, "none");
668 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200669 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200670 weston_config_section_get_string(section, "close-animation", &s, "fade");
671 shell->win_close_animation_type = get_animation_type(s);
672 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700673 weston_config_section_get_string(section,
674 "startup-animation", &s, "fade");
675 shell->startup_animation_type = get_animation_type(s);
676 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700677 if (shell->startup_animation_type == ANIMATION_ZOOM)
678 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100679 weston_config_section_get_string(section, "focus-animation", &s, "none");
680 shell->focus_animation_type = get_animation_type(s);
681 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400682 weston_config_section_get_uint(section, "num-workspaces",
683 &shell->workspaces.num,
684 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200685}
686
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800687struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100688get_default_output(struct weston_compositor *compositor)
689{
690 return container_of(compositor->output_list.next,
691 struct weston_output, link);
692}
693
Pekka Paalanen8274d902014-08-06 19:36:51 +0300694static int
695focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
696{
697 return snprintf(buf, len, "focus highlight effect for output %s",
698 surface->output->name);
699}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100700
701/* no-op func for checking focus surface */
702static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600703focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100704{
705}
706
707static struct focus_surface *
708get_focus_surface(struct weston_surface *surface)
709{
710 if (surface->configure == focus_surface_configure)
711 return surface->configure_private;
712 else
713 return NULL;
714}
715
716static bool
717is_focus_surface (struct weston_surface *es)
718{
719 return (es->configure == focus_surface_configure);
720}
721
722static bool
723is_focus_view (struct weston_view *view)
724{
725 return is_focus_surface (view->surface);
726}
727
728static struct focus_surface *
729create_focus_surface(struct weston_compositor *ec,
730 struct weston_output *output)
731{
732 struct focus_surface *fsurf = NULL;
733 struct weston_surface *surface = NULL;
734
735 fsurf = malloc(sizeof *fsurf);
736 if (!fsurf)
737 return NULL;
738
739 fsurf->surface = weston_surface_create(ec);
740 surface = fsurf->surface;
741 if (surface == NULL) {
742 free(fsurf);
743 return NULL;
744 }
745
746 surface->configure = focus_surface_configure;
747 surface->output = output;
748 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300749 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100750
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800751 fsurf->view = weston_view_create(surface);
752 if (fsurf->view == NULL) {
753 weston_surface_destroy(surface);
754 free(fsurf);
755 return NULL;
756 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100757 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100758
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600759 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600760 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100761 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
762 pixman_region32_fini(&surface->opaque);
763 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
764 output->width, output->height);
765 pixman_region32_fini(&surface->input);
766 pixman_region32_init(&surface->input);
767
768 wl_list_init(&fsurf->workspace_transform.link);
769
770 return fsurf;
771}
772
773static void
774focus_surface_destroy(struct focus_surface *fsurf)
775{
776 weston_surface_destroy(fsurf->surface);
777 free(fsurf);
778}
779
780static void
781focus_animation_done(struct weston_view_animation *animation, void *data)
782{
783 struct workspace *ws = data;
784
785 ws->focus_animation = NULL;
786}
787
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200788static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200789focus_state_destroy(struct focus_state *state)
790{
791 wl_list_remove(&state->seat_destroy_listener.link);
792 wl_list_remove(&state->surface_destroy_listener.link);
793 free(state);
794}
795
796static void
797focus_state_seat_destroy(struct wl_listener *listener, void *data)
798{
799 struct focus_state *state = container_of(listener,
800 struct focus_state,
801 seat_destroy_listener);
802
803 wl_list_remove(&state->link);
804 focus_state_destroy(state);
805}
806
807static void
808focus_state_surface_destroy(struct wl_listener *listener, void *data)
809{
810 struct focus_state *state = container_of(listener,
811 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400812 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400813 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500814 struct weston_surface *main_surface, *next;
815 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200816
Pekka Paalanen01388e22013-04-25 13:57:44 +0300817 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
818
Kristian Høgsberge3778222012-07-31 17:29:30 -0400819 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300820 wl_list_for_each(view,
821 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500822 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400823 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100824 if (is_focus_view(view))
825 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400826
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400828 break;
829 }
830
Pekka Paalanen01388e22013-04-25 13:57:44 +0300831 /* if the focus was a sub-surface, activate its main surface */
832 if (main_surface != state->keyboard_focus)
833 next = main_surface;
834
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400836 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100838 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400839 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100840 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
841 if (state->ws->focus_animation)
842 weston_view_animation_destroy(state->ws->focus_animation);
843
844 state->ws->focus_animation = weston_fade_run(
845 state->ws->fsurf_front->view,
846 state->ws->fsurf_front->view->alpha, 0.0, 300,
847 focus_animation_done, state->ws);
848 }
849
Kristian Høgsberge3778222012-07-31 17:29:30 -0400850 wl_list_remove(&state->link);
851 focus_state_destroy(state);
852 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200853}
854
855static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400856focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200857{
Jonas Ådahl04769742012-06-13 00:01:24 +0200858 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200859
860 state = malloc(sizeof *state);
861 if (state == NULL)
862 return NULL;
863
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100864 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400865 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200866 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400867 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200868
869 state->seat_destroy_listener.notify = focus_state_seat_destroy;
870 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400871 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200872 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400873 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200874
875 return state;
876}
877
Jonas Ådahl8538b222012-08-29 22:13:03 +0200878static struct focus_state *
879ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
880{
881 struct workspace *ws = get_current_workspace(shell);
882 struct focus_state *state;
883
884 wl_list_for_each(state, &ws->focus_list, link)
885 if (state->seat == seat)
886 break;
887
888 if (&state->link == &ws->focus_list)
889 state = focus_state_create(seat, ws);
890
891 return state;
892}
893
Jonas Ådahl04769742012-06-13 00:01:24 +0200894static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800895focus_state_set_focus(struct focus_state *state,
896 struct weston_surface *surface)
897{
898 if (state->keyboard_focus) {
899 wl_list_remove(&state->surface_destroy_listener.link);
900 wl_list_init(&state->surface_destroy_listener.link);
901 }
902
903 state->keyboard_focus = surface;
904 if (surface)
905 wl_signal_add(&surface->destroy_signal,
906 &state->surface_destroy_listener);
907}
908
909static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400910restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200911{
912 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400913 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100914 struct wl_list pending_seat_list;
915 struct weston_seat *seat, *next_seat;
916
917 /* Temporarily steal the list of seats so that we can keep
918 * track of the seats we've already processed */
919 wl_list_init(&pending_seat_list);
920 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
921 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200922
923 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500924 struct weston_keyboard *keyboard =
925 weston_seat_get_keyboard(state->seat);
926
Neil Roberts4237f502014-04-09 16:33:31 +0100927 wl_list_remove(&state->seat->link);
928 wl_list_insert(&shell->compositor->seat_list,
929 &state->seat->link);
930
Derek Foreman1281a362015-07-31 16:55:32 -0500931 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800932 continue;
933
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400934 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200935
Derek Foreman1281a362015-07-31 16:55:32 -0500936 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200937 }
Neil Roberts4237f502014-04-09 16:33:31 +0100938
939 /* For any remaining seats that we don't have a focus state
940 * for we'll reset the keyboard focus to NULL */
941 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500942 struct weston_keyboard *keyboard =
943 weston_seat_get_keyboard(seat);
944
Neil Roberts4237f502014-04-09 16:33:31 +0100945 wl_list_insert(&shell->compositor->seat_list, &seat->link);
946
Derek Foreman1281a362015-07-31 16:55:32 -0500947 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100948 continue;
949
Derek Foreman1281a362015-07-31 16:55:32 -0500950 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100951 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200952}
953
954static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200955replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
956 struct weston_seat *seat)
957{
Derek Foreman1281a362015-07-31 16:55:32 -0500958 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200960
961 wl_list_for_each(state, &ws->focus_list, link) {
962 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500963 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200964 return;
965 }
966 }
967}
968
969static void
970drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
971 struct weston_surface *surface)
972{
973 struct focus_state *state;
974
975 wl_list_for_each(state, &ws->focus_list, link)
976 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800977 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200978}
979
980static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100981animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
982 struct weston_view *from, struct weston_view *to)
983{
984 struct weston_output *output;
985 bool focus_surface_created = false;
986
987 /* FIXME: Only support dim animation using two layers */
988 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
989 return;
990
991 output = get_default_output(shell->compositor);
992 if (ws->fsurf_front == NULL && (from || to)) {
993 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800994 if (ws->fsurf_front == NULL)
995 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800997
998 ws->fsurf_back = create_focus_surface(shell->compositor, output);
999 if (ws->fsurf_back == NULL) {
1000 focus_surface_destroy(ws->fsurf_front);
1001 return;
1002 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001004
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001005 focus_surface_created = true;
1006 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001007 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1008 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001009 }
1010
1011 if (ws->focus_animation) {
1012 weston_view_animation_destroy(ws->focus_animation);
1013 ws->focus_animation = NULL;
1014 }
1015
1016 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001017 weston_layer_entry_insert(&to->layer_link,
1018 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001019 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001020 weston_layer_entry_insert(&ws->layer.view_list,
1021 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001022
1023 if (focus_surface_created) {
1024 ws->focus_animation = weston_fade_run(
1025 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001026 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001027 focus_animation_done, ws);
1028 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001029 weston_layer_entry_insert(&from->layer_link,
1030 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 ws->focus_animation = weston_stable_fade_run(
1032 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001033 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 focus_animation_done, ws);
1035 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001036 weston_layer_entry_insert(&ws->layer.view_list,
1037 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001038 ws->focus_animation = weston_stable_fade_run(
1039 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001040 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001041 focus_animation_done, ws);
1042 }
1043}
1044
1045static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001046workspace_destroy(struct workspace *ws)
1047{
Jonas Ådahl04769742012-06-13 00:01:24 +02001048 struct focus_state *state, *next;
1049
1050 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1051 focus_state_destroy(state);
1052
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001053 if (ws->fsurf_front)
1054 focus_surface_destroy(ws->fsurf_front);
1055 if (ws->fsurf_back)
1056 focus_surface_destroy(ws->fsurf_back);
1057
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001058 free(ws);
1059}
1060
Jonas Ådahl04769742012-06-13 00:01:24 +02001061static void
1062seat_destroyed(struct wl_listener *listener, void *data)
1063{
1064 struct weston_seat *seat = data;
1065 struct focus_state *state, *next;
1066 struct workspace *ws = container_of(listener,
1067 struct workspace,
1068 seat_destroyed_listener);
1069
1070 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1071 if (state->seat == seat)
1072 wl_list_remove(&state->link);
1073}
1074
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001075static struct workspace *
1076workspace_create(void)
1077{
1078 struct workspace *ws = malloc(sizeof *ws);
1079 if (ws == NULL)
1080 return NULL;
1081
1082 weston_layer_init(&ws->layer, NULL);
1083
Jonas Ådahl04769742012-06-13 00:01:24 +02001084 wl_list_init(&ws->focus_list);
1085 wl_list_init(&ws->seat_destroyed_listener.link);
1086 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001087 ws->fsurf_front = NULL;
1088 ws->fsurf_back = NULL;
1089 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001090
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001091 return ws;
1092}
1093
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001094static int
1095workspace_is_empty(struct workspace *ws)
1096{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001097 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098}
1099
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001100static struct workspace *
1101get_workspace(struct desktop_shell *shell, unsigned int index)
1102{
1103 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001104 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001105 pws += index;
1106 return *pws;
1107}
1108
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001109struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001110get_current_workspace(struct desktop_shell *shell)
1111{
1112 return get_workspace(shell, shell->workspaces.current);
1113}
1114
1115static void
1116activate_workspace(struct desktop_shell *shell, unsigned int index)
1117{
1118 struct workspace *ws;
1119
1120 ws = get_workspace(shell, index);
1121 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1122
1123 shell->workspaces.current = index;
1124}
1125
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126static unsigned int
1127get_output_height(struct weston_output *output)
1128{
1129 return abs(output->region.extents.y1 - output->region.extents.y2);
1130}
1131
1132static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001133view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135 struct weston_transform *transform;
1136
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001137 if (is_focus_view(view)) {
1138 struct focus_surface *fsurf = get_focus_surface(view->surface);
1139 transform = &fsurf->workspace_transform;
1140 } else {
1141 struct shell_surface *shsurf = get_shell_surface(view->surface);
1142 transform = &shsurf->workspace_transform;
1143 }
1144
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001147 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001149 weston_matrix_init(&transform->matrix);
1150 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001153}
1154
1155static void
1156workspace_translate_out(struct workspace *ws, double fraction)
1157{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159 unsigned int height;
1160 double d;
1161
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001162 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001164 d = height * fraction;
1165
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001166 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001167 }
1168}
1169
1170static void
1171workspace_translate_in(struct workspace *ws, double fraction)
1172{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174 unsigned int height;
1175 double d;
1176
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001177 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001179
1180 if (fraction > 0)
1181 d = -(height - height * fraction);
1182 else
1183 d = height + height * fraction;
1184
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001185 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001186 }
1187}
1188
1189static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001190reverse_workspace_change_animation(struct desktop_shell *shell,
1191 unsigned int index,
1192 struct workspace *from,
1193 struct workspace *to)
1194{
1195 shell->workspaces.current = index;
1196
1197 shell->workspaces.anim_to = to;
1198 shell->workspaces.anim_from = from;
1199 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1200 shell->workspaces.anim_timestamp = 0;
1201
Scott Moreau4272e632012-08-13 09:58:41 -06001202 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001203}
1204
1205static void
1206workspace_deactivate_transforms(struct workspace *ws)
1207{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001209 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001210
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001211 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001212 if (is_focus_view(view)) {
1213 struct focus_surface *fsurf = get_focus_surface(view->surface);
1214 transform = &fsurf->workspace_transform;
1215 } else {
1216 struct shell_surface *shsurf = get_shell_surface(view->surface);
1217 transform = &shsurf->workspace_transform;
1218 }
1219
1220 if (!wl_list_empty(&transform->link)) {
1221 wl_list_remove(&transform->link);
1222 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001223 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001225 }
1226}
1227
1228static void
1229finish_workspace_change_animation(struct desktop_shell *shell,
1230 struct workspace *from,
1231 struct workspace *to)
1232{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001233 struct weston_view *view;
1234
Scott Moreau4272e632012-08-13 09:58:41 -06001235 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001236
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001237 /* Views that extend past the bottom of the output are still
1238 * visible after the workspace animation ends but before its layer
1239 * is hidden. In that case, we need to damage below those views so
1240 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001241 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001242 weston_view_damage_below(view);
1243
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001244 wl_list_remove(&shell->workspaces.animation.link);
1245 workspace_deactivate_transforms(from);
1246 workspace_deactivate_transforms(to);
1247 shell->workspaces.anim_to = NULL;
1248
1249 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1250}
1251
1252static void
1253animate_workspace_change_frame(struct weston_animation *animation,
1254 struct weston_output *output, uint32_t msecs)
1255{
1256 struct desktop_shell *shell =
1257 container_of(animation, struct desktop_shell,
1258 workspaces.animation);
1259 struct workspace *from = shell->workspaces.anim_from;
1260 struct workspace *to = shell->workspaces.anim_to;
1261 uint32_t t;
1262 double x, y;
1263
1264 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1265 finish_workspace_change_animation(shell, from, to);
1266 return;
1267 }
1268
1269 if (shell->workspaces.anim_timestamp == 0) {
1270 if (shell->workspaces.anim_current == 0.0)
1271 shell->workspaces.anim_timestamp = msecs;
1272 else
1273 shell->workspaces.anim_timestamp =
1274 msecs -
1275 /* Invers of movement function 'y' below. */
1276 (asin(1.0 - shell->workspaces.anim_current) *
1277 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1278 M_2_PI);
1279 }
1280
1281 t = msecs - shell->workspaces.anim_timestamp;
1282
1283 /*
1284 * x = [0, π/2]
1285 * y(x) = sin(x)
1286 */
1287 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1288 y = sin(x);
1289
1290 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001291 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001292
1293 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1294 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1295 shell->workspaces.anim_current = y;
1296
Scott Moreau4272e632012-08-13 09:58:41 -06001297 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001298 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001299 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001300 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001301}
1302
1303static void
1304animate_workspace_change(struct desktop_shell *shell,
1305 unsigned int index,
1306 struct workspace *from,
1307 struct workspace *to)
1308{
1309 struct weston_output *output;
1310
1311 int dir;
1312
1313 if (index > shell->workspaces.current)
1314 dir = -1;
1315 else
1316 dir = 1;
1317
1318 shell->workspaces.current = index;
1319
1320 shell->workspaces.anim_dir = dir;
1321 shell->workspaces.anim_from = from;
1322 shell->workspaces.anim_to = to;
1323 shell->workspaces.anim_current = 0.0;
1324 shell->workspaces.anim_timestamp = 0;
1325
1326 output = container_of(shell->compositor->output_list.next,
1327 struct weston_output, link);
1328 wl_list_insert(&output->animation_list,
1329 &shell->workspaces.animation.link);
1330
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001331 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001332
1333 workspace_translate_in(to, 0);
1334
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001335 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001336
Scott Moreau4272e632012-08-13 09:58:41 -06001337 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001338}
1339
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001340static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001341update_workspace(struct desktop_shell *shell, unsigned int index,
1342 struct workspace *from, struct workspace *to)
1343{
1344 shell->workspaces.current = index;
1345 wl_list_insert(&from->layer.link, &to->layer.link);
1346 wl_list_remove(&from->layer.link);
1347}
1348
1349static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001350change_workspace(struct desktop_shell *shell, unsigned int index)
1351{
1352 struct workspace *from;
1353 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001354 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001355
1356 if (index == shell->workspaces.current)
1357 return;
1358
1359 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001360 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001361 return;
1362
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001363 from = get_current_workspace(shell);
1364 to = get_workspace(shell, index);
1365
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001366 if (shell->workspaces.anim_from == to &&
1367 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001368 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001369 reverse_workspace_change_animation(shell, index, from, to);
1370 return;
1371 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001372
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001373 if (shell->workspaces.anim_to != NULL)
1374 finish_workspace_change_animation(shell,
1375 shell->workspaces.anim_from,
1376 shell->workspaces.anim_to);
1377
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001379
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001380 if (shell->focus_animation_type != ANIMATION_NONE) {
1381 wl_list_for_each(state, &from->focus_list, link)
1382 if (state->keyboard_focus)
1383 animate_focus_change(shell, from,
1384 get_default_view(state->keyboard_focus), NULL);
1385
1386 wl_list_for_each(state, &to->focus_list, link)
1387 if (state->keyboard_focus)
1388 animate_focus_change(shell, to,
1389 NULL, get_default_view(state->keyboard_focus));
1390 }
1391
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001392 if (workspace_is_empty(to) && workspace_is_empty(from))
1393 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001394 else
1395 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001396}
1397
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001398static bool
1399workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1400{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001401 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001402 struct wl_list *e;
1403
1404 if (wl_list_empty(list))
1405 return false;
1406
1407 e = list->next;
1408
1409 if (e->next != list)
1410 return false;
1411
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001412 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001413}
1414
1415static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001416surface_keyboard_focus_lost(struct weston_surface *surface)
1417{
1418 struct weston_compositor *compositor = surface->compositor;
1419 struct weston_seat *seat;
1420 struct weston_surface *focus;
1421
1422 wl_list_for_each(seat, &compositor->seat_list, link) {
1423 struct weston_keyboard *keyboard =
1424 weston_seat_get_keyboard(seat);
1425
1426 if (!keyboard)
1427 continue;
1428
1429 focus = weston_surface_get_main_surface(keyboard->focus);
1430 if (focus == surface)
1431 weston_keyboard_set_focus(keyboard, NULL);
1432 }
1433}
1434
1435static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001436take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001437 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001438 unsigned int index)
1439{
Derek Foreman1281a362015-07-31 16:55:32 -05001440 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001441 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001443 struct shell_surface *shsurf;
1444 struct workspace *from;
1445 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001446 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001447
Derek Foreman1281a362015-07-31 16:55:32 -05001448 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 view = get_default_view(surface);
1450 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001451 index == shell->workspaces.current ||
1452 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001453 return;
1454
1455 from = get_current_workspace(shell);
1456 to = get_workspace(shell, index);
1457
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001458 weston_layer_entry_remove(&view->layer_link);
1459 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001460
Philip Withnall659163d2013-11-25 18:01:36 +00001461 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001462 if (shsurf != NULL)
1463 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001464
Jonas Ådahle9d22502012-08-29 22:13:01 +02001465 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001466 drop_focus_state(shell, from, surface);
1467
1468 if (shell->workspaces.anim_from == to &&
1469 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001470 wl_list_remove(&to->layer.link);
1471 wl_list_insert(from->layer.link.prev, &to->layer.link);
1472
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001473 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001474
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001475 return;
1476 }
1477
1478 if (shell->workspaces.anim_to != NULL)
1479 finish_workspace_change_animation(shell,
1480 shell->workspaces.anim_from,
1481 shell->workspaces.anim_to);
1482
1483 if (workspace_is_empty(from) &&
1484 workspace_has_only(to, surface))
1485 update_workspace(shell, index, from, to);
1486 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001487 if (shsurf != NULL &&
1488 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001489 wl_list_insert(&shell->workspaces.anim_sticky_list,
1490 &shsurf->workspace_transform.link);
1491
1492 animate_workspace_change(shell, index, from, to);
1493 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001494
Jonas Ådahl8538b222012-08-29 22:13:03 +02001495 state = ensure_focus_state(shell, seat);
1496 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001497 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001498}
1499
1500static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001501touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001502 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001503{
1504}
1505
1506static void
1507touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1508{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001509 struct weston_touch_move_grab *move =
1510 (struct weston_touch_move_grab *) container_of(
1511 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001512
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001513 if (touch_id == 0)
1514 move->active = 0;
1515
Jonas Ådahl9484b692013-12-02 22:05:03 +01001516 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001517 shell_touch_grab_end(&move->base);
1518 free(move);
1519 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001520}
1521
1522static void
1523touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001524 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001525{
1526 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1527 struct shell_surface *shsurf = move->base.shsurf;
1528 struct weston_surface *es;
1529 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1530 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1531
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001532 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001533 return;
1534
1535 es = shsurf->surface;
1536
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001537 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001538
1539 weston_compositor_schedule_repaint(es->compositor);
1540}
1541
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001542static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001543touch_move_grab_frame(struct weston_touch_grab *grab)
1544{
1545}
1546
1547static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001548touch_move_grab_cancel(struct weston_touch_grab *grab)
1549{
1550 struct weston_touch_move_grab *move =
1551 (struct weston_touch_move_grab *) container_of(
1552 grab, struct shell_touch_grab, grab);
1553
1554 shell_touch_grab_end(&move->base);
1555 free(move);
1556}
1557
Rusty Lynch1084da52013-08-15 09:10:08 -07001558static const struct weston_touch_grab_interface touch_move_grab_interface = {
1559 touch_move_grab_down,
1560 touch_move_grab_up,
1561 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001562 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001563 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001564};
1565
1566static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001567surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001568{
1569 struct weston_touch_move_grab *move;
1570
1571 if (!shsurf)
1572 return -1;
1573
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001574 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001575 return 0;
1576
1577 move = malloc(sizeof *move);
1578 if (!move)
1579 return -1;
1580
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001581 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001583 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001584 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001585 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001586
1587 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001588 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001589
1590 return 0;
1591}
1592
1593static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001594noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001595{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001596}
1597
1598static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001599noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001600 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001601{
1602}
1603
1604static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001605noop_grab_axis_source(struct weston_pointer_grab *grab,
1606 uint32_t source)
1607{
1608}
1609
1610static void
1611noop_grab_frame(struct weston_pointer_grab *grab)
1612{
1613}
1614
1615static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001616constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1617{
1618 struct shell_surface *shsurf = move->base.shsurf;
1619 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001620 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001621 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001622 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001623
1624 x = wl_fixed_to_int(pointer->x + move->dx);
1625 y = wl_fixed_to_int(pointer->y + move->dy);
1626
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001627 if (shsurf->shell->panel_position ==
1628 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001629 get_output_work_area(shsurf->shell,
1630 shsurf->surface->output,
1631 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001632
Derek Foreman6feb0f92015-04-15 12:23:56 -05001633 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1634 if (bottom - safety < area.y)
1635 y = area.y + safety - shsurf->geometry.height
1636 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001637
1638 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001639 y + shsurf->geometry.y < area.y)
1640 y = area.y - shsurf->geometry.y;
1641
Jonny Lambd73c6942014-08-20 15:53:20 +02001642 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001643
1644 *cx = x;
1645 *cy = y;
1646}
1647
1648static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001649move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001650 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001651{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001652 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001653 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001654 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001655 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001656
Jonas Ådahld2510102014-10-05 21:39:14 +02001657 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001658 if (!shsurf)
1659 return;
1660
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001661 constrain_position(move, &cx, &cy);
1662
1663 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001664
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001666}
1667
1668static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001669move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001670 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001671{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001672 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1673 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001674 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001675 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001676
Daniel Stone4dbadb12012-05-30 16:31:51 +01001677 if (pointer->button_count == 0 &&
1678 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001679 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001680 free(grab);
1681 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001682}
1683
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001684static void
1685move_grab_cancel(struct weston_pointer_grab *grab)
1686{
1687 struct shell_grab *shell_grab =
1688 container_of(grab, struct shell_grab, grab);
1689
1690 shell_grab_end(shell_grab);
1691 free(grab);
1692}
1693
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001694static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001695 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001696 move_grab_motion,
1697 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001698 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001699 noop_grab_axis_source,
1700 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001701 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001702};
1703
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001705surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001706 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707{
1708 struct weston_move_grab *move;
1709
1710 if (!shsurf)
1711 return -1;
1712
Derek Foreman45a7c272015-09-11 14:27:40 -05001713 shsurf = find_toplevel_surface(shsurf);
1714
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001715 if (shsurf->grabbed ||
1716 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717 return 0;
1718
1719 move = malloc(sizeof *move);
1720 if (!move)
1721 return -1;
1722
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001724 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001726 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001727 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728
1729 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001730 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731
1732 return 0;
1733}
1734
1735static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001736common_surface_move(struct wl_resource *resource,
1737 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001739 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001740 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1741 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001742 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001743 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744
Derek Foreman1281a362015-07-31 16:55:32 -05001745 if (pointer &&
1746 pointer->focus &&
1747 pointer->button_count > 0 &&
1748 pointer->grab_serial == serial) {
1749 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001750 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001751 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001752 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001753 } else if (touch &&
1754 touch->focus &&
1755 touch->grab_serial == serial) {
1756 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001757 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001758 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001759 wl_resource_post_no_memory(resource);
1760 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001761}
1762
Rafael Antognollie2a34552013-12-03 15:35:45 -02001763static void
1764shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1765 struct wl_resource *seat_resource, uint32_t serial)
1766{
1767 common_surface_move(resource, seat_resource, serial);
1768}
1769
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770struct weston_resize_grab {
1771 struct shell_grab base;
1772 uint32_t edges;
1773 int32_t width, height;
1774};
1775
1776static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001777resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001778 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001779{
1780 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001781 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001782 struct shell_surface *shsurf = resize->base.shsurf;
1783 int32_t width, height;
1784 wl_fixed_t from_x, from_y;
1785 wl_fixed_t to_x, to_y;
1786
Jonas Ådahld2510102014-10-05 21:39:14 +02001787 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001788
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001789 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001790 return;
1791
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 weston_view_from_global_fixed(shsurf->view,
1793 pointer->grab_x, pointer->grab_y,
1794 &from_x, &from_y);
1795 weston_view_from_global_fixed(shsurf->view,
1796 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001797
1798 width = resize->width;
1799 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1800 width += wl_fixed_to_int(from_x - to_x);
1801 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1802 width += wl_fixed_to_int(to_x - from_x);
1803 }
1804
1805 height = resize->height;
1806 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1807 height += wl_fixed_to_int(from_y - to_y);
1808 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1809 height += wl_fixed_to_int(to_y - from_y);
1810 }
1811
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001812 if (width < 1)
1813 width = 1;
1814 if (height < 1)
1815 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001816 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001817}
1818
1819static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001820send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821{
1822 struct shell_surface *shsurf = get_shell_surface(surface);
1823
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001824 assert(shsurf);
1825
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001826 if (shsurf->resource)
1827 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001828 shsurf->resize_edges,
1829 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001830}
1831
1832static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001833 send_configure,
1834 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001835};
1836
1837static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001838resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001839 uint32_t time, uint32_t button, uint32_t state_w)
1840{
1841 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001842 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001843 enum wl_pointer_button_state state = state_w;
1844
1845 if (pointer->button_count == 0 &&
1846 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1847 shell_grab_end(&resize->base);
1848 free(grab);
1849 }
1850}
1851
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001852static void
1853resize_grab_cancel(struct weston_pointer_grab *grab)
1854{
1855 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1856
1857 shell_grab_end(&resize->base);
1858 free(grab);
1859}
1860
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001861static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001862 noop_grab_focus,
1863 resize_grab_motion,
1864 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001865 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001866 noop_grab_axis_source,
1867 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001868 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001869};
1870
Giulio Camuffob8366642013-04-25 13:57:46 +03001871/*
1872 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001873 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001874static void
1875surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1876 int32_t *y, int32_t *w, int32_t *h) {
1877 pixman_region32_t region;
1878 pixman_box32_t *box;
1879 struct weston_subsurface *subsurface;
1880
1881 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001882 surface->width,
1883 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001884
1885 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1886 pixman_region32_union_rect(&region, &region,
1887 subsurface->position.x,
1888 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889 subsurface->surface->width,
1890 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001891 }
1892
1893 box = pixman_region32_extents(&region);
1894 if (x)
1895 *x = box->x1;
1896 if (y)
1897 *y = box->y1;
1898 if (w)
1899 *w = box->x2 - box->x1;
1900 if (h)
1901 *h = box->y2 - box->y1;
1902
1903 pixman_region32_fini(&region);
1904}
1905
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001906static int
1907surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001908 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001909{
1910 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001911 const unsigned resize_topbottom =
1912 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1913 const unsigned resize_leftright =
1914 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1915 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001916
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001917 if (shsurf->grabbed ||
1918 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001919 return 0;
1920
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001921 /* Check for invalid edge combinations. */
1922 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1923 (edges & resize_topbottom) == resize_topbottom ||
1924 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001925 return 0;
1926
1927 resize = malloc(sizeof *resize);
1928 if (!resize)
1929 return -1;
1930
1931 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001932
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001933 resize->width = shsurf->geometry.width;
1934 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001935
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001936 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001937 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001938 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001939 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001940
1941 return 0;
1942}
1943
1944static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001945common_surface_resize(struct wl_resource *resource,
1946 struct wl_resource *seat_resource, uint32_t serial,
1947 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001948{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001949 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001950 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001951 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001952 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001953
Derek Foreman1281a362015-07-31 16:55:32 -05001954 if (!pointer ||
1955 pointer->button_count == 0 ||
1956 pointer->grab_serial != serial ||
1957 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001958 return;
1959
Derek Foreman1281a362015-07-31 16:55:32 -05001960 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001961 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001962 return;
1963
Derek Foreman1281a362015-07-31 16:55:32 -05001964 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001965 wl_resource_post_no_memory(resource);
1966}
1967
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001968static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001969shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1970 struct wl_resource *seat_resource, uint32_t serial,
1971 uint32_t edges)
1972{
1973 common_surface_resize(resource, seat_resource, serial, edges);
1974}
1975
1976static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001977busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001978{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001979 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001980 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001981 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001982 wl_fixed_t sx, sy;
1983
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 view = weston_compositor_pick_view(pointer->seat->compositor,
1985 pointer->x, pointer->y,
1986 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001987
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001988 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1989 shell_grab_end(grab);
1990 free(grab);
1991 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001992}
1993
1994static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001995busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001996 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001997{
Jonas Ådahld2510102014-10-05 21:39:14 +02001998 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001999}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002000
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002001static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002002busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002003 uint32_t time, uint32_t button, uint32_t state)
2004{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002005 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002006 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002007 struct weston_pointer *pointer = grab->grab.pointer;
2008 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002009
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002010 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002011 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002012 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002013 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002014 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002015 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002016 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002017}
2018
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002019static void
2020busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2021{
2022 struct shell_grab *grab = (struct shell_grab *) base;
2023
2024 shell_grab_end(grab);
2025 free(grab);
2026}
2027
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002028static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002029 busy_cursor_grab_focus,
2030 busy_cursor_grab_motion,
2031 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002032 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002033 noop_grab_axis_source,
2034 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002035 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036};
2037
2038static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002039set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040{
2041 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002042
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002043 if (pointer->grab->interface == &busy_cursor_grab_interface)
2044 return;
2045
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002046 grab = malloc(sizeof *grab);
2047 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002048 return;
2049
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002050 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002051 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002052 /* Mark the shsurf as ungrabbed so that button binding is able
2053 * to move it. */
2054 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002055}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002056
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002057static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002058end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002059{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002060 struct shell_grab *grab;
2061 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002062
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002063 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002064 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2065
2066 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002067 continue;
2068
Derek Foreman1281a362015-07-31 16:55:32 -05002069 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002070 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002071 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002072 wl_resource_get_client(grab->shsurf->resource) == client) {
2073 shell_grab_end(grab);
2074 free(grab);
2075 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002076 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002077}
2078
Scott Moreau9521d5e2012-04-19 13:06:17 -06002079static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002080handle_shell_client_destroy(struct wl_listener *listener, void *data);
2081
2082static int
2083xdg_ping_timeout_handler(void *data)
2084{
2085 struct shell_client *sc = data;
2086 struct weston_seat *seat;
2087 struct shell_surface *shsurf;
2088
2089 /* Client is not responding */
2090 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002091 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002092 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2093
2094 if (!pointer ||
2095 !pointer->focus ||
2096 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002097 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002098
Derek Foreman1281a362015-07-31 16:55:32 -05002099 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002100 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002101 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002102 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002103 }
2104
2105 return 1;
2106}
2107
2108static void
2109handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2110{
2111 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002112 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002113 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002114 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002115
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002116 if (sc->unresponsive) {
2117 xdg_ping_timeout_handler(sc);
2118 return;
2119 }
2120
2121 sc->ping_serial = serial;
2122 loop = wl_display_get_event_loop(compositor->wl_display);
2123 if (sc->ping_timer == NULL)
2124 sc->ping_timer =
2125 wl_event_loop_add_timer(loop,
2126 xdg_ping_timeout_handler, sc);
2127 if (sc->ping_timer == NULL)
2128 return;
2129
2130 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2131
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002132 if (shell_surface_is_xdg_surface(shsurf) ||
2133 shell_surface_is_xdg_popup(shsurf))
2134 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002135 else if (shell_surface_is_wl_shell_surface(shsurf)
2136 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002137 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002138}
2139
Scott Moreauff1db4a2012-04-17 19:06:18 -06002140static void
2141ping_handler(struct weston_surface *surface, uint32_t serial)
2142{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002143 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002144
2145 if (!shsurf)
2146 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002147 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002148 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002149 if (shsurf->surface == shsurf->shell->grab_surface)
2150 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002151 if (!shsurf->owner)
2152 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002153
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002154 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002155}
2156
2157static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002158handle_pointer_focus(struct wl_listener *listener, void *data)
2159{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002160 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002162 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002163 uint32_t serial;
2164
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002166 return;
2167
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002169 serial = wl_display_next_serial(compositor->wl_display);
2170 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002171}
2172
2173static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002174shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2175{
2176 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002177 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002178}
2179
2180static void
2181shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2182{
2183 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002184 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002185}
2186
2187static void
2188handle_keyboard_focus(struct wl_listener *listener, void *data)
2189{
2190 struct weston_keyboard *keyboard = data;
2191 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2192
2193 if (seat->focused_surface) {
2194 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2195 if (shsurf)
2196 shell_surface_lose_keyboard_focus(shsurf);
2197 }
2198
2199 seat->focused_surface = keyboard->focus;
2200
2201 if (seat->focused_surface) {
2202 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2203 if (shsurf)
2204 shell_surface_gain_keyboard_focus(shsurf);
2205 }
2206}
2207
2208static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002209shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002210{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002211 if (sc->ping_serial != serial)
2212 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002213
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002214 sc->unresponsive = 0;
2215 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002216
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002217 if (sc->ping_timer) {
2218 wl_event_source_remove(sc->ping_timer);
2219 sc->ping_timer = NULL;
2220 }
2221
2222}
2223
2224static void
2225shell_surface_pong(struct wl_client *client,
2226 struct wl_resource *resource, uint32_t serial)
2227{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002228 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2229 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002230
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002231 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002232}
2233
2234static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002235set_title(struct shell_surface *shsurf, const char *title)
2236{
2237 free(shsurf->title);
2238 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002239 shsurf->surface->timeline.force_refresh = 1;
2240}
2241
2242static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002243set_pid(struct shell_surface *shsurf, pid_t pid)
2244{
2245 /* We have no use for it */
2246}
2247
2248static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002249set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2250{
2251 shsurf->type = t;
2252 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002253}
2254
2255static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002256set_window_geometry(struct shell_surface *shsurf,
2257 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002258{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002259 shsurf->next_geometry.x = x;
2260 shsurf->next_geometry.y = y;
2261 shsurf->next_geometry.width = width;
2262 shsurf->next_geometry.height = height;
2263 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002264}
2265
2266static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002267shell_surface_set_title(struct wl_client *client,
2268 struct wl_resource *resource, const char *title)
2269{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002270 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002271
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002272 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002273}
2274
2275static void
2276shell_surface_set_class(struct wl_client *client,
2277 struct wl_resource *resource, const char *class)
2278{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002279 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002280
2281 free(shsurf->class);
2282 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002283 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002284}
2285
Alex Wu4539b082012-03-01 12:57:46 +08002286static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002287restore_output_mode(struct weston_output *output)
2288{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002289 if (output->original_mode)
2290 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002291}
2292
2293static void
2294restore_all_output_modes(struct weston_compositor *compositor)
2295{
2296 struct weston_output *output;
2297
2298 wl_list_for_each(output, &compositor->output_list, link)
2299 restore_output_mode(output);
2300}
2301
Philip Withnall07926d92013-11-25 18:01:40 +00002302/* The surface will be inserted into the list immediately after the link
2303 * returned by this function (i.e. will be stacked immediately above the
2304 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002305static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002306shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2307{
2308 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002309 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002310
2311 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002312 case SHELL_SURFACE_XWAYLAND:
2313 return &shsurf->shell->fullscreen_layer.view_list;
2314
2315 case SHELL_SURFACE_NONE:
2316 return NULL;
2317
Kristian Høgsbergead52422014-01-01 13:51:52 -08002318 case SHELL_SURFACE_POPUP:
2319 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002320 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002321 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002322 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002323 /* Move the surface to its parent layer so
2324 * that surfaces which are transient for
2325 * fullscreen surfaces don't get hidden by the
2326 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002327
2328 /* TODO: Handle a parent with multiple views */
2329 parent = get_default_view(shsurf->parent);
2330 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002331 return container_of(parent->layer_link.link.prev,
2332 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002333 }
Philip Withnall07926d92013-11-25 18:01:40 +00002334
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002335 /* Move the surface to a normal workspace layer so that surfaces
2336 * which were previously fullscreen or transient are no longer
2337 * rendered on top. */
2338 ws = get_current_workspace(shsurf->shell);
2339 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002340 }
2341
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002342 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002343}
2344
Philip Withnall648a4dd2013-11-25 18:01:44 +00002345static void
2346shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2347{
2348 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002349 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002350
2351 /* Move the child layers to the same workspace as shsurf. They will be
2352 * stacked above shsurf. */
2353 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002354 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002355 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002356 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002357 prev = container_of(shsurf->view->layer_link.link.prev,
2358 struct weston_layer_entry, link);
2359 weston_layer_entry_remove(&child->view->layer_link);
2360 weston_layer_entry_insert(prev,
2361 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002362 weston_view_geometry_dirty(child->view);
2363 weston_surface_damage(child->surface);
2364
2365 /* Recurse. We don’t expect this to recurse very far (if
2366 * at all) because that would imply we have transient
2367 * (or popup) children of transient surfaces, which
2368 * would be unusual. */
2369 shell_surface_update_child_surface_layers(child);
2370 }
2371 }
2372}
2373
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002374/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002375 * geometry to ensure the changes are redrawn.
2376 *
2377 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2378 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002379static void
2380shell_surface_update_layer(struct shell_surface *shsurf)
2381{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002382 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002383
2384 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2385
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002386 if (new_layer_link == NULL)
2387 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002388 if (new_layer_link == &shsurf->view->layer_link)
2389 return;
2390
2391 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002392 weston_layer_entry_remove(&shsurf->view->layer_link);
2393 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002394 weston_view_geometry_dirty(shsurf->view);
2395 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002396
2397 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002398}
2399
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002400static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002401shell_surface_set_parent(struct shell_surface *shsurf,
2402 struct weston_surface *parent)
2403{
2404 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002405
2406 wl_list_remove(&shsurf->children_link);
2407 wl_list_init(&shsurf->children_link);
2408
2409 /* Insert into the parent surface’s child list. */
2410 if (parent != NULL) {
2411 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2412 if (parent_shsurf != NULL)
2413 wl_list_insert(&parent_shsurf->children_list,
2414 &shsurf->children_link);
2415 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002416}
2417
2418static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002419shell_surface_set_output(struct shell_surface *shsurf,
2420 struct weston_output *output)
2421{
2422 struct weston_surface *es = shsurf->surface;
2423
2424 /* get the default output, if the client set it as NULL
2425 check whether the ouput is available */
2426 if (output)
2427 shsurf->output = output;
2428 else if (es->output)
2429 shsurf->output = es->output;
2430 else
2431 shsurf->output = get_default_output(es->compositor);
2432}
2433
2434static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002435surface_clear_next_states(struct shell_surface *shsurf)
2436{
2437 shsurf->next_state.maximized = false;
2438 shsurf->next_state.fullscreen = false;
2439
2440 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2441 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2442 shsurf->state_changed = true;
2443}
2444
2445static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002446set_toplevel(struct shell_surface *shsurf)
2447{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002448 shell_surface_set_parent(shsurf, NULL);
2449 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002450 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002451
2452 /* The layer_link is updated in set_surface_type(),
2453 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002454}
2455
2456static void
2457shell_surface_set_toplevel(struct wl_client *client,
2458 struct wl_resource *resource)
2459{
2460 struct shell_surface *surface = wl_resource_get_user_data(resource);
2461
2462 set_toplevel(surface);
2463}
2464
2465static void
2466set_transient(struct shell_surface *shsurf,
2467 struct weston_surface *parent, int x, int y, uint32_t flags)
2468{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002469 assert(parent != NULL);
2470
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002471 shell_surface_set_parent(shsurf, parent);
2472
2473 surface_clear_next_states(shsurf);
2474
Philip Withnallbecb77e2013-11-25 18:01:30 +00002475 shsurf->transient.x = x;
2476 shsurf->transient.y = y;
2477 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002478
Rafael Antognollied207b42013-12-03 15:35:43 -02002479 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002480 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002481 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002482
2483 /* The layer_link is updated in set_surface_type(),
2484 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485}
2486
2487static void
2488shell_surface_set_transient(struct wl_client *client,
2489 struct wl_resource *resource,
2490 struct wl_resource *parent_resource,
2491 int x, int y, uint32_t flags)
2492{
2493 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2494 struct weston_surface *parent =
2495 wl_resource_get_user_data(parent_resource);
2496
2497 set_transient(shsurf, parent, x, y, flags);
2498}
2499
2500static void
2501set_fullscreen(struct shell_surface *shsurf,
2502 uint32_t method,
2503 uint32_t framerate,
2504 struct weston_output *output)
2505{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002506 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002507 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002508
2509 shsurf->fullscreen_output = shsurf->output;
2510 shsurf->fullscreen.type = method;
2511 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002512
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002513 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002514}
2515
2516static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002517weston_view_set_initial_position(struct weston_view *view,
2518 struct desktop_shell *shell);
2519
2520static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002521unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002522{
Philip Withnallf85fe842013-11-25 18:01:37 +00002523 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002524 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2525 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002526 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002527 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002528
Alex Wu4539b082012-03-01 12:57:46 +08002529 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2530 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002531
Alex Wu4539b082012-03-01 12:57:46 +08002532 wl_list_remove(&shsurf->fullscreen.transform.link);
2533 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002534
Jason Ekstranda7af7042013-10-12 22:38:11 -05002535 if (shsurf->fullscreen.black_view)
2536 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2537 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002538
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002539 if (shsurf->saved_position_valid)
2540 weston_view_set_position(shsurf->view,
2541 shsurf->saved_x, shsurf->saved_y);
2542 else
2543 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2544
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002545 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002547 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002548 shsurf->saved_rotation_valid = false;
2549 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002550
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002551 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002552}
2553
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002554static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002555shell_surface_set_fullscreen(struct wl_client *client,
2556 struct wl_resource *resource,
2557 uint32_t method,
2558 uint32_t framerate,
2559 struct wl_resource *output_resource)
2560{
2561 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2562 struct weston_output *output;
2563
Emmanuel Gil Peyrot28834b62016-05-10 03:22:43 +02002564 if (shsurf->fullscreen_output == shsurf->output &&
2565 shsurf->fullscreen.type == method &&
2566 shsurf->fullscreen.framerate == framerate) {
2567 send_configure_for_surface(shsurf);
2568 return;
2569 }
2570
Philip Withnallbecb77e2013-11-25 18:01:30 +00002571 if (output_resource)
2572 output = wl_resource_get_user_data(output_resource);
2573 else
2574 output = NULL;
2575
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002576 shell_surface_set_parent(shsurf, NULL);
2577
Rafael Antognolli03b16592013-12-03 15:35:42 -02002578 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002579 shsurf->next_state.fullscreen = true;
2580 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002581 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582}
2583
2584static void
2585set_popup(struct shell_surface *shsurf,
2586 struct weston_surface *parent,
2587 struct weston_seat *seat,
2588 uint32_t serial,
2589 int32_t x,
2590 int32_t y)
2591{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002592 assert(parent != NULL);
2593
Philip Withnallbecb77e2013-11-25 18:01:30 +00002594 shsurf->popup.shseat = get_shell_seat(seat);
2595 shsurf->popup.serial = serial;
2596 shsurf->popup.x = x;
2597 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002598
Pekka Paalanenb5026542014-11-12 15:09:24 +02002599 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002600}
2601
2602static void
2603shell_surface_set_popup(struct wl_client *client,
2604 struct wl_resource *resource,
2605 struct wl_resource *seat_resource,
2606 uint32_t serial,
2607 struct wl_resource *parent_resource,
2608 int32_t x, int32_t y, uint32_t flags)
2609{
2610 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002611 struct weston_surface *parent =
2612 wl_resource_get_user_data(parent_resource);
2613
2614 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002615
Rafael Antognolli03b16592013-12-03 15:35:42 -02002616 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002617 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002618 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002619 wl_resource_get_user_data(seat_resource),
2620 serial, x, y);
2621}
2622
2623static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002624unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002625{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002626 /* undo all maximized things here */
2627 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002628
2629 if (shsurf->saved_position_valid)
2630 weston_view_set_position(shsurf->view,
2631 shsurf->saved_x, shsurf->saved_y);
2632 else
2633 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002634
2635 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002636 wl_list_insert(&shsurf->view->geometry.transformation_list,
2637 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002638 shsurf->saved_rotation_valid = false;
2639 }
2640
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002641 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002642}
2643
Philip Withnallbecb77e2013-11-25 18:01:30 +00002644static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002645set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002646{
2647 struct shell_surface *shsurf;
2648 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002649 struct weston_view *view;
2650
2651 view = get_default_view(surface);
2652 if (!view)
2653 return;
2654
2655 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2656
2657 shsurf = get_shell_surface(surface);
2658 current_ws = get_current_workspace(shsurf->shell);
2659
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002660 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002661 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002662
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002663 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002664 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002665
2666 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002667 weston_view_damage_below(view);
2668}
2669
2670static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002671shell_surface_set_maximized(struct wl_client *client,
2672 struct wl_resource *resource,
2673 struct wl_resource *output_resource)
2674{
2675 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2676 struct weston_output *output;
2677
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002678 surface_clear_next_states(shsurf);
2679 shsurf->next_state.maximized = true;
2680 shsurf->state_changed = true;
2681
Pekka Paalanenb5026542014-11-12 15:09:24 +02002682 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002683 shell_surface_set_parent(shsurf, NULL);
2684
Philip Withnallbecb77e2013-11-25 18:01:30 +00002685 if (output_resource)
2686 output = wl_resource_get_user_data(output_resource);
2687 else
2688 output = NULL;
2689
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002690 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002691
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002692 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002693}
2694
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002695/* This is only ever called from set_surface_type(), so there’s no need to
2696 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002697static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002698reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002699{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002700 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002701 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002702 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002703 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002704
Pekka Paalanen98262232011-12-01 10:42:22 +02002705 return 0;
2706}
2707
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002708static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002709set_full_output(struct shell_surface *shsurf)
2710{
2711 shsurf->saved_x = shsurf->view->geometry.x;
2712 shsurf->saved_y = shsurf->view->geometry.y;
2713 shsurf->saved_position_valid = true;
2714
2715 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2716 wl_list_remove(&shsurf->rotation.transform.link);
2717 wl_list_init(&shsurf->rotation.transform.link);
2718 weston_view_geometry_dirty(shsurf->view);
2719 shsurf->saved_rotation_valid = true;
2720 }
2721}
2722
2723static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002724set_surface_type(struct shell_surface *shsurf)
2725{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002726 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002728
Philip Withnallbecb77e2013-11-25 18:01:30 +00002729 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002730
Rafael Antognolli03b16592013-12-03 15:35:42 -02002731 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002732 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002733
2734 switch (shsurf->type) {
2735 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002736 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002737 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002738 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 weston_view_set_position(shsurf->view,
2740 pev->geometry.x + shsurf->transient.x,
2741 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002742 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002743 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002744
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002745 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 weston_view_set_position(shsurf->view, shsurf->transient.x,
2747 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002748 break;
2749
Philip Withnall0f640e22013-11-25 18:01:31 +00002750 case SHELL_SURFACE_POPUP:
2751 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002752 default:
2753 break;
2754 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002755
2756 /* Update the surface’s layer. */
2757 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002758}
2759
Tiago Vignattibe143262012-04-16 17:31:41 +03002760static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002761shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002762{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002763 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002764}
2765
Pekka Paalanen8274d902014-08-06 19:36:51 +03002766static int
2767black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2768{
2769 struct weston_surface *fs_surface = surface->configure_private;
2770 int n;
2771 int rem;
2772 int ret;
2773
2774 n = snprintf(buf, len, "black background surface for ");
2775 if (n < 0)
2776 return n;
2777
2778 rem = (int)len - n;
2779 if (rem < 0)
2780 rem = 0;
2781
2782 if (fs_surface->get_label)
2783 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2784 else
2785 ret = snprintf(buf + n, rem, "<unknown>");
2786
2787 if (ret < 0)
2788 return n;
2789
2790 return n + ret;
2791}
2792
Alex Wu21858432012-04-01 20:13:08 +08002793static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002794black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002795
Jason Ekstranda7af7042013-10-12 22:38:11 -05002796static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002797create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002798 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002799 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002800{
2801 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002803
2804 surface = weston_surface_create(ec);
2805 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002806 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002807 return NULL;
2808 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 view = weston_view_create(surface);
2810 if (surface == NULL) {
2811 weston_log("no memory\n");
2812 weston_surface_destroy(surface);
2813 return NULL;
2814 }
Alex Wu4539b082012-03-01 12:57:46 +08002815
Alex Wu21858432012-04-01 20:13:08 +08002816 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002817 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002818 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002819 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002820 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002821 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002822 pixman_region32_fini(&surface->input);
2823 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002824
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002825 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002826 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827
2828 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002829}
2830
Philip Withnalled8f1a92013-11-25 18:01:43 +00002831static void
2832shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2833{
2834 struct weston_output *output = shsurf->fullscreen_output;
2835
Rafael Antognolli03b16592013-12-03 15:35:42 -02002836 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002837
2838 if (!shsurf->fullscreen.black_view)
2839 shsurf->fullscreen.black_view =
2840 create_black_surface(shsurf->surface->compositor,
2841 shsurf->surface,
2842 output->x, output->y,
2843 output->width,
2844 output->height);
2845
2846 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002847 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2848 weston_layer_entry_insert(&shsurf->view->layer_link,
2849 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002850 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2851 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002852
2853 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002854}
2855
Alex Wu4539b082012-03-01 12:57:46 +08002856/* Create black surface and append it to the associated fullscreen surface.
2857 * Handle size dismatch and positioning according to the method. */
2858static void
2859shell_configure_fullscreen(struct shell_surface *shsurf)
2860{
2861 struct weston_output *output = shsurf->fullscreen_output;
2862 struct weston_surface *surface = shsurf->surface;
2863 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002864 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002865 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002866
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002867 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2868 restore_output_mode(output);
2869
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002870 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002871 weston_layer_entry_remove(&shsurf->view->layer_link);
2872 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002873
Philip Withnalled8f1a92013-11-25 18:01:43 +00002874 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002875
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002877 &surf_width, &surf_height);
2878
Alex Wu4539b082012-03-01 12:57:46 +08002879 switch (shsurf->fullscreen.type) {
2880 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002881 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002883 break;
2884 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002885 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002886 if (output->width == surf_width &&
2887 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002888 weston_view_set_position(shsurf->view,
2889 output->x - surf_x,
2890 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002891 break;
2892 }
2893
Alex Wu4539b082012-03-01 12:57:46 +08002894 matrix = &shsurf->fullscreen.transform.matrix;
2895 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002896
Scott Moreau1bad5db2012-08-18 01:04:05 -06002897 output_aspect = (float) output->width /
2898 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002899 /* XXX: Use surf_width and surf_height here? */
2900 surface_aspect = (float) surface->width /
2901 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002902 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002903 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002904 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002905 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002906 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002907 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002908
Alex Wu4539b082012-03-01 12:57:46 +08002909 weston_matrix_scale(matrix, scale, scale, 1);
2910 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002911 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002912 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002913 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2914 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002915 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002916
Alex Wu4539b082012-03-01 12:57:46 +08002917 break;
2918 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002919 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002920 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002921 surf_width * surface->buffer_viewport.buffer.scale,
2922 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002923 shsurf->fullscreen.framerate};
2924
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002925 if (weston_output_mode_switch_to_temporary(output, &mode,
2926 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927 weston_view_set_position(shsurf->view,
2928 output->x - surf_x,
2929 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002930 shsurf->fullscreen.black_view->surface->width = output->width;
2931 shsurf->fullscreen.black_view->surface->height = output->height;
2932 weston_view_set_position(shsurf->fullscreen.black_view,
2933 output->x - surf_x,
2934 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002935 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002936 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002937 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002938 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002940 }
Alex Wubd3354b2012-04-17 17:20:49 +08002941 }
Alex Wu4539b082012-03-01 12:57:46 +08002942 break;
2943 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002944 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002945 break;
2946 default:
2947 break;
2948 }
2949}
2950
Alex Wu4539b082012-03-01 12:57:46 +08002951static void
2952shell_map_fullscreen(struct shell_surface *shsurf)
2953{
Alex Wubd3354b2012-04-17 17:20:49 +08002954 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002955}
2956
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002957static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002958set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2959{
2960 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002961 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002962 shsurf->transient.x = x;
2963 shsurf->transient.y = y;
2964 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002965
2966 shell_surface_set_parent(shsurf, NULL);
2967
Pekka Paalanenb5026542014-11-12 15:09:24 +02002968 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002969 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002970}
2971
Kristian Høgsberg47792412014-05-04 13:47:06 -07002972static void
2973shell_interface_set_fullscreen(struct shell_surface *shsurf,
2974 uint32_t method,
2975 uint32_t framerate,
2976 struct weston_output *output)
2977{
2978 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002979 shsurf->next_state.fullscreen = true;
2980 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002981
2982 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002983}
2984
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002985static struct weston_output *
2986get_focused_output(struct weston_compositor *compositor)
2987{
2988 struct weston_seat *seat;
2989 struct weston_output *output = NULL;
2990
2991 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002992 struct weston_touch *touch = weston_seat_get_touch(seat);
2993 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2994 struct weston_keyboard *keyboard =
2995 weston_seat_get_keyboard(seat);
2996
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002997 /* Priority has touch focus, then pointer and
2998 * then keyboard focus. We should probably have
2999 * three for loops and check frist for touch,
3000 * then for pointer, etc. but unless somebody has some
3001 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003002 if (touch && touch->focus)
3003 output = touch->focus->output;
3004 else if (pointer && pointer->focus)
3005 output = pointer->focus->output;
3006 else if (keyboard && keyboard->focus)
3007 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003008
3009 if (output)
3010 break;
3011 }
3012
3013 return output;
3014}
3015
3016static void
3017shell_interface_set_maximized(struct shell_surface *shsurf)
3018{
3019 struct weston_output *output;
3020
3021 surface_clear_next_states(shsurf);
3022 shsurf->next_state.maximized = true;
3023 shsurf->state_changed = true;
3024 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3025
3026 if (!weston_surface_is_mapped(shsurf->surface))
3027 output = get_focused_output(shsurf->surface->compositor);
3028 else
3029 output = shsurf->surface->output;
3030
3031 shell_surface_set_output(shsurf, output);
3032 send_configure_for_surface(shsurf);
3033}
3034
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003035static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003036shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003037{
Derek Foremane4d6c832015-08-05 14:48:11 -05003038 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003039}
3040
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003041static int
3042shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003043 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003044 uint32_t edges)
3045{
Derek Foremane4d6c832015-08-05 14:48:11 -05003046 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003047}
3048
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003049static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003050
3051static void
3052destroy_shell_seat(struct wl_listener *listener, void *data)
3053{
3054 struct shell_seat *shseat =
3055 container_of(listener,
3056 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003057 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003058
3059 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003060 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061 shseat->popup_grab.client = NULL;
3062
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003063 wl_list_for_each_safe(shsurf, next,
3064 &shseat->popup_grab.surfaces_list,
3065 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003066 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003067 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003068 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069 }
3070
3071 wl_list_remove(&shseat->seat_destroy_listener.link);
3072 free(shseat);
3073}
3074
Jason Ekstrand024177c2014-04-21 19:42:58 -05003075static void
3076shell_seat_caps_changed(struct wl_listener *listener, void *data)
3077{
Derek Foreman1281a362015-07-31 16:55:32 -05003078 struct weston_keyboard *keyboard;
3079 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003080 struct shell_seat *seat;
3081
3082 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003083 keyboard = weston_seat_get_keyboard(seat->seat);
3084 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003085
Derek Foreman1281a362015-07-31 16:55:32 -05003086 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003087 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003088 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003089 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003090 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003091 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003092 wl_list_init(&seat->keyboard_focus_listener.link);
3093 }
3094
Derek Foreman1281a362015-07-31 16:55:32 -05003095 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003096 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003097 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003098 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003099 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003100 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003101 wl_list_init(&seat->pointer_focus_listener.link);
3102 }
3103}
3104
Giulio Camuffo5085a752013-03-25 21:42:45 +01003105static struct shell_seat *
3106create_shell_seat(struct weston_seat *seat)
3107{
3108 struct shell_seat *shseat;
3109
3110 shseat = calloc(1, sizeof *shseat);
3111 if (!shseat) {
3112 weston_log("no memory to allocate shell seat\n");
3113 return NULL;
3114 }
3115
3116 shseat->seat = seat;
3117 wl_list_init(&shseat->popup_grab.surfaces_list);
3118
3119 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3120 wl_signal_add(&seat->destroy_signal,
3121 &shseat->seat_destroy_listener);
3122
Jason Ekstrand024177c2014-04-21 19:42:58 -05003123 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3124 wl_list_init(&shseat->keyboard_focus_listener.link);
3125
3126 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3127 wl_list_init(&shseat->pointer_focus_listener.link);
3128
3129 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3130 wl_signal_add(&seat->updated_caps_signal,
3131 &shseat->caps_changed_listener);
3132 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3133
Giulio Camuffo5085a752013-03-25 21:42:45 +01003134 return shseat;
3135}
3136
3137static struct shell_seat *
3138get_shell_seat(struct weston_seat *seat)
3139{
3140 struct wl_listener *listener;
3141
3142 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003143 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003144
3145 return container_of(listener,
3146 struct shell_seat, seat_destroy_listener);
3147}
3148
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003149static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003150popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003151{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003152 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003153 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003154 struct shell_seat *shseat =
3155 container_of(grab, struct shell_seat, popup_grab.grab);
3156 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003157 wl_fixed_t sx, sy;
3158
Jason Ekstranda7af7042013-10-12 22:38:11 -05003159 view = weston_compositor_pick_view(pointer->seat->compositor,
3160 pointer->x, pointer->y,
3161 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003162
Jason Ekstranda7af7042013-10-12 22:38:11 -05003163 if (view && view->surface->resource &&
3164 wl_resource_get_client(view->surface->resource) == client) {
3165 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003166 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003167 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003168 }
3169}
3170
3171static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003172popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003173 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003174{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003175 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003176 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003177 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003178 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003179 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003180
Jonas Ådahl61917c82014-08-11 22:44:02 +02003181 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003182 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003183 weston_view_from_global_fixed(pointer->focus, x, y,
3184 &pointer->sx, &pointer->sy);
3185 }
3186
Jonas Ådahld2510102014-10-05 21:39:14 +02003187 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003188
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003189 if (!pointer->focus_client)
3190 return;
3191
3192 resource_list = &pointer->focus_client->pointer_resources;
3193 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003194 weston_view_from_global_fixed(pointer->focus,
3195 pointer->x, pointer->y,
3196 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003197 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003198 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003199}
3200
3201static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003202popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003203 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204{
3205 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003206 struct shell_seat *shseat =
3207 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003208 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003209 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003210 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003211 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003213 if (grab->pointer->focus_client)
3214 resource_list = &grab->pointer->focus_client->pointer_resources;
3215 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003216 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003217 wl_resource_for_each(resource, resource_list) {
3218 wl_pointer_send_button(resource, serial,
3219 time, button, state);
3220 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003221 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003222 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003223 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003224 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003225 }
3226
Daniel Stone4dbadb12012-05-30 16:31:51 +01003227 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003228 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003229}
3230
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003231static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003232popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003233 uint32_t time,
3234 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003235{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003236 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003237}
3238
3239static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003240popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3241{
3242 weston_pointer_send_axis_source(grab->pointer, source);
3243}
3244
3245static void
3246popup_grab_frame(struct weston_pointer_grab *grab)
3247{
3248 weston_pointer_send_frame(grab->pointer);
3249}
3250
3251static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003252popup_grab_cancel(struct weston_pointer_grab *grab)
3253{
3254 popup_grab_end(grab->pointer);
3255}
3256
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003257static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003258 popup_grab_focus,
3259 popup_grab_motion,
3260 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003261 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003262 popup_grab_axis_source,
3263 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003264 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003265};
3266
3267static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003268touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003269 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003270{
3271 struct wl_resource *resource;
3272 struct shell_seat *shseat =
3273 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3274 struct wl_display *display = shseat->seat->compositor->wl_display;
3275 uint32_t serial;
3276 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003277 wl_fixed_t sx, sy;
3278
3279 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003280
3281 resource_list = &grab->touch->focus_resource_list;
3282 if (!wl_list_empty(resource_list)) {
3283 serial = wl_display_get_serial(display);
3284 wl_resource_for_each(resource, resource_list) {
3285 wl_touch_send_down(resource, serial, time,
3286 grab->touch->focus->surface->resource,
3287 touch_id, sx, sy);
3288 }
3289 }
3290}
3291
3292static void
3293touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3294{
3295 struct wl_resource *resource;
3296 struct shell_seat *shseat =
3297 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3298 struct wl_display *display = shseat->seat->compositor->wl_display;
3299 uint32_t serial;
3300 struct wl_list *resource_list;
3301
3302 resource_list = &grab->touch->focus_resource_list;
3303 if (!wl_list_empty(resource_list)) {
3304 serial = wl_display_get_serial(display);
3305 wl_resource_for_each(resource, resource_list) {
3306 wl_touch_send_up(resource, serial, time, touch_id);
3307 }
3308 }
3309}
3310
3311static void
3312touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003313 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003314{
3315 struct wl_resource *resource;
3316 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003317 wl_fixed_t sx, sy;
3318
3319 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003320
3321 resource_list = &grab->touch->focus_resource_list;
3322 if (!wl_list_empty(resource_list)) {
3323 wl_resource_for_each(resource, resource_list) {
3324 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3325 }
3326 }
3327}
3328
3329static void
3330touch_popup_grab_frame(struct weston_touch_grab *grab)
3331{
3332}
3333
3334static void
3335touch_popup_grab_cancel(struct weston_touch_grab *grab)
3336{
3337 touch_popup_grab_end(grab->touch);
3338}
3339
3340static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3341 touch_popup_grab_down,
3342 touch_popup_grab_up,
3343 touch_popup_grab_motion,
3344 touch_popup_grab_frame,
3345 touch_popup_grab_cancel,
3346};
3347
3348static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003349shell_surface_send_popup_done(struct shell_surface *shsurf)
3350{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003351 if (shsurf->resource == NULL)
3352 return;
3353 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003354 wl_shell_surface_send_popup_done(shsurf->resource);
3355 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003356 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003357}
3358
3359static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003360popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003361{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003362 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003363 struct shell_seat *shseat =
3364 container_of(grab, struct shell_seat, popup_grab.grab);
3365 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003366 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003367
3368 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003369 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003370 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003371 shseat->popup_grab.grab.interface = NULL;
3372 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003373 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003374 wl_list_for_each_safe(shsurf, next,
3375 &shseat->popup_grab.surfaces_list,
3376 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003377 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003378 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003379 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003380 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003381 wl_list_init(&shseat->popup_grab.surfaces_list);
3382 }
3383}
3384
3385static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003386touch_popup_grab_end(struct weston_touch *touch)
3387{
3388 struct weston_touch_grab *grab = touch->grab;
3389 struct shell_seat *shseat =
3390 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3391 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003392 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003393
3394 if (touch->grab->interface == &touch_popup_grab_interface) {
3395 weston_touch_end_grab(grab->touch);
3396 shseat->popup_grab.client = NULL;
3397 shseat->popup_grab.touch_grab.interface = NULL;
3398 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3399 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003400 wl_list_for_each_safe(shsurf, next,
3401 &shseat->popup_grab.surfaces_list,
3402 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003403 shell_surface_send_popup_done(shsurf);
3404 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003405 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003406 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003407 wl_list_init(&shseat->popup_grab.surfaces_list);
3408 }
3409}
3410
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003411static struct shell_surface *
3412get_top_popup(struct shell_seat *shseat)
3413{
3414 struct shell_surface *shsurf;
3415
3416 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3417 return NULL;
3418 } else {
3419 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3420 struct shell_surface,
3421 popup.grab_link);
3422 return shsurf;
3423 }
3424}
3425
3426static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003427add_popup_grab(struct shell_surface *shsurf,
3428 struct shell_seat *shseat,
3429 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003430{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003431 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003432 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003433 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3434 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003435
3436 parent = get_shell_surface(shsurf->parent);
3437 top_surface = get_top_popup(shseat);
3438 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003439 (!parent ||
3440 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003441 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003442 wl_resource_post_error(shsurf->owner_resource,
3443 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003444 "xdg_popup was not created on the "
3445 "topmost popup");
3446 return -1;
3447 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003448
3449 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003450 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003451 shseat->popup_grab.client =
3452 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003453
3454 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003455 shseat->popup_grab.grab.interface =
3456 &popup_grab_interface;
3457
3458 /* We must make sure here that this popup was opened
3459 * after a mouse press, and not just by moving around
3460 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003461 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003462 shseat->popup_grab.initial_up = 0;
3463 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003464 shseat->popup_grab.touch_grab.interface =
3465 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003466 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003467
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003468 wl_list_insert(&shseat->popup_grab.surfaces_list,
3469 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003470
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003471 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003472 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003473 &shseat->popup_grab.grab);
3474 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003475 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003476 &shseat->popup_grab.touch_grab);
3477 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003478 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003479 wl_list_insert(&shseat->popup_grab.surfaces_list,
3480 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003481 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003482
3483 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003484}
3485
3486static void
3487remove_popup_grab(struct shell_surface *shsurf)
3488{
3489 struct shell_seat *shseat = shsurf->popup.shseat;
3490
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003491 if (shell_surface_is_xdg_popup(shsurf) &&
3492 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003493 wl_resource_post_error(shsurf->owner_resource,
3494 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003495 "xdg_popup was destroyed while it was "
3496 "not the topmost popup.");
3497 return;
3498 }
3499
Giulio Camuffo5085a752013-03-25 21:42:45 +01003500 wl_list_remove(&shsurf->popup.grab_link);
3501 wl_list_init(&shsurf->popup.grab_link);
3502 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003503 if (shseat->popup_grab.type == POINTER) {
3504 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3505 shseat->popup_grab.grab.interface = NULL;
3506 } else if (shseat->popup_grab.type == TOUCH) {
3507 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3508 shseat->popup_grab.touch_grab.interface = NULL;
3509 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003510 }
3511}
3512
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003513static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003514shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003515{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003516 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003517 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003518 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3519 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003520
Jason Ekstranda7af7042013-10-12 22:38:11 -05003521 shsurf->surface->output = parent_view->output;
3522 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003523
Jason Ekstranda7af7042013-10-12 22:38:11 -05003524 weston_view_set_transform_parent(shsurf->view, parent_view);
3525 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3526 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003527
Derek Foreman1281a362015-07-31 16:55:32 -05003528 if (pointer &&
3529 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003530 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3531 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003532 } else if (touch &&
3533 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003534 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3535 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003536 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003537 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003538 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003539 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003540
3541 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003542}
3543
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003544static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003545 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003546 shell_surface_move,
3547 shell_surface_resize,
3548 shell_surface_set_toplevel,
3549 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003550 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003551 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003552 shell_surface_set_maximized,
3553 shell_surface_set_title,
3554 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003555};
3556
3557static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003558destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003559{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003560 struct shell_surface *child, *next;
3561
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003562 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3563
Giulio Camuffo5085a752013-03-25 21:42:45 +01003564 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3565 remove_popup_grab(shsurf);
3566 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003567
Alex Wubd3354b2012-04-17 17:20:49 +08003568 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003569 shell_surface_is_top_fullscreen(shsurf))
3570 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003571
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 if (shsurf->fullscreen.black_view)
3573 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003574
Alex Wubd3354b2012-04-17 17:20:49 +08003575 /* As destroy_resource() use wl_list_for_each_safe(),
3576 * we can always remove the listener.
3577 */
3578 wl_list_remove(&shsurf->surface_destroy_listener.link);
3579 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003580 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003581 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003582
Jason Ekstranda7af7042013-10-12 22:38:11 -05003583 weston_view_destroy(shsurf->view);
3584
Philip Withnall648a4dd2013-11-25 18:01:44 +00003585 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003586 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3587 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003588
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003589 wl_list_remove(&shsurf->link);
3590 free(shsurf);
3591}
3592
3593static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003594shell_destroy_shell_surface(struct wl_resource *resource)
3595{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003596 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003597
Bryan Caina46b9462014-04-04 17:41:24 -05003598 if (!wl_list_empty(&shsurf->popup.grab_link))
3599 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003600 if (shsurf->resource)
3601 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003602 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003603}
3604
3605static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003606shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003607{
3608 struct shell_surface *shsurf = container_of(listener,
3609 struct shell_surface,
3610 surface_destroy_listener);
3611
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003612 if (shsurf->resource)
3613 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003614
3615 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003616}
3617
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003618static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003619fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003620{
3621 struct shell_surface *shsurf = data;
3622
3623 weston_surface_destroy(shsurf->surface);
3624}
3625
3626static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003627fade_out_done(struct weston_view_animation *animation, void *data)
3628{
3629 struct shell_surface *shsurf = data;
3630 struct wl_event_loop *loop;
3631
3632 loop = wl_display_get_event_loop(
3633 shsurf->surface->compositor->wl_display);
3634
3635 if (!shsurf->destroying) {
3636 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3637 shsurf->destroying = true;
3638 }
3639}
3640
3641static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003642handle_resource_destroy(struct wl_listener *listener, void *data)
3643{
3644 struct shell_surface *shsurf =
3645 container_of(listener, struct shell_surface,
3646 resource_destroy_listener);
3647
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003648 if (!weston_surface_is_mapped(shsurf->surface))
3649 return;
3650
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003651 shsurf->surface->ref_count++;
3652
3653 pixman_region32_fini(&shsurf->surface->pending.input);
3654 pixman_region32_init(&shsurf->surface->pending.input);
3655 pixman_region32_fini(&shsurf->surface->input);
3656 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003657 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3658 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3659 fade_out_done, shsurf);
3660 } else {
3661 weston_surface_destroy(shsurf->surface);
3662 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003663}
3664
3665static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003666shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003667
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003668struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003669get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003670{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003671 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003672 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003673 else
3674 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003675}
3676
Rafael Antognollie2a34552013-12-03 15:35:45 -02003677static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003678create_common_surface(struct shell_client *owner, void *shell,
3679 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003680 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003681{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003682 struct shell_surface *shsurf;
3683
Pekka Paalanen50b67472014-10-01 15:02:41 +03003684 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003685
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003686 shsurf = calloc(1, sizeof *shsurf);
3687 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003688 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003689 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003690 }
3691
Jason Ekstranda7af7042013-10-12 22:38:11 -05003692 shsurf->view = weston_view_create(surface);
3693 if (!shsurf->view) {
3694 weston_log("no memory to allocate shell surface\n");
3695 free(shsurf);
3696 return NULL;
3697 }
3698
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003699 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003700 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003701 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003702
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003703 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3704 wl_resource_add_destroy_listener(surface->resource,
3705 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003706 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003707
Tiago Vignattibc052c92012-04-19 16:18:18 +03003708 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003709 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003710 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003711 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003712 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003713 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3714 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003715 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003716 wl_list_init(&shsurf->fullscreen.transform.link);
3717
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003718 shsurf->output = get_default_output(shsurf->shell->compositor);
3719
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003720 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003721 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003722 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003723 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003724
3725 /* init link so its safe to always remove it in destroy_shell_surface */
3726 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003727 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003728
Pekka Paalanen460099f2012-01-20 16:48:25 +02003729 /* empty when not in use */
3730 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003731 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003732
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003733 wl_list_init(&shsurf->workspace_transform.link);
3734
Philip Withnall648a4dd2013-11-25 18:01:44 +00003735 wl_list_init(&shsurf->children_link);
3736 wl_list_init(&shsurf->children_list);
3737 shsurf->parent = NULL;
3738
Pekka Paalanenb5026542014-11-12 15:09:24 +02003739 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003740
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003741 shsurf->client = client;
3742
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003743 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003744}
3745
Rafael Antognollie2a34552013-12-03 15:35:45 -02003746static struct shell_surface *
3747create_shell_surface(void *shell, struct weston_surface *surface,
3748 const struct weston_shell_client *client)
3749{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003750 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003751}
3752
Tiago Vignattibc052c92012-04-19 16:18:18 +03003753static void
3754shell_get_shell_surface(struct wl_client *client,
3755 struct wl_resource *resource,
3756 uint32_t id,
3757 struct wl_resource *surface_resource)
3758{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003759 struct weston_surface *surface =
3760 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003761 struct shell_client *sc = wl_resource_get_user_data(resource);
3762 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003763 struct shell_surface *shsurf;
3764
Pekka Paalanen50b67472014-10-01 15:02:41 +03003765 if (weston_surface_set_role(surface, "wl_shell_surface",
3766 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003767 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003768
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003769 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003770 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003771 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003772 return;
3773 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003774
Jason Ekstranda85118c2013-06-27 20:17:02 -05003775 shsurf->resource =
3776 wl_resource_create(client,
3777 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003778 if (!shsurf->resource) {
3779 wl_resource_post_no_memory(surface_resource);
3780 return;
3781 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003782 wl_resource_set_implementation(shsurf->resource,
3783 &shell_surface_implementation,
3784 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003785 wl_list_insert(&sc->surface_list,
3786 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003787}
3788
Rafael Antognollie2a34552013-12-03 15:35:45 -02003789static bool
3790shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3791{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003792 /* A shell surface without a resource is created from xwayland
3793 * and is considered a wl_shell surface for now. */
3794
3795 return shsurf->resource == NULL ||
3796 wl_resource_instance_of(shsurf->resource,
3797 &wl_shell_surface_interface,
3798 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003799}
3800
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003801static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003802 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003803};
3804
Rafael Antognollie2a34552013-12-03 15:35:45 -02003805/****************************
3806 * xdg-shell implementation */
3807
3808static void
3809xdg_surface_destroy(struct wl_client *client,
3810 struct wl_resource *resource)
3811{
3812 wl_resource_destroy(resource);
3813}
3814
3815static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003816xdg_surface_set_parent(struct wl_client *client,
3817 struct wl_resource *resource,
3818 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003819{
3820 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003821 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003822
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003823 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003824 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003825 shell_surface_set_parent(shsurf, parent->surface);
3826 } else {
3827 shell_surface_set_parent(shsurf, NULL);
3828 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003829}
3830
3831static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003832xdg_surface_set_app_id(struct wl_client *client,
3833 struct wl_resource *resource,
3834 const char *app_id)
3835{
3836 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3837
3838 free(shsurf->class);
3839 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003840 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003841}
3842
3843static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003844xdg_surface_show_window_menu(struct wl_client *client,
3845 struct wl_resource *surface_resource,
3846 struct wl_resource *seat_resource,
3847 uint32_t serial,
3848 int32_t x,
3849 int32_t y)
3850{
3851 /* TODO */
3852}
3853
3854static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003855xdg_surface_set_title(struct wl_client *client,
3856 struct wl_resource *resource, const char *title)
3857{
3858 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3859
3860 set_title(shsurf, title);
3861}
3862
3863static void
3864xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3865 struct wl_resource *seat_resource, uint32_t serial)
3866{
3867 common_surface_move(resource, seat_resource, serial);
3868}
3869
3870static void
3871xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3872 struct wl_resource *seat_resource, uint32_t serial,
3873 uint32_t edges)
3874{
3875 common_surface_resize(resource, seat_resource, serial, edges);
3876}
3877
3878static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003879xdg_surface_ack_configure(struct wl_client *client,
3880 struct wl_resource *resource,
3881 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003882{
3883 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3884
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003885 if (shsurf->state_requested) {
3886 shsurf->next_state = shsurf->requested_state;
3887 shsurf->state_changed = true;
3888 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003889 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003890}
3891
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003892static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003893xdg_surface_set_window_geometry(struct wl_client *client,
3894 struct wl_resource *resource,
3895 int32_t x,
3896 int32_t y,
3897 int32_t width,
3898 int32_t height)
3899{
3900 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3901
3902 set_window_geometry(shsurf, x, y, width, height);
3903}
3904
3905static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003906xdg_surface_set_maximized(struct wl_client *client,
3907 struct wl_resource *resource)
3908{
3909 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003910 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003911
3912 shsurf->state_requested = true;
3913 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003914
3915 if (!weston_surface_is_mapped(shsurf->surface))
3916 output = get_focused_output(shsurf->surface->compositor);
3917 else
3918 output = shsurf->surface->output;
3919
3920 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003921 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003922}
3923
3924static void
3925xdg_surface_unset_maximized(struct wl_client *client,
3926 struct wl_resource *resource)
3927{
3928 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3929
3930 shsurf->state_requested = true;
3931 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003932 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003933}
3934
3935static void
3936xdg_surface_set_fullscreen(struct wl_client *client,
3937 struct wl_resource *resource,
3938 struct wl_resource *output_resource)
3939{
3940 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3941 struct weston_output *output;
3942
3943 shsurf->state_requested = true;
3944 shsurf->requested_state.fullscreen = true;
3945
3946 if (output_resource)
3947 output = wl_resource_get_user_data(output_resource);
3948 else
3949 output = NULL;
3950
Marek Chalupa052917a2014-09-02 11:35:12 +02003951 /* handle clients launching in fullscreen */
3952 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3953 /* Set the output to the one that has focus currently. */
3954 assert(shsurf->surface);
3955 output = get_focused_output(shsurf->surface->compositor);
3956 }
3957
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003958 shell_surface_set_output(shsurf, output);
3959 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003960
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003961 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003962}
3963
3964static void
3965xdg_surface_unset_fullscreen(struct wl_client *client,
3966 struct wl_resource *resource)
3967{
3968 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3969
3970 shsurf->state_requested = true;
3971 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003972 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003973}
3974
3975static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003976xdg_surface_set_minimized(struct wl_client *client,
3977 struct wl_resource *resource)
3978{
3979 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3980
3981 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3982 return;
3983
3984 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003985 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003986}
3987
Rafael Antognollie2a34552013-12-03 15:35:45 -02003988static const struct xdg_surface_interface xdg_surface_implementation = {
3989 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003990 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003991 xdg_surface_set_title,
3992 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003993 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003994 xdg_surface_move,
3995 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003996 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003997 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003998 xdg_surface_set_maximized,
3999 xdg_surface_unset_maximized,
4000 xdg_surface_set_fullscreen,
4001 xdg_surface_unset_fullscreen,
4002 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004003};
4004
4005static void
4006xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004007 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004008{
4009 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004010 uint32_t *s;
4011 struct wl_array states;
4012 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004013
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004014 assert(shsurf);
4015
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004016 if (!shsurf->resource)
4017 return;
4018
4019 wl_array_init(&states);
4020 if (shsurf->requested_state.fullscreen) {
4021 s = wl_array_add(&states, sizeof *s);
4022 *s = XDG_SURFACE_STATE_FULLSCREEN;
4023 } else if (shsurf->requested_state.maximized) {
4024 s = wl_array_add(&states, sizeof *s);
4025 *s = XDG_SURFACE_STATE_MAXIMIZED;
4026 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004027 if (shsurf->resize_edges != 0) {
4028 s = wl_array_add(&states, sizeof *s);
4029 *s = XDG_SURFACE_STATE_RESIZING;
4030 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004031 if (shsurf->focus_count > 0) {
4032 s = wl_array_add(&states, sizeof *s);
4033 *s = XDG_SURFACE_STATE_ACTIVATED;
4034 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004035
4036 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4037 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4038
4039 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004040}
4041
4042static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004043 xdg_send_configure,
4044 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004045};
4046
4047static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004048xdg_shell_destroy(struct wl_client *client,
4049 struct wl_resource *resource)
4050{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004051 struct shell_client *sc = wl_resource_get_user_data(resource);
4052 struct wl_resource *shsurf_resource;
4053 struct shell_surface *shsurf;
4054
4055 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4056 shsurf = wl_resource_get_user_data(shsurf_resource);
4057 if (shsurf->owner_resource == resource) {
4058 wl_resource_post_error(
4059 resource,
4060 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4061 "not all child surface objects destroyed");
4062 return;
4063 }
4064 }
4065
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004066 wl_resource_destroy(resource);
4067}
4068
4069static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004070xdg_use_unstable_version(struct wl_client *client,
4071 struct wl_resource *resource,
4072 int32_t version)
4073{
4074 if (version > 1) {
4075 wl_resource_post_error(resource,
4076 1,
4077 "xdg-shell:: version not implemented yet.");
4078 return;
4079 }
4080}
4081
4082static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004083create_xdg_surface(struct shell_client *owner, void *shell,
4084 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004085 const struct weston_shell_client *client)
4086{
4087 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004088
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004089 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004090 if (!shsurf)
4091 return NULL;
4092
Pekka Paalanenb5026542014-11-12 15:09:24 +02004093 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004094
4095 return shsurf;
4096}
4097
4098static void
4099xdg_get_xdg_surface(struct wl_client *client,
4100 struct wl_resource *resource,
4101 uint32_t id,
4102 struct wl_resource *surface_resource)
4103{
4104 struct weston_surface *surface =
4105 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004106 struct shell_client *sc = wl_resource_get_user_data(resource);
4107 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004108 struct shell_surface *shsurf;
4109
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004110 shsurf = get_shell_surface(surface);
4111 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4112 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4113 "This wl_surface is already an "
4114 "xdg_surface");
4115 return;
4116 }
4117
Pekka Paalanen50b67472014-10-01 15:02:41 +03004118 if (weston_surface_set_role(surface, "xdg_surface",
4119 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004120 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004121
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004122 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004123 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004124 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004125 return;
4126 }
4127
4128 shsurf->resource =
4129 wl_resource_create(client,
4130 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004131 if (!shsurf->resource) {
4132 wl_resource_post_no_memory(surface_resource);
4133 return;
4134 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004135 wl_resource_set_implementation(shsurf->resource,
4136 &xdg_surface_implementation,
4137 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004138 shsurf->owner_resource = resource;
4139 wl_list_insert(&sc->surface_list,
4140 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004141}
4142
4143static bool
4144shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4145{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004146 return shsurf->resource &&
4147 wl_resource_instance_of(shsurf->resource,
4148 &xdg_surface_interface,
4149 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004150}
4151
4152/* xdg-popup implementation */
4153
4154static void
4155xdg_popup_destroy(struct wl_client *client,
4156 struct wl_resource *resource)
4157{
4158 wl_resource_destroy(resource);
4159}
4160
Rafael Antognollie2a34552013-12-03 15:35:45 -02004161static const struct xdg_popup_interface xdg_popup_implementation = {
4162 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004163};
4164
4165static void
4166xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004167 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004168{
4169}
4170
4171static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004172 xdg_popup_send_configure,
4173 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004174};
4175
4176static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004177create_xdg_popup(struct shell_client *owner, void *shell,
4178 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004179 const struct weston_shell_client *client,
4180 struct weston_surface *parent,
4181 struct shell_seat *seat,
4182 uint32_t serial,
4183 int32_t x, int32_t y)
4184{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004185 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004186
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004187 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004188 if (!shsurf)
4189 return NULL;
4190
Pekka Paalanenb5026542014-11-12 15:09:24 +02004191 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004192 shsurf->popup.shseat = seat;
4193 shsurf->popup.serial = serial;
4194 shsurf->popup.x = x;
4195 shsurf->popup.y = y;
4196 shell_surface_set_parent(shsurf, parent);
4197
4198 return shsurf;
4199}
4200
4201static void
4202xdg_get_xdg_popup(struct wl_client *client,
4203 struct wl_resource *resource,
4204 uint32_t id,
4205 struct wl_resource *surface_resource,
4206 struct wl_resource *parent_resource,
4207 struct wl_resource *seat_resource,
4208 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004209 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004210{
4211 struct weston_surface *surface =
4212 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004213 struct shell_client *sc = wl_resource_get_user_data(resource);
4214 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004215 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004216 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004217 struct weston_surface *parent;
4218 struct shell_seat *seat;
4219
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004220 shsurf = get_shell_surface(surface);
4221 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4222 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4223 "This wl_surface is already an "
4224 "xdg_popup");
4225 return;
4226 }
4227
Pekka Paalanen50b67472014-10-01 15:02:41 +03004228 if (weston_surface_set_role(surface, "xdg_popup",
4229 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004230 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004231
4232 if (!parent_resource) {
4233 wl_resource_post_error(surface_resource,
4234 WL_DISPLAY_ERROR_INVALID_OBJECT,
4235 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004236 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004237 }
4238
4239 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004240 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004241
Jonas Ådahlee45a552015-03-18 16:37:47 +08004242 /* Verify that we are creating the top most popup when mapping,
4243 * as it's not until then we know whether it was mapped as most
4244 * top level or not. */
4245
4246 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004247 if (!parent_shsurf ||
4248 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4249 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004250 wl_resource_post_error(resource,
4251 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4252 "xdg_popup parent was invalid");
4253 return;
4254 }
4255
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004256 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004257 parent, seat, serial, x, y);
4258 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004259 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004260 return;
4261 }
4262
4263 shsurf->resource =
4264 wl_resource_create(client,
4265 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004266 if (!shsurf->resource) {
4267 wl_resource_post_no_memory(surface_resource);
4268 return;
4269 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004270 wl_resource_set_implementation(shsurf->resource,
4271 &xdg_popup_implementation,
4272 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004273 shsurf->owner_resource = resource;
4274 wl_list_insert(&sc->surface_list,
4275 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004276}
4277
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004278static void
4279xdg_pong(struct wl_client *client,
4280 struct wl_resource *resource, uint32_t serial)
4281{
4282 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004283
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004284 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004285}
4286
Rafael Antognollie2a34552013-12-03 15:35:45 -02004287static bool
4288shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4289{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004290 return (shsurf->resource &&
4291 wl_resource_instance_of(shsurf->resource,
4292 &xdg_popup_interface,
4293 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004294}
4295
4296static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004297 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004298 xdg_use_unstable_version,
4299 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004300 xdg_get_xdg_popup,
4301 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004302};
4303
4304static int
4305xdg_shell_unversioned_dispatch(const void *implementation,
4306 void *_target, uint32_t opcode,
4307 const struct wl_message *message,
4308 union wl_argument *args)
4309{
4310 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004311 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004312
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004313 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004314 wl_resource_post_error(resource,
4315 WL_DISPLAY_ERROR_INVALID_OBJECT,
4316 "must call use_unstable_version first");
4317 return 0;
4318 }
4319
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004320#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004321
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004322 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4323 "shell implementation doesn't match protocol version");
4324
Rafael Antognollie2a34552013-12-03 15:35:45 -02004325 if (args[0].i != XDG_SERVER_VERSION) {
4326 wl_resource_post_error(resource,
4327 WL_DISPLAY_ERROR_INVALID_OBJECT,
4328 "incompatible version, server is %d "
4329 "client wants %d",
4330 XDG_SERVER_VERSION, args[0].i);
4331 return 0;
4332 }
4333
4334 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004335 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004336
4337 return 1;
4338}
4339
4340/* end of xdg-shell implementation */
4341/***********************************/
4342
Kristian Høgsberg07937562011-04-12 17:25:42 -04004343static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004344shell_fade(struct desktop_shell *shell, enum fade_type type);
4345
Pekka Paalanen77346a62011-11-30 16:26:35 +02004346static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004347configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004348{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004350
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004351 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004352 if (v->output == ev->output && v != ev) {
4353 weston_view_unmap(v);
4354 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004355 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004356 }
4357 }
4358
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004359 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004360
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004361 if (wl_list_empty(&ev->layer_link.link)) {
4362 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004364 }
4365}
4366
Pekka Paalanen8274d902014-08-06 19:36:51 +03004367static int
4368background_get_label(struct weston_surface *surface, char *buf, size_t len)
4369{
4370 return snprintf(buf, len, "background for output %s",
4371 surface->output->name);
4372}
4373
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004374static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004375background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004376{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004377 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004379
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 view = container_of(es->views.next, struct weston_view, surface_link);
4381
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004382 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004383}
4384
4385static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004386desktop_shell_set_background(struct wl_client *client,
4387 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004388 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004389 struct wl_resource *surface_resource)
4390{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004391 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004392 struct weston_surface *surface =
4393 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004394 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004395
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004396 if (surface->configure) {
4397 wl_resource_post_error(surface_resource,
4398 WL_DISPLAY_ERROR_INVALID_OBJECT,
4399 "surface role already assigned");
4400 return;
4401 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004402
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4404 weston_view_destroy(view);
4405 view = weston_view_create(surface);
4406
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004407 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004408 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004409 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004410 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004412 weston_desktop_shell_send_configure(resource, 0,
4413 surface_resource,
4414 surface->output->width,
4415 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004416}
4417
Pekka Paalanen8274d902014-08-06 19:36:51 +03004418static int
4419panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4420{
4421 return snprintf(buf, len, "panel for output %s",
4422 surface->output->name);
4423}
4424
Kristian Høgsberg75840622011-09-06 13:48:16 -04004425static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004426panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004427{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004428 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004429 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004430
Jason Ekstranda7af7042013-10-12 22:38:11 -05004431 view = container_of(es->views.next, struct weston_view, surface_link);
4432
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004433 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004434}
4435
4436static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004437desktop_shell_set_panel(struct wl_client *client,
4438 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004439 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004440 struct wl_resource *surface_resource)
4441{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004442 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004443 struct weston_surface *surface =
4444 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004445 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004446
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004447 if (surface->configure) {
4448 wl_resource_post_error(surface_resource,
4449 WL_DISPLAY_ERROR_INVALID_OBJECT,
4450 "surface role already assigned");
4451 return;
4452 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004453
Jason Ekstranda7af7042013-10-12 22:38:11 -05004454 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4455 weston_view_destroy(view);
4456 view = weston_view_create(surface);
4457
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004458 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004459 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004460 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004461 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004462 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004463 weston_desktop_shell_send_configure(resource, 0,
4464 surface_resource,
4465 surface->output->width,
4466 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004467}
4468
Pekka Paalanen8274d902014-08-06 19:36:51 +03004469static int
4470lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4471{
4472 return snprintf(buf, len, "lock window");
4473}
4474
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004475static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004476lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004477{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004478 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004479 struct weston_view *view;
4480
4481 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004482
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004483 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004484 return;
4485
Jason Ekstranda7af7042013-10-12 22:38:11 -05004486 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004487
4488 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004489 weston_layer_entry_insert(&shell->lock_layer.view_list,
4490 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004491 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004492 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004493 }
4494}
4495
4496static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004497handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004498{
Tiago Vignattibe143262012-04-16 17:31:41 +03004499 struct desktop_shell *shell =
4500 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004501
Martin Minarik6d118362012-06-07 18:01:59 +02004502 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004503 shell->lock_surface = NULL;
4504}
4505
4506static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004507desktop_shell_set_lock_surface(struct wl_client *client,
4508 struct wl_resource *resource,
4509 struct wl_resource *surface_resource)
4510{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004511 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004512 struct weston_surface *surface =
4513 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004514
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004515 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004516
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004517 if (!shell->locked)
4518 return;
4519
Pekka Paalanen98262232011-12-01 10:42:22 +02004520 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004521
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004522 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004523 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004524 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004525
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004526 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004527 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004528 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004529 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004530}
4531
4532static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004533resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004534{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004535 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004536
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004537 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004538 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004539 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004540 &shell->input_panel_layer.link);
4541 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004542 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004543 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004544 wl_list_insert(&shell->compositor->cursor_layer.link,
4545 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004546 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004547 wl_list_insert(&shell->fullscreen_layer.link,
4548 &shell->panel_layer.link);
4549 wl_list_insert(&shell->panel_layer.link,
4550 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004551
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004552 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004553
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004554 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004555 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004556 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004557}
4558
4559static void
4560desktop_shell_unlock(struct wl_client *client,
4561 struct wl_resource *resource)
4562{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004563 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004564
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004565 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004566
4567 if (shell->locked)
4568 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004569}
4570
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004571static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004572desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004573 struct wl_resource *resource,
4574 struct wl_resource *surface_resource)
4575{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004576 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004577
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004578 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004579 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004580}
4581
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004582static void
4583desktop_shell_desktop_ready(struct wl_client *client,
4584 struct wl_resource *resource)
4585{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004586 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004587
4588 shell_fade_startup(shell);
4589}
4590
Jonny Lamb765760d2014-08-20 15:53:19 +02004591static void
4592desktop_shell_set_panel_position(struct wl_client *client,
4593 struct wl_resource *resource,
4594 uint32_t position)
4595{
4596 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4597
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004598 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4599 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4600 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4601 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004602 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004603 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004604 "bad position argument");
4605 return;
4606 }
4607
4608 shell->panel_position = position;
4609}
4610
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004611static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004612 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004613 desktop_shell_set_panel,
4614 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004615 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004616 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004617 desktop_shell_desktop_ready,
4618 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004619};
4620
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004621static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004622get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004623{
4624 struct shell_surface *shsurf;
4625
4626 shsurf = get_shell_surface(surface);
4627 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004628 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004629 return shsurf->type;
4630}
4631
Kristian Høgsberg75840622011-09-06 13:48:16 -04004632static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004633move_binding(struct weston_pointer *pointer, uint32_t time,
4634 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004635{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004636 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004637 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004638 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004639
Derek Foreman8ae2db52015-07-15 13:00:36 -05004640 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004641 return;
4642
Derek Foreman8ae2db52015-07-15 13:00:36 -05004643 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004644
Pekka Paalanen01388e22013-04-25 13:57:44 +03004645 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004646 if (surface == NULL)
4647 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004648
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004649 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004650 if (shsurf == NULL || shsurf->state.fullscreen ||
4651 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004652 return;
4653
Derek Foreman794fa0e2015-07-15 13:00:38 -05004654 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004655}
4656
4657static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004658maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4659 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004660{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004661 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004662 struct weston_surface *surface;
4663 struct shell_surface *shsurf;
4664
4665 surface = weston_surface_get_main_surface(focus);
4666 if (surface == NULL)
4667 return;
4668
4669 shsurf = get_shell_surface(surface);
4670 if (shsurf == NULL)
4671 return;
4672
4673 if (!shell_surface_is_xdg_surface(shsurf))
4674 return;
4675
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004676 shsurf->state_requested = true;
4677 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004678 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004679}
4680
4681static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004682fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4683 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004684{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004685 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004686 struct weston_surface *surface;
4687 struct shell_surface *shsurf;
4688
4689 surface = weston_surface_get_main_surface(focus);
4690 if (surface == NULL)
4691 return;
4692
4693 shsurf = get_shell_surface(surface);
4694 if (shsurf == NULL)
4695 return;
4696
4697 if (!shell_surface_is_xdg_surface(shsurf))
4698 return;
4699
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004700 shsurf->state_requested = true;
4701 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004702 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004703 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004704}
4705
4706static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004707touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004708{
Derek Foreman362656b2014-09-04 10:23:05 -05004709 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004710 struct weston_surface *surface;
4711 struct shell_surface *shsurf;
4712
Derek Foreman8ae2db52015-07-15 13:00:36 -05004713 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004714 return;
4715
Derek Foreman8ae2db52015-07-15 13:00:36 -05004716 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004717 surface = weston_surface_get_main_surface(focus);
4718 if (surface == NULL)
4719 return;
4720
4721 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004722 if (shsurf == NULL || shsurf->state.fullscreen ||
4723 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004724 return;
4725
Derek Foremanb7674ae2015-07-15 13:00:37 -05004726 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004727}
4728
4729static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004730resize_binding(struct weston_pointer *pointer, uint32_t time,
4731 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004732{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004733 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004734 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004735 uint32_t edges = 0;
4736 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004737 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004738
Derek Foreman8ae2db52015-07-15 13:00:36 -05004739 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004740 return;
4741
Derek Foreman8ae2db52015-07-15 13:00:36 -05004742 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004743
Pekka Paalanen01388e22013-04-25 13:57:44 +03004744 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004745 if (surface == NULL)
4746 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004747
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004748 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004749 if (shsurf == NULL || shsurf->state.fullscreen ||
4750 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004751 return;
4752
Jason Ekstranda7af7042013-10-12 22:38:11 -05004753 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004754 wl_fixed_to_int(pointer->grab_x),
4755 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004756 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004757
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004758 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004759 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004760 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004761 edges |= 0;
4762 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004763 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004764
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004765 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004766 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004767 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004768 edges |= 0;
4769 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004770 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004771
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004772 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004773}
4774
4775static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004776surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004777 struct weston_pointer_axis_event *event,
4778 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004779{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004780 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004781 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004782 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004783 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004784
Pekka Paalanen01388e22013-04-25 13:57:44 +03004785 /* XXX: broken for windows containing sub-surfaces */
4786 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004787 if (surface == NULL)
4788 return;
4789
4790 shsurf = get_shell_surface(surface);
4791 if (!shsurf)
4792 return;
4793
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004794 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004795
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 if (shsurf->view->alpha > 1.0)
4797 shsurf->view->alpha = 1.0;
4798 if (shsurf->view->alpha < step)
4799 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004800
Jason Ekstranda7af7042013-10-12 22:38:11 -05004801 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004802 weston_surface_damage(surface);
4803}
4804
4805static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004806do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004807 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004808{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004809 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004810 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004811 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004812 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004813
Derek Foreman1281a362015-07-31 16:55:32 -05004814 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004815 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4816 return;
4817 }
4818
Scott Moreauccbf29d2012-02-22 14:21:41 -07004819 wl_list_for_each(output, &compositor->output_list, link) {
4820 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004821 wl_fixed_to_double(pointer->x),
4822 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004823 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004825 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004827 increment = -output->zoom.increment;
4828 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004829 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004830 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004831 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004832 else
4833 increment = 0;
4834
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004835 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004836
Scott Moreaue6603982012-06-11 13:07:51 -06004837 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004838 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004839 else if (output->zoom.level > output->zoom.max_level)
4840 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004841
4842 if (!output->zoom.active) {
4843 if (output->zoom.level <= 0.0)
4844 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004845 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004846 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004847
Scott Moreaue6603982012-06-11 13:07:51 -06004848 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004849
Jason Ekstranda7af7042013-10-12 22:38:11 -05004850 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004851 }
4852 }
4853}
4854
Scott Moreauccbf29d2012-02-22 14:21:41 -07004855static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004856zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004857 struct weston_pointer_axis_event *event,
4858 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004859{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004860 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004861}
4862
4863static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004864zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4865 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004866{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004867 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004868}
4869
4870static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004871terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4872 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004873{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004874 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004875
Daniel Stone325fc2d2012-05-30 16:31:58 +01004876 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004877}
4878
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004879static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004880rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004881 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004882{
4883 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004884 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004885 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004886 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004887 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004888
Jonas Ådahld2510102014-10-05 21:39:14 +02004889 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004890
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004891 if (!shsurf)
4892 return;
4893
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004894 cx = 0.5f * shsurf->surface->width;
4895 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004896
Daniel Stone37816df2012-05-16 18:45:18 +01004897 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4898 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004899 r = sqrtf(dx * dx + dy * dy);
4900
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004901 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004902 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004903
4904 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004905 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004906 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004907
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004908 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004909 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004910
4911 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004912 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004913 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004914 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004915 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004916
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004917 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004918 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004919 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004920 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004921 wl_list_init(&shsurf->rotation.transform.link);
4922 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004923 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004924 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004925
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004926 /* We need to adjust the position of the surface
4927 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004928 cposx = shsurf->view->geometry.x + cx;
4929 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004930 dposx = rotate->center.x - cposx;
4931 dposy = rotate->center.y - cposy;
4932 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004933 weston_view_set_position(shsurf->view,
4934 shsurf->view->geometry.x + dposx,
4935 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004936 }
4937
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004938 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004939 * lazily applies the damage due to rotation update.
4940 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004941 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004942}
4943
4944static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004945rotate_grab_button(struct weston_pointer_grab *grab,
4946 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004947{
4948 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004949 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004950 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004951 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004952 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004953
Daniel Stone4dbadb12012-05-30 16:31:51 +01004954 if (pointer->button_count == 0 &&
4955 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004956 if (shsurf)
4957 weston_matrix_multiply(&shsurf->rotation.rotation,
4958 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004959 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004960 free(rotate);
4961 }
4962}
4963
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004964static void
4965rotate_grab_cancel(struct weston_pointer_grab *grab)
4966{
4967 struct rotate_grab *rotate =
4968 container_of(grab, struct rotate_grab, base.grab);
4969
4970 shell_grab_end(&rotate->base);
4971 free(rotate);
4972}
4973
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004974static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004975 noop_grab_focus,
4976 rotate_grab_motion,
4977 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02004978 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10004979 noop_grab_axis_source,
4980 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004981 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004982};
4983
4984static void
Derek Foreman74de4692015-07-15 13:00:39 -05004985surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004986{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004987 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004988 float dx, dy;
4989 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004990
Derek Foreman45a7c272015-09-11 14:27:40 -05004991 surface = find_toplevel_surface(surface);
4992
Pekka Paalanen460099f2012-01-20 16:48:25 +02004993 rotate = malloc(sizeof *rotate);
4994 if (!rotate)
4995 return;
4996
Jason Ekstranda7af7042013-10-12 22:38:11 -05004997 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004998 surface->surface->width * 0.5f,
4999 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005000 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005001
Derek Foreman74de4692015-07-15 13:00:39 -05005002 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5003 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005004 r = sqrtf(dx * dx + dy * dy);
5005 if (r > 20.0f) {
5006 struct weston_matrix inverse;
5007
5008 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005009 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005010 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005011
5012 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005013 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005014 } else {
5015 weston_matrix_init(&surface->rotation.rotation);
5016 weston_matrix_init(&rotate->rotation);
5017 }
5018
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005019 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005020 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005021}
5022
5023static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005024rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005025 void *data)
5026{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005027 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005028 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005029 struct shell_surface *surface;
5030
Derek Foreman8ae2db52015-07-15 13:00:36 -05005031 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005032 return;
5033
Derek Foreman8ae2db52015-07-15 13:00:36 -05005034 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005035
Pekka Paalanen01388e22013-04-25 13:57:44 +03005036 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005037 if (base_surface == NULL)
5038 return;
5039
5040 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005041 if (surface == NULL || surface->state.fullscreen ||
5042 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005043 return;
5044
Derek Foreman74de4692015-07-15 13:00:39 -05005045 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005046}
5047
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005048/* Move all fullscreen layers down to the current workspace and hide their
5049 * black views. The surfaces' state is set to both fullscreen and lowered,
5050 * and this is reversed when such a surface is re-configured, see
5051 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5052 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005053 * lowering_output = NULL - Lower on all outputs, else only lower on the
5054 * specified output.
5055 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005056 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005057 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005058void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005059lower_fullscreen_layer(struct desktop_shell *shell,
5060 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005061{
5062 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005063 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005064
5065 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005066 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005067 &shell->fullscreen_layer.view_list.link,
5068 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005069 struct shell_surface *shsurf = get_shell_surface(view->surface);
5070
5071 if (!shsurf)
5072 continue;
5073
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005074 /* Only lower surfaces which have lowering_output as their fullscreen
5075 * output, unless a NULL output asks for lowering on all outputs.
5076 */
5077 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5078 continue;
5079
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005080 /* We can have a non-fullscreen popup for a fullscreen surface
5081 * in the fullscreen layer. */
5082 if (shsurf->state.fullscreen) {
5083 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005084 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5085 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005086 weston_view_damage_below(shsurf->fullscreen.black_view);
5087
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005088 }
5089
5090 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005091 weston_layer_entry_remove(&view->layer_link);
5092 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005093 weston_view_damage_below(view);
5094 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005095
5096 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005097 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005098}
5099
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005100void
Tiago Vignattibe143262012-04-16 17:31:41 +03005101activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005102 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005103{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005104 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005105 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005106 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005107 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005108 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005109
Pekka Paalanen01388e22013-04-25 13:57:44 +03005110 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005111 shsurf = get_shell_surface(main_surface);
5112 assert(shsurf);
5113
5114 /* Only demote fullscreen surfaces on the output of activated shsurf.
5115 * Leave fullscreen surfaces on unrelated outputs alone. */
5116 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005117
Daniel Stone37816df2012-05-16 18:45:18 +01005118 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005119
Jonas Ådahl8538b222012-08-29 22:13:03 +02005120 state = ensure_focus_state(shell, seat);
5121 if (state == NULL)
5122 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005123
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005124 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005125 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005126
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005127 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005128 shell_configure_fullscreen(shsurf);
5129 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005130 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005131
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005132 /* Update the surface’s layer. This brings it to the top of the stacking
5133 * order as appropriate. */
5134 shell_surface_update_layer(shsurf);
5135
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005136 if (shell->focus_animation_type != ANIMATION_NONE) {
5137 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005138 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005139 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005140}
5141
Alex Wu21858432012-04-01 20:13:08 +08005142/* no-op func for checking black surface */
5143static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005144black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005145{
5146}
5147
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005148static bool
Alex Wu21858432012-04-01 20:13:08 +08005149is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5150{
5151 if (es->configure == black_surface_configure) {
5152 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005153 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005154 return true;
5155 }
5156 return false;
5157}
5158
Kristian Høgsberg75840622011-09-06 13:48:16 -04005159static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005160activate_binding(struct weston_seat *seat,
5161 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005162 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005163{
Derek Foremanbbde6432015-12-10 13:08:01 -06005164 struct focus_state *state;
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005165 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005166 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005167
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005168 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005169
Pekka Paalanen01388e22013-04-25 13:57:44 +03005170 if (is_black_surface(focus, &main_surface))
5171 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005172
Pekka Paalanen01388e22013-04-25 13:57:44 +03005173 main_surface = weston_surface_get_main_surface(focus);
5174 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005175 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005176
Derek Foremanbbde6432015-12-10 13:08:01 -06005177 state = ensure_focus_state(shell, seat);
5178 if (state == NULL)
5179 return;
5180
5181 if (state->keyboard_focus == focus)
5182 return;
5183
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005184 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005185}
5186
5187static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005188click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5189 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005190{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005191 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005192 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005193 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005194 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005195
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005196 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005197}
5198
5199static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005200touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5201 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005202{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005203 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005204 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005205 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005206 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005207
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005208 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005209}
5210
5211static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005212unfocus_all_seats(struct desktop_shell *shell)
5213{
5214 struct weston_seat *seat, *next;
5215
5216 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005217 struct weston_keyboard *keyboard =
5218 weston_seat_get_keyboard(seat);
5219
5220 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005221 continue;
5222
Derek Foreman1281a362015-07-31 16:55:32 -05005223 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005224 }
5225}
5226
5227static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005228lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005229{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005230 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005231
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005232 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005233 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005234 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005235 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005236
5237 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005238
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005239 /* Hide all surfaces by removing the fullscreen, panel and
5240 * toplevel layers. This way nothing else can show or receive
5241 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005242
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005243 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005244 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005245 if (shell->showing_input_panels)
5246 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005247 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005248 wl_list_insert(&shell->compositor->cursor_layer.link,
5249 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005250
Derek Foreman004b4a12015-07-20 16:28:13 -05005251 weston_compositor_sleep(shell->compositor);
5252
Neil Robertsb4a91702014-04-09 16:33:32 +01005253 /* Remove the keyboard focus on all seats. This will be
5254 * restored to the workspace's saved state via
5255 * restore_focus_state when the compositor is unlocked */
5256 unfocus_all_seats(shell);
5257
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005258 /* TODO: disable bindings that should not work while locked. */
5259
5260 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005261}
5262
5263static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005264unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005265{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005266 struct wl_resource *shell_resource;
5267
Pekka Paalanend81c2162011-11-16 13:47:34 +02005268 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005269 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005270 return;
5271 }
5272
5273 /* If desktop-shell client has gone away, unlock immediately. */
5274 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005275 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005276 return;
5277 }
5278
5279 if (shell->prepare_event_sent)
5280 return;
5281
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005282 shell_resource = shell->child.desktop_shell;
5283 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005284 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005285}
5286
5287static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005288shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005289{
5290 struct desktop_shell *shell = data;
5291
5292 shell->fade.animation = NULL;
5293
5294 switch (shell->fade.type) {
5295 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005296 weston_surface_destroy(shell->fade.view->surface);
5297 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005298 break;
5299 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005300 lock(shell);
5301 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005302 default:
5303 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005304 }
5305}
5306
Jason Ekstranda7af7042013-10-12 22:38:11 -05005307static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005308shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005309{
5310 struct weston_compositor *compositor = shell->compositor;
5311 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005312 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005313
5314 surface = weston_surface_create(compositor);
5315 if (!surface)
5316 return NULL;
5317
Jason Ekstranda7af7042013-10-12 22:38:11 -05005318 view = weston_view_create(surface);
5319 if (!view) {
5320 weston_surface_destroy(surface);
5321 return NULL;
5322 }
5323
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005324 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005325 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005326 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005327 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5328 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005329 pixman_region32_init(&surface->input);
5330
Jason Ekstranda7af7042013-10-12 22:38:11 -05005331 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005332}
5333
5334static void
5335shell_fade(struct desktop_shell *shell, enum fade_type type)
5336{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005337 float tint;
5338
5339 switch (type) {
5340 case FADE_IN:
5341 tint = 0.0;
5342 break;
5343 case FADE_OUT:
5344 tint = 1.0;
5345 break;
5346 default:
5347 weston_log("shell: invalid fade type\n");
5348 return;
5349 }
5350
5351 shell->fade.type = type;
5352
Jason Ekstranda7af7042013-10-12 22:38:11 -05005353 if (shell->fade.view == NULL) {
5354 shell->fade.view = shell_fade_create_surface(shell);
5355 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005356 return;
5357
Jason Ekstranda7af7042013-10-12 22:38:11 -05005358 shell->fade.view->alpha = 1.0 - tint;
5359 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005360 }
5361
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005362 if (shell->fade.view->output == NULL) {
5363 /* If the black view gets a NULL output, we lost the
5364 * last output and we'll just cancel the fade. This
5365 * happens when you close the last window under the
5366 * X11 or Wayland backends. */
5367 shell->locked = false;
5368 weston_surface_destroy(shell->fade.view->surface);
5369 shell->fade.view = NULL;
5370 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005371 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005372 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005373 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005375 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005376 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005377 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005378}
5379
5380static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005381do_shell_fade_startup(void *data)
5382{
5383 struct desktop_shell *shell = data;
5384
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005385 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005386 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005387 } else {
5388 weston_log("desktop shell: "
5389 "unexpected fade-in animation type %d\n",
5390 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005391 weston_surface_destroy(shell->fade.view->surface);
5392 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005393 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005394}
5395
5396static void
5397shell_fade_startup(struct desktop_shell *shell)
5398{
5399 struct wl_event_loop *loop;
5400
5401 if (!shell->fade.startup_timer)
5402 return;
5403
5404 wl_event_source_remove(shell->fade.startup_timer);
5405 shell->fade.startup_timer = NULL;
5406
5407 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5408 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5409}
5410
5411static int
5412fade_startup_timeout(void *data)
5413{
5414 struct desktop_shell *shell = data;
5415
5416 shell_fade_startup(shell);
5417 return 0;
5418}
5419
5420static void
5421shell_fade_init(struct desktop_shell *shell)
5422{
5423 /* Make compositor output all black, and wait for the desktop-shell
5424 * client to signal it is ready, then fade in. The timer triggers a
5425 * fade-in, in case the desktop-shell client takes too long.
5426 */
5427
5428 struct wl_event_loop *loop;
5429
Jason Ekstranda7af7042013-10-12 22:38:11 -05005430 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005431 weston_log("%s: warning: fade surface already exists\n",
5432 __func__);
5433 return;
5434 }
5435
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005436 if (shell->startup_animation_type == ANIMATION_NONE)
5437 return;
5438
Jason Ekstranda7af7042013-10-12 22:38:11 -05005439 shell->fade.view = shell_fade_create_surface(shell);
5440 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005441 return;
5442
Jason Ekstranda7af7042013-10-12 22:38:11 -05005443 weston_view_update_transform(shell->fade.view);
5444 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005445
5446 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5447 shell->fade.startup_timer =
5448 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5449 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5450}
5451
5452static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005453idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005454{
5455 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005456 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005457 struct weston_seat *seat;
5458
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005459 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005460 struct weston_touch *touch = weston_seat_get_touch(seat);
5461 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5462
5463 if (pointer)
5464 popup_grab_end(pointer);
5465 if (touch)
5466 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005467 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005468
5469 shell_fade(shell, FADE_OUT);
5470 /* lock() is called from shell_fade_done() */
5471}
5472
5473static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005474wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005475{
5476 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005477 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005478
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005479 unlock(shell);
5480}
5481
5482static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005483transform_handler(struct wl_listener *listener, void *data)
5484{
5485 struct weston_surface *surface = data;
5486 struct shell_surface *shsurf = get_shell_surface(surface);
5487 struct weston_view *view;;
5488 int x, y;
5489
5490 if (!shsurf || !shsurf->client->send_position)
5491 return;
5492
5493 view = shsurf->view;
5494 if (!view || !weston_view_is_mapped(view))
5495 return;
5496
5497 x = view->geometry.x;
5498 y = view->geometry.y;
5499
5500 shsurf->client->send_position(surface, x, y);
5501}
5502
5503static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005504center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005505{
Giulio Camuffob8366642013-04-25 13:57:46 +03005506 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005507 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005508
Jason Ekstranda7af7042013-10-12 22:38:11 -05005509 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005510
5511 x = output->x + (output->width - width) / 2 - surf_x / 2;
5512 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005513
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005514 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005515}
5516
5517static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005518weston_view_set_initial_position(struct weston_view *view,
5519 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005520{
5521 struct weston_compositor *compositor = shell->compositor;
5522 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005523 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005524 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005525 struct weston_output *output, *target_output = NULL;
5526 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005527 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005528
5529 /* As a heuristic place the new window on the same output as the
5530 * pointer. Falling back to the output containing 0, 0.
5531 *
5532 * TODO: Do something clever for touch too?
5533 */
5534 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005535 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5536
5537 if (pointer) {
5538 ix = wl_fixed_to_int(pointer->x);
5539 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005540 break;
5541 }
5542 }
5543
5544 wl_list_for_each(output, &compositor->output_list, link) {
5545 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5546 target_output = output;
5547 break;
5548 }
5549 }
5550
5551 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005552 weston_view_set_position(view, 10 + random() % 400,
5553 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005554 return;
5555 }
5556
5557 /* Valid range within output where the surface will still be onscreen.
5558 * If this is negative it means that the surface is bigger than
5559 * output.
5560 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005561 get_output_work_area(shell, target_output, &area);
5562
Derek Foremanf814c5d2015-04-15 12:23:54 -05005563 x = area.x;
5564 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005565 range_x = area.width - view->surface->width;
5566 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005567
Rob Bradford4cb88c72012-08-13 15:18:44 +01005568 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005569 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005570
5571 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005572 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005573
Jason Ekstranda7af7042013-10-12 22:38:11 -05005574 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005575}
5576
5577static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005578set_maximized_position(struct desktop_shell *shell,
5579 struct shell_surface *shsurf)
5580{
5581 int32_t surf_x, surf_y;
5582 pixman_rectangle32_t area;
5583
Jonny Lambd73c6942014-08-20 15:53:20 +02005584 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005585 if (shsurf->has_set_geometry) {
5586 surf_x = shsurf->geometry.x;
5587 surf_y = shsurf->geometry.y;
5588 } else {
5589 surface_subsurfaces_boundingbox(shsurf->surface,
5590 &surf_x, &surf_y, NULL, NULL);
5591 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005592
5593 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005594 area.x - surf_x,
5595 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005596}
5597
5598static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005599map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005600 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005601{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005602 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005603 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005604
Pekka Paalanen77346a62011-11-30 16:26:35 +02005605 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005606 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005607 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005608 if (shsurf->state.fullscreen) {
5609 center_on_output(shsurf->view, shsurf->fullscreen_output);
5610 shell_map_fullscreen(shsurf);
5611 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005612 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005613 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005614 weston_view_set_initial_position(shsurf->view, shell);
5615 }
Juan Zhao96879df2012-02-07 08:45:41 +08005616 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005617 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005618 if (shell_map_popup(shsurf) != 0)
5619 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005620 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005621 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005622 weston_view_set_position(shsurf->view,
5623 shsurf->view->geometry.x + sx,
5624 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005625 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005626 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005627 default:
5628 ;
5629 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005630
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005631 /* Surface stacking order, see also activate(). */
5632 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005633
Jason Ekstranda7af7042013-10-12 22:38:11 -05005634 if (shsurf->type != SHELL_SURFACE_NONE) {
5635 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005636 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005637 shsurf->surface->output = shsurf->output;
5638 shsurf->view->output = shsurf->output;
5639 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005640 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005641
Jason Ekstranda7af7042013-10-12 22:38:11 -05005642 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005643 /* XXX: xwayland's using the same fields for transient type */
5644 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005645 if (shsurf->transient.flags ==
5646 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5647 break;
5648 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005649 if (shsurf->state.relative &&
5650 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5651 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005652 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005653 break;
5654 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005655 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005656 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005657 case SHELL_SURFACE_POPUP:
5658 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005659 default:
5660 break;
5661 }
5662
Rafael Antognolli03b16592013-12-03 15:35:42 -02005663 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5664 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005665 {
5666 switch (shell->win_animation_type) {
5667 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005668 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005669 break;
5670 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005671 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005672 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005673 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005674 default:
5675 break;
5676 }
5677 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005678}
5679
5680static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005681configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005682 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005683{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005684 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005685 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005686
Pekka Paalanen77346a62011-11-30 16:26:35 +02005687 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005688
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005689 assert(shsurf);
5690
Rafael Antognolli03b16592013-12-03 15:35:42 -02005691 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005692 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005693 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005694 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005695 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005696 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005697 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005698
Alex Wu4539b082012-03-01 12:57:46 +08005699 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005700 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005701 wl_list_for_each(view, &surface->views, surface_link)
5702 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005703
Rafael Antognolli03b16592013-12-03 15:35:42 -02005704 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005705 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005706 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005707}
5708
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005709static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005710shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005711{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005712 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005713 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005714 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005715
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005716 assert(shsurf);
5717
5718 shell = shsurf->shell;
5719
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005720 if (!weston_surface_is_mapped(es) &&
5721 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005722 remove_popup_grab(shsurf);
5723 }
5724
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005725 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005726 return;
5727
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005728 if (shsurf->has_next_geometry) {
5729 shsurf->geometry = shsurf->next_geometry;
5730 shsurf->has_next_geometry = false;
5731 shsurf->has_set_geometry = true;
5732 } else if (!shsurf->has_set_geometry) {
5733 surface_subsurfaces_boundingbox(shsurf->surface,
5734 &shsurf->geometry.x,
5735 &shsurf->geometry.y,
5736 &shsurf->geometry.width,
5737 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005738 }
5739
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005740 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005741 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005742 type_changed = 1;
5743 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005744
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005745 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005746 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005747 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005748 shsurf->last_width != es->width ||
5749 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005750 float from_x, from_y;
5751 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005752
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005753 if (shsurf->resize_edges) {
5754 sx = 0;
5755 sy = 0;
5756 }
5757
5758 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5759 sx = shsurf->last_width - es->width;
5760 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5761 sy = shsurf->last_height - es->height;
5762
5763 shsurf->last_width = es->width;
5764 shsurf->last_height = es->height;
5765
Jason Ekstranda7af7042013-10-12 22:38:11 -05005766 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5767 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005768 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005769 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005770 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005771 }
5772}
5773
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005774static bool
5775check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5776{
5777 struct timespec now;
5778
5779 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5780 return false;
5781
5782 /*
5783 * If the shell helper client dies before the session has been
5784 * up for roughly 30 seconds, better just make Weston shut down,
5785 * because the user likely has no way to interact with the desktop
5786 * anyway.
5787 */
5788 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5789 weston_log("Error: %s apparently cannot run at all.\n",
5790 shell->client);
5791 weston_log_continue(STAMP_SPACE "Quitting...");
5792 wl_display_terminate(shell->compositor->wl_display);
5793
5794 return true;
5795 }
5796
5797 return false;
5798}
5799
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005800static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005801
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005802static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005803respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005804{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005805 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005806
5807 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5808 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005809 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005810 shell->child.deathstamp = time;
5811 shell->child.deathcount = 0;
5812 }
5813
5814 shell->child.deathcount++;
5815 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005816 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005817 return;
5818 }
5819
Pekka Paalanen826dc142014-08-27 12:11:53 +03005820 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005821 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005822}
5823
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005824static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005825desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5826{
5827 struct desktop_shell *shell;
5828
5829 shell = container_of(listener, struct desktop_shell,
5830 child.client_destroy_listener);
5831
Pekka Paalanen826dc142014-08-27 12:11:53 +03005832 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005833 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005834 /*
5835 * unbind_desktop_shell() will reset shell->child.desktop_shell
5836 * before the respawned process has a chance to create a new
5837 * desktop_shell object, because we are being called from the
5838 * wl_client destructor which destroys all wl_resources before
5839 * returning.
5840 */
5841
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005842 if (!check_desktop_shell_crash_too_early(shell))
5843 respawn_desktop_shell_process(shell);
5844
Pekka Paalanen826dc142014-08-27 12:11:53 +03005845 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005846}
5847
5848static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005849launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005850{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005851 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005852
Pekka Paalanen826dc142014-08-27 12:11:53 +03005853 shell->child.client = weston_client_start(shell->compositor,
5854 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005855
Arnaud Vrac42631192014-08-25 20:56:46 +02005856 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005857 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005858 return;
5859 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005860
5861 shell->child.client_destroy_listener.notify =
5862 desktop_shell_client_destroy;
5863 wl_client_add_destroy_listener(shell->child.client,
5864 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005865}
5866
5867static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005868handle_shell_client_destroy(struct wl_listener *listener, void *data)
5869{
5870 struct shell_client *sc =
5871 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005872 struct wl_resource *shsurf_resource;
5873 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005874
5875 if (sc->ping_timer)
5876 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005877
5878 /* Since we're about to free shell_client, we remove it from the
5879 * head of the surface list so we don't use that freed list node
5880 * during surface clean up later on.
5881 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005882 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5883 shsurf = wl_resource_get_user_data(shsurf_resource);
5884 shsurf->owner = NULL;
5885 }
Derek Foremane42d7542015-05-29 10:46:44 -05005886 wl_list_remove(&sc->surface_list);
5887
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005888 free(sc);
5889}
5890
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005891static struct shell_client *
5892shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5893 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005894{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005895 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005896
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005897 sc = zalloc(sizeof *sc);
5898 if (sc == NULL) {
5899 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005900 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005901 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005902
5903 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005904 if (sc->resource == NULL) {
5905 free(sc);
5906 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005907 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005908 }
5909
5910 sc->client = client;
5911 sc->shell = shell;
5912 sc->destroy_listener.notify = handle_shell_client_destroy;
5913 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005914 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005915
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005916 return sc;
5917}
5918
5919static void
5920bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5921{
5922 struct desktop_shell *shell = data;
5923 struct shell_client *sc;
5924
5925 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5926 if (sc)
5927 wl_resource_set_implementation(sc->resource,
5928 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005929 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005930}
5931
5932static void
5933bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5934{
5935 struct desktop_shell *shell = data;
5936 struct shell_client *sc;
5937
5938 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5939 if (sc)
5940 wl_resource_set_dispatcher(sc->resource,
5941 xdg_shell_unversioned_dispatch,
5942 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005943}
5944
5945static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005946unbind_desktop_shell(struct wl_resource *resource)
5947{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005948 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005949
5950 if (shell->locked)
5951 resume_desktop(shell);
5952
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005953 shell->child.desktop_shell = NULL;
5954 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005955}
5956
5957static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005958bind_desktop_shell(struct wl_client *client,
5959 void *data, uint32_t version, uint32_t id)
5960{
Tiago Vignattibe143262012-04-16 17:31:41 +03005961 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005962 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005963
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005964 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5965 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005966
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005967 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005968 wl_resource_set_implementation(resource,
5969 &desktop_shell_implementation,
5970 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005971 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005972 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005973 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005974
5975 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5976 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005977}
5978
Kristian Høgsberg07045392012-02-19 18:52:44 -05005979struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005980 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005981 struct weston_surface *current;
5982 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005983 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005984 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005985};
5986
5987static void
5988switcher_next(struct switcher *switcher)
5989{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005990 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005991 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005992 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005993 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005994
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005995 /* temporary re-display minimized surfaces */
5996 struct weston_view *tmp;
5997 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005998 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5999 weston_layer_entry_remove(&view->layer_link);
6000 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006001 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6002 *minimized = view;
6003 }
6004
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006005 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006006 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006007 if (shsurf &&
6008 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6009 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006010 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006011 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006012 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006013 next = view->surface;
6014 prev = view->surface;
6015 view->alpha = 0.25;
6016 weston_view_geometry_dirty(view);
6017 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006018 }
Alex Wu1659daa2012-04-01 20:13:09 +08006019
Jason Ekstranda7af7042013-10-12 22:38:11 -05006020 if (is_black_surface(view->surface, NULL)) {
6021 view->alpha = 0.25;
6022 weston_view_geometry_dirty(view);
6023 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006024 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006025 }
6026
6027 if (next == NULL)
6028 next = first;
6029
Alex Wu07b26062012-03-12 16:06:01 +08006030 if (next == NULL)
6031 return;
6032
Kristian Høgsberg07045392012-02-19 18:52:44 -05006033 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006034 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006035
6036 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006037 wl_list_for_each(view, &next->views, surface_link)
6038 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006039
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006040 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006041 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006042 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006043}
6044
6045static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006046switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047{
6048 struct switcher *switcher =
6049 container_of(listener, struct switcher, listener);
6050
6051 switcher_next(switcher);
6052}
6053
6054static void
Daniel Stone351eb612012-05-31 15:27:47 -04006055switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006056{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006057 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006058 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006059 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006060
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006061 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006062 if (is_focus_view(view))
6063 continue;
6064
Jason Ekstranda7af7042013-10-12 22:38:11 -05006065 view->alpha = 1.0;
6066 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006067 }
6068
Alex Wu07b26062012-03-12 16:06:01 +08006069 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006070 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006071 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006072 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006073 weston_keyboard_end_grab(keyboard);
6074 if (keyboard->input_method_resource)
6075 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006076
6077 /* re-hide surfaces that were temporary shown during the switch */
6078 struct weston_view **minimized;
6079 wl_array_for_each(minimized, &switcher->minimized_array) {
6080 /* with the exception of the current selected */
6081 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006082 weston_layer_entry_remove(&(*minimized)->layer_link);
6083 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006084 weston_view_damage_below(*minimized);
6085 }
6086 }
6087 wl_array_release(&switcher->minimized_array);
6088
Kristian Høgsberg07045392012-02-19 18:52:44 -05006089 free(switcher);
6090}
6091
6092static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006093switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006094 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006095{
6096 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006097 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006098
Daniel Stonec9785ea2012-05-30 16:31:52 +01006099 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006100 switcher_next(switcher);
6101}
6102
6103static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006104switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006105 uint32_t mods_depressed, uint32_t mods_latched,
6106 uint32_t mods_locked, uint32_t group)
6107{
6108 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006109 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006110
Daniel Stone351eb612012-05-31 15:27:47 -04006111 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6112 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006113}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006114
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006115static void
6116switcher_cancel(struct weston_keyboard_grab *grab)
6117{
6118 struct switcher *switcher = container_of(grab, struct switcher, grab);
6119
6120 switcher_destroy(switcher);
6121}
6122
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006123static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006124 switcher_key,
6125 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006126 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006127};
6128
6129static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006130switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6131 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006132{
Tiago Vignattibe143262012-04-16 17:31:41 +03006133 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006134 struct switcher *switcher;
6135
6136 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006137 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006138 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006139 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006140 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006141 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006142
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006143 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006144 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006145 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006146 weston_keyboard_start_grab(keyboard, &switcher->grab);
6147 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006148 switcher_next(switcher);
6149}
6150
Pekka Paalanen3c647232011-12-22 13:43:43 +02006151static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006152backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6153 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006154{
6155 struct weston_compositor *compositor = data;
6156 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006157 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006158
6159 /* TODO: we're limiting to simple use cases, where we assume just
6160 * control on the primary display. We'd have to extend later if we
6161 * ever get support for setting backlights on random desktop LCD
6162 * panels though */
6163 output = get_default_output(compositor);
6164 if (!output)
6165 return;
6166
6167 if (!output->set_backlight)
6168 return;
6169
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006170 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6171 backlight_new = output->backlight_current - 25;
6172 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6173 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006174
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006175 if (backlight_new < 5)
6176 backlight_new = 5;
6177 if (backlight_new > 255)
6178 backlight_new = 255;
6179
6180 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006181 output->set_backlight(output, output->backlight_current);
6182}
6183
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006184static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006185force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6186 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006187{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006188 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006189 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006190 struct desktop_shell *shell = data;
6191 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006192 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006193
Derek Foreman8ae2db52015-07-15 13:00:36 -05006194 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006195 if (!focus_surface)
6196 return;
6197
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006198 wl_signal_emit(&compositor->kill_signal, focus_surface);
6199
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006200 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006201 wl_client_get_credentials(client, &pid, NULL, NULL);
6202
6203 /* Skip clients that we launched ourselves (the credentials of
6204 * the socketpair is ours) */
6205 if (pid == getpid())
6206 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006207
Daniel Stone325fc2d2012-05-30 16:31:58 +01006208 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006209}
6210
6211static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006212workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006213 uint32_t key, void *data)
6214{
6215 struct desktop_shell *shell = data;
6216 unsigned int new_index = shell->workspaces.current;
6217
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006218 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006219 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006220 if (new_index != 0)
6221 new_index--;
6222
6223 change_workspace(shell, new_index);
6224}
6225
6226static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006227workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006228 uint32_t key, void *data)
6229{
6230 struct desktop_shell *shell = data;
6231 unsigned int new_index = shell->workspaces.current;
6232
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006233 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006234 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006235 if (new_index < shell->workspaces.num - 1)
6236 new_index++;
6237
6238 change_workspace(shell, new_index);
6239}
6240
6241static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006242workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006243 uint32_t key, void *data)
6244{
6245 struct desktop_shell *shell = data;
6246 unsigned int new_index;
6247
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006248 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006249 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006250 new_index = key - KEY_F1;
6251 if (new_index >= shell->workspaces.num)
6252 new_index = shell->workspaces.num - 1;
6253
6254 change_workspace(shell, new_index);
6255}
6256
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006257static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006258workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6259 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006260{
6261 struct desktop_shell *shell = data;
6262 unsigned int new_index = shell->workspaces.current;
6263
6264 if (shell->locked)
6265 return;
6266
6267 if (new_index != 0)
6268 new_index--;
6269
Derek Foreman8ae2db52015-07-15 13:00:36 -05006270 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006271}
6272
6273static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006274workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6275 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006276{
6277 struct desktop_shell *shell = data;
6278 unsigned int new_index = shell->workspaces.current;
6279
6280 if (shell->locked)
6281 return;
6282
6283 if (new_index < shell->workspaces.num - 1)
6284 new_index++;
6285
Derek Foreman8ae2db52015-07-15 13:00:36 -05006286 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006287}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006288
6289static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006290shell_reposition_view_on_output_destroy(struct weston_view *view)
6291{
6292 struct weston_output *output, *first_output;
6293 struct weston_compositor *ec = view->surface->compositor;
6294 struct shell_surface *shsurf;
6295 float x, y;
6296 int visible;
6297
6298 x = view->geometry.x;
6299 y = view->geometry.y;
6300
6301 /* At this point the destroyed output is not in the list anymore.
6302 * If the view is still visible somewhere, we leave where it is,
6303 * otherwise, move it to the first output. */
6304 visible = 0;
6305 wl_list_for_each(output, &ec->output_list, link) {
6306 if (pixman_region32_contains_point(&output->region,
6307 x, y, NULL)) {
6308 visible = 1;
6309 break;
6310 }
6311 }
6312
6313 if (!visible) {
6314 first_output = container_of(ec->output_list.next,
6315 struct weston_output, link);
6316
6317 x = first_output->x + first_output->width / 4;
6318 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006319
6320 weston_view_set_position(view, x, y);
6321 } else {
6322 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006323 }
6324
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006325
6326 shsurf = get_shell_surface(view->surface);
6327
6328 if (shsurf) {
6329 shsurf->saved_position_valid = false;
6330 shsurf->next_state.maximized = false;
6331 shsurf->next_state.fullscreen = false;
6332 shsurf->state_changed = true;
6333 }
6334}
6335
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006336void
6337shell_for_each_layer(struct desktop_shell *shell,
6338 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006339{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006340 struct workspace **ws;
6341
6342 func(shell, &shell->fullscreen_layer, data);
6343 func(shell, &shell->panel_layer, data);
6344 func(shell, &shell->background_layer, data);
6345 func(shell, &shell->lock_layer, data);
6346 func(shell, &shell->input_panel_layer, data);
6347
6348 wl_array_for_each(ws, &shell->workspaces.array)
6349 func(shell, &(*ws)->layer, data);
6350}
6351
6352static void
6353shell_output_destroy_move_layer(struct desktop_shell *shell,
6354 struct weston_layer *layer,
6355 void *data)
6356{
6357 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006358 struct weston_view *view;
6359
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006360 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006361 if (view->output != output)
6362 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006363
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006364 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006365 }
6366}
6367
6368static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006369handle_output_destroy(struct wl_listener *listener, void *data)
6370{
6371 struct shell_output *output_listener =
6372 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006373 struct weston_output *output = output_listener->output;
6374 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006375
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006376 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006377
Xiong Zhang6b481422013-10-23 13:58:32 +08006378 wl_list_remove(&output_listener->destroy_listener.link);
6379 wl_list_remove(&output_listener->link);
6380 free(output_listener);
6381}
6382
6383static void
6384create_shell_output(struct desktop_shell *shell,
6385 struct weston_output *output)
6386{
6387 struct shell_output *shell_output;
6388
6389 shell_output = zalloc(sizeof *shell_output);
6390 if (shell_output == NULL)
6391 return;
6392
6393 shell_output->output = output;
6394 shell_output->shell = shell;
6395 shell_output->destroy_listener.notify = handle_output_destroy;
6396 wl_signal_add(&output->destroy_signal,
6397 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006398 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006399}
6400
6401static void
6402handle_output_create(struct wl_listener *listener, void *data)
6403{
6404 struct desktop_shell *shell =
6405 container_of(listener, struct desktop_shell, output_create_listener);
6406 struct weston_output *output = (struct weston_output *)data;
6407
6408 create_shell_output(shell, output);
6409}
6410
6411static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006412handle_output_move_layer(struct desktop_shell *shell,
6413 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006414{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006415 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006416 struct weston_view *view;
6417 float x, y;
6418
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006419 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006420 if (view->output != output)
6421 continue;
6422
6423 x = view->geometry.x + output->move_x;
6424 y = view->geometry.y + output->move_y;
6425 weston_view_set_position(view, x, y);
6426 }
6427}
6428
6429static void
6430handle_output_move(struct wl_listener *listener, void *data)
6431{
6432 struct desktop_shell *shell;
6433
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006434 shell = container_of(listener, struct desktop_shell,
6435 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006436
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006437 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006438}
6439
6440static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006441setup_output_destroy_handler(struct weston_compositor *ec,
6442 struct desktop_shell *shell)
6443{
6444 struct weston_output *output;
6445
6446 wl_list_init(&shell->output_list);
6447 wl_list_for_each(output, &ec->output_list, link)
6448 create_shell_output(shell, output);
6449
6450 shell->output_create_listener.notify = handle_output_create;
6451 wl_signal_add(&ec->output_created_signal,
6452 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006453
6454 shell->output_move_listener.notify = handle_output_move;
6455 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006456}
6457
6458static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006459shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006460{
Tiago Vignattibe143262012-04-16 17:31:41 +03006461 struct desktop_shell *shell =
6462 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006463 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006464 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006465
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006466 /* Force state to unlocked so we don't try to fade */
6467 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006468
6469 if (shell->child.client) {
6470 /* disable respawn */
6471 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006472 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006473 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006474
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006475 wl_list_remove(&shell->idle_listener.link);
6476 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006477 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006478
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006479 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006480 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006481
Xiong Zhang6b481422013-10-23 13:58:32 +08006482 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6483 wl_list_remove(&shell_output->destroy_listener.link);
6484 wl_list_remove(&shell_output->link);
6485 free(shell_output);
6486 }
6487
6488 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006489 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006490
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006491 wl_array_for_each(ws, &shell->workspaces.array)
6492 workspace_destroy(*ws);
6493 wl_array_release(&shell->workspaces.array);
6494
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006495 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006496 free(shell);
6497}
6498
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006499static void
6500shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6501{
6502 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006503 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006504
Bob Ham744e6532016-01-12 10:21:48 +00006505 if (shell->allow_zap)
6506 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6507 MODIFIER_CTRL | MODIFIER_ALT,
6508 terminate_binding, ec);
6509
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006510 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006511 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6512 click_to_activate_binding,
6513 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006514 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6515 click_to_activate_binding,
6516 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006517 weston_compositor_add_touch_binding(ec, 0,
6518 touch_to_activate_binding,
6519 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006520 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6521 MODIFIER_SUPER | MODIFIER_ALT,
6522 surface_opacity_binding, NULL);
6523 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6524 MODIFIER_SUPER, zoom_axis_binding,
6525 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006526 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6527 backlight_binding, ec);
6528 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6529 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006530
6531 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006532 if (shell->exposay_modifier)
6533 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6534 exposay_binding, shell);
6535
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006536 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006537 if (!mod)
6538 return;
6539
Daniel Stone325fc2d2012-05-30 16:31:58 +01006540 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6541 zoom_key_binding, NULL);
6542 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6543 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006544 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6545 maximize_binding, NULL);
6546 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6547 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006548 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6549 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006550 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006551 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006552 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006553 weston_compositor_add_button_binding(ec, BTN_LEFT,
6554 mod | MODIFIER_SHIFT,
6555 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006556
6557 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006558 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006559 rotate_binding, NULL);
6560
Daniel Stone325fc2d2012-05-30 16:31:58 +01006561 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6562 shell);
6563 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6564 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006565 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6566 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006567 weston_compositor_add_key_binding(ec, KEY_K, mod,
6568 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006569 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6570 workspace_up_binding, shell);
6571 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6572 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006573 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6574 workspace_move_surface_up_binding,
6575 shell);
6576 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6577 workspace_move_surface_down_binding,
6578 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006579
6580 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6581 if (shell->workspaces.num > 1) {
6582 num_workspace_bindings = shell->workspaces.num;
6583 if (num_workspace_bindings > 6)
6584 num_workspace_bindings = 6;
6585 for (i = 0; i < num_workspace_bindings; i++)
6586 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6587 workspace_f_binding,
6588 shell);
6589 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006590
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006591 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006592}
6593
Jason Ekstrand024177c2014-04-21 19:42:58 -05006594static void
6595handle_seat_created(struct wl_listener *listener, void *data)
6596{
6597 struct weston_seat *seat = data;
6598
6599 create_shell_seat(seat);
6600}
6601
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006602WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006603module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006604 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006605{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006606 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006607 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006608 struct workspace **pws;
6609 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006610 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006611
Peter Huttererf3d62272013-08-08 11:57:05 +10006612 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006613 if (shell == NULL)
6614 return -1;
6615
Kristian Høgsberg75840622011-09-06 13:48:16 -04006616 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006617
6618 shell->destroy_listener.notify = shell_destroy;
6619 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006620 shell->idle_listener.notify = idle_handler;
6621 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6622 shell->wake_listener.notify = wake_handler;
6623 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006624 shell->transform_listener.notify = transform_handler;
6625 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006626
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006627 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006628 ec->shell_interface.create_shell_surface = create_shell_surface;
6629 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006630 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006631 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006632 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006633 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006634 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006635 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006636 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006637 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006638 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006639 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006640
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006641 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6642 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006643 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6644 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006645 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006646
6647 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006648 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006649
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006650 if (input_panel_setup(shell) < 0)
6651 return -1;
6652
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006653 shell->text_backend = text_backend_init(ec);
6654 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006655 return -1;
6656
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006657 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006658
Daniel Stonedf8133b2013-11-19 11:37:14 +01006659 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6660 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6661
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006662 for (i = 0; i < shell->workspaces.num; i++) {
6663 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6664 if (pws == NULL)
6665 return -1;
6666
6667 *pws = workspace_create();
6668 if (*pws == NULL)
6669 return -1;
6670 }
6671 activate_workspace(shell, 0);
6672
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006673 weston_layer_init(&shell->minimized_layer, NULL);
6674
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006675 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006676 wl_list_init(&shell->workspaces.animation.link);
6677 shell->workspaces.animation.frame = animate_workspace_change_frame;
6678
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006679 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006680 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006681 return -1;
6682
Rafael Antognollie2a34552013-12-03 15:35:45 -02006683 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6684 shell, bind_xdg_shell) == NULL)
6685 return -1;
6686
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006687 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006688 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006689 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006690 return -1;
6691
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006692 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006693
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006694 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006695
Xiong Zhang6b481422013-10-23 13:58:32 +08006696 setup_output_destroy_handler(ec, shell);
6697
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006698 loop = wl_display_get_event_loop(ec->wl_display);
6699 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006700
Jason Ekstrand024177c2014-04-21 19:42:58 -05006701 wl_list_for_each(seat, &ec->seat_list, link)
6702 handle_seat_created(NULL, seat);
6703 shell->seat_create_listener.notify = handle_seat_created;
6704 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006705
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006706 screenshooter_create(ec);
6707
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006708 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006709
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006710 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006711
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006712 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6713
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006714 return 0;
6715}