blob: 1c39a12d111baf072350f2e50fc165d617f14149 [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
2564 if (output_resource)
2565 output = wl_resource_get_user_data(output_resource);
2566 else
2567 output = NULL;
2568
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002569 shell_surface_set_parent(shsurf, NULL);
2570
Rafael Antognolli03b16592013-12-03 15:35:42 -02002571 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002572 shsurf->next_state.fullscreen = true;
2573 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002574 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002575}
2576
2577static void
2578set_popup(struct shell_surface *shsurf,
2579 struct weston_surface *parent,
2580 struct weston_seat *seat,
2581 uint32_t serial,
2582 int32_t x,
2583 int32_t y)
2584{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002585 assert(parent != NULL);
2586
Philip Withnallbecb77e2013-11-25 18:01:30 +00002587 shsurf->popup.shseat = get_shell_seat(seat);
2588 shsurf->popup.serial = serial;
2589 shsurf->popup.x = x;
2590 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002591
Pekka Paalanenb5026542014-11-12 15:09:24 +02002592 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002593}
2594
2595static void
2596shell_surface_set_popup(struct wl_client *client,
2597 struct wl_resource *resource,
2598 struct wl_resource *seat_resource,
2599 uint32_t serial,
2600 struct wl_resource *parent_resource,
2601 int32_t x, int32_t y, uint32_t flags)
2602{
2603 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002604 struct weston_surface *parent =
2605 wl_resource_get_user_data(parent_resource);
2606
2607 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002608
Rafael Antognolli03b16592013-12-03 15:35:42 -02002609 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002610 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002611 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002612 wl_resource_get_user_data(seat_resource),
2613 serial, x, y);
2614}
2615
2616static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002617unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002618{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002619 /* undo all maximized things here */
2620 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002621
2622 if (shsurf->saved_position_valid)
2623 weston_view_set_position(shsurf->view,
2624 shsurf->saved_x, shsurf->saved_y);
2625 else
2626 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002627
2628 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002629 wl_list_insert(&shsurf->view->geometry.transformation_list,
2630 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002631 shsurf->saved_rotation_valid = false;
2632 }
2633
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002634 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002635}
2636
Philip Withnallbecb77e2013-11-25 18:01:30 +00002637static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002638set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002639{
2640 struct shell_surface *shsurf;
2641 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002642 struct weston_view *view;
2643
2644 view = get_default_view(surface);
2645 if (!view)
2646 return;
2647
2648 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2649
2650 shsurf = get_shell_surface(surface);
2651 current_ws = get_current_workspace(shsurf->shell);
2652
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002653 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002654 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002655
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002656 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002657 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002658
2659 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002660 weston_view_damage_below(view);
2661}
2662
2663static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002664shell_surface_set_maximized(struct wl_client *client,
2665 struct wl_resource *resource,
2666 struct wl_resource *output_resource)
2667{
2668 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2669 struct weston_output *output;
2670
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002671 surface_clear_next_states(shsurf);
2672 shsurf->next_state.maximized = true;
2673 shsurf->state_changed = true;
2674
Pekka Paalanenb5026542014-11-12 15:09:24 +02002675 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002676 shell_surface_set_parent(shsurf, NULL);
2677
Philip Withnallbecb77e2013-11-25 18:01:30 +00002678 if (output_resource)
2679 output = wl_resource_get_user_data(output_resource);
2680 else
2681 output = NULL;
2682
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002683 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002684
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002685 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002686}
2687
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002688/* This is only ever called from set_surface_type(), so there’s no need to
2689 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002690static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002691reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002692{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002693 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002694 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002695 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002696 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002697
Pekka Paalanen98262232011-12-01 10:42:22 +02002698 return 0;
2699}
2700
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002701static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002702set_full_output(struct shell_surface *shsurf)
2703{
2704 shsurf->saved_x = shsurf->view->geometry.x;
2705 shsurf->saved_y = shsurf->view->geometry.y;
2706 shsurf->saved_position_valid = true;
2707
2708 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2709 wl_list_remove(&shsurf->rotation.transform.link);
2710 wl_list_init(&shsurf->rotation.transform.link);
2711 weston_view_geometry_dirty(shsurf->view);
2712 shsurf->saved_rotation_valid = true;
2713 }
2714}
2715
2716static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002717set_surface_type(struct shell_surface *shsurf)
2718{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002719 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002721
Philip Withnallbecb77e2013-11-25 18:01:30 +00002722 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002723
Rafael Antognolli03b16592013-12-03 15:35:42 -02002724 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002725 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002726
2727 switch (shsurf->type) {
2728 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002729 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002730 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002731 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002732 weston_view_set_position(shsurf->view,
2733 pev->geometry.x + shsurf->transient.x,
2734 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002735 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002736 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002737
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002738 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 weston_view_set_position(shsurf->view, shsurf->transient.x,
2740 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002741 break;
2742
Philip Withnall0f640e22013-11-25 18:01:31 +00002743 case SHELL_SURFACE_POPUP:
2744 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002745 default:
2746 break;
2747 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002748
2749 /* Update the surface’s layer. */
2750 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002751}
2752
Tiago Vignattibe143262012-04-16 17:31:41 +03002753static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002754shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002755{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002756 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002757}
2758
Pekka Paalanen8274d902014-08-06 19:36:51 +03002759static int
2760black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2761{
2762 struct weston_surface *fs_surface = surface->configure_private;
2763 int n;
2764 int rem;
2765 int ret;
2766
2767 n = snprintf(buf, len, "black background surface for ");
2768 if (n < 0)
2769 return n;
2770
2771 rem = (int)len - n;
2772 if (rem < 0)
2773 rem = 0;
2774
2775 if (fs_surface->get_label)
2776 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2777 else
2778 ret = snprintf(buf + n, rem, "<unknown>");
2779
2780 if (ret < 0)
2781 return n;
2782
2783 return n + ret;
2784}
2785
Alex Wu21858432012-04-01 20:13:08 +08002786static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002787black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002788
Jason Ekstranda7af7042013-10-12 22:38:11 -05002789static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002790create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002791 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002792 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002793{
2794 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002795 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002796
2797 surface = weston_surface_create(ec);
2798 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002799 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002800 return NULL;
2801 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802 view = weston_view_create(surface);
2803 if (surface == NULL) {
2804 weston_log("no memory\n");
2805 weston_surface_destroy(surface);
2806 return NULL;
2807 }
Alex Wu4539b082012-03-01 12:57:46 +08002808
Alex Wu21858432012-04-01 20:13:08 +08002809 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002810 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002811 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002812 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002813 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002814 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002815 pixman_region32_fini(&surface->input);
2816 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002817
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002818 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002819 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820
2821 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002822}
2823
Philip Withnalled8f1a92013-11-25 18:01:43 +00002824static void
2825shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2826{
2827 struct weston_output *output = shsurf->fullscreen_output;
2828
Rafael Antognolli03b16592013-12-03 15:35:42 -02002829 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002830
2831 if (!shsurf->fullscreen.black_view)
2832 shsurf->fullscreen.black_view =
2833 create_black_surface(shsurf->surface->compositor,
2834 shsurf->surface,
2835 output->x, output->y,
2836 output->width,
2837 output->height);
2838
2839 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002840 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2841 weston_layer_entry_insert(&shsurf->view->layer_link,
2842 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002843 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2844 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002845
2846 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002847}
2848
Alex Wu4539b082012-03-01 12:57:46 +08002849/* Create black surface and append it to the associated fullscreen surface.
2850 * Handle size dismatch and positioning according to the method. */
2851static void
2852shell_configure_fullscreen(struct shell_surface *shsurf)
2853{
2854 struct weston_output *output = shsurf->fullscreen_output;
2855 struct weston_surface *surface = shsurf->surface;
2856 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002857 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002858 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002859
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002860 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2861 restore_output_mode(output);
2862
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002863 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002864 weston_layer_entry_remove(&shsurf->view->layer_link);
2865 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002866
Philip Withnalled8f1a92013-11-25 18:01:43 +00002867 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002868
Jason Ekstranda7af7042013-10-12 22:38:11 -05002869 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002870 &surf_width, &surf_height);
2871
Alex Wu4539b082012-03-01 12:57:46 +08002872 switch (shsurf->fullscreen.type) {
2873 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002874 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002875 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002876 break;
2877 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002878 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002879 if (output->width == surf_width &&
2880 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002881 weston_view_set_position(shsurf->view,
2882 output->x - surf_x,
2883 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002884 break;
2885 }
2886
Alex Wu4539b082012-03-01 12:57:46 +08002887 matrix = &shsurf->fullscreen.transform.matrix;
2888 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002889
Scott Moreau1bad5db2012-08-18 01:04:05 -06002890 output_aspect = (float) output->width /
2891 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002892 /* XXX: Use surf_width and surf_height here? */
2893 surface_aspect = (float) surface->width /
2894 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002895 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002896 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002897 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002898 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002899 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002900 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002901
Alex Wu4539b082012-03-01 12:57:46 +08002902 weston_matrix_scale(matrix, scale, scale, 1);
2903 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002904 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002905 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002906 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2907 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002908 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002909
Alex Wu4539b082012-03-01 12:57:46 +08002910 break;
2911 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002912 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002913 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002914 surf_width * surface->buffer_viewport.buffer.scale,
2915 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002916 shsurf->fullscreen.framerate};
2917
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002918 if (weston_output_mode_switch_to_temporary(output, &mode,
2919 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002920 weston_view_set_position(shsurf->view,
2921 output->x - surf_x,
2922 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002923 shsurf->fullscreen.black_view->surface->width = output->width;
2924 shsurf->fullscreen.black_view->surface->height = output->height;
2925 weston_view_set_position(shsurf->fullscreen.black_view,
2926 output->x - surf_x,
2927 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002928 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002929 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002930 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002931 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002932 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002933 }
Alex Wubd3354b2012-04-17 17:20:49 +08002934 }
Alex Wu4539b082012-03-01 12:57:46 +08002935 break;
2936 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002937 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002938 break;
2939 default:
2940 break;
2941 }
2942}
2943
Alex Wu4539b082012-03-01 12:57:46 +08002944static void
2945shell_map_fullscreen(struct shell_surface *shsurf)
2946{
Alex Wubd3354b2012-04-17 17:20:49 +08002947 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002948}
2949
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002950static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002951set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2952{
2953 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002954 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002955 shsurf->transient.x = x;
2956 shsurf->transient.y = y;
2957 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002958
2959 shell_surface_set_parent(shsurf, NULL);
2960
Pekka Paalanenb5026542014-11-12 15:09:24 +02002961 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002962 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002963}
2964
Kristian Høgsberg47792412014-05-04 13:47:06 -07002965static void
2966shell_interface_set_fullscreen(struct shell_surface *shsurf,
2967 uint32_t method,
2968 uint32_t framerate,
2969 struct weston_output *output)
2970{
2971 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002972 shsurf->next_state.fullscreen = true;
2973 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002974
2975 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002976}
2977
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002978static struct weston_output *
2979get_focused_output(struct weston_compositor *compositor)
2980{
2981 struct weston_seat *seat;
2982 struct weston_output *output = NULL;
2983
2984 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002985 struct weston_touch *touch = weston_seat_get_touch(seat);
2986 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2987 struct weston_keyboard *keyboard =
2988 weston_seat_get_keyboard(seat);
2989
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002990 /* Priority has touch focus, then pointer and
2991 * then keyboard focus. We should probably have
2992 * three for loops and check frist for touch,
2993 * then for pointer, etc. but unless somebody has some
2994 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002995 if (touch && touch->focus)
2996 output = touch->focus->output;
2997 else if (pointer && pointer->focus)
2998 output = pointer->focus->output;
2999 else if (keyboard && keyboard->focus)
3000 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003001
3002 if (output)
3003 break;
3004 }
3005
3006 return output;
3007}
3008
3009static void
3010shell_interface_set_maximized(struct shell_surface *shsurf)
3011{
3012 struct weston_output *output;
3013
3014 surface_clear_next_states(shsurf);
3015 shsurf->next_state.maximized = true;
3016 shsurf->state_changed = true;
3017 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3018
3019 if (!weston_surface_is_mapped(shsurf->surface))
3020 output = get_focused_output(shsurf->surface->compositor);
3021 else
3022 output = shsurf->surface->output;
3023
3024 shell_surface_set_output(shsurf, output);
3025 send_configure_for_surface(shsurf);
3026}
3027
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003028static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003029shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003030{
Derek Foremane4d6c832015-08-05 14:48:11 -05003031 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003032}
3033
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003034static int
3035shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003036 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003037 uint32_t edges)
3038{
Derek Foremane4d6c832015-08-05 14:48:11 -05003039 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003040}
3041
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003042static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003043
3044static void
3045destroy_shell_seat(struct wl_listener *listener, void *data)
3046{
3047 struct shell_seat *shseat =
3048 container_of(listener,
3049 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003050 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003051
3052 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003053 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003054 shseat->popup_grab.client = NULL;
3055
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003056 wl_list_for_each_safe(shsurf, next,
3057 &shseat->popup_grab.surfaces_list,
3058 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003059 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003060 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003062 }
3063
3064 wl_list_remove(&shseat->seat_destroy_listener.link);
3065 free(shseat);
3066}
3067
Jason Ekstrand024177c2014-04-21 19:42:58 -05003068static void
3069shell_seat_caps_changed(struct wl_listener *listener, void *data)
3070{
Derek Foreman1281a362015-07-31 16:55:32 -05003071 struct weston_keyboard *keyboard;
3072 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003073 struct shell_seat *seat;
3074
3075 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003076 keyboard = weston_seat_get_keyboard(seat->seat);
3077 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003078
Derek Foreman1281a362015-07-31 16:55:32 -05003079 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003080 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003081 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003082 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003083 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003084 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003085 wl_list_init(&seat->keyboard_focus_listener.link);
3086 }
3087
Derek Foreman1281a362015-07-31 16:55:32 -05003088 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003089 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003090 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003091 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003092 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003093 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003094 wl_list_init(&seat->pointer_focus_listener.link);
3095 }
3096}
3097
Giulio Camuffo5085a752013-03-25 21:42:45 +01003098static struct shell_seat *
3099create_shell_seat(struct weston_seat *seat)
3100{
3101 struct shell_seat *shseat;
3102
3103 shseat = calloc(1, sizeof *shseat);
3104 if (!shseat) {
3105 weston_log("no memory to allocate shell seat\n");
3106 return NULL;
3107 }
3108
3109 shseat->seat = seat;
3110 wl_list_init(&shseat->popup_grab.surfaces_list);
3111
3112 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3113 wl_signal_add(&seat->destroy_signal,
3114 &shseat->seat_destroy_listener);
3115
Jason Ekstrand024177c2014-04-21 19:42:58 -05003116 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3117 wl_list_init(&shseat->keyboard_focus_listener.link);
3118
3119 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3120 wl_list_init(&shseat->pointer_focus_listener.link);
3121
3122 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3123 wl_signal_add(&seat->updated_caps_signal,
3124 &shseat->caps_changed_listener);
3125 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3126
Giulio Camuffo5085a752013-03-25 21:42:45 +01003127 return shseat;
3128}
3129
3130static struct shell_seat *
3131get_shell_seat(struct weston_seat *seat)
3132{
3133 struct wl_listener *listener;
3134
3135 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003136 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003137
3138 return container_of(listener,
3139 struct shell_seat, seat_destroy_listener);
3140}
3141
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003142static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003143popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003144{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003145 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003146 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003147 struct shell_seat *shseat =
3148 container_of(grab, struct shell_seat, popup_grab.grab);
3149 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003150 wl_fixed_t sx, sy;
3151
Jason Ekstranda7af7042013-10-12 22:38:11 -05003152 view = weston_compositor_pick_view(pointer->seat->compositor,
3153 pointer->x, pointer->y,
3154 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003155
Jason Ekstranda7af7042013-10-12 22:38:11 -05003156 if (view && view->surface->resource &&
3157 wl_resource_get_client(view->surface->resource) == client) {
3158 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003159 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003160 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003161 }
3162}
3163
3164static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003165popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003166 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003167{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003168 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003169 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003170 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003171 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003172 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003173
Jonas Ådahl61917c82014-08-11 22:44:02 +02003174 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003175 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003176 weston_view_from_global_fixed(pointer->focus, x, y,
3177 &pointer->sx, &pointer->sy);
3178 }
3179
Jonas Ådahld2510102014-10-05 21:39:14 +02003180 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003181
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003182 if (!pointer->focus_client)
3183 return;
3184
3185 resource_list = &pointer->focus_client->pointer_resources;
3186 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003187 weston_view_from_global_fixed(pointer->focus,
3188 pointer->x, pointer->y,
3189 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003190 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003191 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003192}
3193
3194static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003195popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003196 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003197{
3198 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003199 struct shell_seat *shseat =
3200 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003201 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003202 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003203 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003204 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003205
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003206 if (grab->pointer->focus_client)
3207 resource_list = &grab->pointer->focus_client->pointer_resources;
3208 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003209 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003210 wl_resource_for_each(resource, resource_list) {
3211 wl_pointer_send_button(resource, serial,
3212 time, button, state);
3213 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003214 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003215 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003216 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003217 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003218 }
3219
Daniel Stone4dbadb12012-05-30 16:31:51 +01003220 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003221 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003222}
3223
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003224static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003225popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003226 uint32_t time,
3227 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003228{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003229 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003230}
3231
3232static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003233popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3234{
3235 weston_pointer_send_axis_source(grab->pointer, source);
3236}
3237
3238static void
3239popup_grab_frame(struct weston_pointer_grab *grab)
3240{
3241 weston_pointer_send_frame(grab->pointer);
3242}
3243
3244static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003245popup_grab_cancel(struct weston_pointer_grab *grab)
3246{
3247 popup_grab_end(grab->pointer);
3248}
3249
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003250static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003251 popup_grab_focus,
3252 popup_grab_motion,
3253 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003254 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003255 popup_grab_axis_source,
3256 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003257 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003258};
3259
3260static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003261touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003262 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003263{
3264 struct wl_resource *resource;
3265 struct shell_seat *shseat =
3266 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3267 struct wl_display *display = shseat->seat->compositor->wl_display;
3268 uint32_t serial;
3269 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003270 wl_fixed_t sx, sy;
3271
3272 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003273
3274 resource_list = &grab->touch->focus_resource_list;
3275 if (!wl_list_empty(resource_list)) {
3276 serial = wl_display_get_serial(display);
3277 wl_resource_for_each(resource, resource_list) {
3278 wl_touch_send_down(resource, serial, time,
3279 grab->touch->focus->surface->resource,
3280 touch_id, sx, sy);
3281 }
3282 }
3283}
3284
3285static void
3286touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3287{
3288 struct wl_resource *resource;
3289 struct shell_seat *shseat =
3290 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3291 struct wl_display *display = shseat->seat->compositor->wl_display;
3292 uint32_t serial;
3293 struct wl_list *resource_list;
3294
3295 resource_list = &grab->touch->focus_resource_list;
3296 if (!wl_list_empty(resource_list)) {
3297 serial = wl_display_get_serial(display);
3298 wl_resource_for_each(resource, resource_list) {
3299 wl_touch_send_up(resource, serial, time, touch_id);
3300 }
3301 }
3302}
3303
3304static void
3305touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003306 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003307{
3308 struct wl_resource *resource;
3309 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003310 wl_fixed_t sx, sy;
3311
3312 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003313
3314 resource_list = &grab->touch->focus_resource_list;
3315 if (!wl_list_empty(resource_list)) {
3316 wl_resource_for_each(resource, resource_list) {
3317 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3318 }
3319 }
3320}
3321
3322static void
3323touch_popup_grab_frame(struct weston_touch_grab *grab)
3324{
3325}
3326
3327static void
3328touch_popup_grab_cancel(struct weston_touch_grab *grab)
3329{
3330 touch_popup_grab_end(grab->touch);
3331}
3332
3333static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3334 touch_popup_grab_down,
3335 touch_popup_grab_up,
3336 touch_popup_grab_motion,
3337 touch_popup_grab_frame,
3338 touch_popup_grab_cancel,
3339};
3340
3341static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003342shell_surface_send_popup_done(struct shell_surface *shsurf)
3343{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003344 if (shsurf->resource == NULL)
3345 return;
3346 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003347 wl_shell_surface_send_popup_done(shsurf->resource);
3348 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003349 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003350}
3351
3352static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003353popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003354{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003355 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003356 struct shell_seat *shseat =
3357 container_of(grab, struct shell_seat, popup_grab.grab);
3358 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003359 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003360
3361 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003362 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003363 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003364 shseat->popup_grab.grab.interface = NULL;
3365 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003366 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003367 wl_list_for_each_safe(shsurf, next,
3368 &shseat->popup_grab.surfaces_list,
3369 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003370 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003371 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003372 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003373 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003374 wl_list_init(&shseat->popup_grab.surfaces_list);
3375 }
3376}
3377
3378static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003379touch_popup_grab_end(struct weston_touch *touch)
3380{
3381 struct weston_touch_grab *grab = touch->grab;
3382 struct shell_seat *shseat =
3383 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3384 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003385 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003386
3387 if (touch->grab->interface == &touch_popup_grab_interface) {
3388 weston_touch_end_grab(grab->touch);
3389 shseat->popup_grab.client = NULL;
3390 shseat->popup_grab.touch_grab.interface = NULL;
3391 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3392 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003393 wl_list_for_each_safe(shsurf, next,
3394 &shseat->popup_grab.surfaces_list,
3395 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003396 shell_surface_send_popup_done(shsurf);
3397 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003398 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003399 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003400 wl_list_init(&shseat->popup_grab.surfaces_list);
3401 }
3402}
3403
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003404static struct shell_surface *
3405get_top_popup(struct shell_seat *shseat)
3406{
3407 struct shell_surface *shsurf;
3408
3409 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3410 return NULL;
3411 } else {
3412 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3413 struct shell_surface,
3414 popup.grab_link);
3415 return shsurf;
3416 }
3417}
3418
3419static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003420add_popup_grab(struct shell_surface *shsurf,
3421 struct shell_seat *shseat,
3422 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003423{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003424 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003425 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003426 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3427 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003428
3429 parent = get_shell_surface(shsurf->parent);
3430 top_surface = get_top_popup(shseat);
3431 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003432 (!parent ||
3433 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003434 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003435 wl_resource_post_error(shsurf->owner_resource,
3436 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003437 "xdg_popup was not created on the "
3438 "topmost popup");
3439 return -1;
3440 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003441
3442 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003443 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003444 shseat->popup_grab.client =
3445 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003446
3447 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003448 shseat->popup_grab.grab.interface =
3449 &popup_grab_interface;
3450
3451 /* We must make sure here that this popup was opened
3452 * after a mouse press, and not just by moving around
3453 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003454 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003455 shseat->popup_grab.initial_up = 0;
3456 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003457 shseat->popup_grab.touch_grab.interface =
3458 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003459 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003460
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003461 wl_list_insert(&shseat->popup_grab.surfaces_list,
3462 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003463
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003464 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003465 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003466 &shseat->popup_grab.grab);
3467 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003468 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003469 &shseat->popup_grab.touch_grab);
3470 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003471 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003472 wl_list_insert(&shseat->popup_grab.surfaces_list,
3473 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003474 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003475
3476 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003477}
3478
3479static void
3480remove_popup_grab(struct shell_surface *shsurf)
3481{
3482 struct shell_seat *shseat = shsurf->popup.shseat;
3483
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003484 if (shell_surface_is_xdg_popup(shsurf) &&
3485 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003486 wl_resource_post_error(shsurf->owner_resource,
3487 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003488 "xdg_popup was destroyed while it was "
3489 "not the topmost popup.");
3490 return;
3491 }
3492
Giulio Camuffo5085a752013-03-25 21:42:45 +01003493 wl_list_remove(&shsurf->popup.grab_link);
3494 wl_list_init(&shsurf->popup.grab_link);
3495 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003496 if (shseat->popup_grab.type == POINTER) {
3497 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3498 shseat->popup_grab.grab.interface = NULL;
3499 } else if (shseat->popup_grab.type == TOUCH) {
3500 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3501 shseat->popup_grab.touch_grab.interface = NULL;
3502 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003503 }
3504}
3505
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003506static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003507shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003508{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003509 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003510 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003511 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3512 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003513
Jason Ekstranda7af7042013-10-12 22:38:11 -05003514 shsurf->surface->output = parent_view->output;
3515 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003516
Jason Ekstranda7af7042013-10-12 22:38:11 -05003517 weston_view_set_transform_parent(shsurf->view, parent_view);
3518 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3519 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003520
Derek Foreman1281a362015-07-31 16:55:32 -05003521 if (pointer &&
3522 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003523 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3524 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003525 } else if (touch &&
3526 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003527 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3528 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003529 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003530 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003531 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003532 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003533
3534 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003535}
3536
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003537static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003538 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003539 shell_surface_move,
3540 shell_surface_resize,
3541 shell_surface_set_toplevel,
3542 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003543 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003544 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003545 shell_surface_set_maximized,
3546 shell_surface_set_title,
3547 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003548};
3549
3550static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003551destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003552{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003553 struct shell_surface *child, *next;
3554
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003555 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3556
Giulio Camuffo5085a752013-03-25 21:42:45 +01003557 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3558 remove_popup_grab(shsurf);
3559 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003560
Alex Wubd3354b2012-04-17 17:20:49 +08003561 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003562 shell_surface_is_top_fullscreen(shsurf))
3563 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003564
Jason Ekstranda7af7042013-10-12 22:38:11 -05003565 if (shsurf->fullscreen.black_view)
3566 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003567
Alex Wubd3354b2012-04-17 17:20:49 +08003568 /* As destroy_resource() use wl_list_for_each_safe(),
3569 * we can always remove the listener.
3570 */
3571 wl_list_remove(&shsurf->surface_destroy_listener.link);
3572 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003573 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003574 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003575
Jason Ekstranda7af7042013-10-12 22:38:11 -05003576 weston_view_destroy(shsurf->view);
3577
Philip Withnall648a4dd2013-11-25 18:01:44 +00003578 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003579 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3580 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003581
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003582 wl_list_remove(&shsurf->link);
3583 free(shsurf);
3584}
3585
3586static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003587shell_destroy_shell_surface(struct wl_resource *resource)
3588{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003589 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003590
Bryan Caina46b9462014-04-04 17:41:24 -05003591 if (!wl_list_empty(&shsurf->popup.grab_link))
3592 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003593 if (shsurf->resource)
3594 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003595 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003596}
3597
3598static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003599shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003600{
3601 struct shell_surface *shsurf = container_of(listener,
3602 struct shell_surface,
3603 surface_destroy_listener);
3604
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003605 if (shsurf->resource)
3606 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003607
3608 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003609}
3610
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003611static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003612fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003613{
3614 struct shell_surface *shsurf = data;
3615
3616 weston_surface_destroy(shsurf->surface);
3617}
3618
3619static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003620fade_out_done(struct weston_view_animation *animation, void *data)
3621{
3622 struct shell_surface *shsurf = data;
3623 struct wl_event_loop *loop;
3624
3625 loop = wl_display_get_event_loop(
3626 shsurf->surface->compositor->wl_display);
3627
3628 if (!shsurf->destroying) {
3629 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3630 shsurf->destroying = true;
3631 }
3632}
3633
3634static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003635handle_resource_destroy(struct wl_listener *listener, void *data)
3636{
3637 struct shell_surface *shsurf =
3638 container_of(listener, struct shell_surface,
3639 resource_destroy_listener);
3640
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003641 if (!weston_surface_is_mapped(shsurf->surface))
3642 return;
3643
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003644 shsurf->surface->ref_count++;
3645
3646 pixman_region32_fini(&shsurf->surface->pending.input);
3647 pixman_region32_init(&shsurf->surface->pending.input);
3648 pixman_region32_fini(&shsurf->surface->input);
3649 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003650 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3651 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3652 fade_out_done, shsurf);
3653 } else {
3654 weston_surface_destroy(shsurf->surface);
3655 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003656}
3657
3658static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003659shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003660
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003661struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003662get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003663{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003664 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003665 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003666 else
3667 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003668}
3669
Rafael Antognollie2a34552013-12-03 15:35:45 -02003670static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003671create_common_surface(struct shell_client *owner, void *shell,
3672 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003673 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003674{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003675 struct shell_surface *shsurf;
3676
Pekka Paalanen50b67472014-10-01 15:02:41 +03003677 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003678
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003679 shsurf = calloc(1, sizeof *shsurf);
3680 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003681 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003682 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003683 }
3684
Jason Ekstranda7af7042013-10-12 22:38:11 -05003685 shsurf->view = weston_view_create(surface);
3686 if (!shsurf->view) {
3687 weston_log("no memory to allocate shell surface\n");
3688 free(shsurf);
3689 return NULL;
3690 }
3691
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003692 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003693 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003694 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003695
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003696 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3697 wl_resource_add_destroy_listener(surface->resource,
3698 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003699 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003700
Tiago Vignattibc052c92012-04-19 16:18:18 +03003701 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003702 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003703 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003704 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003705 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003706 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3707 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003708 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003709 wl_list_init(&shsurf->fullscreen.transform.link);
3710
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003711 shsurf->output = get_default_output(shsurf->shell->compositor);
3712
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003713 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003714 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003715 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003716 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003717
3718 /* init link so its safe to always remove it in destroy_shell_surface */
3719 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003720 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003721
Pekka Paalanen460099f2012-01-20 16:48:25 +02003722 /* empty when not in use */
3723 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003724 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003725
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003726 wl_list_init(&shsurf->workspace_transform.link);
3727
Philip Withnall648a4dd2013-11-25 18:01:44 +00003728 wl_list_init(&shsurf->children_link);
3729 wl_list_init(&shsurf->children_list);
3730 shsurf->parent = NULL;
3731
Pekka Paalanenb5026542014-11-12 15:09:24 +02003732 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003733
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003734 shsurf->client = client;
3735
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003736 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003737}
3738
Rafael Antognollie2a34552013-12-03 15:35:45 -02003739static struct shell_surface *
3740create_shell_surface(void *shell, struct weston_surface *surface,
3741 const struct weston_shell_client *client)
3742{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003743 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003744}
3745
Tiago Vignattibc052c92012-04-19 16:18:18 +03003746static void
3747shell_get_shell_surface(struct wl_client *client,
3748 struct wl_resource *resource,
3749 uint32_t id,
3750 struct wl_resource *surface_resource)
3751{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003752 struct weston_surface *surface =
3753 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003754 struct shell_client *sc = wl_resource_get_user_data(resource);
3755 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003756 struct shell_surface *shsurf;
3757
Pekka Paalanen50b67472014-10-01 15:02:41 +03003758 if (weston_surface_set_role(surface, "wl_shell_surface",
3759 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003760 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003761
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003762 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003763 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003764 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003765 return;
3766 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003767
Jason Ekstranda85118c2013-06-27 20:17:02 -05003768 shsurf->resource =
3769 wl_resource_create(client,
3770 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003771 if (!shsurf->resource) {
3772 wl_resource_post_no_memory(surface_resource);
3773 return;
3774 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003775 wl_resource_set_implementation(shsurf->resource,
3776 &shell_surface_implementation,
3777 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003778 wl_list_insert(&sc->surface_list,
3779 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003780}
3781
Rafael Antognollie2a34552013-12-03 15:35:45 -02003782static bool
3783shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3784{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003785 /* A shell surface without a resource is created from xwayland
3786 * and is considered a wl_shell surface for now. */
3787
3788 return shsurf->resource == NULL ||
3789 wl_resource_instance_of(shsurf->resource,
3790 &wl_shell_surface_interface,
3791 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003792}
3793
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003794static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003795 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003796};
3797
Rafael Antognollie2a34552013-12-03 15:35:45 -02003798/****************************
3799 * xdg-shell implementation */
3800
3801static void
3802xdg_surface_destroy(struct wl_client *client,
3803 struct wl_resource *resource)
3804{
3805 wl_resource_destroy(resource);
3806}
3807
3808static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003809xdg_surface_set_parent(struct wl_client *client,
3810 struct wl_resource *resource,
3811 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003812{
3813 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003814 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003815
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003816 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003817 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003818 shell_surface_set_parent(shsurf, parent->surface);
3819 } else {
3820 shell_surface_set_parent(shsurf, NULL);
3821 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003822}
3823
3824static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003825xdg_surface_set_app_id(struct wl_client *client,
3826 struct wl_resource *resource,
3827 const char *app_id)
3828{
3829 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3830
3831 free(shsurf->class);
3832 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003833 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003834}
3835
3836static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003837xdg_surface_show_window_menu(struct wl_client *client,
3838 struct wl_resource *surface_resource,
3839 struct wl_resource *seat_resource,
3840 uint32_t serial,
3841 int32_t x,
3842 int32_t y)
3843{
3844 /* TODO */
3845}
3846
3847static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003848xdg_surface_set_title(struct wl_client *client,
3849 struct wl_resource *resource, const char *title)
3850{
3851 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3852
3853 set_title(shsurf, title);
3854}
3855
3856static void
3857xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3858 struct wl_resource *seat_resource, uint32_t serial)
3859{
3860 common_surface_move(resource, seat_resource, serial);
3861}
3862
3863static void
3864xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3865 struct wl_resource *seat_resource, uint32_t serial,
3866 uint32_t edges)
3867{
3868 common_surface_resize(resource, seat_resource, serial, edges);
3869}
3870
3871static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003872xdg_surface_ack_configure(struct wl_client *client,
3873 struct wl_resource *resource,
3874 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003875{
3876 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3877
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003878 if (shsurf->state_requested) {
3879 shsurf->next_state = shsurf->requested_state;
3880 shsurf->state_changed = true;
3881 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003882 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003883}
3884
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003885static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003886xdg_surface_set_window_geometry(struct wl_client *client,
3887 struct wl_resource *resource,
3888 int32_t x,
3889 int32_t y,
3890 int32_t width,
3891 int32_t height)
3892{
3893 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3894
3895 set_window_geometry(shsurf, x, y, width, height);
3896}
3897
3898static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003899xdg_surface_set_maximized(struct wl_client *client,
3900 struct wl_resource *resource)
3901{
3902 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003903 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003904
3905 shsurf->state_requested = true;
3906 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003907
3908 if (!weston_surface_is_mapped(shsurf->surface))
3909 output = get_focused_output(shsurf->surface->compositor);
3910 else
3911 output = shsurf->surface->output;
3912
3913 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003914 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003915}
3916
3917static void
3918xdg_surface_unset_maximized(struct wl_client *client,
3919 struct wl_resource *resource)
3920{
3921 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3922
3923 shsurf->state_requested = true;
3924 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003925 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003926}
3927
3928static void
3929xdg_surface_set_fullscreen(struct wl_client *client,
3930 struct wl_resource *resource,
3931 struct wl_resource *output_resource)
3932{
3933 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3934 struct weston_output *output;
3935
3936 shsurf->state_requested = true;
3937 shsurf->requested_state.fullscreen = true;
3938
3939 if (output_resource)
3940 output = wl_resource_get_user_data(output_resource);
3941 else
3942 output = NULL;
3943
Marek Chalupa052917a2014-09-02 11:35:12 +02003944 /* handle clients launching in fullscreen */
3945 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3946 /* Set the output to the one that has focus currently. */
3947 assert(shsurf->surface);
3948 output = get_focused_output(shsurf->surface->compositor);
3949 }
3950
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003951 shell_surface_set_output(shsurf, output);
3952 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003953
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003954 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003955}
3956
3957static void
3958xdg_surface_unset_fullscreen(struct wl_client *client,
3959 struct wl_resource *resource)
3960{
3961 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3962
3963 shsurf->state_requested = true;
3964 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003965 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003966}
3967
3968static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003969xdg_surface_set_minimized(struct wl_client *client,
3970 struct wl_resource *resource)
3971{
3972 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3973
3974 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3975 return;
3976
3977 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003978 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003979}
3980
Rafael Antognollie2a34552013-12-03 15:35:45 -02003981static const struct xdg_surface_interface xdg_surface_implementation = {
3982 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003983 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003984 xdg_surface_set_title,
3985 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003986 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003987 xdg_surface_move,
3988 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003989 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003990 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003991 xdg_surface_set_maximized,
3992 xdg_surface_unset_maximized,
3993 xdg_surface_set_fullscreen,
3994 xdg_surface_unset_fullscreen,
3995 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003996};
3997
3998static void
3999xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004000 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004001{
4002 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004003 uint32_t *s;
4004 struct wl_array states;
4005 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004006
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004007 assert(shsurf);
4008
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004009 if (!shsurf->resource)
4010 return;
4011
4012 wl_array_init(&states);
4013 if (shsurf->requested_state.fullscreen) {
4014 s = wl_array_add(&states, sizeof *s);
4015 *s = XDG_SURFACE_STATE_FULLSCREEN;
4016 } else if (shsurf->requested_state.maximized) {
4017 s = wl_array_add(&states, sizeof *s);
4018 *s = XDG_SURFACE_STATE_MAXIMIZED;
4019 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004020 if (shsurf->resize_edges != 0) {
4021 s = wl_array_add(&states, sizeof *s);
4022 *s = XDG_SURFACE_STATE_RESIZING;
4023 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004024 if (shsurf->focus_count > 0) {
4025 s = wl_array_add(&states, sizeof *s);
4026 *s = XDG_SURFACE_STATE_ACTIVATED;
4027 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004028
4029 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4030 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4031
4032 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004033}
4034
4035static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004036 xdg_send_configure,
4037 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004038};
4039
4040static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004041xdg_shell_destroy(struct wl_client *client,
4042 struct wl_resource *resource)
4043{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004044 struct shell_client *sc = wl_resource_get_user_data(resource);
4045 struct wl_resource *shsurf_resource;
4046 struct shell_surface *shsurf;
4047
4048 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4049 shsurf = wl_resource_get_user_data(shsurf_resource);
4050 if (shsurf->owner_resource == resource) {
4051 wl_resource_post_error(
4052 resource,
4053 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4054 "not all child surface objects destroyed");
4055 return;
4056 }
4057 }
4058
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004059 wl_resource_destroy(resource);
4060}
4061
4062static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004063xdg_use_unstable_version(struct wl_client *client,
4064 struct wl_resource *resource,
4065 int32_t version)
4066{
4067 if (version > 1) {
4068 wl_resource_post_error(resource,
4069 1,
4070 "xdg-shell:: version not implemented yet.");
4071 return;
4072 }
4073}
4074
4075static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004076create_xdg_surface(struct shell_client *owner, void *shell,
4077 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004078 const struct weston_shell_client *client)
4079{
4080 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004081
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004082 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004083 if (!shsurf)
4084 return NULL;
4085
Pekka Paalanenb5026542014-11-12 15:09:24 +02004086 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004087
4088 return shsurf;
4089}
4090
4091static void
4092xdg_get_xdg_surface(struct wl_client *client,
4093 struct wl_resource *resource,
4094 uint32_t id,
4095 struct wl_resource *surface_resource)
4096{
4097 struct weston_surface *surface =
4098 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004099 struct shell_client *sc = wl_resource_get_user_data(resource);
4100 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004101 struct shell_surface *shsurf;
4102
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004103 shsurf = get_shell_surface(surface);
4104 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4105 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4106 "This wl_surface is already an "
4107 "xdg_surface");
4108 return;
4109 }
4110
Pekka Paalanen50b67472014-10-01 15:02:41 +03004111 if (weston_surface_set_role(surface, "xdg_surface",
4112 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004113 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004114
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004115 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004116 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004117 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004118 return;
4119 }
4120
4121 shsurf->resource =
4122 wl_resource_create(client,
4123 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004124 if (!shsurf->resource) {
4125 wl_resource_post_no_memory(surface_resource);
4126 return;
4127 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004128 wl_resource_set_implementation(shsurf->resource,
4129 &xdg_surface_implementation,
4130 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004131 shsurf->owner_resource = resource;
4132 wl_list_insert(&sc->surface_list,
4133 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004134}
4135
4136static bool
4137shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4138{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004139 return shsurf->resource &&
4140 wl_resource_instance_of(shsurf->resource,
4141 &xdg_surface_interface,
4142 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004143}
4144
4145/* xdg-popup implementation */
4146
4147static void
4148xdg_popup_destroy(struct wl_client *client,
4149 struct wl_resource *resource)
4150{
4151 wl_resource_destroy(resource);
4152}
4153
Rafael Antognollie2a34552013-12-03 15:35:45 -02004154static const struct xdg_popup_interface xdg_popup_implementation = {
4155 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004156};
4157
4158static void
4159xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004160 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004161{
4162}
4163
4164static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004165 xdg_popup_send_configure,
4166 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004167};
4168
4169static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004170create_xdg_popup(struct shell_client *owner, void *shell,
4171 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004172 const struct weston_shell_client *client,
4173 struct weston_surface *parent,
4174 struct shell_seat *seat,
4175 uint32_t serial,
4176 int32_t x, int32_t y)
4177{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004178 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004179
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004180 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004181 if (!shsurf)
4182 return NULL;
4183
Pekka Paalanenb5026542014-11-12 15:09:24 +02004184 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004185 shsurf->popup.shseat = seat;
4186 shsurf->popup.serial = serial;
4187 shsurf->popup.x = x;
4188 shsurf->popup.y = y;
4189 shell_surface_set_parent(shsurf, parent);
4190
4191 return shsurf;
4192}
4193
4194static void
4195xdg_get_xdg_popup(struct wl_client *client,
4196 struct wl_resource *resource,
4197 uint32_t id,
4198 struct wl_resource *surface_resource,
4199 struct wl_resource *parent_resource,
4200 struct wl_resource *seat_resource,
4201 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004202 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004203{
4204 struct weston_surface *surface =
4205 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004206 struct shell_client *sc = wl_resource_get_user_data(resource);
4207 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004208 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004209 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004210 struct weston_surface *parent;
4211 struct shell_seat *seat;
4212
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004213 shsurf = get_shell_surface(surface);
4214 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4215 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4216 "This wl_surface is already an "
4217 "xdg_popup");
4218 return;
4219 }
4220
Pekka Paalanen50b67472014-10-01 15:02:41 +03004221 if (weston_surface_set_role(surface, "xdg_popup",
4222 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004223 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004224
4225 if (!parent_resource) {
4226 wl_resource_post_error(surface_resource,
4227 WL_DISPLAY_ERROR_INVALID_OBJECT,
4228 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004229 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004230 }
4231
4232 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004233 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004234
Jonas Ådahlee45a552015-03-18 16:37:47 +08004235 /* Verify that we are creating the top most popup when mapping,
4236 * as it's not until then we know whether it was mapped as most
4237 * top level or not. */
4238
4239 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004240 if (!parent_shsurf ||
4241 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4242 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004243 wl_resource_post_error(resource,
4244 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4245 "xdg_popup parent was invalid");
4246 return;
4247 }
4248
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004249 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004250 parent, seat, serial, x, y);
4251 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004252 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004253 return;
4254 }
4255
4256 shsurf->resource =
4257 wl_resource_create(client,
4258 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004259 if (!shsurf->resource) {
4260 wl_resource_post_no_memory(surface_resource);
4261 return;
4262 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004263 wl_resource_set_implementation(shsurf->resource,
4264 &xdg_popup_implementation,
4265 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004266 shsurf->owner_resource = resource;
4267 wl_list_insert(&sc->surface_list,
4268 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004269}
4270
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004271static void
4272xdg_pong(struct wl_client *client,
4273 struct wl_resource *resource, uint32_t serial)
4274{
4275 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004276
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004277 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004278}
4279
Rafael Antognollie2a34552013-12-03 15:35:45 -02004280static bool
4281shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4282{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004283 return (shsurf->resource &&
4284 wl_resource_instance_of(shsurf->resource,
4285 &xdg_popup_interface,
4286 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004287}
4288
4289static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004290 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004291 xdg_use_unstable_version,
4292 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004293 xdg_get_xdg_popup,
4294 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004295};
4296
4297static int
4298xdg_shell_unversioned_dispatch(const void *implementation,
4299 void *_target, uint32_t opcode,
4300 const struct wl_message *message,
4301 union wl_argument *args)
4302{
4303 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004304 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004305
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004306 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004307 wl_resource_post_error(resource,
4308 WL_DISPLAY_ERROR_INVALID_OBJECT,
4309 "must call use_unstable_version first");
4310 return 0;
4311 }
4312
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004313#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004314
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004315 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4316 "shell implementation doesn't match protocol version");
4317
Rafael Antognollie2a34552013-12-03 15:35:45 -02004318 if (args[0].i != XDG_SERVER_VERSION) {
4319 wl_resource_post_error(resource,
4320 WL_DISPLAY_ERROR_INVALID_OBJECT,
4321 "incompatible version, server is %d "
4322 "client wants %d",
4323 XDG_SERVER_VERSION, args[0].i);
4324 return 0;
4325 }
4326
4327 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004328 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004329
4330 return 1;
4331}
4332
4333/* end of xdg-shell implementation */
4334/***********************************/
4335
Kristian Høgsberg07937562011-04-12 17:25:42 -04004336static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004337shell_fade(struct desktop_shell *shell, enum fade_type type);
4338
Pekka Paalanen77346a62011-11-30 16:26:35 +02004339static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004340configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004341{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004343
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004344 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004345 if (v->output == ev->output && v != ev) {
4346 weston_view_unmap(v);
4347 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004348 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004349 }
4350 }
4351
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004352 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004353
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004354 if (wl_list_empty(&ev->layer_link.link)) {
4355 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004356 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004357 }
4358}
4359
Pekka Paalanen8274d902014-08-06 19:36:51 +03004360static int
4361background_get_label(struct weston_surface *surface, char *buf, size_t len)
4362{
4363 return snprintf(buf, len, "background for output %s",
4364 surface->output->name);
4365}
4366
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004367static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004368background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004369{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004370 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004372
Jason Ekstranda7af7042013-10-12 22:38:11 -05004373 view = container_of(es->views.next, struct weston_view, surface_link);
4374
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004375 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004376}
4377
4378static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004379desktop_shell_set_background(struct wl_client *client,
4380 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004381 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004382 struct wl_resource *surface_resource)
4383{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004384 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004385 struct weston_surface *surface =
4386 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004387 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004388
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004389 if (surface->configure) {
4390 wl_resource_post_error(surface_resource,
4391 WL_DISPLAY_ERROR_INVALID_OBJECT,
4392 "surface role already assigned");
4393 return;
4394 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004395
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4397 weston_view_destroy(view);
4398 view = weston_view_create(surface);
4399
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004400 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004401 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004402 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004403 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004405 weston_desktop_shell_send_configure(resource, 0,
4406 surface_resource,
4407 surface->output->width,
4408 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004409}
4410
Pekka Paalanen8274d902014-08-06 19:36:51 +03004411static int
4412panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4413{
4414 return snprintf(buf, len, "panel for output %s",
4415 surface->output->name);
4416}
4417
Kristian Høgsberg75840622011-09-06 13:48:16 -04004418static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004419panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004420{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004421 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004423
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 view = container_of(es->views.next, struct weston_view, surface_link);
4425
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004426 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004427}
4428
4429static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004430desktop_shell_set_panel(struct wl_client *client,
4431 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004432 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004433 struct wl_resource *surface_resource)
4434{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004435 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004436 struct weston_surface *surface =
4437 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004438 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004439
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004440 if (surface->configure) {
4441 wl_resource_post_error(surface_resource,
4442 WL_DISPLAY_ERROR_INVALID_OBJECT,
4443 "surface role already assigned");
4444 return;
4445 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004446
Jason Ekstranda7af7042013-10-12 22:38:11 -05004447 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4448 weston_view_destroy(view);
4449 view = weston_view_create(surface);
4450
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004451 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004452 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004453 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004454 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004456 weston_desktop_shell_send_configure(resource, 0,
4457 surface_resource,
4458 surface->output->width,
4459 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004460}
4461
Pekka Paalanen8274d902014-08-06 19:36:51 +03004462static int
4463lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4464{
4465 return snprintf(buf, len, "lock window");
4466}
4467
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004468static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004469lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004470{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004471 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004472 struct weston_view *view;
4473
4474 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004475
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004476 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004477 return;
4478
Jason Ekstranda7af7042013-10-12 22:38:11 -05004479 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004480
4481 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004482 weston_layer_entry_insert(&shell->lock_layer.view_list,
4483 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004484 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004485 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004486 }
4487}
4488
4489static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004490handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004491{
Tiago Vignattibe143262012-04-16 17:31:41 +03004492 struct desktop_shell *shell =
4493 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004494
Martin Minarik6d118362012-06-07 18:01:59 +02004495 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004496 shell->lock_surface = NULL;
4497}
4498
4499static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004500desktop_shell_set_lock_surface(struct wl_client *client,
4501 struct wl_resource *resource,
4502 struct wl_resource *surface_resource)
4503{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004504 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004505 struct weston_surface *surface =
4506 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004507
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004508 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004509
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004510 if (!shell->locked)
4511 return;
4512
Pekka Paalanen98262232011-12-01 10:42:22 +02004513 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004514
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004515 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004516 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004517 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004518
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004519 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004520 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004521 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004522 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004523}
4524
4525static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004526resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004527{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004528 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004529
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004530 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004531 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004532 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004533 &shell->input_panel_layer.link);
4534 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004535 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004536 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004537 wl_list_insert(&shell->compositor->cursor_layer.link,
4538 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004539 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004540 wl_list_insert(&shell->fullscreen_layer.link,
4541 &shell->panel_layer.link);
4542 wl_list_insert(&shell->panel_layer.link,
4543 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004544
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004545 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004546
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004547 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004548 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004549 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004550}
4551
4552static void
4553desktop_shell_unlock(struct wl_client *client,
4554 struct wl_resource *resource)
4555{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004556 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004557
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004558 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004559
4560 if (shell->locked)
4561 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004562}
4563
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004564static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004565desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004566 struct wl_resource *resource,
4567 struct wl_resource *surface_resource)
4568{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004569 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004570
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004571 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004572 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004573}
4574
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004575static void
4576desktop_shell_desktop_ready(struct wl_client *client,
4577 struct wl_resource *resource)
4578{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004579 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004580
4581 shell_fade_startup(shell);
4582}
4583
Jonny Lamb765760d2014-08-20 15:53:19 +02004584static void
4585desktop_shell_set_panel_position(struct wl_client *client,
4586 struct wl_resource *resource,
4587 uint32_t position)
4588{
4589 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4590
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004591 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4592 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4593 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4594 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004595 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004596 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004597 "bad position argument");
4598 return;
4599 }
4600
4601 shell->panel_position = position;
4602}
4603
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004604static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004605 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004606 desktop_shell_set_panel,
4607 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004608 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004609 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004610 desktop_shell_desktop_ready,
4611 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004612};
4613
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004614static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004615get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004616{
4617 struct shell_surface *shsurf;
4618
4619 shsurf = get_shell_surface(surface);
4620 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004621 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004622 return shsurf->type;
4623}
4624
Kristian Høgsberg75840622011-09-06 13:48:16 -04004625static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004626move_binding(struct weston_pointer *pointer, uint32_t time,
4627 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004628{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004629 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004630 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004631 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004632
Derek Foreman8ae2db52015-07-15 13:00:36 -05004633 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004634 return;
4635
Derek Foreman8ae2db52015-07-15 13:00:36 -05004636 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004637
Pekka Paalanen01388e22013-04-25 13:57:44 +03004638 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004639 if (surface == NULL)
4640 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004641
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004642 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004643 if (shsurf == NULL || shsurf->state.fullscreen ||
4644 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004645 return;
4646
Derek Foreman794fa0e2015-07-15 13:00:38 -05004647 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004648}
4649
4650static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004651maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4652 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004653{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004654 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004655 struct weston_surface *surface;
4656 struct shell_surface *shsurf;
4657
4658 surface = weston_surface_get_main_surface(focus);
4659 if (surface == NULL)
4660 return;
4661
4662 shsurf = get_shell_surface(surface);
4663 if (shsurf == NULL)
4664 return;
4665
4666 if (!shell_surface_is_xdg_surface(shsurf))
4667 return;
4668
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004669 shsurf->state_requested = true;
4670 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004671 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004672}
4673
4674static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004675fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4676 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004677{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004678 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004679 struct weston_surface *surface;
4680 struct shell_surface *shsurf;
4681
4682 surface = weston_surface_get_main_surface(focus);
4683 if (surface == NULL)
4684 return;
4685
4686 shsurf = get_shell_surface(surface);
4687 if (shsurf == NULL)
4688 return;
4689
4690 if (!shell_surface_is_xdg_surface(shsurf))
4691 return;
4692
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004693 shsurf->state_requested = true;
4694 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004695 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004696 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004697}
4698
4699static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004700touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004701{
Derek Foreman362656b2014-09-04 10:23:05 -05004702 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004703 struct weston_surface *surface;
4704 struct shell_surface *shsurf;
4705
Derek Foreman8ae2db52015-07-15 13:00:36 -05004706 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004707 return;
4708
Derek Foreman8ae2db52015-07-15 13:00:36 -05004709 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004710 surface = weston_surface_get_main_surface(focus);
4711 if (surface == NULL)
4712 return;
4713
4714 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004715 if (shsurf == NULL || shsurf->state.fullscreen ||
4716 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004717 return;
4718
Derek Foremanb7674ae2015-07-15 13:00:37 -05004719 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004720}
4721
4722static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004723resize_binding(struct weston_pointer *pointer, uint32_t time,
4724 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004725{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004726 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004727 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004728 uint32_t edges = 0;
4729 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004730 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004731
Derek Foreman8ae2db52015-07-15 13:00:36 -05004732 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004733 return;
4734
Derek Foreman8ae2db52015-07-15 13:00:36 -05004735 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004736
Pekka Paalanen01388e22013-04-25 13:57:44 +03004737 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004738 if (surface == NULL)
4739 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004740
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004741 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004742 if (shsurf == NULL || shsurf->state.fullscreen ||
4743 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004744 return;
4745
Jason Ekstranda7af7042013-10-12 22:38:11 -05004746 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004747 wl_fixed_to_int(pointer->grab_x),
4748 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004749 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004750
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004751 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004752 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004753 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004754 edges |= 0;
4755 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004756 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004757
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004758 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004759 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004760 else if (y < 2 * shsurf->surface->height / 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_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004764
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004765 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004766}
4767
4768static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004769surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004770 struct weston_pointer_axis_event *event,
4771 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004772{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004773 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004774 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004775 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004776 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004777
Pekka Paalanen01388e22013-04-25 13:57:44 +03004778 /* XXX: broken for windows containing sub-surfaces */
4779 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004780 if (surface == NULL)
4781 return;
4782
4783 shsurf = get_shell_surface(surface);
4784 if (!shsurf)
4785 return;
4786
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004787 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004788
Jason Ekstranda7af7042013-10-12 22:38:11 -05004789 if (shsurf->view->alpha > 1.0)
4790 shsurf->view->alpha = 1.0;
4791 if (shsurf->view->alpha < step)
4792 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004793
Jason Ekstranda7af7042013-10-12 22:38:11 -05004794 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004795 weston_surface_damage(surface);
4796}
4797
4798static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004799do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004800 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004801{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004802 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004803 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004804 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004805 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004806
Derek Foreman1281a362015-07-31 16:55:32 -05004807 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004808 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4809 return;
4810 }
4811
Scott Moreauccbf29d2012-02-22 14:21:41 -07004812 wl_list_for_each(output, &compositor->output_list, link) {
4813 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004814 wl_fixed_to_double(pointer->x),
4815 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004816 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004817 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004818 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004819 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004820 increment = -output->zoom.increment;
4821 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004822 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004823 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004824 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004825 else
4826 increment = 0;
4827
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004828 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004829
Scott Moreaue6603982012-06-11 13:07:51 -06004830 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004831 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004832 else if (output->zoom.level > output->zoom.max_level)
4833 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004834
4835 if (!output->zoom.active) {
4836 if (output->zoom.level <= 0.0)
4837 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004838 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004839 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004840
Scott Moreaue6603982012-06-11 13:07:51 -06004841 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004842
Jason Ekstranda7af7042013-10-12 22:38:11 -05004843 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004844 }
4845 }
4846}
4847
Scott Moreauccbf29d2012-02-22 14:21:41 -07004848static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004849zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004850 struct weston_pointer_axis_event *event,
4851 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004852{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004853 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004854}
4855
4856static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004857zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4858 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004859{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004860 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004861}
4862
4863static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004864terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4865 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004866{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004867 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004868
Daniel Stone325fc2d2012-05-30 16:31:58 +01004869 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004870}
4871
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004872static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004873rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004874 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004875{
4876 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004877 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004878 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004879 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004880 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004881
Jonas Ådahld2510102014-10-05 21:39:14 +02004882 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004883
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004884 if (!shsurf)
4885 return;
4886
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004887 cx = 0.5f * shsurf->surface->width;
4888 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004889
Daniel Stone37816df2012-05-16 18:45:18 +01004890 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4891 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004892 r = sqrtf(dx * dx + dy * dy);
4893
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004894 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004895 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004896
4897 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004898 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004899 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004900
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004901 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004902 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004903
4904 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004905 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004906 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004907 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004908 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004909
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004910 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004911 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004912 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004913 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004914 wl_list_init(&shsurf->rotation.transform.link);
4915 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004916 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004917 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004918
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004919 /* We need to adjust the position of the surface
4920 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004921 cposx = shsurf->view->geometry.x + cx;
4922 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004923 dposx = rotate->center.x - cposx;
4924 dposy = rotate->center.y - cposy;
4925 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004926 weston_view_set_position(shsurf->view,
4927 shsurf->view->geometry.x + dposx,
4928 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004929 }
4930
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004931 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004932 * lazily applies the damage due to rotation update.
4933 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004934 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935}
4936
4937static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004938rotate_grab_button(struct weston_pointer_grab *grab,
4939 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004940{
4941 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004942 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004943 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004944 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004945 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004946
Daniel Stone4dbadb12012-05-30 16:31:51 +01004947 if (pointer->button_count == 0 &&
4948 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004949 if (shsurf)
4950 weston_matrix_multiply(&shsurf->rotation.rotation,
4951 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004952 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004953 free(rotate);
4954 }
4955}
4956
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004957static void
4958rotate_grab_cancel(struct weston_pointer_grab *grab)
4959{
4960 struct rotate_grab *rotate =
4961 container_of(grab, struct rotate_grab, base.grab);
4962
4963 shell_grab_end(&rotate->base);
4964 free(rotate);
4965}
4966
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004967static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004968 noop_grab_focus,
4969 rotate_grab_motion,
4970 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02004971 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10004972 noop_grab_axis_source,
4973 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004974 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004975};
4976
4977static void
Derek Foreman74de4692015-07-15 13:00:39 -05004978surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004979{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004980 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004981 float dx, dy;
4982 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004983
Derek Foreman45a7c272015-09-11 14:27:40 -05004984 surface = find_toplevel_surface(surface);
4985
Pekka Paalanen460099f2012-01-20 16:48:25 +02004986 rotate = malloc(sizeof *rotate);
4987 if (!rotate)
4988 return;
4989
Jason Ekstranda7af7042013-10-12 22:38:11 -05004990 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004991 surface->surface->width * 0.5f,
4992 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004993 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004994
Derek Foreman74de4692015-07-15 13:00:39 -05004995 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4996 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004997 r = sqrtf(dx * dx + dy * dy);
4998 if (r > 20.0f) {
4999 struct weston_matrix inverse;
5000
5001 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005002 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005003 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005004
5005 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005006 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005007 } else {
5008 weston_matrix_init(&surface->rotation.rotation);
5009 weston_matrix_init(&rotate->rotation);
5010 }
5011
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005012 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005013 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005014}
5015
5016static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005017rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005018 void *data)
5019{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005020 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005021 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005022 struct shell_surface *surface;
5023
Derek Foreman8ae2db52015-07-15 13:00:36 -05005024 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005025 return;
5026
Derek Foreman8ae2db52015-07-15 13:00:36 -05005027 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005028
Pekka Paalanen01388e22013-04-25 13:57:44 +03005029 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005030 if (base_surface == NULL)
5031 return;
5032
5033 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005034 if (surface == NULL || surface->state.fullscreen ||
5035 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005036 return;
5037
Derek Foreman74de4692015-07-15 13:00:39 -05005038 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005039}
5040
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005041/* Move all fullscreen layers down to the current workspace and hide their
5042 * black views. The surfaces' state is set to both fullscreen and lowered,
5043 * and this is reversed when such a surface is re-configured, see
5044 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5045 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005046 * lowering_output = NULL - Lower on all outputs, else only lower on the
5047 * specified output.
5048 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005049 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005050 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005051void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005052lower_fullscreen_layer(struct desktop_shell *shell,
5053 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005054{
5055 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005056 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005057
5058 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005059 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005060 &shell->fullscreen_layer.view_list.link,
5061 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005062 struct shell_surface *shsurf = get_shell_surface(view->surface);
5063
5064 if (!shsurf)
5065 continue;
5066
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005067 /* Only lower surfaces which have lowering_output as their fullscreen
5068 * output, unless a NULL output asks for lowering on all outputs.
5069 */
5070 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5071 continue;
5072
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005073 /* We can have a non-fullscreen popup for a fullscreen surface
5074 * in the fullscreen layer. */
5075 if (shsurf->state.fullscreen) {
5076 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005077 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5078 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005079 weston_view_damage_below(shsurf->fullscreen.black_view);
5080
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005081 }
5082
5083 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005084 weston_layer_entry_remove(&view->layer_link);
5085 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005086 weston_view_damage_below(view);
5087 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005088
5089 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005090 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005091}
5092
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005093void
Tiago Vignattibe143262012-04-16 17:31:41 +03005094activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005095 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005096{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005097 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005098 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005099 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005100 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005101 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005102
Pekka Paalanen01388e22013-04-25 13:57:44 +03005103 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005104 shsurf = get_shell_surface(main_surface);
5105 assert(shsurf);
5106
5107 /* Only demote fullscreen surfaces on the output of activated shsurf.
5108 * Leave fullscreen surfaces on unrelated outputs alone. */
5109 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005110
Daniel Stone37816df2012-05-16 18:45:18 +01005111 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005112
Jonas Ådahl8538b222012-08-29 22:13:03 +02005113 state = ensure_focus_state(shell, seat);
5114 if (state == NULL)
5115 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005116
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005117 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005118 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005119
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005120 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005121 shell_configure_fullscreen(shsurf);
5122 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005123 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005124
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005125 /* Update the surface’s layer. This brings it to the top of the stacking
5126 * order as appropriate. */
5127 shell_surface_update_layer(shsurf);
5128
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005129 if (shell->focus_animation_type != ANIMATION_NONE) {
5130 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005131 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005132 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005133}
5134
Alex Wu21858432012-04-01 20:13:08 +08005135/* no-op func for checking black surface */
5136static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005137black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005138{
5139}
5140
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005141static bool
Alex Wu21858432012-04-01 20:13:08 +08005142is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5143{
5144 if (es->configure == black_surface_configure) {
5145 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005146 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005147 return true;
5148 }
5149 return false;
5150}
5151
Kristian Høgsberg75840622011-09-06 13:48:16 -04005152static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005153activate_binding(struct weston_seat *seat,
5154 struct desktop_shell *shell,
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005155 struct weston_view *focus_view)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005156{
Derek Foremanbbde6432015-12-10 13:08:01 -06005157 struct focus_state *state;
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005158 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005159 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005160
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005161 focus = focus_view->surface;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005162
Pekka Paalanen01388e22013-04-25 13:57:44 +03005163 if (is_black_surface(focus, &main_surface))
5164 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005165
Pekka Paalanen01388e22013-04-25 13:57:44 +03005166 main_surface = weston_surface_get_main_surface(focus);
5167 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005168 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005169
Derek Foremanbbde6432015-12-10 13:08:01 -06005170 state = ensure_focus_state(shell, seat);
5171 if (state == NULL)
5172 return;
5173
5174 if (state->keyboard_focus == focus)
5175 return;
5176
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005177 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005178}
5179
5180static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005181click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5182 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005183{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005184 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005185 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005186 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005187 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005188
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005189 activate_binding(pointer->seat, data, pointer->focus);
Neil Robertsa28c6932013-10-03 16:43:04 +01005190}
5191
5192static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005193touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5194 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005195{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005196 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005197 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005198 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005199 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005200
Jonas Ådahlcd0f1ac2015-06-26 12:37:56 +08005201 activate_binding(touch->seat, data, touch->focus);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005202}
5203
5204static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005205unfocus_all_seats(struct desktop_shell *shell)
5206{
5207 struct weston_seat *seat, *next;
5208
5209 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005210 struct weston_keyboard *keyboard =
5211 weston_seat_get_keyboard(seat);
5212
5213 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005214 continue;
5215
Derek Foreman1281a362015-07-31 16:55:32 -05005216 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005217 }
5218}
5219
5220static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005221lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005222{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005223 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005224
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005225 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005226 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005227 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005228 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005229
5230 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005231
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005232 /* Hide all surfaces by removing the fullscreen, panel and
5233 * toplevel layers. This way nothing else can show or receive
5234 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005235
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005236 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005237 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005238 if (shell->showing_input_panels)
5239 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005240 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005241 wl_list_insert(&shell->compositor->cursor_layer.link,
5242 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005243
Derek Foreman004b4a12015-07-20 16:28:13 -05005244 weston_compositor_sleep(shell->compositor);
5245
Neil Robertsb4a91702014-04-09 16:33:32 +01005246 /* Remove the keyboard focus on all seats. This will be
5247 * restored to the workspace's saved state via
5248 * restore_focus_state when the compositor is unlocked */
5249 unfocus_all_seats(shell);
5250
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005251 /* TODO: disable bindings that should not work while locked. */
5252
5253 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005254}
5255
5256static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005257unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005258{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005259 struct wl_resource *shell_resource;
5260
Pekka Paalanend81c2162011-11-16 13:47:34 +02005261 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005262 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005263 return;
5264 }
5265
5266 /* If desktop-shell client has gone away, unlock immediately. */
5267 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005268 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005269 return;
5270 }
5271
5272 if (shell->prepare_event_sent)
5273 return;
5274
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005275 shell_resource = shell->child.desktop_shell;
5276 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005277 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005278}
5279
5280static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005281shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005282{
5283 struct desktop_shell *shell = data;
5284
5285 shell->fade.animation = NULL;
5286
5287 switch (shell->fade.type) {
5288 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005289 weston_surface_destroy(shell->fade.view->surface);
5290 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005291 break;
5292 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005293 lock(shell);
5294 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005295 default:
5296 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005297 }
5298}
5299
Jason Ekstranda7af7042013-10-12 22:38:11 -05005300static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005301shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005302{
5303 struct weston_compositor *compositor = shell->compositor;
5304 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005305 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005306
5307 surface = weston_surface_create(compositor);
5308 if (!surface)
5309 return NULL;
5310
Jason Ekstranda7af7042013-10-12 22:38:11 -05005311 view = weston_view_create(surface);
5312 if (!view) {
5313 weston_surface_destroy(surface);
5314 return NULL;
5315 }
5316
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005317 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005318 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005319 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005320 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5321 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005322 pixman_region32_init(&surface->input);
5323
Jason Ekstranda7af7042013-10-12 22:38:11 -05005324 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005325}
5326
5327static void
5328shell_fade(struct desktop_shell *shell, enum fade_type type)
5329{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005330 float tint;
5331
5332 switch (type) {
5333 case FADE_IN:
5334 tint = 0.0;
5335 break;
5336 case FADE_OUT:
5337 tint = 1.0;
5338 break;
5339 default:
5340 weston_log("shell: invalid fade type\n");
5341 return;
5342 }
5343
5344 shell->fade.type = type;
5345
Jason Ekstranda7af7042013-10-12 22:38:11 -05005346 if (shell->fade.view == NULL) {
5347 shell->fade.view = shell_fade_create_surface(shell);
5348 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005349 return;
5350
Jason Ekstranda7af7042013-10-12 22:38:11 -05005351 shell->fade.view->alpha = 1.0 - tint;
5352 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005353 }
5354
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005355 if (shell->fade.view->output == NULL) {
5356 /* If the black view gets a NULL output, we lost the
5357 * last output and we'll just cancel the fade. This
5358 * happens when you close the last window under the
5359 * X11 or Wayland backends. */
5360 shell->locked = false;
5361 weston_surface_destroy(shell->fade.view->surface);
5362 shell->fade.view = NULL;
5363 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005364 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005365 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005366 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005367 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005368 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005369 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005370 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005371}
5372
5373static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005374do_shell_fade_startup(void *data)
5375{
5376 struct desktop_shell *shell = data;
5377
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005378 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005379 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005380 } else {
5381 weston_log("desktop shell: "
5382 "unexpected fade-in animation type %d\n",
5383 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005384 weston_surface_destroy(shell->fade.view->surface);
5385 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005386 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005387}
5388
5389static void
5390shell_fade_startup(struct desktop_shell *shell)
5391{
5392 struct wl_event_loop *loop;
5393
5394 if (!shell->fade.startup_timer)
5395 return;
5396
5397 wl_event_source_remove(shell->fade.startup_timer);
5398 shell->fade.startup_timer = NULL;
5399
5400 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5401 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5402}
5403
5404static int
5405fade_startup_timeout(void *data)
5406{
5407 struct desktop_shell *shell = data;
5408
5409 shell_fade_startup(shell);
5410 return 0;
5411}
5412
5413static void
5414shell_fade_init(struct desktop_shell *shell)
5415{
5416 /* Make compositor output all black, and wait for the desktop-shell
5417 * client to signal it is ready, then fade in. The timer triggers a
5418 * fade-in, in case the desktop-shell client takes too long.
5419 */
5420
5421 struct wl_event_loop *loop;
5422
Jason Ekstranda7af7042013-10-12 22:38:11 -05005423 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005424 weston_log("%s: warning: fade surface already exists\n",
5425 __func__);
5426 return;
5427 }
5428
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005429 if (shell->startup_animation_type == ANIMATION_NONE)
5430 return;
5431
Jason Ekstranda7af7042013-10-12 22:38:11 -05005432 shell->fade.view = shell_fade_create_surface(shell);
5433 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005434 return;
5435
Jason Ekstranda7af7042013-10-12 22:38:11 -05005436 weston_view_update_transform(shell->fade.view);
5437 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005438
5439 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5440 shell->fade.startup_timer =
5441 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5442 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5443}
5444
5445static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005446idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005447{
5448 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005449 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005450 struct weston_seat *seat;
5451
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005452 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005453 struct weston_touch *touch = weston_seat_get_touch(seat);
5454 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5455
5456 if (pointer)
5457 popup_grab_end(pointer);
5458 if (touch)
5459 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005460 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005461
5462 shell_fade(shell, FADE_OUT);
5463 /* lock() is called from shell_fade_done() */
5464}
5465
5466static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005467wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005468{
5469 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005470 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005471
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005472 unlock(shell);
5473}
5474
5475static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005476transform_handler(struct wl_listener *listener, void *data)
5477{
5478 struct weston_surface *surface = data;
5479 struct shell_surface *shsurf = get_shell_surface(surface);
5480 struct weston_view *view;;
5481 int x, y;
5482
5483 if (!shsurf || !shsurf->client->send_position)
5484 return;
5485
5486 view = shsurf->view;
5487 if (!view || !weston_view_is_mapped(view))
5488 return;
5489
5490 x = view->geometry.x;
5491 y = view->geometry.y;
5492
5493 shsurf->client->send_position(surface, x, y);
5494}
5495
5496static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005497center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005498{
Giulio Camuffob8366642013-04-25 13:57:46 +03005499 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005500 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005501
Jason Ekstranda7af7042013-10-12 22:38:11 -05005502 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005503
5504 x = output->x + (output->width - width) / 2 - surf_x / 2;
5505 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005506
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005507 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005508}
5509
5510static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005511weston_view_set_initial_position(struct weston_view *view,
5512 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005513{
5514 struct weston_compositor *compositor = shell->compositor;
5515 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005516 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005517 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005518 struct weston_output *output, *target_output = NULL;
5519 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005520 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005521
5522 /* As a heuristic place the new window on the same output as the
5523 * pointer. Falling back to the output containing 0, 0.
5524 *
5525 * TODO: Do something clever for touch too?
5526 */
5527 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005528 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5529
5530 if (pointer) {
5531 ix = wl_fixed_to_int(pointer->x);
5532 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005533 break;
5534 }
5535 }
5536
5537 wl_list_for_each(output, &compositor->output_list, link) {
5538 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5539 target_output = output;
5540 break;
5541 }
5542 }
5543
5544 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005545 weston_view_set_position(view, 10 + random() % 400,
5546 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005547 return;
5548 }
5549
5550 /* Valid range within output where the surface will still be onscreen.
5551 * If this is negative it means that the surface is bigger than
5552 * output.
5553 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005554 get_output_work_area(shell, target_output, &area);
5555
Derek Foremanf814c5d2015-04-15 12:23:54 -05005556 x = area.x;
5557 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005558 range_x = area.width - view->surface->width;
5559 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005560
Rob Bradford4cb88c72012-08-13 15:18:44 +01005561 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005562 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005563
5564 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005565 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005566
Jason Ekstranda7af7042013-10-12 22:38:11 -05005567 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005568}
5569
5570static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005571set_maximized_position(struct desktop_shell *shell,
5572 struct shell_surface *shsurf)
5573{
5574 int32_t surf_x, surf_y;
5575 pixman_rectangle32_t area;
5576
Jonny Lambd73c6942014-08-20 15:53:20 +02005577 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005578 if (shsurf->has_set_geometry) {
5579 surf_x = shsurf->geometry.x;
5580 surf_y = shsurf->geometry.y;
5581 } else {
5582 surface_subsurfaces_boundingbox(shsurf->surface,
5583 &surf_x, &surf_y, NULL, NULL);
5584 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005585
5586 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005587 area.x - surf_x,
5588 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005589}
5590
5591static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005592map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005593 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005594{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005595 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005596 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005597
Pekka Paalanen77346a62011-11-30 16:26:35 +02005598 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005599 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005600 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005601 if (shsurf->state.fullscreen) {
5602 center_on_output(shsurf->view, shsurf->fullscreen_output);
5603 shell_map_fullscreen(shsurf);
5604 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005605 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005606 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005607 weston_view_set_initial_position(shsurf->view, shell);
5608 }
Juan Zhao96879df2012-02-07 08:45:41 +08005609 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005610 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005611 if (shell_map_popup(shsurf) != 0)
5612 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005613 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005614 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005615 weston_view_set_position(shsurf->view,
5616 shsurf->view->geometry.x + sx,
5617 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005618 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005619 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005620 default:
5621 ;
5622 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005623
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005624 /* Surface stacking order, see also activate(). */
5625 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005626
Jason Ekstranda7af7042013-10-12 22:38:11 -05005627 if (shsurf->type != SHELL_SURFACE_NONE) {
5628 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005629 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005630 shsurf->surface->output = shsurf->output;
5631 shsurf->view->output = shsurf->output;
5632 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005633 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005634
Jason Ekstranda7af7042013-10-12 22:38:11 -05005635 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005636 /* XXX: xwayland's using the same fields for transient type */
5637 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005638 if (shsurf->transient.flags ==
5639 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5640 break;
5641 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005642 if (shsurf->state.relative &&
5643 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5644 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005645 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005646 break;
5647 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005648 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005649 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005650 case SHELL_SURFACE_POPUP:
5651 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005652 default:
5653 break;
5654 }
5655
Rafael Antognolli03b16592013-12-03 15:35:42 -02005656 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5657 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005658 {
5659 switch (shell->win_animation_type) {
5660 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005661 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005662 break;
5663 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005664 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005665 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005666 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005667 default:
5668 break;
5669 }
5670 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005671}
5672
5673static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005674configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005675 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005676{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005677 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005678 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005679
Pekka Paalanen77346a62011-11-30 16:26:35 +02005680 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005681
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005682 assert(shsurf);
5683
Rafael Antognolli03b16592013-12-03 15:35:42 -02005684 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005685 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005686 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005687 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005688 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005689 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005690 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005691
Alex Wu4539b082012-03-01 12:57:46 +08005692 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005693 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005694 wl_list_for_each(view, &surface->views, surface_link)
5695 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005696
Rafael Antognolli03b16592013-12-03 15:35:42 -02005697 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005698 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005699 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005700}
5701
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005702static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005703shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005704{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005705 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005706 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005707 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005708
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005709 assert(shsurf);
5710
5711 shell = shsurf->shell;
5712
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005713 if (!weston_surface_is_mapped(es) &&
5714 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005715 remove_popup_grab(shsurf);
5716 }
5717
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005718 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005719 return;
5720
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005721 if (shsurf->has_next_geometry) {
5722 shsurf->geometry = shsurf->next_geometry;
5723 shsurf->has_next_geometry = false;
5724 shsurf->has_set_geometry = true;
5725 } else if (!shsurf->has_set_geometry) {
5726 surface_subsurfaces_boundingbox(shsurf->surface,
5727 &shsurf->geometry.x,
5728 &shsurf->geometry.y,
5729 &shsurf->geometry.width,
5730 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005731 }
5732
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005733 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005734 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005735 type_changed = 1;
5736 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005737
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005738 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005739 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005740 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005741 shsurf->last_width != es->width ||
5742 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005743 float from_x, from_y;
5744 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005745
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005746 if (shsurf->resize_edges) {
5747 sx = 0;
5748 sy = 0;
5749 }
5750
5751 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5752 sx = shsurf->last_width - es->width;
5753 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5754 sy = shsurf->last_height - es->height;
5755
5756 shsurf->last_width = es->width;
5757 shsurf->last_height = es->height;
5758
Jason Ekstranda7af7042013-10-12 22:38:11 -05005759 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5760 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005761 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005762 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005763 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005764 }
5765}
5766
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005767static bool
5768check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5769{
5770 struct timespec now;
5771
5772 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5773 return false;
5774
5775 /*
5776 * If the shell helper client dies before the session has been
5777 * up for roughly 30 seconds, better just make Weston shut down,
5778 * because the user likely has no way to interact with the desktop
5779 * anyway.
5780 */
5781 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5782 weston_log("Error: %s apparently cannot run at all.\n",
5783 shell->client);
5784 weston_log_continue(STAMP_SPACE "Quitting...");
5785 wl_display_terminate(shell->compositor->wl_display);
5786
5787 return true;
5788 }
5789
5790 return false;
5791}
5792
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005793static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005794
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005795static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005796respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005797{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005798 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005799
5800 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5801 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005802 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005803 shell->child.deathstamp = time;
5804 shell->child.deathcount = 0;
5805 }
5806
5807 shell->child.deathcount++;
5808 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005809 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005810 return;
5811 }
5812
Pekka Paalanen826dc142014-08-27 12:11:53 +03005813 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005814 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005815}
5816
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005817static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005818desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5819{
5820 struct desktop_shell *shell;
5821
5822 shell = container_of(listener, struct desktop_shell,
5823 child.client_destroy_listener);
5824
Pekka Paalanen826dc142014-08-27 12:11:53 +03005825 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005826 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005827 /*
5828 * unbind_desktop_shell() will reset shell->child.desktop_shell
5829 * before the respawned process has a chance to create a new
5830 * desktop_shell object, because we are being called from the
5831 * wl_client destructor which destroys all wl_resources before
5832 * returning.
5833 */
5834
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005835 if (!check_desktop_shell_crash_too_early(shell))
5836 respawn_desktop_shell_process(shell);
5837
Pekka Paalanen826dc142014-08-27 12:11:53 +03005838 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005839}
5840
5841static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005842launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005843{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005844 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005845
Pekka Paalanen826dc142014-08-27 12:11:53 +03005846 shell->child.client = weston_client_start(shell->compositor,
5847 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005848
Arnaud Vrac42631192014-08-25 20:56:46 +02005849 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005850 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005851 return;
5852 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005853
5854 shell->child.client_destroy_listener.notify =
5855 desktop_shell_client_destroy;
5856 wl_client_add_destroy_listener(shell->child.client,
5857 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005858}
5859
5860static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005861handle_shell_client_destroy(struct wl_listener *listener, void *data)
5862{
5863 struct shell_client *sc =
5864 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005865 struct wl_resource *shsurf_resource;
5866 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005867
5868 if (sc->ping_timer)
5869 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005870
5871 /* Since we're about to free shell_client, we remove it from the
5872 * head of the surface list so we don't use that freed list node
5873 * during surface clean up later on.
5874 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005875 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5876 shsurf = wl_resource_get_user_data(shsurf_resource);
5877 shsurf->owner = NULL;
5878 }
Derek Foremane42d7542015-05-29 10:46:44 -05005879 wl_list_remove(&sc->surface_list);
5880
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005881 free(sc);
5882}
5883
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005884static struct shell_client *
5885shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5886 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005887{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005888 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005889
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005890 sc = zalloc(sizeof *sc);
5891 if (sc == NULL) {
5892 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005893 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005894 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005895
5896 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005897 if (sc->resource == NULL) {
5898 free(sc);
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 }
5902
5903 sc->client = client;
5904 sc->shell = shell;
5905 sc->destroy_listener.notify = handle_shell_client_destroy;
5906 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005907 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005908
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005909 return sc;
5910}
5911
5912static void
5913bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5914{
5915 struct desktop_shell *shell = data;
5916 struct shell_client *sc;
5917
5918 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5919 if (sc)
5920 wl_resource_set_implementation(sc->resource,
5921 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005922 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005923}
5924
5925static void
5926bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5927{
5928 struct desktop_shell *shell = data;
5929 struct shell_client *sc;
5930
5931 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5932 if (sc)
5933 wl_resource_set_dispatcher(sc->resource,
5934 xdg_shell_unversioned_dispatch,
5935 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005936}
5937
5938static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005939unbind_desktop_shell(struct wl_resource *resource)
5940{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005941 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005942
5943 if (shell->locked)
5944 resume_desktop(shell);
5945
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005946 shell->child.desktop_shell = NULL;
5947 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005948}
5949
5950static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005951bind_desktop_shell(struct wl_client *client,
5952 void *data, uint32_t version, uint32_t id)
5953{
Tiago Vignattibe143262012-04-16 17:31:41 +03005954 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005955 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005956
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005957 resource = wl_resource_create(client, &weston_desktop_shell_interface,
5958 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005959
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005960 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005961 wl_resource_set_implementation(resource,
5962 &desktop_shell_implementation,
5963 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005964 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005965 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005966 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005967
5968 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5969 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005970}
5971
Kristian Høgsberg07045392012-02-19 18:52:44 -05005972struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005973 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005974 struct weston_surface *current;
5975 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005976 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005977 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005978};
5979
5980static void
5981switcher_next(struct switcher *switcher)
5982{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005983 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005984 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005985 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005986 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005987
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005988 /* temporary re-display minimized surfaces */
5989 struct weston_view *tmp;
5990 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005991 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5992 weston_layer_entry_remove(&view->layer_link);
5993 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005994 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5995 *minimized = view;
5996 }
5997
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005998 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005999 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006000 if (shsurf &&
6001 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6002 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006003 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006004 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006005 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006006 next = view->surface;
6007 prev = view->surface;
6008 view->alpha = 0.25;
6009 weston_view_geometry_dirty(view);
6010 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006011 }
Alex Wu1659daa2012-04-01 20:13:09 +08006012
Jason Ekstranda7af7042013-10-12 22:38:11 -05006013 if (is_black_surface(view->surface, NULL)) {
6014 view->alpha = 0.25;
6015 weston_view_geometry_dirty(view);
6016 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006017 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006018 }
6019
6020 if (next == NULL)
6021 next = first;
6022
Alex Wu07b26062012-03-12 16:06:01 +08006023 if (next == NULL)
6024 return;
6025
Kristian Høgsberg07045392012-02-19 18:52:44 -05006026 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006027 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006028
6029 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006030 wl_list_for_each(view, &next->views, surface_link)
6031 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006032
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006033 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006034 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006035 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006036}
6037
6038static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006039switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006040{
6041 struct switcher *switcher =
6042 container_of(listener, struct switcher, listener);
6043
6044 switcher_next(switcher);
6045}
6046
6047static void
Daniel Stone351eb612012-05-31 15:27:47 -04006048switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006049{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006050 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006051 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006052 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006053
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006054 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006055 if (is_focus_view(view))
6056 continue;
6057
Jason Ekstranda7af7042013-10-12 22:38:11 -05006058 view->alpha = 1.0;
6059 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006060 }
6061
Alex Wu07b26062012-03-12 16:06:01 +08006062 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006063 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006064 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006065 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006066 weston_keyboard_end_grab(keyboard);
6067 if (keyboard->input_method_resource)
6068 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006069
6070 /* re-hide surfaces that were temporary shown during the switch */
6071 struct weston_view **minimized;
6072 wl_array_for_each(minimized, &switcher->minimized_array) {
6073 /* with the exception of the current selected */
6074 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006075 weston_layer_entry_remove(&(*minimized)->layer_link);
6076 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006077 weston_view_damage_below(*minimized);
6078 }
6079 }
6080 wl_array_release(&switcher->minimized_array);
6081
Kristian Høgsberg07045392012-02-19 18:52:44 -05006082 free(switcher);
6083}
6084
6085static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006086switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006087 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006088{
6089 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006090 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006091
Daniel Stonec9785ea2012-05-30 16:31:52 +01006092 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006093 switcher_next(switcher);
6094}
6095
6096static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006097switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006098 uint32_t mods_depressed, uint32_t mods_latched,
6099 uint32_t mods_locked, uint32_t group)
6100{
6101 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006102 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006103
Daniel Stone351eb612012-05-31 15:27:47 -04006104 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6105 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006106}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006107
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006108static void
6109switcher_cancel(struct weston_keyboard_grab *grab)
6110{
6111 struct switcher *switcher = container_of(grab, struct switcher, grab);
6112
6113 switcher_destroy(switcher);
6114}
6115
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006116static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006117 switcher_key,
6118 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006119 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006120};
6121
6122static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006123switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6124 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006125{
Tiago Vignattibe143262012-04-16 17:31:41 +03006126 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006127 struct switcher *switcher;
6128
6129 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006130 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006131 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006132 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006133 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006134 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006135
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006136 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006137 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006138 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006139 weston_keyboard_start_grab(keyboard, &switcher->grab);
6140 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006141 switcher_next(switcher);
6142}
6143
Pekka Paalanen3c647232011-12-22 13:43:43 +02006144static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006145backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6146 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006147{
6148 struct weston_compositor *compositor = data;
6149 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006150 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006151
6152 /* TODO: we're limiting to simple use cases, where we assume just
6153 * control on the primary display. We'd have to extend later if we
6154 * ever get support for setting backlights on random desktop LCD
6155 * panels though */
6156 output = get_default_output(compositor);
6157 if (!output)
6158 return;
6159
6160 if (!output->set_backlight)
6161 return;
6162
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006163 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6164 backlight_new = output->backlight_current - 25;
6165 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6166 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006167
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006168 if (backlight_new < 5)
6169 backlight_new = 5;
6170 if (backlight_new > 255)
6171 backlight_new = 255;
6172
6173 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006174 output->set_backlight(output, output->backlight_current);
6175}
6176
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006177static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006178force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6179 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006180{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006181 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006182 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006183 struct desktop_shell *shell = data;
6184 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006185 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006186
Derek Foreman8ae2db52015-07-15 13:00:36 -05006187 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006188 if (!focus_surface)
6189 return;
6190
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006191 wl_signal_emit(&compositor->kill_signal, focus_surface);
6192
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006193 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006194 wl_client_get_credentials(client, &pid, NULL, NULL);
6195
6196 /* Skip clients that we launched ourselves (the credentials of
6197 * the socketpair is ours) */
6198 if (pid == getpid())
6199 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006200
Daniel Stone325fc2d2012-05-30 16:31:58 +01006201 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006202}
6203
6204static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006205workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006206 uint32_t key, void *data)
6207{
6208 struct desktop_shell *shell = data;
6209 unsigned int new_index = shell->workspaces.current;
6210
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006211 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006212 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006213 if (new_index != 0)
6214 new_index--;
6215
6216 change_workspace(shell, new_index);
6217}
6218
6219static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006220workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006221 uint32_t key, void *data)
6222{
6223 struct desktop_shell *shell = data;
6224 unsigned int new_index = shell->workspaces.current;
6225
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006226 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006227 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006228 if (new_index < shell->workspaces.num - 1)
6229 new_index++;
6230
6231 change_workspace(shell, new_index);
6232}
6233
6234static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006235workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006236 uint32_t key, void *data)
6237{
6238 struct desktop_shell *shell = data;
6239 unsigned int new_index;
6240
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006241 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006242 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006243 new_index = key - KEY_F1;
6244 if (new_index >= shell->workspaces.num)
6245 new_index = shell->workspaces.num - 1;
6246
6247 change_workspace(shell, new_index);
6248}
6249
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006250static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006251workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6252 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006253{
6254 struct desktop_shell *shell = data;
6255 unsigned int new_index = shell->workspaces.current;
6256
6257 if (shell->locked)
6258 return;
6259
6260 if (new_index != 0)
6261 new_index--;
6262
Derek Foreman8ae2db52015-07-15 13:00:36 -05006263 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006264}
6265
6266static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006267workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6268 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006269{
6270 struct desktop_shell *shell = data;
6271 unsigned int new_index = shell->workspaces.current;
6272
6273 if (shell->locked)
6274 return;
6275
6276 if (new_index < shell->workspaces.num - 1)
6277 new_index++;
6278
Derek Foreman8ae2db52015-07-15 13:00:36 -05006279 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006280}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006281
6282static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006283shell_reposition_view_on_output_destroy(struct weston_view *view)
6284{
6285 struct weston_output *output, *first_output;
6286 struct weston_compositor *ec = view->surface->compositor;
6287 struct shell_surface *shsurf;
6288 float x, y;
6289 int visible;
6290
6291 x = view->geometry.x;
6292 y = view->geometry.y;
6293
6294 /* At this point the destroyed output is not in the list anymore.
6295 * If the view is still visible somewhere, we leave where it is,
6296 * otherwise, move it to the first output. */
6297 visible = 0;
6298 wl_list_for_each(output, &ec->output_list, link) {
6299 if (pixman_region32_contains_point(&output->region,
6300 x, y, NULL)) {
6301 visible = 1;
6302 break;
6303 }
6304 }
6305
6306 if (!visible) {
6307 first_output = container_of(ec->output_list.next,
6308 struct weston_output, link);
6309
6310 x = first_output->x + first_output->width / 4;
6311 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006312
6313 weston_view_set_position(view, x, y);
6314 } else {
6315 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006316 }
6317
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006318
6319 shsurf = get_shell_surface(view->surface);
6320
6321 if (shsurf) {
6322 shsurf->saved_position_valid = false;
6323 shsurf->next_state.maximized = false;
6324 shsurf->next_state.fullscreen = false;
6325 shsurf->state_changed = true;
6326 }
6327}
6328
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006329void
6330shell_for_each_layer(struct desktop_shell *shell,
6331 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006332{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006333 struct workspace **ws;
6334
6335 func(shell, &shell->fullscreen_layer, data);
6336 func(shell, &shell->panel_layer, data);
6337 func(shell, &shell->background_layer, data);
6338 func(shell, &shell->lock_layer, data);
6339 func(shell, &shell->input_panel_layer, data);
6340
6341 wl_array_for_each(ws, &shell->workspaces.array)
6342 func(shell, &(*ws)->layer, data);
6343}
6344
6345static void
6346shell_output_destroy_move_layer(struct desktop_shell *shell,
6347 struct weston_layer *layer,
6348 void *data)
6349{
6350 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006351 struct weston_view *view;
6352
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006353 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006354 if (view->output != output)
6355 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006356
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006357 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006358 }
6359}
6360
6361static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006362handle_output_destroy(struct wl_listener *listener, void *data)
6363{
6364 struct shell_output *output_listener =
6365 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006366 struct weston_output *output = output_listener->output;
6367 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006368
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006369 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006370
Xiong Zhang6b481422013-10-23 13:58:32 +08006371 wl_list_remove(&output_listener->destroy_listener.link);
6372 wl_list_remove(&output_listener->link);
6373 free(output_listener);
6374}
6375
6376static void
6377create_shell_output(struct desktop_shell *shell,
6378 struct weston_output *output)
6379{
6380 struct shell_output *shell_output;
6381
6382 shell_output = zalloc(sizeof *shell_output);
6383 if (shell_output == NULL)
6384 return;
6385
6386 shell_output->output = output;
6387 shell_output->shell = shell;
6388 shell_output->destroy_listener.notify = handle_output_destroy;
6389 wl_signal_add(&output->destroy_signal,
6390 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006391 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006392}
6393
6394static void
6395handle_output_create(struct wl_listener *listener, void *data)
6396{
6397 struct desktop_shell *shell =
6398 container_of(listener, struct desktop_shell, output_create_listener);
6399 struct weston_output *output = (struct weston_output *)data;
6400
6401 create_shell_output(shell, output);
6402}
6403
6404static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006405handle_output_move_layer(struct desktop_shell *shell,
6406 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006407{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006408 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006409 struct weston_view *view;
6410 float x, y;
6411
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006412 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006413 if (view->output != output)
6414 continue;
6415
6416 x = view->geometry.x + output->move_x;
6417 y = view->geometry.y + output->move_y;
6418 weston_view_set_position(view, x, y);
6419 }
6420}
6421
6422static void
6423handle_output_move(struct wl_listener *listener, void *data)
6424{
6425 struct desktop_shell *shell;
6426
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006427 shell = container_of(listener, struct desktop_shell,
6428 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006429
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006430 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006431}
6432
6433static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006434setup_output_destroy_handler(struct weston_compositor *ec,
6435 struct desktop_shell *shell)
6436{
6437 struct weston_output *output;
6438
6439 wl_list_init(&shell->output_list);
6440 wl_list_for_each(output, &ec->output_list, link)
6441 create_shell_output(shell, output);
6442
6443 shell->output_create_listener.notify = handle_output_create;
6444 wl_signal_add(&ec->output_created_signal,
6445 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006446
6447 shell->output_move_listener.notify = handle_output_move;
6448 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006449}
6450
6451static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006452shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006453{
Tiago Vignattibe143262012-04-16 17:31:41 +03006454 struct desktop_shell *shell =
6455 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006456 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006457 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006458
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006459 /* Force state to unlocked so we don't try to fade */
6460 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006461
6462 if (shell->child.client) {
6463 /* disable respawn */
6464 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006465 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006466 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006467
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006468 wl_list_remove(&shell->idle_listener.link);
6469 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006470 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006471
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006472 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006473 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006474
Xiong Zhang6b481422013-10-23 13:58:32 +08006475 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6476 wl_list_remove(&shell_output->destroy_listener.link);
6477 wl_list_remove(&shell_output->link);
6478 free(shell_output);
6479 }
6480
6481 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006482 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006483
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006484 wl_array_for_each(ws, &shell->workspaces.array)
6485 workspace_destroy(*ws);
6486 wl_array_release(&shell->workspaces.array);
6487
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006488 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006489 free(shell);
6490}
6491
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006492static void
6493shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6494{
6495 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006496 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006497
Bob Ham744e6532016-01-12 10:21:48 +00006498 if (shell->allow_zap)
6499 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6500 MODIFIER_CTRL | MODIFIER_ALT,
6501 terminate_binding, ec);
6502
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006503 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006504 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6505 click_to_activate_binding,
6506 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006507 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6508 click_to_activate_binding,
6509 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006510 weston_compositor_add_touch_binding(ec, 0,
6511 touch_to_activate_binding,
6512 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006513 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6514 MODIFIER_SUPER | MODIFIER_ALT,
6515 surface_opacity_binding, NULL);
6516 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6517 MODIFIER_SUPER, zoom_axis_binding,
6518 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006519 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6520 backlight_binding, ec);
6521 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6522 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006523
6524 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006525 if (shell->exposay_modifier)
6526 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6527 exposay_binding, shell);
6528
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006529 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006530 if (!mod)
6531 return;
6532
Daniel Stone325fc2d2012-05-30 16:31:58 +01006533 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6534 zoom_key_binding, NULL);
6535 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6536 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006537 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6538 maximize_binding, NULL);
6539 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6540 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006541 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6542 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006543 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006544 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006545 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006546 weston_compositor_add_button_binding(ec, BTN_LEFT,
6547 mod | MODIFIER_SHIFT,
6548 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006549
6550 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006551 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006552 rotate_binding, NULL);
6553
Daniel Stone325fc2d2012-05-30 16:31:58 +01006554 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6555 shell);
6556 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6557 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006558 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6559 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006560 weston_compositor_add_key_binding(ec, KEY_K, mod,
6561 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006562 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6563 workspace_up_binding, shell);
6564 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6565 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006566 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6567 workspace_move_surface_up_binding,
6568 shell);
6569 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6570 workspace_move_surface_down_binding,
6571 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006572
6573 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6574 if (shell->workspaces.num > 1) {
6575 num_workspace_bindings = shell->workspaces.num;
6576 if (num_workspace_bindings > 6)
6577 num_workspace_bindings = 6;
6578 for (i = 0; i < num_workspace_bindings; i++)
6579 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6580 workspace_f_binding,
6581 shell);
6582 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006583
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006584 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006585}
6586
Jason Ekstrand024177c2014-04-21 19:42:58 -05006587static void
6588handle_seat_created(struct wl_listener *listener, void *data)
6589{
6590 struct weston_seat *seat = data;
6591
6592 create_shell_seat(seat);
6593}
6594
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006595WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006596module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006597 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006598{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006599 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006600 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006601 struct workspace **pws;
6602 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006603 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006604
Peter Huttererf3d62272013-08-08 11:57:05 +10006605 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006606 if (shell == NULL)
6607 return -1;
6608
Kristian Høgsberg75840622011-09-06 13:48:16 -04006609 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006610
6611 shell->destroy_listener.notify = shell_destroy;
6612 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006613 shell->idle_listener.notify = idle_handler;
6614 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6615 shell->wake_listener.notify = wake_handler;
6616 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006617 shell->transform_listener.notify = transform_handler;
6618 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006619
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006620 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006621 ec->shell_interface.create_shell_surface = create_shell_surface;
6622 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006623 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006624 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006625 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006626 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006627 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006628 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006629 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006630 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006631 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006632 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006633
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006634 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6635 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006636 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6637 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006638 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006639
6640 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006641 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006642
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006643 if (input_panel_setup(shell) < 0)
6644 return -1;
6645
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006646 shell->text_backend = text_backend_init(ec);
6647 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006648 return -1;
6649
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006650 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006651
Daniel Stonedf8133b2013-11-19 11:37:14 +01006652 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6653 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6654
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006655 for (i = 0; i < shell->workspaces.num; i++) {
6656 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6657 if (pws == NULL)
6658 return -1;
6659
6660 *pws = workspace_create();
6661 if (*pws == NULL)
6662 return -1;
6663 }
6664 activate_workspace(shell, 0);
6665
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006666 weston_layer_init(&shell->minimized_layer, NULL);
6667
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006668 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006669 wl_list_init(&shell->workspaces.animation.link);
6670 shell->workspaces.animation.frame = animate_workspace_change_frame;
6671
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006672 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006673 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006674 return -1;
6675
Rafael Antognollie2a34552013-12-03 15:35:45 -02006676 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6677 shell, bind_xdg_shell) == NULL)
6678 return -1;
6679
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006680 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006681 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006682 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006683 return -1;
6684
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006685 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006686
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006687 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006688
Xiong Zhang6b481422013-10-23 13:58:32 +08006689 setup_output_destroy_handler(ec, shell);
6690
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006691 loop = wl_display_get_event_loop(ec->wl_display);
6692 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006693
Jason Ekstrand024177c2014-04-21 19:42:58 -05006694 wl_list_for_each(seat, &ec->seat_list, link)
6695 handle_seat_created(NULL, seat);
6696 shell->seat_create_listener.notify = handle_seat_created;
6697 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006698
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006699 screenshooter_create(ec);
6700
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006701 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006702
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006703 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006704
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006705 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6706
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006707 return 0;
6708}