blob: 3c6a3da7c4c0ab3ef6b2708520cf74b404303e34 [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"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070041#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070042#include "shared/helpers.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020043#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050044
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020046#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010048#ifndef static_assert
49#define static_assert(cond, msg)
50#endif
51
Jonas Ådahl04769742012-06-13 00:01:24 +020052struct focus_state {
53 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020062 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050063 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030064 SHELL_SURFACE_POPUP,
65 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020066};
67
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050068struct shell_client;
69
Philip Withnall648a4dd2013-11-25 18:01:44 +000070/*
71 * Surface stacking and ordering.
72 *
73 * This is handled using several linked lists of surfaces, organised into
74 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
75 * above all of the surfaces in the layer below. The set of layers is static and
76 * in the following order (top-most first):
77 * • Lock layer (only ever displayed on its own)
78 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010079 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Fullscreen layer
81 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000082 * • Workspace layers
83 * • Background layer
84 *
85 * The list of layers may be manipulated to remove whole layers of surfaces from
86 * display. For example, when locking the screen, all layers except the lock
87 * layer are removed.
88 *
89 * A surface’s layer is modified on configuring the surface, in
90 * set_surface_type() (which is only called when the surface’s type change is
91 * _committed_). If a surface’s type changes (e.g. when making a window
92 * fullscreen) its layer changes too.
93 *
94 * In order to allow popup and transient surfaces to be correctly stacked above
95 * their parent surfaces, each surface tracks both its parent surface, and a
96 * linked list of its children. When a surface’s layer is updated, so are the
97 * layers of its children. Note that child surfaces are *not* the same as
98 * subsurfaces — child/parent surfaces are purely for maintaining stacking
99 * order.
100 *
101 * The children_link list of siblings of a surface (i.e. those surfaces which
102 * have the same parent) only contains weston_surfaces which have a
103 * shell_surface. Stacking is not implemented for non-shell_surface
104 * weston_surfaces. This means that the following implication does *not* hold:
105 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
106 */
107
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200108struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500109 struct wl_resource *resource;
110 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500111 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800112 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500115 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600116 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700118 struct wl_listener resource_destroy_listener;
119
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400120 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000121 struct wl_list children_list; /* child surfaces of this one */
122 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300123 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200124
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800125 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400126 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500127 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200128 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800129 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200130 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800131 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700132 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800133 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100134
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500137 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200138 } rotation;
139
140 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100141 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100143 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400144 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 } popup;
146
Alex Wu4539b082012-03-01 12:57:46 +0800147 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300148 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400149 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300150 } transient;
151
152 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800153 enum wl_shell_surface_fullscreen_method type;
154 struct weston_transform transform; /* matrix from x, y */
155 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500156 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800157 } fullscreen;
158
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200159 struct weston_transform workspace_transform;
160
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500161 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500162 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100163 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400164
165 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700167 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200168 bool maximized;
169 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200170 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100171 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200173 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500174 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500175
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700176 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400177 int32_t x, y, width, height;
178 } geometry, next_geometry;
179 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700180
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500181 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500182
183 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400187 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188 struct shell_surface *shsurf;
189 struct wl_listener shsurf_destroy_listener;
190};
191
Rusty Lynch1084da52013-08-15 09:10:08 -0700192struct shell_touch_grab {
193 struct weston_touch_grab grab;
194 struct shell_surface *shsurf;
195 struct wl_listener shsurf_destroy_listener;
196 struct weston_touch *touch;
197};
198
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199struct weston_move_grab {
200 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100201 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500202 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203};
204
Rusty Lynch1084da52013-08-15 09:10:08 -0700205struct weston_touch_move_grab {
206 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800207 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700208 wl_fixed_t dx, dy;
209};
210
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300212 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500213 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200215 float x;
216 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200217 } center;
218};
219
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220struct shell_seat {
221 struct weston_seat *seat;
222 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500223 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224
Jason Ekstrand024177c2014-04-21 19:42:58 -0500225 struct wl_listener caps_changed_listener;
226 struct wl_listener pointer_focus_listener;
227 struct wl_listener keyboard_focus_listener;
228
Giulio Camuffo5085a752013-03-25 21:42:45 +0100229 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400230 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200231 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100232 struct wl_list surfaces_list;
233 struct wl_client *client;
234 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200235 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100236 } popup_grab;
237};
238
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800239struct shell_client {
240 struct wl_resource *resource;
241 struct wl_client *client;
242 struct desktop_shell *shell;
243 struct wl_listener destroy_listener;
244 struct wl_event_source *ping_timer;
245 uint32_t ping_serial;
246 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800247 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800248};
249
Alex Wubd3354b2012-04-17 17:20:49 +0800250static struct desktop_shell *
251shell_surface_get_shell(struct shell_surface *shsurf);
252
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500253static void
Derek Foreman74de4692015-07-15 13:00:39 -0500254surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500255
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300256static void
257shell_fade_startup(struct desktop_shell *shell);
258
Philip Withnallbecb77e2013-11-25 18:01:30 +0000259static struct shell_seat *
260get_shell_seat(struct weston_seat *seat);
261
Jonny Lambd73c6942014-08-20 15:53:20 +0200262static void
263get_output_panel_size(struct desktop_shell *shell,
264 struct weston_output *output,
265 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700266
Philip Withnall648a4dd2013-11-25 18:01:44 +0000267static void
268shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
269
Alex Wubd3354b2012-04-17 17:20:49 +0800270static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200271shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
272
273static bool
274shell_surface_is_xdg_surface(struct shell_surface *shsurf);
275
276static bool
277shell_surface_is_xdg_popup(struct shell_surface *shsurf);
278
279static void
280shell_surface_set_parent(struct shell_surface *shsurf,
281 struct weston_surface *parent);
282
Pekka Paalanen8274d902014-08-06 19:36:51 +0300283static int
284shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
285{
286 struct shell_surface *shsurf;
287 const char *typestr[] = {
288 [SHELL_SURFACE_NONE] = "unidentified",
289 [SHELL_SURFACE_TOPLEVEL] = "top-level",
290 [SHELL_SURFACE_POPUP] = "popup",
291 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
292 };
293 const char *t, *c;
294
295 shsurf = get_shell_surface(surface);
296 if (!shsurf)
297 return snprintf(buf, len, "unidentified window");
298
299 t = shsurf->title;
300 c = shsurf->class;
301
302 return snprintf(buf, len, "%s window%s%s%s%s%s",
303 typestr[shsurf->type],
304 t ? " '" : "", t ?: "", t ? "'" : "",
305 c ? " of " : "", c ?: "");
306}
307
Rafael Antognollie2a34552013-12-03 15:35:45 -0200308static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800309shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
310{
311 struct desktop_shell *shell;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200312 struct weston_view *view;
313 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800314
315 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100316
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300317 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800318 return false;
319
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200320 /* Find topmost shsurf on the same fullscreen output on which shsurf
321 * is displaying. We don't care about other outputs.
322 */
323 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
324 layer_link.link) {
325 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
326
327 if (cand_shsurf &&
328 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
329 top_fs_shsurf = cand_shsurf;
330 break;
331 }
332 }
333
334 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800335}
336
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500337static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339{
340 struct shell_grab *grab;
341
342 grab = container_of(listener, struct shell_grab,
343 shsurf_destroy_listener);
344
345 grab->shsurf = NULL;
346}
347
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800348struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349get_default_view(struct weston_surface *surface)
350{
351 struct shell_surface *shsurf;
352 struct weston_view *view;
353
354 if (!surface || wl_list_empty(&surface->views))
355 return NULL;
356
357 shsurf = get_shell_surface(surface);
358 if (shsurf)
359 return shsurf->view;
360
361 wl_list_for_each(view, &surface->views, surface_link)
362 if (weston_view_is_mapped(view))
363 return view;
364
365 return container_of(surface->views.next, struct weston_view, surface_link);
366}
367
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400369popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200370static void
371touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400372
373static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400377 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500381 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300382
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400383 popup_grab_end(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -0500384 if (touch)
385 touch_popup_grab_end(touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400386
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300387 grab->grab.interface = interface;
388 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400389 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500390 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400391 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300392
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700393 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400394 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400395 if (shell->child.desktop_shell) {
396 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
397 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398 weston_pointer_set_focus(pointer,
399 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400400 wl_fixed_from_int(0),
401 wl_fixed_from_int(0));
402 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300403}
404
Jonny Lambd73c6942014-08-20 15:53:20 +0200405static void
406get_output_panel_size(struct desktop_shell *shell,
407 struct weston_output *output,
408 int *width,
409 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700410{
411 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200412
413 *width = 0;
414 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700415
416 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200417 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700418
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300419 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200420 float x, y;
421
422 if (view->surface->output != output)
423 continue;
424
425 switch (shell->panel_position) {
426 case DESKTOP_SHELL_PANEL_POSITION_TOP:
427 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200428 weston_view_to_global_float(view,
429 view->surface->width, 0,
430 &x, &y);
431
Derek Foreman612341f2015-04-15 12:23:55 -0500432 *width = (int)x - output->x;
433 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200434 return;
435
436 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
437 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
438 weston_view_to_global_float(view,
439 0, view->surface->height,
440 &x, &y);
441
Derek Foreman612341f2015-04-15 12:23:55 -0500442 *width = view->surface->width + (int)x - output->x;
443 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200444 return;
445
446 default:
447 /* we've already set width and height to
448 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700449 break;
450 }
451 }
452
Jonny Lambd73c6942014-08-20 15:53:20 +0200453 /* the correct view wasn't found */
454}
455
456static void
457get_output_work_area(struct desktop_shell *shell,
458 struct weston_output *output,
459 pixman_rectangle32_t *area)
460{
461 int32_t panel_width = 0, panel_height = 0;
462
Derek Foremanf814c5d2015-04-15 12:23:54 -0500463 area->x = output->x;
464 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200465
466 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200467 switch (shell->panel_position) {
468 case DESKTOP_SHELL_PANEL_POSITION_TOP:
469 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500470 area->y += panel_height;
Jonny Lambd73c6942014-08-20 15:53:20 +0200471 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
472 area->width = output->width;
473 area->height = output->height - panel_height;
474 break;
475 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500476 area->x += panel_width;
Jonny Lambd73c6942014-08-20 15:53:20 +0200477 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
478 area->width = output->width - panel_width;
479 area->height = output->height;
480 break;
481 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700482}
483
Derek Foreman45a7c272015-09-11 14:27:40 -0500484static struct shell_surface *
485find_toplevel_surface(struct shell_surface *in_surface)
486{
487 struct shell_surface *surface = in_surface;
488
489 if (!surface)
490 return NULL;
491
492 while (surface->parent)
493 surface = get_shell_surface(surface->parent);
494
495 /* If no top level surface was found, just use whatever surface was
496 originally provided. */
497 if (!surface || surface->type != SHELL_SURFACE_TOPLEVEL)
498 surface = in_surface;
499
500 return surface;
501}
502
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700503static void
504send_configure_for_surface(struct shell_surface *shsurf)
505{
506 int32_t width, height;
507 struct surface_state *state;
508
509 if (shsurf->state_requested)
510 state = &shsurf->requested_state;
511 else if (shsurf->state_changed)
512 state = &shsurf->next_state;
513 else
514 state = &shsurf->state;
515
516 if (state->fullscreen) {
517 width = shsurf->output->width;
518 height = shsurf->output->height;
519 } else if (state->maximized) {
520 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200521 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700522
523 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200524 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700525
Jonny Lambd73c6942014-08-20 15:53:20 +0200526 width = area.width;
527 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900528 } else if (shsurf->resize_edges) {
529 width = shsurf->geometry.width;
530 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700531 } else {
532 width = 0;
533 height = 0;
534 }
535
536 shsurf->client->send_configure(shsurf->surface, width, height);
537}
538
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300539static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400540shell_surface_state_changed(struct shell_surface *shsurf)
541{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700542 if (shell_surface_is_xdg_surface(shsurf))
543 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400544}
545
546static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300547shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300548{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700549 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400550 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700551 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400552
553 if (grab->shsurf->resize_edges) {
554 grab->shsurf->resize_edges = 0;
555 shell_surface_state_changed(grab->shsurf);
556 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700557 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300558
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700559 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300560}
561
562static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700563shell_touch_grab_start(struct shell_touch_grab *grab,
564 const struct weston_touch_grab_interface *interface,
565 struct shell_surface *shsurf,
566 struct weston_touch *touch)
567{
568 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500569 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800570
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200571 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500572 if (pointer)
573 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700574
Rusty Lynch1084da52013-08-15 09:10:08 -0700575 grab->grab.interface = interface;
576 grab->shsurf = shsurf;
577 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
578 wl_signal_add(&shsurf->destroy_signal,
579 &grab->shsurf_destroy_listener);
580
581 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700582 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700583
584 weston_touch_start_grab(touch, &grab->grab);
585 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500586 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500587 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700588}
589
590static void
591shell_touch_grab_end(struct shell_touch_grab *grab)
592{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700593 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700594 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700595 grab->shsurf->grabbed = 0;
596 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700597
598 weston_touch_end_grab(grab->touch);
599}
600
601static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800603 struct weston_output *output);
604
Daniel Stone496ca172012-05-30 16:31:42 +0100605static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300606get_modifier(char *modifier)
607{
608 if (!modifier)
609 return MODIFIER_SUPER;
610
611 if (!strcmp("ctrl", modifier))
612 return MODIFIER_CTRL;
613 else if (!strcmp("alt", modifier))
614 return MODIFIER_ALT;
615 else if (!strcmp("super", modifier))
616 return MODIFIER_SUPER;
617 else
618 return MODIFIER_SUPER;
619}
620
Juan Zhaoe10d2792012-04-25 19:09:52 +0800621static enum animation_type
622get_animation_type(char *animation)
623{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800624 if (!animation)
625 return ANIMATION_NONE;
626
Juan Zhaoe10d2792012-04-25 19:09:52 +0800627 if (!strcmp("zoom", animation))
628 return ANIMATION_ZOOM;
629 else if (!strcmp("fade", animation))
630 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100631 else if (!strcmp("dim-layer", animation))
632 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800633 else
634 return ANIMATION_NONE;
635}
636
Alex Wu4539b082012-03-01 12:57:46 +0800637static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400638shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200639{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400640 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500641 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300642 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200643
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400644 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400645 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300646 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
647 WESTON_SHELL_CLIENT);
648 if (ret < 0)
649 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400650 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500651 "client", &s, client);
652 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100653 shell->client = s;
654 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400655 "binding-modifier", &s, "super");
656 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200657 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800658
659 weston_config_section_get_string(section,
660 "exposay-modifier", &s, "none");
661 if (strcmp(s, "none") == 0)
662 shell->exposay_modifier = 0;
663 else
664 shell->exposay_modifier = get_modifier(s);
665 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 Ådahle9d22502012-08-29 22:13:01 +02001190broadcast_current_workspace_state(struct desktop_shell *shell)
1191{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001192 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001193
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001194 wl_resource_for_each(resource, &shell->workspaces.client_list)
1195 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001196 shell->workspaces.current,
1197 shell->workspaces.num);
1198}
1199
1200static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001201reverse_workspace_change_animation(struct desktop_shell *shell,
1202 unsigned int index,
1203 struct workspace *from,
1204 struct workspace *to)
1205{
1206 shell->workspaces.current = index;
1207
1208 shell->workspaces.anim_to = to;
1209 shell->workspaces.anim_from = from;
1210 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1211 shell->workspaces.anim_timestamp = 0;
1212
Scott Moreau4272e632012-08-13 09:58:41 -06001213 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001214}
1215
1216static void
1217workspace_deactivate_transforms(struct workspace *ws)
1218{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001220 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001221
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001222 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001223 if (is_focus_view(view)) {
1224 struct focus_surface *fsurf = get_focus_surface(view->surface);
1225 transform = &fsurf->workspace_transform;
1226 } else {
1227 struct shell_surface *shsurf = get_shell_surface(view->surface);
1228 transform = &shsurf->workspace_transform;
1229 }
1230
1231 if (!wl_list_empty(&transform->link)) {
1232 wl_list_remove(&transform->link);
1233 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001236 }
1237}
1238
1239static void
1240finish_workspace_change_animation(struct desktop_shell *shell,
1241 struct workspace *from,
1242 struct workspace *to)
1243{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001244 struct weston_view *view;
1245
Scott Moreau4272e632012-08-13 09:58:41 -06001246 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001247
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001248 /* Views that extend past the bottom of the output are still
1249 * visible after the workspace animation ends but before its layer
1250 * is hidden. In that case, we need to damage below those views so
1251 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001252 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001253 weston_view_damage_below(view);
1254
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001255 wl_list_remove(&shell->workspaces.animation.link);
1256 workspace_deactivate_transforms(from);
1257 workspace_deactivate_transforms(to);
1258 shell->workspaces.anim_to = NULL;
1259
1260 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1261}
1262
1263static void
1264animate_workspace_change_frame(struct weston_animation *animation,
1265 struct weston_output *output, uint32_t msecs)
1266{
1267 struct desktop_shell *shell =
1268 container_of(animation, struct desktop_shell,
1269 workspaces.animation);
1270 struct workspace *from = shell->workspaces.anim_from;
1271 struct workspace *to = shell->workspaces.anim_to;
1272 uint32_t t;
1273 double x, y;
1274
1275 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1276 finish_workspace_change_animation(shell, from, to);
1277 return;
1278 }
1279
1280 if (shell->workspaces.anim_timestamp == 0) {
1281 if (shell->workspaces.anim_current == 0.0)
1282 shell->workspaces.anim_timestamp = msecs;
1283 else
1284 shell->workspaces.anim_timestamp =
1285 msecs -
1286 /* Invers of movement function 'y' below. */
1287 (asin(1.0 - shell->workspaces.anim_current) *
1288 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1289 M_2_PI);
1290 }
1291
1292 t = msecs - shell->workspaces.anim_timestamp;
1293
1294 /*
1295 * x = [0, π/2]
1296 * y(x) = sin(x)
1297 */
1298 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1299 y = sin(x);
1300
1301 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001302 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001303
1304 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1305 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1306 shell->workspaces.anim_current = y;
1307
Scott Moreau4272e632012-08-13 09:58:41 -06001308 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001309 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001310 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001311 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001312}
1313
1314static void
1315animate_workspace_change(struct desktop_shell *shell,
1316 unsigned int index,
1317 struct workspace *from,
1318 struct workspace *to)
1319{
1320 struct weston_output *output;
1321
1322 int dir;
1323
1324 if (index > shell->workspaces.current)
1325 dir = -1;
1326 else
1327 dir = 1;
1328
1329 shell->workspaces.current = index;
1330
1331 shell->workspaces.anim_dir = dir;
1332 shell->workspaces.anim_from = from;
1333 shell->workspaces.anim_to = to;
1334 shell->workspaces.anim_current = 0.0;
1335 shell->workspaces.anim_timestamp = 0;
1336
1337 output = container_of(shell->compositor->output_list.next,
1338 struct weston_output, link);
1339 wl_list_insert(&output->animation_list,
1340 &shell->workspaces.animation.link);
1341
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001342 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001343
1344 workspace_translate_in(to, 0);
1345
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001346 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001347
Scott Moreau4272e632012-08-13 09:58:41 -06001348 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001349}
1350
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001351static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001352update_workspace(struct desktop_shell *shell, unsigned int index,
1353 struct workspace *from, struct workspace *to)
1354{
1355 shell->workspaces.current = index;
1356 wl_list_insert(&from->layer.link, &to->layer.link);
1357 wl_list_remove(&from->layer.link);
1358}
1359
1360static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001361change_workspace(struct desktop_shell *shell, unsigned int index)
1362{
1363 struct workspace *from;
1364 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001365 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001366
1367 if (index == shell->workspaces.current)
1368 return;
1369
1370 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001371 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001372 return;
1373
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001374 from = get_current_workspace(shell);
1375 to = get_workspace(shell, index);
1376
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001377 if (shell->workspaces.anim_from == to &&
1378 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001379 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001380 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001381 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001382 return;
1383 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001384
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001385 if (shell->workspaces.anim_to != NULL)
1386 finish_workspace_change_animation(shell,
1387 shell->workspaces.anim_from,
1388 shell->workspaces.anim_to);
1389
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001390 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001391
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001392 if (shell->focus_animation_type != ANIMATION_NONE) {
1393 wl_list_for_each(state, &from->focus_list, link)
1394 if (state->keyboard_focus)
1395 animate_focus_change(shell, from,
1396 get_default_view(state->keyboard_focus), NULL);
1397
1398 wl_list_for_each(state, &to->focus_list, link)
1399 if (state->keyboard_focus)
1400 animate_focus_change(shell, to,
1401 NULL, get_default_view(state->keyboard_focus));
1402 }
1403
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001404 if (workspace_is_empty(to) && workspace_is_empty(from))
1405 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001406 else
1407 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001408
1409 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001410}
1411
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001412static bool
1413workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1414{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001415 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001416 struct wl_list *e;
1417
1418 if (wl_list_empty(list))
1419 return false;
1420
1421 e = list->next;
1422
1423 if (e->next != list)
1424 return false;
1425
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001426 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001427}
1428
1429static void
Philip Withnall659163d2013-11-25 18:01:36 +00001430move_surface_to_workspace(struct desktop_shell *shell,
1431 struct shell_surface *shsurf,
1432 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001433{
1434 struct workspace *from;
1435 struct workspace *to;
1436 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001437 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001438 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001439
1440 if (workspace == shell->workspaces.current)
1441 return;
1442
Philip Withnall659163d2013-11-25 18:01:36 +00001443 view = get_default_view(shsurf->surface);
1444 if (!view)
1445 return;
1446
1447 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1448
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001449 if (workspace >= shell->workspaces.num)
1450 workspace = shell->workspaces.num - 1;
1451
Jonas Ådahle9d22502012-08-29 22:13:01 +02001452 from = get_current_workspace(shell);
1453 to = get_workspace(shell, workspace);
1454
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001455 weston_layer_entry_remove(&view->layer_link);
1456 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001457
Philip Withnall648a4dd2013-11-25 18:01:44 +00001458 shell_surface_update_child_surface_layers(shsurf);
1459
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001461 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001462 struct weston_keyboard *keyboard =
1463 weston_seat_get_keyboard(seat);
1464
1465 if (!keyboard)
Pekka Paalanen01388e22013-04-25 13:57:44 +03001466 continue;
1467
Derek Foreman1281a362015-07-31 16:55:32 -05001468 focus = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 if (focus == view->surface)
Derek Foreman1281a362015-07-31 16:55:32 -05001470 weston_keyboard_set_focus(keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001471 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001472
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001474}
1475
1476static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001477take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001478 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001479 unsigned int index)
1480{
Derek Foreman1281a362015-07-31 16:55:32 -05001481 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001482 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001484 struct shell_surface *shsurf;
1485 struct workspace *from;
1486 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001487 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001488
Derek Foreman1281a362015-07-31 16:55:32 -05001489 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 view = get_default_view(surface);
1491 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001492 index == shell->workspaces.current ||
1493 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001494 return;
1495
1496 from = get_current_workspace(shell);
1497 to = get_workspace(shell, index);
1498
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001499 weston_layer_entry_remove(&view->layer_link);
1500 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001501
Philip Withnall659163d2013-11-25 18:01:36 +00001502 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001503 if (shsurf != NULL)
1504 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001505
Jonas Ådahle9d22502012-08-29 22:13:01 +02001506 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001507 drop_focus_state(shell, from, surface);
1508
1509 if (shell->workspaces.anim_from == to &&
1510 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001511 wl_list_remove(&to->layer.link);
1512 wl_list_insert(from->layer.link.prev, &to->layer.link);
1513
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001514 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001515 broadcast_current_workspace_state(shell);
1516
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001517 return;
1518 }
1519
1520 if (shell->workspaces.anim_to != NULL)
1521 finish_workspace_change_animation(shell,
1522 shell->workspaces.anim_from,
1523 shell->workspaces.anim_to);
1524
1525 if (workspace_is_empty(from) &&
1526 workspace_has_only(to, surface))
1527 update_workspace(shell, index, from, to);
1528 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001529 if (shsurf != NULL &&
1530 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001531 wl_list_insert(&shell->workspaces.anim_sticky_list,
1532 &shsurf->workspace_transform.link);
1533
1534 animate_workspace_change(shell, index, from, to);
1535 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001536
1537 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001538
1539 state = ensure_focus_state(shell, seat);
1540 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001541 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001542}
1543
1544static void
1545workspace_manager_move_surface(struct wl_client *client,
1546 struct wl_resource *resource,
1547 struct wl_resource *surface_resource,
1548 uint32_t workspace)
1549{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001550 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001551 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001552 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001553 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001554 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001555
Pekka Paalanen01388e22013-04-25 13:57:44 +03001556 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001557 shell_surface = get_shell_surface(main_surface);
1558 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001560
1561 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001562}
1563
1564static const struct workspace_manager_interface workspace_manager_implementation = {
1565 workspace_manager_move_surface,
1566};
1567
1568static void
1569unbind_resource(struct wl_resource *resource)
1570{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001571 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001572}
1573
1574static void
1575bind_workspace_manager(struct wl_client *client,
1576 void *data, uint32_t version, uint32_t id)
1577{
1578 struct desktop_shell *shell = data;
1579 struct wl_resource *resource;
1580
Jason Ekstranda85118c2013-06-27 20:17:02 -05001581 resource = wl_resource_create(client,
1582 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001583
1584 if (resource == NULL) {
1585 weston_log("couldn't add workspace manager object");
1586 return;
1587 }
1588
Jason Ekstranda85118c2013-06-27 20:17:02 -05001589 wl_resource_set_implementation(resource,
1590 &workspace_manager_implementation,
1591 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001592 wl_list_insert(&shell->workspaces.client_list,
1593 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001594
1595 workspace_manager_send_state(resource,
1596 shell->workspaces.current,
1597 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001598}
1599
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001600static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001601touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001602 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001603{
1604}
1605
1606static void
1607touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1608{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001609 struct weston_touch_move_grab *move =
1610 (struct weston_touch_move_grab *) container_of(
1611 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001612
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001613 if (touch_id == 0)
1614 move->active = 0;
1615
Jonas Ådahl9484b692013-12-02 22:05:03 +01001616 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001617 shell_touch_grab_end(&move->base);
1618 free(move);
1619 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001620}
1621
1622static void
1623touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001624 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001625{
1626 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1627 struct shell_surface *shsurf = move->base.shsurf;
1628 struct weston_surface *es;
1629 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1630 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1631
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001632 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001633 return;
1634
1635 es = shsurf->surface;
1636
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001637 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001638
1639 weston_compositor_schedule_repaint(es->compositor);
1640}
1641
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001642static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001643touch_move_grab_frame(struct weston_touch_grab *grab)
1644{
1645}
1646
1647static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001648touch_move_grab_cancel(struct weston_touch_grab *grab)
1649{
1650 struct weston_touch_move_grab *move =
1651 (struct weston_touch_move_grab *) container_of(
1652 grab, struct shell_touch_grab, grab);
1653
1654 shell_touch_grab_end(&move->base);
1655 free(move);
1656}
1657
Rusty Lynch1084da52013-08-15 09:10:08 -07001658static const struct weston_touch_grab_interface touch_move_grab_interface = {
1659 touch_move_grab_down,
1660 touch_move_grab_up,
1661 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001662 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001663 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001664};
1665
1666static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001667surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001668{
1669 struct weston_touch_move_grab *move;
1670
1671 if (!shsurf)
1672 return -1;
1673
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001674 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001675 return 0;
1676
1677 move = malloc(sizeof *move);
1678 if (!move)
1679 return -1;
1680
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001681 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001683 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001684 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001685 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001686
1687 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001688 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001689
1690 return 0;
1691}
1692
1693static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001694noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001695{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001696}
1697
1698static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001699constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1700{
1701 struct shell_surface *shsurf = move->base.shsurf;
1702 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001703 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001704 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001705 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001706
1707 x = wl_fixed_to_int(pointer->x + move->dx);
1708 y = wl_fixed_to_int(pointer->y + move->dy);
1709
Jonny Lambd73c6942014-08-20 15:53:20 +02001710 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001711 get_output_work_area(shsurf->shell,
1712 shsurf->surface->output,
1713 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001714
Derek Foreman6feb0f92015-04-15 12:23:56 -05001715 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1716 if (bottom - safety < area.y)
1717 y = area.y + safety - shsurf->geometry.height
1718 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001719
1720 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001721 y + shsurf->geometry.y < area.y)
1722 y = area.y - shsurf->geometry.y;
1723
Jonny Lambd73c6942014-08-20 15:53:20 +02001724 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001725
1726 *cx = x;
1727 *cy = y;
1728}
1729
1730static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001731move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1732 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001733{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001734 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001735 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001736 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001737 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001738
1739 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001740 if (!shsurf)
1741 return;
1742
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001743 constrain_position(move, &cx, &cy);
1744
1745 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001746
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001748}
1749
1750static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001751move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001752 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001753{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001754 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1755 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001756 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001757 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001758
Daniel Stone4dbadb12012-05-30 16:31:51 +01001759 if (pointer->button_count == 0 &&
1760 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001761 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001762 free(grab);
1763 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001764}
1765
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001766static void
1767move_grab_cancel(struct weston_pointer_grab *grab)
1768{
1769 struct shell_grab *shell_grab =
1770 container_of(grab, struct shell_grab, grab);
1771
1772 shell_grab_end(shell_grab);
1773 free(grab);
1774}
1775
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001776static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001777 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001778 move_grab_motion,
1779 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001780 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001781};
1782
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001783static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001784surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001785 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786{
1787 struct weston_move_grab *move;
1788
1789 if (!shsurf)
1790 return -1;
1791
Derek Foreman45a7c272015-09-11 14:27:40 -05001792 shsurf = find_toplevel_surface(shsurf);
1793
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001794 if (shsurf->grabbed ||
1795 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001796 return 0;
1797
1798 move = malloc(sizeof *move);
1799 if (!move)
1800 return -1;
1801
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001803 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001805 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001806 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001807
1808 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Derek Foreman794fa0e2015-07-15 13:00:38 -05001809 pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001810
1811 return 0;
1812}
1813
1814static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001815common_surface_move(struct wl_resource *resource,
1816 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001817{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001818 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001819 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1820 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001821 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001822 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001823
Derek Foreman1281a362015-07-31 16:55:32 -05001824 if (pointer &&
1825 pointer->focus &&
1826 pointer->button_count > 0 &&
1827 pointer->grab_serial == serial) {
1828 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001829 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001830 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001831 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001832 } else if (touch &&
1833 touch->focus &&
1834 touch->grab_serial == serial) {
1835 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001836 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001837 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001838 wl_resource_post_no_memory(resource);
1839 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001840}
1841
Rafael Antognollie2a34552013-12-03 15:35:45 -02001842static void
1843shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1844 struct wl_resource *seat_resource, uint32_t serial)
1845{
1846 common_surface_move(resource, seat_resource, serial);
1847}
1848
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001849struct weston_resize_grab {
1850 struct shell_grab base;
1851 uint32_t edges;
1852 int32_t width, height;
1853};
1854
1855static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001856resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1857 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001858{
1859 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001860 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001861 struct shell_surface *shsurf = resize->base.shsurf;
1862 int32_t width, height;
1863 wl_fixed_t from_x, from_y;
1864 wl_fixed_t to_x, to_y;
1865
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001866 weston_pointer_move(pointer, x, y);
1867
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001868 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001869 return;
1870
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 weston_view_from_global_fixed(shsurf->view,
1872 pointer->grab_x, pointer->grab_y,
1873 &from_x, &from_y);
1874 weston_view_from_global_fixed(shsurf->view,
1875 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001876
1877 width = resize->width;
1878 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1879 width += wl_fixed_to_int(from_x - to_x);
1880 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1881 width += wl_fixed_to_int(to_x - from_x);
1882 }
1883
1884 height = resize->height;
1885 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1886 height += wl_fixed_to_int(from_y - to_y);
1887 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1888 height += wl_fixed_to_int(to_y - from_y);
1889 }
1890
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001891 if (width < 1)
1892 width = 1;
1893 if (height < 1)
1894 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001895 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001896}
1897
1898static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001899send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001900{
1901 struct shell_surface *shsurf = get_shell_surface(surface);
1902
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001903 assert(shsurf);
1904
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001905 if (shsurf->resource)
1906 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001907 shsurf->resize_edges,
1908 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001909}
1910
1911static const struct weston_shell_client shell_client = {
1912 send_configure
1913};
1914
1915static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001916resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001917 uint32_t time, uint32_t button, uint32_t state_w)
1918{
1919 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001920 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001921 enum wl_pointer_button_state state = state_w;
1922
1923 if (pointer->button_count == 0 &&
1924 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1925 shell_grab_end(&resize->base);
1926 free(grab);
1927 }
1928}
1929
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001930static void
1931resize_grab_cancel(struct weston_pointer_grab *grab)
1932{
1933 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1934
1935 shell_grab_end(&resize->base);
1936 free(grab);
1937}
1938
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001939static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001940 noop_grab_focus,
1941 resize_grab_motion,
1942 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001943 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001944};
1945
Giulio Camuffob8366642013-04-25 13:57:46 +03001946/*
1947 * Returns the bounding box of a surface and all its sub-surfaces,
1948 * in the surface coordinates system. */
1949static void
1950surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1951 int32_t *y, int32_t *w, int32_t *h) {
1952 pixman_region32_t region;
1953 pixman_box32_t *box;
1954 struct weston_subsurface *subsurface;
1955
1956 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001957 surface->width,
1958 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001959
1960 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1961 pixman_region32_union_rect(&region, &region,
1962 subsurface->position.x,
1963 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964 subsurface->surface->width,
1965 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001966 }
1967
1968 box = pixman_region32_extents(&region);
1969 if (x)
1970 *x = box->x1;
1971 if (y)
1972 *y = box->y1;
1973 if (w)
1974 *w = box->x2 - box->x1;
1975 if (h)
1976 *h = box->y2 - box->y1;
1977
1978 pixman_region32_fini(&region);
1979}
1980
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001981static int
1982surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001983 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001984{
1985 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001986 const unsigned resize_topbottom =
1987 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1988 const unsigned resize_leftright =
1989 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1990 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001991
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001992 if (shsurf->grabbed ||
1993 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001994 return 0;
1995
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001996 /* Check for invalid edge combinations. */
1997 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1998 (edges & resize_topbottom) == resize_topbottom ||
1999 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002000 return 0;
2001
2002 resize = malloc(sizeof *resize);
2003 if (!resize)
2004 return -1;
2005
2006 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002007
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002008 resize->width = shsurf->geometry.width;
2009 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04002010
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08002011 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04002012 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002013 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05002014 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002015
2016 return 0;
2017}
2018
2019static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002020common_surface_resize(struct wl_resource *resource,
2021 struct wl_resource *seat_resource, uint32_t serial,
2022 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002023{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002024 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002025 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002026 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03002027 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002028
Derek Foreman1281a362015-07-31 16:55:32 -05002029 if (!pointer ||
2030 pointer->button_count == 0 ||
2031 pointer->grab_serial != serial ||
2032 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08002033 return;
2034
Derek Foreman1281a362015-07-31 16:55:32 -05002035 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08002036 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002037 return;
2038
Derek Foreman1281a362015-07-31 16:55:32 -05002039 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002040 wl_resource_post_no_memory(resource);
2041}
2042
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002043static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002044shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2045 struct wl_resource *seat_resource, uint32_t serial,
2046 uint32_t edges)
2047{
2048 common_surface_resize(resource, seat_resource, serial, edges);
2049}
2050
2051static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002052busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002053{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002054 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002055 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002057 wl_fixed_t sx, sy;
2058
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059 view = weston_compositor_pick_view(pointer->seat->compositor,
2060 pointer->x, pointer->y,
2061 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002062
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002063 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2064 shell_grab_end(grab);
2065 free(grab);
2066 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002067}
2068
2069static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002070busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2071 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002072{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002073 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002074}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002075
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002076static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002077busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002078 uint32_t time, uint32_t button, uint32_t state)
2079{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002080 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002081 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002082 struct weston_pointer *pointer = grab->grab.pointer;
2083 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002084
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002085 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002086 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002087 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002088 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002089 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002090 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002091 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002092}
2093
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002094static void
2095busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2096{
2097 struct shell_grab *grab = (struct shell_grab *) base;
2098
2099 shell_grab_end(grab);
2100 free(grab);
2101}
2102
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002103static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002104 busy_cursor_grab_focus,
2105 busy_cursor_grab_motion,
2106 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002107 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002108};
2109
2110static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002111set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002112{
2113 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002114
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002115 if (pointer->grab->interface == &busy_cursor_grab_interface)
2116 return;
2117
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002118 grab = malloc(sizeof *grab);
2119 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002120 return;
2121
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002122 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2123 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002124 /* Mark the shsurf as ungrabbed so that button binding is able
2125 * to move it. */
2126 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002127}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002128
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002129static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002130end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002131{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002132 struct shell_grab *grab;
2133 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002134
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002135 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002136 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2137
2138 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002139 continue;
2140
Derek Foreman1281a362015-07-31 16:55:32 -05002141 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002142 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002143 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002144 wl_resource_get_client(grab->shsurf->resource) == client) {
2145 shell_grab_end(grab);
2146 free(grab);
2147 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002148 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002149}
2150
Scott Moreau9521d5e2012-04-19 13:06:17 -06002151static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002152handle_shell_client_destroy(struct wl_listener *listener, void *data);
2153
2154static int
2155xdg_ping_timeout_handler(void *data)
2156{
2157 struct shell_client *sc = data;
2158 struct weston_seat *seat;
2159 struct shell_surface *shsurf;
2160
2161 /* Client is not responding */
2162 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002163 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002164 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2165
2166 if (!pointer ||
2167 !pointer->focus ||
2168 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002169 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002170
Derek Foreman1281a362015-07-31 16:55:32 -05002171 shsurf = get_shell_surface(pointer->focus->surface);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002172 if (shsurf &&
2173 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002174 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002175 }
2176
2177 return 1;
2178}
2179
2180static void
2181handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2182{
2183 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002184 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002185 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002186 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002187
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002188 if (sc->unresponsive) {
2189 xdg_ping_timeout_handler(sc);
2190 return;
2191 }
2192
2193 sc->ping_serial = serial;
2194 loop = wl_display_get_event_loop(compositor->wl_display);
2195 if (sc->ping_timer == NULL)
2196 sc->ping_timer =
2197 wl_event_loop_add_timer(loop,
2198 xdg_ping_timeout_handler, sc);
2199 if (sc->ping_timer == NULL)
2200 return;
2201
2202 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2203
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002204 if (shell_surface_is_xdg_surface(shsurf) ||
2205 shell_surface_is_xdg_popup(shsurf))
2206 xdg_shell_send_ping(sc->resource, serial);
2207 else if (shell_surface_is_wl_shell_surface(shsurf))
2208 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002209}
2210
Scott Moreauff1db4a2012-04-17 19:06:18 -06002211static void
2212ping_handler(struct weston_surface *surface, uint32_t serial)
2213{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002214 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002215
2216 if (!shsurf)
2217 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002218 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002219 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002220 if (shsurf->surface == shsurf->shell->grab_surface)
2221 return;
2222
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002223 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002224}
2225
2226static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002227handle_pointer_focus(struct wl_listener *listener, void *data)
2228{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002229 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002231 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002232 uint32_t serial;
2233
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002235 return;
2236
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002238 serial = wl_display_next_serial(compositor->wl_display);
2239 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002240}
2241
2242static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002243shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2244{
2245 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002246 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002247}
2248
2249static void
2250shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2251{
2252 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002253 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002254}
2255
2256static void
2257handle_keyboard_focus(struct wl_listener *listener, void *data)
2258{
2259 struct weston_keyboard *keyboard = data;
2260 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2261
2262 if (seat->focused_surface) {
2263 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2264 if (shsurf)
2265 shell_surface_lose_keyboard_focus(shsurf);
2266 }
2267
2268 seat->focused_surface = keyboard->focus;
2269
2270 if (seat->focused_surface) {
2271 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2272 if (shsurf)
2273 shell_surface_gain_keyboard_focus(shsurf);
2274 }
2275}
2276
2277static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002278shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002279{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002280 if (sc->ping_serial != serial)
2281 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002282
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002283 sc->unresponsive = 0;
2284 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002285
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002286 if (sc->ping_timer) {
2287 wl_event_source_remove(sc->ping_timer);
2288 sc->ping_timer = NULL;
2289 }
2290
2291}
2292
2293static void
2294shell_surface_pong(struct wl_client *client,
2295 struct wl_resource *resource, uint32_t serial)
2296{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002297 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2298 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002299
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002300 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002301}
2302
2303static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002304set_title(struct shell_surface *shsurf, const char *title)
2305{
2306 free(shsurf->title);
2307 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002308 shsurf->surface->timeline.force_refresh = 1;
2309}
2310
2311static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002312set_pid(struct shell_surface *shsurf, pid_t pid)
2313{
2314 /* We have no use for it */
2315}
2316
2317static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002318set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2319{
2320 shsurf->type = t;
2321 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002322}
2323
2324static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002325set_window_geometry(struct shell_surface *shsurf,
2326 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002327{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002328 shsurf->next_geometry.x = x;
2329 shsurf->next_geometry.y = y;
2330 shsurf->next_geometry.width = width;
2331 shsurf->next_geometry.height = height;
2332 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002333}
2334
2335static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002336shell_surface_set_title(struct wl_client *client,
2337 struct wl_resource *resource, const char *title)
2338{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002339 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002340
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002341 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002342}
2343
2344static void
2345shell_surface_set_class(struct wl_client *client,
2346 struct wl_resource *resource, const char *class)
2347{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002348 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002349
2350 free(shsurf->class);
2351 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002352 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002353}
2354
Alex Wu4539b082012-03-01 12:57:46 +08002355static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002356restore_output_mode(struct weston_output *output)
2357{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002358 if (output->original_mode)
2359 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002360}
2361
2362static void
2363restore_all_output_modes(struct weston_compositor *compositor)
2364{
2365 struct weston_output *output;
2366
2367 wl_list_for_each(output, &compositor->output_list, link)
2368 restore_output_mode(output);
2369}
2370
Philip Withnall07926d92013-11-25 18:01:40 +00002371/* The surface will be inserted into the list immediately after the link
2372 * returned by this function (i.e. will be stacked immediately above the
2373 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002375shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2376{
2377 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002378 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002379
2380 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002381 case SHELL_SURFACE_XWAYLAND:
2382 return &shsurf->shell->fullscreen_layer.view_list;
2383
2384 case SHELL_SURFACE_NONE:
2385 return NULL;
2386
Kristian Høgsbergead52422014-01-01 13:51:52 -08002387 case SHELL_SURFACE_POPUP:
2388 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002389 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002390 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002391 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002392 /* Move the surface to its parent layer so
2393 * that surfaces which are transient for
2394 * fullscreen surfaces don't get hidden by the
2395 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002396
2397 /* TODO: Handle a parent with multiple views */
2398 parent = get_default_view(shsurf->parent);
2399 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002400 return container_of(parent->layer_link.link.prev,
2401 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002402 }
Philip Withnall07926d92013-11-25 18:01:40 +00002403
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002404 /* Move the surface to a normal workspace layer so that surfaces
2405 * which were previously fullscreen or transient are no longer
2406 * rendered on top. */
2407 ws = get_current_workspace(shsurf->shell);
2408 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002409 }
2410
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002411 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002412}
2413
Philip Withnall648a4dd2013-11-25 18:01:44 +00002414static void
2415shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2416{
2417 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002418 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002419
2420 /* Move the child layers to the same workspace as shsurf. They will be
2421 * stacked above shsurf. */
2422 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002423 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002424 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002425 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002426 prev = container_of(shsurf->view->layer_link.link.prev,
2427 struct weston_layer_entry, link);
2428 weston_layer_entry_remove(&child->view->layer_link);
2429 weston_layer_entry_insert(prev,
2430 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002431 weston_view_geometry_dirty(child->view);
2432 weston_surface_damage(child->surface);
2433
2434 /* Recurse. We don’t expect this to recurse very far (if
2435 * at all) because that would imply we have transient
2436 * (or popup) children of transient surfaces, which
2437 * would be unusual. */
2438 shell_surface_update_child_surface_layers(child);
2439 }
2440 }
2441}
2442
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002443/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002444 * geometry to ensure the changes are redrawn.
2445 *
2446 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2447 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002448static void
2449shell_surface_update_layer(struct shell_surface *shsurf)
2450{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002451 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002452
2453 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2454
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002455 if (new_layer_link == NULL)
2456 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002457 if (new_layer_link == &shsurf->view->layer_link)
2458 return;
2459
2460 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002461 weston_layer_entry_remove(&shsurf->view->layer_link);
2462 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002463 weston_view_geometry_dirty(shsurf->view);
2464 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002465
2466 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002467}
2468
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002469static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002470shell_surface_set_parent(struct shell_surface *shsurf,
2471 struct weston_surface *parent)
2472{
2473 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002474
2475 wl_list_remove(&shsurf->children_link);
2476 wl_list_init(&shsurf->children_link);
2477
2478 /* Insert into the parent surface’s child list. */
2479 if (parent != NULL) {
2480 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2481 if (parent_shsurf != NULL)
2482 wl_list_insert(&parent_shsurf->children_list,
2483 &shsurf->children_link);
2484 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002485}
2486
2487static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002488shell_surface_set_output(struct shell_surface *shsurf,
2489 struct weston_output *output)
2490{
2491 struct weston_surface *es = shsurf->surface;
2492
2493 /* get the default output, if the client set it as NULL
2494 check whether the ouput is available */
2495 if (output)
2496 shsurf->output = output;
2497 else if (es->output)
2498 shsurf->output = es->output;
2499 else
2500 shsurf->output = get_default_output(es->compositor);
2501}
2502
2503static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002504surface_clear_next_states(struct shell_surface *shsurf)
2505{
2506 shsurf->next_state.maximized = false;
2507 shsurf->next_state.fullscreen = false;
2508
2509 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2510 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2511 shsurf->state_changed = true;
2512}
2513
2514static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002515set_toplevel(struct shell_surface *shsurf)
2516{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002517 shell_surface_set_parent(shsurf, NULL);
2518 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002519 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002520
2521 /* The layer_link is updated in set_surface_type(),
2522 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002523}
2524
2525static void
2526shell_surface_set_toplevel(struct wl_client *client,
2527 struct wl_resource *resource)
2528{
2529 struct shell_surface *surface = wl_resource_get_user_data(resource);
2530
2531 set_toplevel(surface);
2532}
2533
2534static void
2535set_transient(struct shell_surface *shsurf,
2536 struct weston_surface *parent, int x, int y, uint32_t flags)
2537{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002538 assert(parent != NULL);
2539
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002540 shell_surface_set_parent(shsurf, parent);
2541
2542 surface_clear_next_states(shsurf);
2543
Philip Withnallbecb77e2013-11-25 18:01:30 +00002544 shsurf->transient.x = x;
2545 shsurf->transient.y = y;
2546 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002547
Rafael Antognollied207b42013-12-03 15:35:43 -02002548 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002549 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002550 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002551
2552 /* The layer_link is updated in set_surface_type(),
2553 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002554}
2555
2556static void
2557shell_surface_set_transient(struct wl_client *client,
2558 struct wl_resource *resource,
2559 struct wl_resource *parent_resource,
2560 int x, int y, uint32_t flags)
2561{
2562 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2563 struct weston_surface *parent =
2564 wl_resource_get_user_data(parent_resource);
2565
2566 set_transient(shsurf, parent, x, y, flags);
2567}
2568
2569static void
2570set_fullscreen(struct shell_surface *shsurf,
2571 uint32_t method,
2572 uint32_t framerate,
2573 struct weston_output *output)
2574{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002575 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002576 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002577
2578 shsurf->fullscreen_output = shsurf->output;
2579 shsurf->fullscreen.type = method;
2580 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002581
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002582 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002583}
2584
2585static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002586weston_view_set_initial_position(struct weston_view *view,
2587 struct desktop_shell *shell);
2588
2589static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002590unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002591{
Philip Withnallf85fe842013-11-25 18:01:37 +00002592 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002593 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2594 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002595 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002596 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002597
Alex Wu4539b082012-03-01 12:57:46 +08002598 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2599 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002600
Alex Wu4539b082012-03-01 12:57:46 +08002601 wl_list_remove(&shsurf->fullscreen.transform.link);
2602 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002603
Jason Ekstranda7af7042013-10-12 22:38:11 -05002604 if (shsurf->fullscreen.black_view)
2605 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2606 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002607
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002608 if (shsurf->saved_position_valid)
2609 weston_view_set_position(shsurf->view,
2610 shsurf->saved_x, shsurf->saved_y);
2611 else
2612 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2613
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002614 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002615 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002616 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002617 shsurf->saved_rotation_valid = false;
2618 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002619
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002620 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002621}
2622
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002623static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002624shell_surface_set_fullscreen(struct wl_client *client,
2625 struct wl_resource *resource,
2626 uint32_t method,
2627 uint32_t framerate,
2628 struct wl_resource *output_resource)
2629{
2630 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2631 struct weston_output *output;
2632
2633 if (output_resource)
2634 output = wl_resource_get_user_data(output_resource);
2635 else
2636 output = NULL;
2637
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002638 shell_surface_set_parent(shsurf, NULL);
2639
Rafael Antognolli03b16592013-12-03 15:35:42 -02002640 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002641 shsurf->next_state.fullscreen = true;
2642 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002643 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002644}
2645
2646static void
2647set_popup(struct shell_surface *shsurf,
2648 struct weston_surface *parent,
2649 struct weston_seat *seat,
2650 uint32_t serial,
2651 int32_t x,
2652 int32_t y)
2653{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002654 assert(parent != NULL);
2655
Philip Withnallbecb77e2013-11-25 18:01:30 +00002656 shsurf->popup.shseat = get_shell_seat(seat);
2657 shsurf->popup.serial = serial;
2658 shsurf->popup.x = x;
2659 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002660
Pekka Paalanenb5026542014-11-12 15:09:24 +02002661 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002662}
2663
2664static void
2665shell_surface_set_popup(struct wl_client *client,
2666 struct wl_resource *resource,
2667 struct wl_resource *seat_resource,
2668 uint32_t serial,
2669 struct wl_resource *parent_resource,
2670 int32_t x, int32_t y, uint32_t flags)
2671{
2672 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002673 struct weston_surface *parent =
2674 wl_resource_get_user_data(parent_resource);
2675
2676 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002677
Rafael Antognolli03b16592013-12-03 15:35:42 -02002678 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002679 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002680 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002681 wl_resource_get_user_data(seat_resource),
2682 serial, x, y);
2683}
2684
2685static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002686unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002687{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002688 /* undo all maximized things here */
2689 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002690
2691 if (shsurf->saved_position_valid)
2692 weston_view_set_position(shsurf->view,
2693 shsurf->saved_x, shsurf->saved_y);
2694 else
2695 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002696
2697 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002698 wl_list_insert(&shsurf->view->geometry.transformation_list,
2699 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002700 shsurf->saved_rotation_valid = false;
2701 }
2702
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002703 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002704}
2705
Philip Withnallbecb77e2013-11-25 18:01:30 +00002706static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002707set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002708{
2709 struct shell_surface *shsurf;
2710 struct workspace *current_ws;
2711 struct weston_seat *seat;
2712 struct weston_surface *focus;
2713 struct weston_view *view;
2714
2715 view = get_default_view(surface);
2716 if (!view)
2717 return;
2718
2719 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2720
2721 shsurf = get_shell_surface(surface);
2722 current_ws = get_current_workspace(shsurf->shell);
2723
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002724 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002725 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002726
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002727 drop_focus_state(shsurf->shell, current_ws, view->surface);
2728 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002729 struct weston_keyboard *keyboard =
2730 weston_seat_get_keyboard(seat);
2731
2732 if (!keyboard)
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002733 continue;
Derek Foreman1281a362015-07-31 16:55:32 -05002734 focus = weston_surface_get_main_surface(keyboard->focus);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002735 if (focus == view->surface)
Derek Foreman1281a362015-07-31 16:55:32 -05002736 weston_keyboard_set_focus(keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002737 }
2738
2739 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002740 weston_view_damage_below(view);
2741}
2742
2743static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002744shell_surface_set_maximized(struct wl_client *client,
2745 struct wl_resource *resource,
2746 struct wl_resource *output_resource)
2747{
2748 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2749 struct weston_output *output;
2750
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002751 surface_clear_next_states(shsurf);
2752 shsurf->next_state.maximized = true;
2753 shsurf->state_changed = true;
2754
Pekka Paalanenb5026542014-11-12 15:09:24 +02002755 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002756 shell_surface_set_parent(shsurf, NULL);
2757
Philip Withnallbecb77e2013-11-25 18:01:30 +00002758 if (output_resource)
2759 output = wl_resource_get_user_data(output_resource);
2760 else
2761 output = NULL;
2762
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002763 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002764
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002765 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002766}
2767
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002768/* This is only ever called from set_surface_type(), so there’s no need to
2769 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002770static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002771reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002772{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002773 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002774 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002775 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002776 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002777
Pekka Paalanen98262232011-12-01 10:42:22 +02002778 return 0;
2779}
2780
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002781static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002782set_full_output(struct shell_surface *shsurf)
2783{
2784 shsurf->saved_x = shsurf->view->geometry.x;
2785 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002786 shsurf->saved_width = shsurf->surface->width;
2787 shsurf->saved_height = shsurf->surface->height;
2788 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002789 shsurf->saved_position_valid = true;
2790
2791 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2792 wl_list_remove(&shsurf->rotation.transform.link);
2793 wl_list_init(&shsurf->rotation.transform.link);
2794 weston_view_geometry_dirty(shsurf->view);
2795 shsurf->saved_rotation_valid = true;
2796 }
2797}
2798
2799static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002800set_surface_type(struct shell_surface *shsurf)
2801{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002802 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002804
Philip Withnallbecb77e2013-11-25 18:01:30 +00002805 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002806
Rafael Antognolli03b16592013-12-03 15:35:42 -02002807 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002808 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002809
2810 switch (shsurf->type) {
2811 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002812 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002813 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002814 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002815 weston_view_set_position(shsurf->view,
2816 pev->geometry.x + shsurf->transient.x,
2817 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002818 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002819 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002820
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002821 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822 weston_view_set_position(shsurf->view, shsurf->transient.x,
2823 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002824 break;
2825
Philip Withnall0f640e22013-11-25 18:01:31 +00002826 case SHELL_SURFACE_POPUP:
2827 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002828 default:
2829 break;
2830 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002831
2832 /* Update the surface’s layer. */
2833 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002834}
2835
Tiago Vignattibe143262012-04-16 17:31:41 +03002836static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002837shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002838{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002839 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002840}
2841
Pekka Paalanen8274d902014-08-06 19:36:51 +03002842static int
2843black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2844{
2845 struct weston_surface *fs_surface = surface->configure_private;
2846 int n;
2847 int rem;
2848 int ret;
2849
2850 n = snprintf(buf, len, "black background surface for ");
2851 if (n < 0)
2852 return n;
2853
2854 rem = (int)len - n;
2855 if (rem < 0)
2856 rem = 0;
2857
2858 if (fs_surface->get_label)
2859 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2860 else
2861 ret = snprintf(buf + n, rem, "<unknown>");
2862
2863 if (ret < 0)
2864 return n;
2865
2866 return n + ret;
2867}
2868
Alex Wu21858432012-04-01 20:13:08 +08002869static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002870black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002871
Jason Ekstranda7af7042013-10-12 22:38:11 -05002872static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002873create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002874 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002875 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002876{
2877 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002878 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002879
2880 surface = weston_surface_create(ec);
2881 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002882 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002883 return NULL;
2884 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002885 view = weston_view_create(surface);
2886 if (surface == NULL) {
2887 weston_log("no memory\n");
2888 weston_surface_destroy(surface);
2889 return NULL;
2890 }
Alex Wu4539b082012-03-01 12:57:46 +08002891
Alex Wu21858432012-04-01 20:13:08 +08002892 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002893 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002894 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002895 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002896 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002897 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002898 pixman_region32_fini(&surface->input);
2899 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002900
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002901 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002902 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002903
2904 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002905}
2906
Philip Withnalled8f1a92013-11-25 18:01:43 +00002907static void
2908shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2909{
2910 struct weston_output *output = shsurf->fullscreen_output;
2911
Rafael Antognolli03b16592013-12-03 15:35:42 -02002912 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002913
2914 if (!shsurf->fullscreen.black_view)
2915 shsurf->fullscreen.black_view =
2916 create_black_surface(shsurf->surface->compositor,
2917 shsurf->surface,
2918 output->x, output->y,
2919 output->width,
2920 output->height);
2921
2922 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002923 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2924 weston_layer_entry_insert(&shsurf->view->layer_link,
2925 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002926 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2927 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002928
2929 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002930}
2931
Alex Wu4539b082012-03-01 12:57:46 +08002932/* Create black surface and append it to the associated fullscreen surface.
2933 * Handle size dismatch and positioning according to the method. */
2934static void
2935shell_configure_fullscreen(struct shell_surface *shsurf)
2936{
2937 struct weston_output *output = shsurf->fullscreen_output;
2938 struct weston_surface *surface = shsurf->surface;
2939 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002940 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002941 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002942
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002943 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2944 restore_output_mode(output);
2945
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002946 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002947 weston_layer_entry_remove(&shsurf->view->layer_link);
2948 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002949
Philip Withnalled8f1a92013-11-25 18:01:43 +00002950 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002951
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002953 &surf_width, &surf_height);
2954
Alex Wu4539b082012-03-01 12:57:46 +08002955 switch (shsurf->fullscreen.type) {
2956 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002957 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002958 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002959 break;
2960 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002961 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002962 if (output->width == surf_width &&
2963 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002964 weston_view_set_position(shsurf->view,
2965 output->x - surf_x,
2966 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002967 break;
2968 }
2969
Alex Wu4539b082012-03-01 12:57:46 +08002970 matrix = &shsurf->fullscreen.transform.matrix;
2971 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002972
Scott Moreau1bad5db2012-08-18 01:04:05 -06002973 output_aspect = (float) output->width /
2974 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002975 /* XXX: Use surf_width and surf_height here? */
2976 surface_aspect = (float) surface->width /
2977 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002978 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002979 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002980 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002981 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002982 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002983 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002984
Alex Wu4539b082012-03-01 12:57:46 +08002985 weston_matrix_scale(matrix, scale, scale, 1);
2986 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002987 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002988 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002989 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2990 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002992
Alex Wu4539b082012-03-01 12:57:46 +08002993 break;
2994 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002995 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002996 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002997 surf_width * surface->buffer_viewport.buffer.scale,
2998 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002999 shsurf->fullscreen.framerate};
3000
Derek Foreman6ae7bc92014-11-04 10:47:33 -06003001 if (weston_output_mode_switch_to_temporary(output, &mode,
3002 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003003 weston_view_set_position(shsurf->view,
3004 output->x - surf_x,
3005 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003006 shsurf->fullscreen.black_view->surface->width = output->width;
3007 shsurf->fullscreen.black_view->surface->height = output->height;
3008 weston_view_set_position(shsurf->fullscreen.black_view,
3009 output->x - surf_x,
3010 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08003011 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02003012 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02003013 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003014 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003015 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02003016 }
Alex Wubd3354b2012-04-17 17:20:49 +08003017 }
Alex Wu4539b082012-03-01 12:57:46 +08003018 break;
3019 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08003021 break;
3022 default:
3023 break;
3024 }
3025}
3026
Alex Wu4539b082012-03-01 12:57:46 +08003027static void
3028shell_map_fullscreen(struct shell_surface *shsurf)
3029{
Alex Wubd3354b2012-04-17 17:20:49 +08003030 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003031}
3032
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003033static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003034set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
3035{
3036 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02003037 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003038 shsurf->transient.x = x;
3039 shsurf->transient.y = y;
3040 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00003041
3042 shell_surface_set_parent(shsurf, NULL);
3043
Pekka Paalanenb5026542014-11-12 15:09:24 +02003044 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08003045 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003046}
3047
Kristian Høgsberg47792412014-05-04 13:47:06 -07003048static void
3049shell_interface_set_fullscreen(struct shell_surface *shsurf,
3050 uint32_t method,
3051 uint32_t framerate,
3052 struct weston_output *output)
3053{
3054 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003055 shsurf->next_state.fullscreen = true;
3056 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003057
3058 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003059}
3060
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003061static struct weston_output *
3062get_focused_output(struct weston_compositor *compositor)
3063{
3064 struct weston_seat *seat;
3065 struct weston_output *output = NULL;
3066
3067 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003068 struct weston_touch *touch = weston_seat_get_touch(seat);
3069 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3070 struct weston_keyboard *keyboard =
3071 weston_seat_get_keyboard(seat);
3072
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003073 /* Priority has touch focus, then pointer and
3074 * then keyboard focus. We should probably have
3075 * three for loops and check frist for touch,
3076 * then for pointer, etc. but unless somebody has some
3077 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003078 if (touch && touch->focus)
3079 output = touch->focus->output;
3080 else if (pointer && pointer->focus)
3081 output = pointer->focus->output;
3082 else if (keyboard && keyboard->focus)
3083 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003084
3085 if (output)
3086 break;
3087 }
3088
3089 return output;
3090}
3091
3092static void
3093shell_interface_set_maximized(struct shell_surface *shsurf)
3094{
3095 struct weston_output *output;
3096
3097 surface_clear_next_states(shsurf);
3098 shsurf->next_state.maximized = true;
3099 shsurf->state_changed = true;
3100 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3101
3102 if (!weston_surface_is_mapped(shsurf->surface))
3103 output = get_focused_output(shsurf->surface->compositor);
3104 else
3105 output = shsurf->surface->output;
3106
3107 shell_surface_set_output(shsurf, output);
3108 send_configure_for_surface(shsurf);
3109}
3110
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003111static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003112shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003113{
Derek Foremane4d6c832015-08-05 14:48:11 -05003114 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003115}
3116
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003117static int
3118shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003119 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003120 uint32_t edges)
3121{
Derek Foremane4d6c832015-08-05 14:48:11 -05003122 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003123}
3124
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003125static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003126
3127static void
3128destroy_shell_seat(struct wl_listener *listener, void *data)
3129{
3130 struct shell_seat *shseat =
3131 container_of(listener,
3132 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003133 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003134
3135 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003136 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003137 shseat->popup_grab.client = NULL;
3138
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003139 wl_list_for_each_safe(shsurf, next,
3140 &shseat->popup_grab.surfaces_list,
3141 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003142 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003143 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003144 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003145 }
3146
3147 wl_list_remove(&shseat->seat_destroy_listener.link);
3148 free(shseat);
3149}
3150
Jason Ekstrand024177c2014-04-21 19:42:58 -05003151static void
3152shell_seat_caps_changed(struct wl_listener *listener, void *data)
3153{
Derek Foreman1281a362015-07-31 16:55:32 -05003154 struct weston_keyboard *keyboard;
3155 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003156 struct shell_seat *seat;
3157
3158 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003159 keyboard = weston_seat_get_keyboard(seat->seat);
3160 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003161
Derek Foreman1281a362015-07-31 16:55:32 -05003162 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003163 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003164 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003165 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003166 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003167 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003168 wl_list_init(&seat->keyboard_focus_listener.link);
3169 }
3170
Derek Foreman1281a362015-07-31 16:55:32 -05003171 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003172 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003173 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003174 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003175 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003176 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003177 wl_list_init(&seat->pointer_focus_listener.link);
3178 }
3179}
3180
Giulio Camuffo5085a752013-03-25 21:42:45 +01003181static struct shell_seat *
3182create_shell_seat(struct weston_seat *seat)
3183{
3184 struct shell_seat *shseat;
3185
3186 shseat = calloc(1, sizeof *shseat);
3187 if (!shseat) {
3188 weston_log("no memory to allocate shell seat\n");
3189 return NULL;
3190 }
3191
3192 shseat->seat = seat;
3193 wl_list_init(&shseat->popup_grab.surfaces_list);
3194
3195 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3196 wl_signal_add(&seat->destroy_signal,
3197 &shseat->seat_destroy_listener);
3198
Jason Ekstrand024177c2014-04-21 19:42:58 -05003199 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3200 wl_list_init(&shseat->keyboard_focus_listener.link);
3201
3202 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3203 wl_list_init(&shseat->pointer_focus_listener.link);
3204
3205 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3206 wl_signal_add(&seat->updated_caps_signal,
3207 &shseat->caps_changed_listener);
3208 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3209
Giulio Camuffo5085a752013-03-25 21:42:45 +01003210 return shseat;
3211}
3212
3213static struct shell_seat *
3214get_shell_seat(struct weston_seat *seat)
3215{
3216 struct wl_listener *listener;
3217
3218 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003219 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003220
3221 return container_of(listener,
3222 struct shell_seat, seat_destroy_listener);
3223}
3224
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003225static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003226popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003227{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003228 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003229 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003230 struct shell_seat *shseat =
3231 container_of(grab, struct shell_seat, popup_grab.grab);
3232 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003233 wl_fixed_t sx, sy;
3234
Jason Ekstranda7af7042013-10-12 22:38:11 -05003235 view = weston_compositor_pick_view(pointer->seat->compositor,
3236 pointer->x, pointer->y,
3237 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003238
Jason Ekstranda7af7042013-10-12 22:38:11 -05003239 if (view && view->surface->resource &&
3240 wl_resource_get_client(view->surface->resource) == client) {
3241 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003242 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003243 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003244 }
3245}
3246
3247static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003248popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3249 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003250{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003251 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003252 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003253 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003254
Jonas Ådahl61917c82014-08-11 22:44:02 +02003255 if (pointer->focus) {
3256 weston_view_from_global_fixed(pointer->focus, x, y,
3257 &pointer->sx, &pointer->sy);
3258 }
3259
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003260 weston_pointer_move(pointer, x, y);
3261
Neil Roberts96d790e2013-09-19 17:32:00 +01003262 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003263 weston_view_from_global_fixed(pointer->focus,
3264 pointer->x, pointer->y,
3265 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003266 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003267 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003268}
3269
3270static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003271popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003272 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003273{
3274 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003275 struct shell_seat *shseat =
3276 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003277 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003278 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003279 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003280 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003281
Neil Roberts96d790e2013-09-19 17:32:00 +01003282 resource_list = &grab->pointer->focus_resource_list;
3283 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003284 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003285 wl_resource_for_each(resource, resource_list) {
3286 wl_pointer_send_button(resource, serial,
3287 time, button, state);
3288 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003289 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003290 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003291 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003292 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003293 }
3294
Daniel Stone4dbadb12012-05-30 16:31:51 +01003295 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003296 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003297}
3298
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003299static void
3300popup_grab_cancel(struct weston_pointer_grab *grab)
3301{
3302 popup_grab_end(grab->pointer);
3303}
3304
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003305static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003306 popup_grab_focus,
3307 popup_grab_motion,
3308 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003309 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003310};
3311
3312static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003313touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003314 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003315{
3316 struct wl_resource *resource;
3317 struct shell_seat *shseat =
3318 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3319 struct wl_display *display = shseat->seat->compositor->wl_display;
3320 uint32_t serial;
3321 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003322 wl_fixed_t sx, sy;
3323
3324 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003325
3326 resource_list = &grab->touch->focus_resource_list;
3327 if (!wl_list_empty(resource_list)) {
3328 serial = wl_display_get_serial(display);
3329 wl_resource_for_each(resource, resource_list) {
3330 wl_touch_send_down(resource, serial, time,
3331 grab->touch->focus->surface->resource,
3332 touch_id, sx, sy);
3333 }
3334 }
3335}
3336
3337static void
3338touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3339{
3340 struct wl_resource *resource;
3341 struct shell_seat *shseat =
3342 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3343 struct wl_display *display = shseat->seat->compositor->wl_display;
3344 uint32_t serial;
3345 struct wl_list *resource_list;
3346
3347 resource_list = &grab->touch->focus_resource_list;
3348 if (!wl_list_empty(resource_list)) {
3349 serial = wl_display_get_serial(display);
3350 wl_resource_for_each(resource, resource_list) {
3351 wl_touch_send_up(resource, serial, time, touch_id);
3352 }
3353 }
3354}
3355
3356static void
3357touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003358 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003359{
3360 struct wl_resource *resource;
3361 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003362 wl_fixed_t sx, sy;
3363
3364 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003365
3366 resource_list = &grab->touch->focus_resource_list;
3367 if (!wl_list_empty(resource_list)) {
3368 wl_resource_for_each(resource, resource_list) {
3369 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3370 }
3371 }
3372}
3373
3374static void
3375touch_popup_grab_frame(struct weston_touch_grab *grab)
3376{
3377}
3378
3379static void
3380touch_popup_grab_cancel(struct weston_touch_grab *grab)
3381{
3382 touch_popup_grab_end(grab->touch);
3383}
3384
3385static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3386 touch_popup_grab_down,
3387 touch_popup_grab_up,
3388 touch_popup_grab_motion,
3389 touch_popup_grab_frame,
3390 touch_popup_grab_cancel,
3391};
3392
3393static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003394shell_surface_send_popup_done(struct shell_surface *shsurf)
3395{
3396 if (shell_surface_is_wl_shell_surface(shsurf))
3397 wl_shell_surface_send_popup_done(shsurf->resource);
3398 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003399 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003400}
3401
3402static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003403popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003404{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003405 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003406 struct shell_seat *shseat =
3407 container_of(grab, struct shell_seat, popup_grab.grab);
3408 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003409 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003410
3411 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003412 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003413 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003414 shseat->popup_grab.grab.interface = NULL;
3415 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003416 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003417 wl_list_for_each_safe(shsurf, next,
3418 &shseat->popup_grab.surfaces_list,
3419 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003420 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003421 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003422 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003423 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003424 wl_list_init(&shseat->popup_grab.surfaces_list);
3425 }
3426}
3427
3428static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003429touch_popup_grab_end(struct weston_touch *touch)
3430{
3431 struct weston_touch_grab *grab = touch->grab;
3432 struct shell_seat *shseat =
3433 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3434 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003435 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003436
3437 if (touch->grab->interface == &touch_popup_grab_interface) {
3438 weston_touch_end_grab(grab->touch);
3439 shseat->popup_grab.client = NULL;
3440 shseat->popup_grab.touch_grab.interface = NULL;
3441 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3442 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003443 wl_list_for_each_safe(shsurf, next,
3444 &shseat->popup_grab.surfaces_list,
3445 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003446 shell_surface_send_popup_done(shsurf);
3447 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003448 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003449 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003450 wl_list_init(&shseat->popup_grab.surfaces_list);
3451 }
3452}
3453
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003454static struct shell_surface *
3455get_top_popup(struct shell_seat *shseat)
3456{
3457 struct shell_surface *shsurf;
3458
3459 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3460 return NULL;
3461 } else {
3462 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3463 struct shell_surface,
3464 popup.grab_link);
3465 return shsurf;
3466 }
3467}
3468
3469static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003470add_popup_grab(struct shell_surface *shsurf,
3471 struct shell_seat *shseat,
3472 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003473{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003474 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003475 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003476 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3477 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003478
3479 parent = get_shell_surface(shsurf->parent);
3480 top_surface = get_top_popup(shseat);
3481 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003482 (!parent ||
3483 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003484 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003485 wl_resource_post_error(shsurf->owner_resource,
3486 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003487 "xdg_popup was not created on the "
3488 "topmost popup");
3489 return -1;
3490 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003491
3492 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003493 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003494 shseat->popup_grab.client =
3495 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003496
3497 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003498 shseat->popup_grab.grab.interface =
3499 &popup_grab_interface;
3500
3501 /* We must make sure here that this popup was opened
3502 * after a mouse press, and not just by moving around
3503 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003504 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003505 shseat->popup_grab.initial_up = 0;
3506 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003507 shseat->popup_grab.touch_grab.interface =
3508 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003509 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003510
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003511 wl_list_insert(&shseat->popup_grab.surfaces_list,
3512 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003513
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003514 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003515 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003516 &shseat->popup_grab.grab);
3517 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003518 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003519 &shseat->popup_grab.touch_grab);
3520 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003521 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003522 wl_list_insert(&shseat->popup_grab.surfaces_list,
3523 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003524 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003525
3526 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003527}
3528
3529static void
3530remove_popup_grab(struct shell_surface *shsurf)
3531{
3532 struct shell_seat *shseat = shsurf->popup.shseat;
3533
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003534 if (shell_surface_is_xdg_popup(shsurf) &&
3535 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003536 wl_resource_post_error(shsurf->owner_resource,
3537 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003538 "xdg_popup was destroyed while it was "
3539 "not the topmost popup.");
3540 return;
3541 }
3542
Giulio Camuffo5085a752013-03-25 21:42:45 +01003543 wl_list_remove(&shsurf->popup.grab_link);
3544 wl_list_init(&shsurf->popup.grab_link);
3545 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003546 if (shseat->popup_grab.type == POINTER) {
3547 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3548 shseat->popup_grab.grab.interface = NULL;
3549 } else if (shseat->popup_grab.type == TOUCH) {
3550 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3551 shseat->popup_grab.touch_grab.interface = NULL;
3552 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003553 }
3554}
3555
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003556static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003557shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003558{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003559 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003560 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003561 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3562 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003563
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564 shsurf->surface->output = parent_view->output;
3565 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003566
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 weston_view_set_transform_parent(shsurf->view, parent_view);
3568 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3569 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003570
Derek Foreman1281a362015-07-31 16:55:32 -05003571 if (pointer &&
3572 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003573 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3574 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003575 } else if (touch &&
3576 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003577 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3578 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003579 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003580 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003581 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003582 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003583
3584 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003585}
3586
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003587static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003588 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003589 shell_surface_move,
3590 shell_surface_resize,
3591 shell_surface_set_toplevel,
3592 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003593 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003594 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003595 shell_surface_set_maximized,
3596 shell_surface_set_title,
3597 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003598};
3599
3600static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003601destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003602{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003603 struct shell_surface *child, *next;
3604
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003605 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3606
Giulio Camuffo5085a752013-03-25 21:42:45 +01003607 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3608 remove_popup_grab(shsurf);
3609 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003610
Alex Wubd3354b2012-04-17 17:20:49 +08003611 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003612 shell_surface_is_top_fullscreen(shsurf))
3613 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003614
Jason Ekstranda7af7042013-10-12 22:38:11 -05003615 if (shsurf->fullscreen.black_view)
3616 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003617
Alex Wubd3354b2012-04-17 17:20:49 +08003618 /* As destroy_resource() use wl_list_for_each_safe(),
3619 * we can always remove the listener.
3620 */
3621 wl_list_remove(&shsurf->surface_destroy_listener.link);
3622 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003623 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003624 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003625
Jason Ekstranda7af7042013-10-12 22:38:11 -05003626 weston_view_destroy(shsurf->view);
3627
Philip Withnall648a4dd2013-11-25 18:01:44 +00003628 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003629 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3630 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003631
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003632 wl_list_remove(&shsurf->link);
3633 free(shsurf);
3634}
3635
3636static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003637shell_destroy_shell_surface(struct wl_resource *resource)
3638{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003639 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003640
Bryan Caina46b9462014-04-04 17:41:24 -05003641 if (!wl_list_empty(&shsurf->popup.grab_link))
3642 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003643 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003644 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003645}
3646
3647static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003648shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003649{
3650 struct shell_surface *shsurf = container_of(listener,
3651 struct shell_surface,
3652 surface_destroy_listener);
3653
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003654 if (shsurf->resource)
3655 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003656
3657 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003658}
3659
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003660static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003661fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003662{
3663 struct shell_surface *shsurf = data;
3664
3665 weston_surface_destroy(shsurf->surface);
3666}
3667
3668static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003669fade_out_done(struct weston_view_animation *animation, void *data)
3670{
3671 struct shell_surface *shsurf = data;
3672 struct wl_event_loop *loop;
3673
3674 loop = wl_display_get_event_loop(
3675 shsurf->surface->compositor->wl_display);
3676
3677 if (!shsurf->destroying) {
3678 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3679 shsurf->destroying = true;
3680 }
3681}
3682
3683static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003684handle_resource_destroy(struct wl_listener *listener, void *data)
3685{
3686 struct shell_surface *shsurf =
3687 container_of(listener, struct shell_surface,
3688 resource_destroy_listener);
3689
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003690 if (!weston_surface_is_mapped(shsurf->surface))
3691 return;
3692
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003693 shsurf->surface->ref_count++;
3694
3695 pixman_region32_fini(&shsurf->surface->pending.input);
3696 pixman_region32_init(&shsurf->surface->pending.input);
3697 pixman_region32_fini(&shsurf->surface->input);
3698 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003699 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3700 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3701 fade_out_done, shsurf);
3702 } else {
3703 weston_surface_destroy(shsurf->surface);
3704 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003705}
3706
3707static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003708shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003709
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003710struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003711get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003712{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003713 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003714 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003715 else
3716 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003717}
3718
Rafael Antognollie2a34552013-12-03 15:35:45 -02003719static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003720create_common_surface(struct shell_client *owner, void *shell,
3721 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003722 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003723{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003724 struct shell_surface *shsurf;
3725
Pekka Paalanen50b67472014-10-01 15:02:41 +03003726 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003727
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003728 shsurf = calloc(1, sizeof *shsurf);
3729 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003730 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003731 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003732 }
3733
Jason Ekstranda7af7042013-10-12 22:38:11 -05003734 shsurf->view = weston_view_create(surface);
3735 if (!shsurf->view) {
3736 weston_log("no memory to allocate shell surface\n");
3737 free(shsurf);
3738 return NULL;
3739 }
3740
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003741 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003742 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003743 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003744
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003745 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3746 wl_resource_add_destroy_listener(surface->resource,
3747 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003748 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003749
Tiago Vignattibc052c92012-04-19 16:18:18 +03003750 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003751 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003752 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003753 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003754 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003755 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003756 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3757 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003759 wl_list_init(&shsurf->fullscreen.transform.link);
3760
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003761 shsurf->output = get_default_output(shsurf->shell->compositor);
3762
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003763 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003764 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003765 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003766 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003767
3768 /* init link so its safe to always remove it in destroy_shell_surface */
3769 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003770 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003771
Pekka Paalanen460099f2012-01-20 16:48:25 +02003772 /* empty when not in use */
3773 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003774 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003775
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003776 wl_list_init(&shsurf->workspace_transform.link);
3777
Philip Withnall648a4dd2013-11-25 18:01:44 +00003778 wl_list_init(&shsurf->children_link);
3779 wl_list_init(&shsurf->children_list);
3780 shsurf->parent = NULL;
3781
Pekka Paalanenb5026542014-11-12 15:09:24 +02003782 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003783
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003784 shsurf->client = client;
3785
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003786 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003787}
3788
Rafael Antognollie2a34552013-12-03 15:35:45 -02003789static struct shell_surface *
3790create_shell_surface(void *shell, struct weston_surface *surface,
3791 const struct weston_shell_client *client)
3792{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003793 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003794}
3795
Jason Ekstranda7af7042013-10-12 22:38:11 -05003796static struct weston_view *
3797get_primary_view(void *shell, struct shell_surface *shsurf)
3798{
3799 return shsurf->view;
3800}
3801
Tiago Vignattibc052c92012-04-19 16:18:18 +03003802static void
3803shell_get_shell_surface(struct wl_client *client,
3804 struct wl_resource *resource,
3805 uint32_t id,
3806 struct wl_resource *surface_resource)
3807{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003808 struct weston_surface *surface =
3809 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003810 struct shell_client *sc = wl_resource_get_user_data(resource);
3811 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003812 struct shell_surface *shsurf;
3813
Pekka Paalanen50b67472014-10-01 15:02:41 +03003814 if (weston_surface_set_role(surface, "wl_shell_surface",
3815 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003816 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003817
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003818 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003819 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003820 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003821 return;
3822 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003823
Jason Ekstranda85118c2013-06-27 20:17:02 -05003824 shsurf->resource =
3825 wl_resource_create(client,
3826 &wl_shell_surface_interface, 1, id);
3827 wl_resource_set_implementation(shsurf->resource,
3828 &shell_surface_implementation,
3829 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003830 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003831}
3832
Rafael Antognollie2a34552013-12-03 15:35:45 -02003833static bool
3834shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3835{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003836 /* A shell surface without a resource is created from xwayland
3837 * and is considered a wl_shell surface for now. */
3838
3839 return shsurf->resource == NULL ||
3840 wl_resource_instance_of(shsurf->resource,
3841 &wl_shell_surface_interface,
3842 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003843}
3844
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003845static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003846 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003847};
3848
Rafael Antognollie2a34552013-12-03 15:35:45 -02003849/****************************
3850 * xdg-shell implementation */
3851
3852static void
3853xdg_surface_destroy(struct wl_client *client,
3854 struct wl_resource *resource)
3855{
3856 wl_resource_destroy(resource);
3857}
3858
3859static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003860xdg_surface_set_parent(struct wl_client *client,
3861 struct wl_resource *resource,
3862 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003863{
3864 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003865 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003866
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003867 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003868 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003869 shell_surface_set_parent(shsurf, parent->surface);
3870 } else {
3871 shell_surface_set_parent(shsurf, NULL);
3872 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003873}
3874
3875static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003876xdg_surface_set_app_id(struct wl_client *client,
3877 struct wl_resource *resource,
3878 const char *app_id)
3879{
3880 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3881
3882 free(shsurf->class);
3883 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003884 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003885}
3886
3887static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003888xdg_surface_show_window_menu(struct wl_client *client,
3889 struct wl_resource *surface_resource,
3890 struct wl_resource *seat_resource,
3891 uint32_t serial,
3892 int32_t x,
3893 int32_t y)
3894{
3895 /* TODO */
3896}
3897
3898static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003899xdg_surface_set_title(struct wl_client *client,
3900 struct wl_resource *resource, const char *title)
3901{
3902 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3903
3904 set_title(shsurf, title);
3905}
3906
3907static void
3908xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3909 struct wl_resource *seat_resource, uint32_t serial)
3910{
3911 common_surface_move(resource, seat_resource, serial);
3912}
3913
3914static void
3915xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3916 struct wl_resource *seat_resource, uint32_t serial,
3917 uint32_t edges)
3918{
3919 common_surface_resize(resource, seat_resource, serial, edges);
3920}
3921
3922static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003923xdg_surface_ack_configure(struct wl_client *client,
3924 struct wl_resource *resource,
3925 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003926{
3927 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3928
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003929 if (shsurf->state_requested) {
3930 shsurf->next_state = shsurf->requested_state;
3931 shsurf->state_changed = true;
3932 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003933 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003934}
3935
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003936static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003937xdg_surface_set_window_geometry(struct wl_client *client,
3938 struct wl_resource *resource,
3939 int32_t x,
3940 int32_t y,
3941 int32_t width,
3942 int32_t height)
3943{
3944 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3945
3946 set_window_geometry(shsurf, x, y, width, height);
3947}
3948
3949static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003950xdg_surface_set_maximized(struct wl_client *client,
3951 struct wl_resource *resource)
3952{
3953 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003954 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003955
3956 shsurf->state_requested = true;
3957 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003958
3959 if (!weston_surface_is_mapped(shsurf->surface))
3960 output = get_focused_output(shsurf->surface->compositor);
3961 else
3962 output = shsurf->surface->output;
3963
3964 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003965 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003966}
3967
3968static void
3969xdg_surface_unset_maximized(struct wl_client *client,
3970 struct wl_resource *resource)
3971{
3972 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3973
3974 shsurf->state_requested = true;
3975 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003976 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003977}
3978
3979static void
3980xdg_surface_set_fullscreen(struct wl_client *client,
3981 struct wl_resource *resource,
3982 struct wl_resource *output_resource)
3983{
3984 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3985 struct weston_output *output;
3986
3987 shsurf->state_requested = true;
3988 shsurf->requested_state.fullscreen = true;
3989
3990 if (output_resource)
3991 output = wl_resource_get_user_data(output_resource);
3992 else
3993 output = NULL;
3994
Marek Chalupa052917a2014-09-02 11:35:12 +02003995 /* handle clients launching in fullscreen */
3996 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3997 /* Set the output to the one that has focus currently. */
3998 assert(shsurf->surface);
3999 output = get_focused_output(shsurf->surface->compositor);
4000 }
4001
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004002 shell_surface_set_output(shsurf, output);
4003 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004004
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004005 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004006}
4007
4008static void
4009xdg_surface_unset_fullscreen(struct wl_client *client,
4010 struct wl_resource *resource)
4011{
4012 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
4013
4014 shsurf->state_requested = true;
4015 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07004016 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004017}
4018
4019static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004020xdg_surface_set_minimized(struct wl_client *client,
4021 struct wl_resource *resource)
4022{
4023 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
4024
4025 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
4026 return;
4027
4028 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01004029 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004030}
4031
Rafael Antognollie2a34552013-12-03 15:35:45 -02004032static const struct xdg_surface_interface xdg_surface_implementation = {
4033 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004034 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004035 xdg_surface_set_title,
4036 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004037 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004038 xdg_surface_move,
4039 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004040 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004041 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004042 xdg_surface_set_maximized,
4043 xdg_surface_unset_maximized,
4044 xdg_surface_set_fullscreen,
4045 xdg_surface_unset_fullscreen,
4046 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004047};
4048
4049static void
4050xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004051 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004052{
4053 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004054 uint32_t *s;
4055 struct wl_array states;
4056 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004057
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004058 assert(shsurf);
4059
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004060 if (!shsurf->resource)
4061 return;
4062
4063 wl_array_init(&states);
4064 if (shsurf->requested_state.fullscreen) {
4065 s = wl_array_add(&states, sizeof *s);
4066 *s = XDG_SURFACE_STATE_FULLSCREEN;
4067 } else if (shsurf->requested_state.maximized) {
4068 s = wl_array_add(&states, sizeof *s);
4069 *s = XDG_SURFACE_STATE_MAXIMIZED;
4070 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004071 if (shsurf->resize_edges != 0) {
4072 s = wl_array_add(&states, sizeof *s);
4073 *s = XDG_SURFACE_STATE_RESIZING;
4074 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004075 if (shsurf->focus_count > 0) {
4076 s = wl_array_add(&states, sizeof *s);
4077 *s = XDG_SURFACE_STATE_ACTIVATED;
4078 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004079
4080 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4081 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4082
4083 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004084}
4085
4086static const struct weston_shell_client xdg_client = {
4087 xdg_send_configure
4088};
4089
4090static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004091xdg_shell_destroy(struct wl_client *client,
4092 struct wl_resource *resource)
4093{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004094 struct shell_client *sc = wl_resource_get_user_data(resource);
4095 struct wl_resource *shsurf_resource;
4096 struct shell_surface *shsurf;
4097
4098 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4099 shsurf = wl_resource_get_user_data(shsurf_resource);
4100 if (shsurf->owner_resource == resource) {
4101 wl_resource_post_error(
4102 resource,
4103 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4104 "not all child surface objects destroyed");
4105 return;
4106 }
4107 }
4108
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004109 wl_resource_destroy(resource);
4110}
4111
4112static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004113xdg_use_unstable_version(struct wl_client *client,
4114 struct wl_resource *resource,
4115 int32_t version)
4116{
4117 if (version > 1) {
4118 wl_resource_post_error(resource,
4119 1,
4120 "xdg-shell:: version not implemented yet.");
4121 return;
4122 }
4123}
4124
4125static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004126create_xdg_surface(struct shell_client *owner, void *shell,
4127 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004128 const struct weston_shell_client *client)
4129{
4130 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004131
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004132 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004133 if (!shsurf)
4134 return NULL;
4135
Pekka Paalanenb5026542014-11-12 15:09:24 +02004136 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004137
4138 return shsurf;
4139}
4140
4141static void
4142xdg_get_xdg_surface(struct wl_client *client,
4143 struct wl_resource *resource,
4144 uint32_t id,
4145 struct wl_resource *surface_resource)
4146{
4147 struct weston_surface *surface =
4148 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004149 struct shell_client *sc = wl_resource_get_user_data(resource);
4150 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004151 struct shell_surface *shsurf;
4152
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004153 shsurf = get_shell_surface(surface);
4154 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4155 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4156 "This wl_surface is already an "
4157 "xdg_surface");
4158 return;
4159 }
4160
Pekka Paalanen50b67472014-10-01 15:02:41 +03004161 if (weston_surface_set_role(surface, "xdg_surface",
4162 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004163 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004164
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004165 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004166 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004167 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004168 return;
4169 }
4170
4171 shsurf->resource =
4172 wl_resource_create(client,
4173 &xdg_surface_interface, 1, id);
4174 wl_resource_set_implementation(shsurf->resource,
4175 &xdg_surface_implementation,
4176 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004177 shsurf->owner_resource = resource;
4178 wl_list_insert(&sc->surface_list,
4179 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004180}
4181
4182static bool
4183shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4184{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004185 return shsurf->resource &&
4186 wl_resource_instance_of(shsurf->resource,
4187 &xdg_surface_interface,
4188 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189}
4190
4191/* xdg-popup implementation */
4192
4193static void
4194xdg_popup_destroy(struct wl_client *client,
4195 struct wl_resource *resource)
4196{
4197 wl_resource_destroy(resource);
4198}
4199
Rafael Antognollie2a34552013-12-03 15:35:45 -02004200static const struct xdg_popup_interface xdg_popup_implementation = {
4201 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004202};
4203
4204static void
4205xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004206 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004207{
4208}
4209
4210static const struct weston_shell_client xdg_popup_client = {
4211 xdg_popup_send_configure
4212};
4213
4214static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004215create_xdg_popup(struct shell_client *owner, void *shell,
4216 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004217 const struct weston_shell_client *client,
4218 struct weston_surface *parent,
4219 struct shell_seat *seat,
4220 uint32_t serial,
4221 int32_t x, int32_t y)
4222{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004223 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004224
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004225 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004226 if (!shsurf)
4227 return NULL;
4228
Pekka Paalanenb5026542014-11-12 15:09:24 +02004229 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004230 shsurf->popup.shseat = seat;
4231 shsurf->popup.serial = serial;
4232 shsurf->popup.x = x;
4233 shsurf->popup.y = y;
4234 shell_surface_set_parent(shsurf, parent);
4235
4236 return shsurf;
4237}
4238
4239static void
4240xdg_get_xdg_popup(struct wl_client *client,
4241 struct wl_resource *resource,
4242 uint32_t id,
4243 struct wl_resource *surface_resource,
4244 struct wl_resource *parent_resource,
4245 struct wl_resource *seat_resource,
4246 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004247 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004248{
4249 struct weston_surface *surface =
4250 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004251 struct shell_client *sc = wl_resource_get_user_data(resource);
4252 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004253 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004254 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255 struct weston_surface *parent;
4256 struct shell_seat *seat;
4257
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004258 shsurf = get_shell_surface(surface);
4259 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4260 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4261 "This wl_surface is already an "
4262 "xdg_popup");
4263 return;
4264 }
4265
Pekka Paalanen50b67472014-10-01 15:02:41 +03004266 if (weston_surface_set_role(surface, "xdg_popup",
4267 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004268 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004269
4270 if (!parent_resource) {
4271 wl_resource_post_error(surface_resource,
4272 WL_DISPLAY_ERROR_INVALID_OBJECT,
4273 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004274 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004275 }
4276
4277 parent = wl_resource_get_user_data(parent_resource);
4278 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4279
Jonas Ådahlee45a552015-03-18 16:37:47 +08004280 /* Verify that we are creating the top most popup when mapping,
4281 * as it's not until then we know whether it was mapped as most
4282 * top level or not. */
4283
4284 parent_shsurf = get_shell_surface(parent);
4285 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4286 !shell_surface_is_xdg_surface(parent_shsurf)) {
4287 wl_resource_post_error(resource,
4288 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4289 "xdg_popup parent was invalid");
4290 return;
4291 }
4292
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004293 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004294 parent, seat, serial, x, y);
4295 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004296 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004297 return;
4298 }
4299
4300 shsurf->resource =
4301 wl_resource_create(client,
4302 &xdg_popup_interface, 1, id);
4303 wl_resource_set_implementation(shsurf->resource,
4304 &xdg_popup_implementation,
4305 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004306 shsurf->owner_resource = resource;
4307 wl_list_insert(&sc->surface_list,
4308 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004309}
4310
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004311static void
4312xdg_pong(struct wl_client *client,
4313 struct wl_resource *resource, uint32_t serial)
4314{
4315 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004316
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004317 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004318}
4319
Rafael Antognollie2a34552013-12-03 15:35:45 -02004320static bool
4321shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4322{
4323 return wl_resource_instance_of(shsurf->resource,
4324 &xdg_popup_interface,
4325 &xdg_popup_implementation);
4326}
4327
4328static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004329 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004330 xdg_use_unstable_version,
4331 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004332 xdg_get_xdg_popup,
4333 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004334};
4335
4336static int
4337xdg_shell_unversioned_dispatch(const void *implementation,
4338 void *_target, uint32_t opcode,
4339 const struct wl_message *message,
4340 union wl_argument *args)
4341{
4342 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004343 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004344
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004345 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004346 wl_resource_post_error(resource,
4347 WL_DISPLAY_ERROR_INVALID_OBJECT,
4348 "must call use_unstable_version first");
4349 return 0;
4350 }
4351
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004352#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004353
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004354 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4355 "shell implementation doesn't match protocol version");
4356
Rafael Antognollie2a34552013-12-03 15:35:45 -02004357 if (args[0].i != XDG_SERVER_VERSION) {
4358 wl_resource_post_error(resource,
4359 WL_DISPLAY_ERROR_INVALID_OBJECT,
4360 "incompatible version, server is %d "
4361 "client wants %d",
4362 XDG_SERVER_VERSION, args[0].i);
4363 return 0;
4364 }
4365
4366 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004367 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004368
4369 return 1;
4370}
4371
4372/* end of xdg-shell implementation */
4373/***********************************/
4374
Kristian Høgsberg07937562011-04-12 17:25:42 -04004375static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004376shell_fade(struct desktop_shell *shell, enum fade_type type);
4377
Pekka Paalanen77346a62011-11-30 16:26:35 +02004378static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004379configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004380{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004381 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004382
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004383 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004384 if (v->output == ev->output && v != ev) {
4385 weston_view_unmap(v);
4386 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004387 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004388 }
4389 }
4390
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004391 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004392
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004393 if (wl_list_empty(&ev->layer_link.link)) {
4394 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004395 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004396 }
4397}
4398
Pekka Paalanen8274d902014-08-06 19:36:51 +03004399static int
4400background_get_label(struct weston_surface *surface, char *buf, size_t len)
4401{
4402 return snprintf(buf, len, "background for output %s",
4403 surface->output->name);
4404}
4405
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004406static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004407background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004408{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004409 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004410 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004411
Jason Ekstranda7af7042013-10-12 22:38:11 -05004412 view = container_of(es->views.next, struct weston_view, surface_link);
4413
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004414 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004415}
4416
4417static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004418desktop_shell_set_background(struct wl_client *client,
4419 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004420 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004421 struct wl_resource *surface_resource)
4422{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004423 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004424 struct weston_surface *surface =
4425 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004426 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004427
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004428 if (surface->configure) {
4429 wl_resource_post_error(surface_resource,
4430 WL_DISPLAY_ERROR_INVALID_OBJECT,
4431 "surface role already assigned");
4432 return;
4433 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004434
Jason Ekstranda7af7042013-10-12 22:38:11 -05004435 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4436 weston_view_destroy(view);
4437 view = weston_view_create(surface);
4438
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004439 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004440 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004441 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004442 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004444 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004445 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004446 surface->output->width,
4447 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004448}
4449
Pekka Paalanen8274d902014-08-06 19:36:51 +03004450static int
4451panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4452{
4453 return snprintf(buf, len, "panel for output %s",
4454 surface->output->name);
4455}
4456
Kristian Høgsberg75840622011-09-06 13:48:16 -04004457static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004458panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004459{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004460 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004461 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004462
Jason Ekstranda7af7042013-10-12 22:38:11 -05004463 view = container_of(es->views.next, struct weston_view, surface_link);
4464
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004465 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004466}
4467
4468static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004469desktop_shell_set_panel(struct wl_client *client,
4470 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004471 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004472 struct wl_resource *surface_resource)
4473{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004474 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004475 struct weston_surface *surface =
4476 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004477 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004478
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004479 if (surface->configure) {
4480 wl_resource_post_error(surface_resource,
4481 WL_DISPLAY_ERROR_INVALID_OBJECT,
4482 "surface role already assigned");
4483 return;
4484 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004485
Jason Ekstranda7af7042013-10-12 22:38:11 -05004486 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4487 weston_view_destroy(view);
4488 view = weston_view_create(surface);
4489
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004490 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004491 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004492 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004493 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004494 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004495 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004496 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004497 surface->output->width,
4498 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004499}
4500
Pekka Paalanen8274d902014-08-06 19:36:51 +03004501static int
4502lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4503{
4504 return snprintf(buf, len, "lock window");
4505}
4506
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004507static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004508lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004509{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004510 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004511 struct weston_view *view;
4512
4513 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004514
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004515 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004516 return;
4517
Jason Ekstranda7af7042013-10-12 22:38:11 -05004518 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004519
4520 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004521 weston_layer_entry_insert(&shell->lock_layer.view_list,
4522 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004523 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004524 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004525 }
4526}
4527
4528static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004529handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004530{
Tiago Vignattibe143262012-04-16 17:31:41 +03004531 struct desktop_shell *shell =
4532 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004533
Martin Minarik6d118362012-06-07 18:01:59 +02004534 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004535 shell->lock_surface = NULL;
4536}
4537
4538static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004539desktop_shell_set_lock_surface(struct wl_client *client,
4540 struct wl_resource *resource,
4541 struct wl_resource *surface_resource)
4542{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004543 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004544 struct weston_surface *surface =
4545 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004546
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004547 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004548
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004549 if (!shell->locked)
4550 return;
4551
Pekka Paalanen98262232011-12-01 10:42:22 +02004552 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004553
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004554 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004555 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004556 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004557
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004558 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004559 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004560 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004561 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004562}
4563
4564static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004565resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004566{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004567 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004568
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004569 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004570 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004571 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004572 &shell->input_panel_layer.link);
4573 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004574 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004575 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004576 wl_list_insert(&shell->compositor->cursor_layer.link,
4577 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004578 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004579 wl_list_insert(&shell->fullscreen_layer.link,
4580 &shell->panel_layer.link);
4581 wl_list_insert(&shell->panel_layer.link,
4582 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004583
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004584 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004585
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004586 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004587 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004588 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004589}
4590
4591static void
4592desktop_shell_unlock(struct wl_client *client,
4593 struct wl_resource *resource)
4594{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004595 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004596
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004597 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004598
4599 if (shell->locked)
4600 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004601}
4602
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004603static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004604desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004605 struct wl_resource *resource,
4606 struct wl_resource *surface_resource)
4607{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004608 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004609
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004610 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004611 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004612}
4613
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004614static void
4615desktop_shell_desktop_ready(struct wl_client *client,
4616 struct wl_resource *resource)
4617{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004618 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004619
4620 shell_fade_startup(shell);
4621}
4622
Jonny Lamb765760d2014-08-20 15:53:19 +02004623static void
4624desktop_shell_set_panel_position(struct wl_client *client,
4625 struct wl_resource *resource,
4626 uint32_t position)
4627{
4628 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4629
4630 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4631 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4632 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4633 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4634 wl_resource_post_error(resource,
4635 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4636 "bad position argument");
4637 return;
4638 }
4639
4640 shell->panel_position = position;
4641}
4642
Kristian Høgsberg75840622011-09-06 13:48:16 -04004643static const struct desktop_shell_interface desktop_shell_implementation = {
4644 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004645 desktop_shell_set_panel,
4646 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004647 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004648 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004649 desktop_shell_desktop_ready,
4650 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004651};
4652
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004653static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004654get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004655{
4656 struct shell_surface *shsurf;
4657
4658 shsurf = get_shell_surface(surface);
4659 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004660 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004661 return shsurf->type;
4662}
4663
Kristian Høgsberg75840622011-09-06 13:48:16 -04004664static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004665move_binding(struct weston_pointer *pointer, uint32_t time,
4666 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004667{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004668 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004669 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004670 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004671
Derek Foreman8ae2db52015-07-15 13:00:36 -05004672 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004673 return;
4674
Derek Foreman8ae2db52015-07-15 13:00:36 -05004675 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004676
Pekka Paalanen01388e22013-04-25 13:57:44 +03004677 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004678 if (surface == NULL)
4679 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004680
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004681 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004682 if (shsurf == NULL || shsurf->state.fullscreen ||
4683 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004684 return;
4685
Derek Foreman794fa0e2015-07-15 13:00:38 -05004686 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004687}
4688
4689static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004690maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4691 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004692{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004693 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004694 struct weston_surface *surface;
4695 struct shell_surface *shsurf;
4696
4697 surface = weston_surface_get_main_surface(focus);
4698 if (surface == NULL)
4699 return;
4700
4701 shsurf = get_shell_surface(surface);
4702 if (shsurf == NULL)
4703 return;
4704
4705 if (!shell_surface_is_xdg_surface(shsurf))
4706 return;
4707
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004708 shsurf->state_requested = true;
4709 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004710 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004711}
4712
4713static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004714fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4715 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004716{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004717 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004718 struct weston_surface *surface;
4719 struct shell_surface *shsurf;
4720
4721 surface = weston_surface_get_main_surface(focus);
4722 if (surface == NULL)
4723 return;
4724
4725 shsurf = get_shell_surface(surface);
4726 if (shsurf == NULL)
4727 return;
4728
4729 if (!shell_surface_is_xdg_surface(shsurf))
4730 return;
4731
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004732 shsurf->state_requested = true;
4733 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004734 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004735 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004736}
4737
4738static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004739touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004740{
Derek Foreman362656b2014-09-04 10:23:05 -05004741 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004742 struct weston_surface *surface;
4743 struct shell_surface *shsurf;
4744
Derek Foreman8ae2db52015-07-15 13:00:36 -05004745 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004746 return;
4747
Derek Foreman8ae2db52015-07-15 13:00:36 -05004748 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004749 surface = weston_surface_get_main_surface(focus);
4750 if (surface == NULL)
4751 return;
4752
4753 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004754 if (shsurf == NULL || shsurf->state.fullscreen ||
4755 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004756 return;
4757
Derek Foremanb7674ae2015-07-15 13:00:37 -05004758 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004759}
4760
4761static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004762resize_binding(struct weston_pointer *pointer, uint32_t time,
4763 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004764{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004765 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004766 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004767 uint32_t edges = 0;
4768 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004769 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004770
Derek Foreman8ae2db52015-07-15 13:00:36 -05004771 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004772 return;
4773
Derek Foreman8ae2db52015-07-15 13:00:36 -05004774 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004775
Pekka Paalanen01388e22013-04-25 13:57:44 +03004776 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004777 if (surface == NULL)
4778 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004779
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004780 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004781 if (shsurf == NULL || shsurf->state.fullscreen ||
4782 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004783 return;
4784
Jason Ekstranda7af7042013-10-12 22:38:11 -05004785 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004786 wl_fixed_to_int(pointer->grab_x),
4787 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004788 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004789
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004790 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004791 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004792 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004793 edges |= 0;
4794 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004795 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004796
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004797 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004798 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004799 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004800 edges |= 0;
4801 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004802 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004803
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004804 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004805}
4806
4807static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004808surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4809 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004810{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004811 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004812 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004813 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004814 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004815
Pekka Paalanen01388e22013-04-25 13:57:44 +03004816 /* XXX: broken for windows containing sub-surfaces */
4817 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004818 if (surface == NULL)
4819 return;
4820
4821 shsurf = get_shell_surface(surface);
4822 if (!shsurf)
4823 return;
4824
Jason Ekstranda7af7042013-10-12 22:38:11 -05004825 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004826
Jason Ekstranda7af7042013-10-12 22:38:11 -05004827 if (shsurf->view->alpha > 1.0)
4828 shsurf->view->alpha = 1.0;
4829 if (shsurf->view->alpha < step)
4830 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004831
Jason Ekstranda7af7042013-10-12 22:38:11 -05004832 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004833 weston_surface_damage(surface);
4834}
4835
4836static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004837do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004838 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004839{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004840 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004841 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004842 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004843 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004844
Derek Foreman1281a362015-07-31 16:55:32 -05004845 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004846 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4847 return;
4848 }
4849
Scott Moreauccbf29d2012-02-22 14:21:41 -07004850 wl_list_for_each(output, &compositor->output_list, link) {
4851 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004852 wl_fixed_to_double(pointer->x),
4853 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004854 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004855 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004856 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004857 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004858 increment = -output->zoom.increment;
4859 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004860 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004861 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004862 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004863 else
4864 increment = 0;
4865
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004866 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004867
Scott Moreaue6603982012-06-11 13:07:51 -06004868 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004869 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004870 else if (output->zoom.level > output->zoom.max_level)
4871 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004872
4873 if (!output->zoom.active) {
4874 if (output->zoom.level <= 0.0)
4875 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004876 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004877 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004878
Scott Moreaue6603982012-06-11 13:07:51 -06004879 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004880
Jason Ekstranda7af7042013-10-12 22:38:11 -05004881 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004882 }
4883 }
4884}
4885
Scott Moreauccbf29d2012-02-22 14:21:41 -07004886static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004887zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4888 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004889{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004890 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004891}
4892
4893static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004894zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4895 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004896{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004897 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004898}
4899
4900static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004901terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4902 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004903{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004904 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004905
Daniel Stone325fc2d2012-05-30 16:31:58 +01004906 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004907}
4908
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004909static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004910rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4911 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004912{
4913 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004914 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004915 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004916 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004917 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004918
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004919 weston_pointer_move(pointer, x, y);
4920
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004921 if (!shsurf)
4922 return;
4923
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004924 cx = 0.5f * shsurf->surface->width;
4925 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004926
Daniel Stone37816df2012-05-16 18:45:18 +01004927 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4928 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004929 r = sqrtf(dx * dx + dy * dy);
4930
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004931 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004932 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004933
4934 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004936 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004937
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004938 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004939 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004940
4941 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004942 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004943 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004944 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004945 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004946
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004947 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004948 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004949 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004950 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004951 wl_list_init(&shsurf->rotation.transform.link);
4952 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004953 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004954 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004955
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004956 /* We need to adjust the position of the surface
4957 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004958 cposx = shsurf->view->geometry.x + cx;
4959 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004960 dposx = rotate->center.x - cposx;
4961 dposy = rotate->center.y - cposy;
4962 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004963 weston_view_set_position(shsurf->view,
4964 shsurf->view->geometry.x + dposx,
4965 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004966 }
4967
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004968 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004969 * lazily applies the damage due to rotation update.
4970 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004971 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004972}
4973
4974static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004975rotate_grab_button(struct weston_pointer_grab *grab,
4976 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004977{
4978 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004979 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004980 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004981 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004982 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004983
Daniel Stone4dbadb12012-05-30 16:31:51 +01004984 if (pointer->button_count == 0 &&
4985 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004986 if (shsurf)
4987 weston_matrix_multiply(&shsurf->rotation.rotation,
4988 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004989 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004990 free(rotate);
4991 }
4992}
4993
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004994static void
4995rotate_grab_cancel(struct weston_pointer_grab *grab)
4996{
4997 struct rotate_grab *rotate =
4998 container_of(grab, struct rotate_grab, base.grab);
4999
5000 shell_grab_end(&rotate->base);
5001 free(rotate);
5002}
5003
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005004static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005005 noop_grab_focus,
5006 rotate_grab_motion,
5007 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005008 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005009};
5010
5011static void
Derek Foreman74de4692015-07-15 13:00:39 -05005012surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005013{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005014 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005015 float dx, dy;
5016 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005017
Derek Foreman45a7c272015-09-11 14:27:40 -05005018 surface = find_toplevel_surface(surface);
5019
Pekka Paalanen460099f2012-01-20 16:48:25 +02005020 rotate = malloc(sizeof *rotate);
5021 if (!rotate)
5022 return;
5023
Jason Ekstranda7af7042013-10-12 22:38:11 -05005024 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005025 surface->surface->width * 0.5f,
5026 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005027 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005028
Derek Foreman74de4692015-07-15 13:00:39 -05005029 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5030 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005031 r = sqrtf(dx * dx + dy * dy);
5032 if (r > 20.0f) {
5033 struct weston_matrix inverse;
5034
5035 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005036 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005037 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005038
5039 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005040 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005041 } else {
5042 weston_matrix_init(&surface->rotation.rotation);
5043 weston_matrix_init(&rotate->rotation);
5044 }
5045
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005046 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Derek Foreman74de4692015-07-15 13:00:39 -05005047 pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005048}
5049
5050static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005051rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005052 void *data)
5053{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005054 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005055 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005056 struct shell_surface *surface;
5057
Derek Foreman8ae2db52015-07-15 13:00:36 -05005058 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005059 return;
5060
Derek Foreman8ae2db52015-07-15 13:00:36 -05005061 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005062
Pekka Paalanen01388e22013-04-25 13:57:44 +03005063 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005064 if (base_surface == NULL)
5065 return;
5066
5067 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005068 if (surface == NULL || surface->state.fullscreen ||
5069 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005070 return;
5071
Derek Foreman74de4692015-07-15 13:00:39 -05005072 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005073}
5074
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005075/* Move all fullscreen layers down to the current workspace and hide their
5076 * black views. The surfaces' state is set to both fullscreen and lowered,
5077 * and this is reversed when such a surface is re-configured, see
5078 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5079 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005080 * lowering_output = NULL - Lower on all outputs, else only lower on the
5081 * specified output.
5082 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005083 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005084 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005085void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005086lower_fullscreen_layer(struct desktop_shell *shell,
5087 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005088{
5089 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005090 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005091
5092 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005093 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005094 &shell->fullscreen_layer.view_list.link,
5095 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005096 struct shell_surface *shsurf = get_shell_surface(view->surface);
5097
5098 if (!shsurf)
5099 continue;
5100
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005101 /* Only lower surfaces which have lowering_output as their fullscreen
5102 * output, unless a NULL output asks for lowering on all outputs.
5103 */
5104 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5105 continue;
5106
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005107 /* We can have a non-fullscreen popup for a fullscreen surface
5108 * in the fullscreen layer. */
5109 if (shsurf->state.fullscreen) {
5110 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005111 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5112 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005113 weston_view_damage_below(shsurf->fullscreen.black_view);
5114
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005115 }
5116
5117 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005118 weston_layer_entry_remove(&view->layer_link);
5119 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005120 weston_view_damage_below(view);
5121 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005122
5123 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005124 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005125}
5126
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005127void
Tiago Vignattibe143262012-04-16 17:31:41 +03005128activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005129 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005130{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005131 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005132 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005133 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005134 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005135 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005136
Pekka Paalanen01388e22013-04-25 13:57:44 +03005137 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005138 shsurf = get_shell_surface(main_surface);
5139 assert(shsurf);
5140
5141 /* Only demote fullscreen surfaces on the output of activated shsurf.
5142 * Leave fullscreen surfaces on unrelated outputs alone. */
5143 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005144
Daniel Stone37816df2012-05-16 18:45:18 +01005145 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005146
Jonas Ådahl8538b222012-08-29 22:13:03 +02005147 state = ensure_focus_state(shell, seat);
5148 if (state == NULL)
5149 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005150
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005151 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005152 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005153
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005154 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005155 shell_configure_fullscreen(shsurf);
5156 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005157 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005158
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005159 /* Update the surface’s layer. This brings it to the top of the stacking
5160 * order as appropriate. */
5161 shell_surface_update_layer(shsurf);
5162
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005163 if (shell->focus_animation_type != ANIMATION_NONE) {
5164 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005165 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005166 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005167}
5168
Alex Wu21858432012-04-01 20:13:08 +08005169/* no-op func for checking black surface */
5170static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005171black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005172{
5173}
5174
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005175static bool
Alex Wu21858432012-04-01 20:13:08 +08005176is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5177{
5178 if (es->configure == black_surface_configure) {
5179 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005180 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005181 return true;
5182 }
5183 return false;
5184}
5185
Kristian Høgsberg75840622011-09-06 13:48:16 -04005186static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005187activate_binding(struct weston_seat *seat,
5188 struct desktop_shell *shell,
5189 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005190{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005191 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005192
Alex Wu9c35e6b2012-03-05 14:13:13 +08005193 if (!focus)
5194 return;
5195
Pekka Paalanen01388e22013-04-25 13:57:44 +03005196 if (is_black_surface(focus, &main_surface))
5197 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005198
Pekka Paalanen01388e22013-04-25 13:57:44 +03005199 main_surface = weston_surface_get_main_surface(focus);
5200 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005201 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005202
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005203 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005204}
5205
5206static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005207click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5208 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005209{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005210 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005211 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005212 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005213 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005214
Derek Foreman8ae2db52015-07-15 13:00:36 -05005215 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005216}
5217
5218static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005219touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5220 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005221{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005222 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005223 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005224 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005225 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005226
Derek Foreman8ae2db52015-07-15 13:00:36 -05005227 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005228}
5229
5230static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005231unfocus_all_seats(struct desktop_shell *shell)
5232{
5233 struct weston_seat *seat, *next;
5234
5235 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005236 struct weston_keyboard *keyboard =
5237 weston_seat_get_keyboard(seat);
5238
5239 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005240 continue;
5241
Derek Foreman1281a362015-07-31 16:55:32 -05005242 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005243 }
5244}
5245
5246static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005247lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005248{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005249 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005250
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005251 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005252 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005253 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005254 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005255
5256 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005257
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005258 /* Hide all surfaces by removing the fullscreen, panel and
5259 * toplevel layers. This way nothing else can show or receive
5260 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005261
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005262 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005263 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005264 if (shell->showing_input_panels)
5265 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005266 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005267 wl_list_insert(&shell->compositor->cursor_layer.link,
5268 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005269
Derek Foreman004b4a12015-07-20 16:28:13 -05005270 weston_compositor_sleep(shell->compositor);
5271
Neil Robertsb4a91702014-04-09 16:33:32 +01005272 /* Remove the keyboard focus on all seats. This will be
5273 * restored to the workspace's saved state via
5274 * restore_focus_state when the compositor is unlocked */
5275 unfocus_all_seats(shell);
5276
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005277 /* TODO: disable bindings that should not work while locked. */
5278
5279 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005280}
5281
5282static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005283unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005284{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005285 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005286 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005287 return;
5288 }
5289
5290 /* If desktop-shell client has gone away, unlock immediately. */
5291 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005292 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005293 return;
5294 }
5295
5296 if (shell->prepare_event_sent)
5297 return;
5298
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005299 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005300 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005301}
5302
5303static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005304shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005305{
5306 struct desktop_shell *shell = data;
5307
5308 shell->fade.animation = NULL;
5309
5310 switch (shell->fade.type) {
5311 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005312 weston_surface_destroy(shell->fade.view->surface);
5313 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005314 break;
5315 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005316 lock(shell);
5317 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005318 default:
5319 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005320 }
5321}
5322
Jason Ekstranda7af7042013-10-12 22:38:11 -05005323static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005324shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005325{
5326 struct weston_compositor *compositor = shell->compositor;
5327 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005328 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005329
5330 surface = weston_surface_create(compositor);
5331 if (!surface)
5332 return NULL;
5333
Jason Ekstranda7af7042013-10-12 22:38:11 -05005334 view = weston_view_create(surface);
5335 if (!view) {
5336 weston_surface_destroy(surface);
5337 return NULL;
5338 }
5339
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005340 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005341 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005342 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005343 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5344 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005345 pixman_region32_init(&surface->input);
5346
Jason Ekstranda7af7042013-10-12 22:38:11 -05005347 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005348}
5349
5350static void
5351shell_fade(struct desktop_shell *shell, enum fade_type type)
5352{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005353 float tint;
5354
5355 switch (type) {
5356 case FADE_IN:
5357 tint = 0.0;
5358 break;
5359 case FADE_OUT:
5360 tint = 1.0;
5361 break;
5362 default:
5363 weston_log("shell: invalid fade type\n");
5364 return;
5365 }
5366
5367 shell->fade.type = type;
5368
Jason Ekstranda7af7042013-10-12 22:38:11 -05005369 if (shell->fade.view == NULL) {
5370 shell->fade.view = shell_fade_create_surface(shell);
5371 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005372 return;
5373
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 shell->fade.view->alpha = 1.0 - tint;
5375 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005376 }
5377
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005378 if (shell->fade.view->output == NULL) {
5379 /* If the black view gets a NULL output, we lost the
5380 * last output and we'll just cancel the fade. This
5381 * happens when you close the last window under the
5382 * X11 or Wayland backends. */
5383 shell->locked = false;
5384 weston_surface_destroy(shell->fade.view->surface);
5385 shell->fade.view = NULL;
5386 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005387 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005388 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005389 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005390 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005391 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005392 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005393 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005394}
5395
5396static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005397do_shell_fade_startup(void *data)
5398{
5399 struct desktop_shell *shell = data;
5400
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005401 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005402 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005403 } else {
5404 weston_log("desktop shell: "
5405 "unexpected fade-in animation type %d\n",
5406 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005407 weston_surface_destroy(shell->fade.view->surface);
5408 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005409 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005410}
5411
5412static void
5413shell_fade_startup(struct desktop_shell *shell)
5414{
5415 struct wl_event_loop *loop;
5416
5417 if (!shell->fade.startup_timer)
5418 return;
5419
5420 wl_event_source_remove(shell->fade.startup_timer);
5421 shell->fade.startup_timer = NULL;
5422
5423 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5424 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5425}
5426
5427static int
5428fade_startup_timeout(void *data)
5429{
5430 struct desktop_shell *shell = data;
5431
5432 shell_fade_startup(shell);
5433 return 0;
5434}
5435
5436static void
5437shell_fade_init(struct desktop_shell *shell)
5438{
5439 /* Make compositor output all black, and wait for the desktop-shell
5440 * client to signal it is ready, then fade in. The timer triggers a
5441 * fade-in, in case the desktop-shell client takes too long.
5442 */
5443
5444 struct wl_event_loop *loop;
5445
Jason Ekstranda7af7042013-10-12 22:38:11 -05005446 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005447 weston_log("%s: warning: fade surface already exists\n",
5448 __func__);
5449 return;
5450 }
5451
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005452 if (shell->startup_animation_type == ANIMATION_NONE)
5453 return;
5454
Jason Ekstranda7af7042013-10-12 22:38:11 -05005455 shell->fade.view = shell_fade_create_surface(shell);
5456 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005457 return;
5458
Jason Ekstranda7af7042013-10-12 22:38:11 -05005459 weston_view_update_transform(shell->fade.view);
5460 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005461
5462 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5463 shell->fade.startup_timer =
5464 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5465 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5466}
5467
5468static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005469idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005470{
5471 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005472 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005473 struct weston_seat *seat;
5474
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005475 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005476 struct weston_touch *touch = weston_seat_get_touch(seat);
5477 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5478
5479 if (pointer)
5480 popup_grab_end(pointer);
5481 if (touch)
5482 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005483 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005484
5485 shell_fade(shell, FADE_OUT);
5486 /* lock() is called from shell_fade_done() */
5487}
5488
5489static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005490wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005491{
5492 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005493 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005494
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005495 unlock(shell);
5496}
5497
5498static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005499center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005500{
Giulio Camuffob8366642013-04-25 13:57:46 +03005501 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005502 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005503
Jason Ekstranda7af7042013-10-12 22:38:11 -05005504 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005505
5506 x = output->x + (output->width - width) / 2 - surf_x / 2;
5507 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005508
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005509 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005510}
5511
5512static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005513weston_view_set_initial_position(struct weston_view *view,
5514 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005515{
5516 struct weston_compositor *compositor = shell->compositor;
5517 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005518 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005519 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005520 struct weston_output *output, *target_output = NULL;
5521 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005522 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005523
5524 /* As a heuristic place the new window on the same output as the
5525 * pointer. Falling back to the output containing 0, 0.
5526 *
5527 * TODO: Do something clever for touch too?
5528 */
5529 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005530 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5531
5532 if (pointer) {
5533 ix = wl_fixed_to_int(pointer->x);
5534 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005535 break;
5536 }
5537 }
5538
5539 wl_list_for_each(output, &compositor->output_list, link) {
5540 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5541 target_output = output;
5542 break;
5543 }
5544 }
5545
5546 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005547 weston_view_set_position(view, 10 + random() % 400,
5548 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005549 return;
5550 }
5551
5552 /* Valid range within output where the surface will still be onscreen.
5553 * If this is negative it means that the surface is bigger than
5554 * output.
5555 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005556 get_output_work_area(shell, target_output, &area);
5557
Derek Foremanf814c5d2015-04-15 12:23:54 -05005558 x = area.x;
5559 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005560 range_x = area.width - view->surface->width;
5561 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005562
Rob Bradford4cb88c72012-08-13 15:18:44 +01005563 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005564 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005565
5566 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005567 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005568
Jason Ekstranda7af7042013-10-12 22:38:11 -05005569 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005570}
5571
5572static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005573set_maximized_position(struct desktop_shell *shell,
5574 struct shell_surface *shsurf)
5575{
5576 int32_t surf_x, surf_y;
5577 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005578 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005579
Jonny Lambd73c6942014-08-20 15:53:20 +02005580 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005581 if (shsurf->has_set_geometry) {
5582 surf_x = shsurf->geometry.x;
5583 surf_y = shsurf->geometry.y;
5584 } else {
5585 surface_subsurfaces_boundingbox(shsurf->surface,
5586 &surf_x, &surf_y, NULL, NULL);
5587 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005588 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005589
5590 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005591 e->x1 + area.x - surf_x,
5592 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005593}
5594
5595static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005596map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005597 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005598{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005599 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005600 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005601
Pekka Paalanen77346a62011-11-30 16:26:35 +02005602 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005603 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005604 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005605 if (shsurf->state.fullscreen) {
5606 center_on_output(shsurf->view, shsurf->fullscreen_output);
5607 shell_map_fullscreen(shsurf);
5608 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005609 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005610 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005611 weston_view_set_initial_position(shsurf->view, shell);
5612 }
Juan Zhao96879df2012-02-07 08:45:41 +08005613 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005614 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005615 if (shell_map_popup(shsurf) != 0)
5616 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005617 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005618 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005619 weston_view_set_position(shsurf->view,
5620 shsurf->view->geometry.x + sx,
5621 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005622 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005623 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005624 default:
5625 ;
5626 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005627
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005628 /* Surface stacking order, see also activate(). */
5629 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005630
Jason Ekstranda7af7042013-10-12 22:38:11 -05005631 if (shsurf->type != SHELL_SURFACE_NONE) {
5632 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005633 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005634 shsurf->surface->output = shsurf->output;
5635 shsurf->view->output = shsurf->output;
5636 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005637 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005638
Jason Ekstranda7af7042013-10-12 22:38:11 -05005639 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005640 /* XXX: xwayland's using the same fields for transient type */
5641 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005642 if (shsurf->transient.flags ==
5643 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5644 break;
5645 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005646 if (shsurf->state.relative &&
5647 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5648 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005649 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005650 break;
5651 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005652 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005653 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005654 case SHELL_SURFACE_POPUP:
5655 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005656 default:
5657 break;
5658 }
5659
Rafael Antognolli03b16592013-12-03 15:35:42 -02005660 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5661 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005662 {
5663 switch (shell->win_animation_type) {
5664 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005665 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005666 break;
5667 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005668 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005669 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005670 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005671 default:
5672 break;
5673 }
5674 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005675}
5676
5677static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005678configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005679 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005680{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005681 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005682 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005683
Pekka Paalanen77346a62011-11-30 16:26:35 +02005684 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005685
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005686 assert(shsurf);
5687
Rafael Antognolli03b16592013-12-03 15:35:42 -02005688 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005689 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005690 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005691 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005692 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005693 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005694 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005695
Alex Wu4539b082012-03-01 12:57:46 +08005696 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005697 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005698 wl_list_for_each(view, &surface->views, surface_link)
5699 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005700
Rafael Antognolli03b16592013-12-03 15:35:42 -02005701 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005702 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005703 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005704}
5705
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005706static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005707shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005708{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005709 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005710 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005711 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005712
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005713 assert(shsurf);
5714
5715 shell = shsurf->shell;
5716
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005717 if (!weston_surface_is_mapped(es) &&
5718 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005719 remove_popup_grab(shsurf);
5720 }
5721
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005722 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005723 return;
5724
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005725 if (shsurf->has_next_geometry) {
5726 shsurf->geometry = shsurf->next_geometry;
5727 shsurf->has_next_geometry = false;
5728 shsurf->has_set_geometry = true;
5729 } else if (!shsurf->has_set_geometry) {
5730 surface_subsurfaces_boundingbox(shsurf->surface,
5731 &shsurf->geometry.x,
5732 &shsurf->geometry.y,
5733 &shsurf->geometry.width,
5734 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005735 }
5736
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005737 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005738 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005739 type_changed = 1;
5740 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005741
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005742 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005743 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005744 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005745 shsurf->last_width != es->width ||
5746 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005747 float from_x, from_y;
5748 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005749
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005750 if (shsurf->resize_edges) {
5751 sx = 0;
5752 sy = 0;
5753 }
5754
5755 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5756 sx = shsurf->last_width - es->width;
5757 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5758 sy = shsurf->last_height - es->height;
5759
5760 shsurf->last_width = es->width;
5761 shsurf->last_height = es->height;
5762
Jason Ekstranda7af7042013-10-12 22:38:11 -05005763 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5764 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005765 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005766 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005767 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005768 }
5769}
5770
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005771static bool
5772check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5773{
5774 struct timespec now;
5775
5776 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5777 return false;
5778
5779 /*
5780 * If the shell helper client dies before the session has been
5781 * up for roughly 30 seconds, better just make Weston shut down,
5782 * because the user likely has no way to interact with the desktop
5783 * anyway.
5784 */
5785 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5786 weston_log("Error: %s apparently cannot run at all.\n",
5787 shell->client);
5788 weston_log_continue(STAMP_SPACE "Quitting...");
5789 wl_display_terminate(shell->compositor->wl_display);
5790
5791 return true;
5792 }
5793
5794 return false;
5795}
5796
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005797static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005798
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005799static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005800respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005801{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005802 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005803
5804 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5805 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005806 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005807 shell->child.deathstamp = time;
5808 shell->child.deathcount = 0;
5809 }
5810
5811 shell->child.deathcount++;
5812 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005813 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005814 return;
5815 }
5816
Pekka Paalanen826dc142014-08-27 12:11:53 +03005817 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005818 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005819}
5820
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005821static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005822desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5823{
5824 struct desktop_shell *shell;
5825
5826 shell = container_of(listener, struct desktop_shell,
5827 child.client_destroy_listener);
5828
Pekka Paalanen826dc142014-08-27 12:11:53 +03005829 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005830 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005831 /*
5832 * unbind_desktop_shell() will reset shell->child.desktop_shell
5833 * before the respawned process has a chance to create a new
5834 * desktop_shell object, because we are being called from the
5835 * wl_client destructor which destroys all wl_resources before
5836 * returning.
5837 */
5838
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005839 if (!check_desktop_shell_crash_too_early(shell))
5840 respawn_desktop_shell_process(shell);
5841
Pekka Paalanen826dc142014-08-27 12:11:53 +03005842 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005843}
5844
5845static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005846launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005847{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005848 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005849
Pekka Paalanen826dc142014-08-27 12:11:53 +03005850 shell->child.client = weston_client_start(shell->compositor,
5851 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005852
Arnaud Vrac42631192014-08-25 20:56:46 +02005853 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005854 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005855 return;
5856 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005857
5858 shell->child.client_destroy_listener.notify =
5859 desktop_shell_client_destroy;
5860 wl_client_add_destroy_listener(shell->child.client,
5861 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005862}
5863
5864static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005865handle_shell_client_destroy(struct wl_listener *listener, void *data)
5866{
5867 struct shell_client *sc =
5868 container_of(listener, struct shell_client, destroy_listener);
5869
5870 if (sc->ping_timer)
5871 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005872
5873 /* Since we're about to free shell_client, we remove it from the
5874 * head of the surface list so we don't use that freed list node
5875 * during surface clean up later on.
5876 */
5877 wl_list_remove(&sc->surface_list);
5878
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005879 free(sc);
5880}
5881
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005882static struct shell_client *
5883shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5884 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005885{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005886 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005887
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005888 sc = zalloc(sizeof *sc);
5889 if (sc == NULL) {
5890 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005891 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005892 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005893
5894 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005895 if (sc->resource == NULL) {
5896 free(sc);
5897 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005898 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005899 }
5900
5901 sc->client = client;
5902 sc->shell = shell;
5903 sc->destroy_listener.notify = handle_shell_client_destroy;
5904 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005905 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005906
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005907 return sc;
5908}
5909
5910static void
5911bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5912{
5913 struct desktop_shell *shell = data;
5914 struct shell_client *sc;
5915
5916 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5917 if (sc)
5918 wl_resource_set_implementation(sc->resource,
5919 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005920 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005921}
5922
5923static void
5924bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5925{
5926 struct desktop_shell *shell = data;
5927 struct shell_client *sc;
5928
5929 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5930 if (sc)
5931 wl_resource_set_dispatcher(sc->resource,
5932 xdg_shell_unversioned_dispatch,
5933 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005934}
5935
5936static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005937unbind_desktop_shell(struct wl_resource *resource)
5938{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005939 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005940
5941 if (shell->locked)
5942 resume_desktop(shell);
5943
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005944 shell->child.desktop_shell = NULL;
5945 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005946}
5947
5948static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005949bind_desktop_shell(struct wl_client *client,
5950 void *data, uint32_t version, uint32_t id)
5951{
Tiago Vignattibe143262012-04-16 17:31:41 +03005952 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005953 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005954
Jason Ekstranda85118c2013-06-27 20:17:02 -05005955 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005956 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005957
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005958 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005959 wl_resource_set_implementation(resource,
5960 &desktop_shell_implementation,
5961 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005962 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005963
5964 if (version < 2)
5965 shell_fade_startup(shell);
5966
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005967 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005968 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005969
5970 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5971 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005972}
5973
Kristian Høgsberg07045392012-02-19 18:52:44 -05005974struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005975 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005976 struct weston_surface *current;
5977 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005978 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005979 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005980};
5981
5982static void
5983switcher_next(struct switcher *switcher)
5984{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005985 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005986 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005987 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005988 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005989
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005990 /* temporary re-display minimized surfaces */
5991 struct weston_view *tmp;
5992 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005993 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5994 weston_layer_entry_remove(&view->layer_link);
5995 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005996 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5997 *minimized = view;
5998 }
5999
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006000 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006001 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006002 if (shsurf &&
6003 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6004 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006005 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006006 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006007 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006008 next = view->surface;
6009 prev = view->surface;
6010 view->alpha = 0.25;
6011 weston_view_geometry_dirty(view);
6012 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006013 }
Alex Wu1659daa2012-04-01 20:13:09 +08006014
Jason Ekstranda7af7042013-10-12 22:38:11 -05006015 if (is_black_surface(view->surface, NULL)) {
6016 view->alpha = 0.25;
6017 weston_view_geometry_dirty(view);
6018 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006019 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006020 }
6021
6022 if (next == NULL)
6023 next = first;
6024
Alex Wu07b26062012-03-12 16:06:01 +08006025 if (next == NULL)
6026 return;
6027
Kristian Høgsberg07045392012-02-19 18:52:44 -05006028 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006029 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006030
6031 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006032 wl_list_for_each(view, &next->views, surface_link)
6033 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006034
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006035 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006036 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006037 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006038}
6039
6040static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006041switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006042{
6043 struct switcher *switcher =
6044 container_of(listener, struct switcher, listener);
6045
6046 switcher_next(switcher);
6047}
6048
6049static void
Daniel Stone351eb612012-05-31 15:27:47 -04006050switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006051{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006052 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006053 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006054 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006055
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006056 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006057 if (is_focus_view(view))
6058 continue;
6059
Jason Ekstranda7af7042013-10-12 22:38:11 -05006060 view->alpha = 1.0;
6061 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006062 }
6063
Alex Wu07b26062012-03-12 16:06:01 +08006064 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006065 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006066 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006067 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006068 weston_keyboard_end_grab(keyboard);
6069 if (keyboard->input_method_resource)
6070 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006071
6072 /* re-hide surfaces that were temporary shown during the switch */
6073 struct weston_view **minimized;
6074 wl_array_for_each(minimized, &switcher->minimized_array) {
6075 /* with the exception of the current selected */
6076 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006077 weston_layer_entry_remove(&(*minimized)->layer_link);
6078 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006079 weston_view_damage_below(*minimized);
6080 }
6081 }
6082 wl_array_release(&switcher->minimized_array);
6083
Kristian Høgsberg07045392012-02-19 18:52:44 -05006084 free(switcher);
6085}
6086
6087static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006088switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006089 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006090{
6091 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006092 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006093
Daniel Stonec9785ea2012-05-30 16:31:52 +01006094 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006095 switcher_next(switcher);
6096}
6097
6098static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006099switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006100 uint32_t mods_depressed, uint32_t mods_latched,
6101 uint32_t mods_locked, uint32_t group)
6102{
6103 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006104 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006105
Daniel Stone351eb612012-05-31 15:27:47 -04006106 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6107 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006108}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006109
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006110static void
6111switcher_cancel(struct weston_keyboard_grab *grab)
6112{
6113 struct switcher *switcher = container_of(grab, struct switcher, grab);
6114
6115 switcher_destroy(switcher);
6116}
6117
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006118static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006119 switcher_key,
6120 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006121 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006122};
6123
6124static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006125switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6126 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006127{
Tiago Vignattibe143262012-04-16 17:31:41 +03006128 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006129 struct switcher *switcher;
6130
6131 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006132 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006133 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006134 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006135 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006136 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006137
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006138 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006139 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006140 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006141 weston_keyboard_start_grab(keyboard, &switcher->grab);
6142 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006143 switcher_next(switcher);
6144}
6145
Pekka Paalanen3c647232011-12-22 13:43:43 +02006146static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006147backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6148 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006149{
6150 struct weston_compositor *compositor = data;
6151 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006152 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006153
6154 /* TODO: we're limiting to simple use cases, where we assume just
6155 * control on the primary display. We'd have to extend later if we
6156 * ever get support for setting backlights on random desktop LCD
6157 * panels though */
6158 output = get_default_output(compositor);
6159 if (!output)
6160 return;
6161
6162 if (!output->set_backlight)
6163 return;
6164
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006165 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6166 backlight_new = output->backlight_current - 25;
6167 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6168 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006169
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006170 if (backlight_new < 5)
6171 backlight_new = 5;
6172 if (backlight_new > 255)
6173 backlight_new = 255;
6174
6175 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006176 output->set_backlight(output, output->backlight_current);
6177}
6178
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006179static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006180force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6181 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006182{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006183 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006184 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006185 struct desktop_shell *shell = data;
6186 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006187 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006188
Derek Foreman8ae2db52015-07-15 13:00:36 -05006189 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006190 if (!focus_surface)
6191 return;
6192
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006193 wl_signal_emit(&compositor->kill_signal, focus_surface);
6194
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006195 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006196 wl_client_get_credentials(client, &pid, NULL, NULL);
6197
6198 /* Skip clients that we launched ourselves (the credentials of
6199 * the socketpair is ours) */
6200 if (pid == getpid())
6201 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006202
Daniel Stone325fc2d2012-05-30 16:31:58 +01006203 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006204}
6205
6206static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006207workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006208 uint32_t key, void *data)
6209{
6210 struct desktop_shell *shell = data;
6211 unsigned int new_index = shell->workspaces.current;
6212
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006213 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006214 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006215 if (new_index != 0)
6216 new_index--;
6217
6218 change_workspace(shell, new_index);
6219}
6220
6221static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006222workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006223 uint32_t key, void *data)
6224{
6225 struct desktop_shell *shell = data;
6226 unsigned int new_index = shell->workspaces.current;
6227
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006228 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006229 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006230 if (new_index < shell->workspaces.num - 1)
6231 new_index++;
6232
6233 change_workspace(shell, new_index);
6234}
6235
6236static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006237workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006238 uint32_t key, void *data)
6239{
6240 struct desktop_shell *shell = data;
6241 unsigned int new_index;
6242
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006243 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006244 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006245 new_index = key - KEY_F1;
6246 if (new_index >= shell->workspaces.num)
6247 new_index = shell->workspaces.num - 1;
6248
6249 change_workspace(shell, new_index);
6250}
6251
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006252static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006253workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6254 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006255{
6256 struct desktop_shell *shell = data;
6257 unsigned int new_index = shell->workspaces.current;
6258
6259 if (shell->locked)
6260 return;
6261
6262 if (new_index != 0)
6263 new_index--;
6264
Derek Foreman8ae2db52015-07-15 13:00:36 -05006265 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006266}
6267
6268static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006269workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6270 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006271{
6272 struct desktop_shell *shell = data;
6273 unsigned int new_index = shell->workspaces.current;
6274
6275 if (shell->locked)
6276 return;
6277
6278 if (new_index < shell->workspaces.num - 1)
6279 new_index++;
6280
Derek Foreman8ae2db52015-07-15 13:00:36 -05006281 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006282}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006283
6284static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006285shell_reposition_view_on_output_destroy(struct weston_view *view)
6286{
6287 struct weston_output *output, *first_output;
6288 struct weston_compositor *ec = view->surface->compositor;
6289 struct shell_surface *shsurf;
6290 float x, y;
6291 int visible;
6292
6293 x = view->geometry.x;
6294 y = view->geometry.y;
6295
6296 /* At this point the destroyed output is not in the list anymore.
6297 * If the view is still visible somewhere, we leave where it is,
6298 * otherwise, move it to the first output. */
6299 visible = 0;
6300 wl_list_for_each(output, &ec->output_list, link) {
6301 if (pixman_region32_contains_point(&output->region,
6302 x, y, NULL)) {
6303 visible = 1;
6304 break;
6305 }
6306 }
6307
6308 if (!visible) {
6309 first_output = container_of(ec->output_list.next,
6310 struct weston_output, link);
6311
6312 x = first_output->x + first_output->width / 4;
6313 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006314
6315 weston_view_set_position(view, x, y);
6316 } else {
6317 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006318 }
6319
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006320
6321 shsurf = get_shell_surface(view->surface);
6322
6323 if (shsurf) {
6324 shsurf->saved_position_valid = false;
6325 shsurf->next_state.maximized = false;
6326 shsurf->next_state.fullscreen = false;
6327 shsurf->state_changed = true;
6328 }
6329}
6330
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006331void
6332shell_for_each_layer(struct desktop_shell *shell,
6333 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006334{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006335 struct workspace **ws;
6336
6337 func(shell, &shell->fullscreen_layer, data);
6338 func(shell, &shell->panel_layer, data);
6339 func(shell, &shell->background_layer, data);
6340 func(shell, &shell->lock_layer, data);
6341 func(shell, &shell->input_panel_layer, data);
6342
6343 wl_array_for_each(ws, &shell->workspaces.array)
6344 func(shell, &(*ws)->layer, data);
6345}
6346
6347static void
6348shell_output_destroy_move_layer(struct desktop_shell *shell,
6349 struct weston_layer *layer,
6350 void *data)
6351{
6352 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006353 struct weston_view *view;
6354
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006355 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006356 if (view->output != output)
6357 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006358
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006359 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006360 }
6361}
6362
6363static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006364handle_output_destroy(struct wl_listener *listener, void *data)
6365{
6366 struct shell_output *output_listener =
6367 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006368 struct weston_output *output = output_listener->output;
6369 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006370
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006371 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006372
Xiong Zhang6b481422013-10-23 13:58:32 +08006373 wl_list_remove(&output_listener->destroy_listener.link);
6374 wl_list_remove(&output_listener->link);
6375 free(output_listener);
6376}
6377
6378static void
6379create_shell_output(struct desktop_shell *shell,
6380 struct weston_output *output)
6381{
6382 struct shell_output *shell_output;
6383
6384 shell_output = zalloc(sizeof *shell_output);
6385 if (shell_output == NULL)
6386 return;
6387
6388 shell_output->output = output;
6389 shell_output->shell = shell;
6390 shell_output->destroy_listener.notify = handle_output_destroy;
6391 wl_signal_add(&output->destroy_signal,
6392 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006393 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006394}
6395
6396static void
6397handle_output_create(struct wl_listener *listener, void *data)
6398{
6399 struct desktop_shell *shell =
6400 container_of(listener, struct desktop_shell, output_create_listener);
6401 struct weston_output *output = (struct weston_output *)data;
6402
6403 create_shell_output(shell, output);
6404}
6405
6406static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006407handle_output_move_layer(struct desktop_shell *shell,
6408 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006409{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006410 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006411 struct weston_view *view;
6412 float x, y;
6413
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006414 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006415 if (view->output != output)
6416 continue;
6417
6418 x = view->geometry.x + output->move_x;
6419 y = view->geometry.y + output->move_y;
6420 weston_view_set_position(view, x, y);
6421 }
6422}
6423
6424static void
6425handle_output_move(struct wl_listener *listener, void *data)
6426{
6427 struct desktop_shell *shell;
6428
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006429 shell = container_of(listener, struct desktop_shell,
6430 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006431
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006432 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006433}
6434
6435static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006436setup_output_destroy_handler(struct weston_compositor *ec,
6437 struct desktop_shell *shell)
6438{
6439 struct weston_output *output;
6440
6441 wl_list_init(&shell->output_list);
6442 wl_list_for_each(output, &ec->output_list, link)
6443 create_shell_output(shell, output);
6444
6445 shell->output_create_listener.notify = handle_output_create;
6446 wl_signal_add(&ec->output_created_signal,
6447 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006448
6449 shell->output_move_listener.notify = handle_output_move;
6450 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006451}
6452
6453static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006454shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006455{
Tiago Vignattibe143262012-04-16 17:31:41 +03006456 struct desktop_shell *shell =
6457 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006458 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006459 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006460
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006461 /* Force state to unlocked so we don't try to fade */
6462 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006463
6464 if (shell->child.client) {
6465 /* disable respawn */
6466 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006467 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006468 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006469
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006470 wl_list_remove(&shell->idle_listener.link);
6471 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006472
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006473 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006474 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006475
Xiong Zhang6b481422013-10-23 13:58:32 +08006476 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6477 wl_list_remove(&shell_output->destroy_listener.link);
6478 wl_list_remove(&shell_output->link);
6479 free(shell_output);
6480 }
6481
6482 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006483 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006484
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006485 wl_array_for_each(ws, &shell->workspaces.array)
6486 workspace_destroy(*ws);
6487 wl_array_release(&shell->workspaces.array);
6488
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006489 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006490 free(shell);
6491}
6492
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006493static void
6494shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6495{
6496 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006497 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006498
6499 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006500 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6501 MODIFIER_CTRL | MODIFIER_ALT,
6502 terminate_binding, ec);
6503 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6504 click_to_activate_binding,
6505 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006506 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6507 click_to_activate_binding,
6508 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006509 weston_compositor_add_touch_binding(ec, 0,
6510 touch_to_activate_binding,
6511 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006512 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6513 MODIFIER_SUPER | MODIFIER_ALT,
6514 surface_opacity_binding, NULL);
6515 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6516 MODIFIER_SUPER, zoom_axis_binding,
6517 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006518
6519 /* configurable bindings */
6520 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006521 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6522 zoom_key_binding, NULL);
6523 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6524 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006525 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6526 maximize_binding, NULL);
6527 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6528 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006529 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6530 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006531 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006532 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006533 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006534 weston_compositor_add_button_binding(ec, BTN_LEFT,
6535 mod | MODIFIER_SHIFT,
6536 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006537
6538 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006539 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006540 rotate_binding, NULL);
6541
Daniel Stone325fc2d2012-05-30 16:31:58 +01006542 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6543 shell);
6544 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6545 ec);
6546 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6547 backlight_binding, ec);
6548 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6549 ec);
6550 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6551 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006552 weston_compositor_add_key_binding(ec, KEY_K, mod,
6553 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006554 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6555 workspace_up_binding, shell);
6556 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6557 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006558 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6559 workspace_move_surface_up_binding,
6560 shell);
6561 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6562 workspace_move_surface_down_binding,
6563 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006564
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006565 if (shell->exposay_modifier)
6566 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6567 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006568
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006569 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6570 if (shell->workspaces.num > 1) {
6571 num_workspace_bindings = shell->workspaces.num;
6572 if (num_workspace_bindings > 6)
6573 num_workspace_bindings = 6;
6574 for (i = 0; i < num_workspace_bindings; i++)
6575 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6576 workspace_f_binding,
6577 shell);
6578 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006579
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006580 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006581}
6582
Jason Ekstrand024177c2014-04-21 19:42:58 -05006583static void
6584handle_seat_created(struct wl_listener *listener, void *data)
6585{
6586 struct weston_seat *seat = data;
6587
6588 create_shell_seat(seat);
6589}
6590
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006591WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006592module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006593 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006594{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006595 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006596 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006597 struct workspace **pws;
6598 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006599 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006600
Peter Huttererf3d62272013-08-08 11:57:05 +10006601 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006602 if (shell == NULL)
6603 return -1;
6604
Kristian Høgsberg75840622011-09-06 13:48:16 -04006605 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006606
6607 shell->destroy_listener.notify = shell_destroy;
6608 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006609 shell->idle_listener.notify = idle_handler;
6610 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6611 shell->wake_listener.notify = wake_handler;
6612 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006613
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006614 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006615 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006616 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006617 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006618 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006619 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006620 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006621 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006622 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006623 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006624 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006625 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006626 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006627
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006628 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6629 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006630 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6631 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006632 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006633
6634 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006635 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006636
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006637 if (input_panel_setup(shell) < 0)
6638 return -1;
6639
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006640 shell->text_backend = text_backend_init(ec);
6641 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006642 return -1;
6643
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006644 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006645
Daniel Stonedf8133b2013-11-19 11:37:14 +01006646 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6647 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6648
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006649 for (i = 0; i < shell->workspaces.num; i++) {
6650 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6651 if (pws == NULL)
6652 return -1;
6653
6654 *pws = workspace_create();
6655 if (*pws == NULL)
6656 return -1;
6657 }
6658 activate_workspace(shell, 0);
6659
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006660 weston_layer_init(&shell->minimized_layer, NULL);
6661
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006662 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006663 wl_list_init(&shell->workspaces.animation.link);
6664 shell->workspaces.animation.frame = animate_workspace_change_frame;
6665
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006666 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006667 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006668 return -1;
6669
Rafael Antognollie2a34552013-12-03 15:35:45 -02006670 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6671 shell, bind_xdg_shell) == NULL)
6672 return -1;
6673
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006674 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006675 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006676 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006677 return -1;
6678
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006679 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6680 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006681 return -1;
6682
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006683 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006684
Jonny Lamb765760d2014-08-20 15:53:19 +02006685 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6686
Xiong Zhang6b481422013-10-23 13:58:32 +08006687 setup_output_destroy_handler(ec, shell);
6688
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006689 loop = wl_display_get_event_loop(ec->wl_display);
6690 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006691
Jason Ekstrand024177c2014-04-21 19:42:58 -05006692 wl_list_for_each(seat, &ec->seat_list, link)
6693 handle_seat_created(NULL, seat);
6694 shell->seat_create_listener.notify = handle_seat_created;
6695 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006696
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006697 screenshooter_create(ec);
6698
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006699 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006700
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006701 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006702
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006703 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6704
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006705 return 0;
6706}