blob: c1308c52db33ddb972bb15fc7c75239dac6f60b9 [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
484static void
485send_configure_for_surface(struct shell_surface *shsurf)
486{
487 int32_t width, height;
488 struct surface_state *state;
489
490 if (shsurf->state_requested)
491 state = &shsurf->requested_state;
492 else if (shsurf->state_changed)
493 state = &shsurf->next_state;
494 else
495 state = &shsurf->state;
496
497 if (state->fullscreen) {
498 width = shsurf->output->width;
499 height = shsurf->output->height;
500 } else if (state->maximized) {
501 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200502 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700503
504 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200505 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700506
Jonny Lambd73c6942014-08-20 15:53:20 +0200507 width = area.width;
508 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900509 } else if (shsurf->resize_edges) {
510 width = shsurf->geometry.width;
511 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700512 } else {
513 width = 0;
514 height = 0;
515 }
516
517 shsurf->client->send_configure(shsurf->surface, width, height);
518}
519
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300520static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400521shell_surface_state_changed(struct shell_surface *shsurf)
522{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700523 if (shell_surface_is_xdg_surface(shsurf))
524 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400525}
526
527static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300528shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300529{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700530 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400531 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700532 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400533
534 if (grab->shsurf->resize_edges) {
535 grab->shsurf->resize_edges = 0;
536 shell_surface_state_changed(grab->shsurf);
537 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700538 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300539
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700540 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300541}
542
543static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700544shell_touch_grab_start(struct shell_touch_grab *grab,
545 const struct weston_touch_grab_interface *interface,
546 struct shell_surface *shsurf,
547 struct weston_touch *touch)
548{
549 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500550 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800551
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200552 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500553 if (pointer)
554 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700555
Rusty Lynch1084da52013-08-15 09:10:08 -0700556 grab->grab.interface = interface;
557 grab->shsurf = shsurf;
558 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
559 wl_signal_add(&shsurf->destroy_signal,
560 &grab->shsurf_destroy_listener);
561
562 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700563 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700564
565 weston_touch_start_grab(touch, &grab->grab);
566 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500567 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500568 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700569}
570
571static void
572shell_touch_grab_end(struct shell_touch_grab *grab)
573{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700574 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700575 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700576 grab->shsurf->grabbed = 0;
577 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700578
579 weston_touch_end_grab(grab->touch);
580}
581
582static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800584 struct weston_output *output);
585
Daniel Stone496ca172012-05-30 16:31:42 +0100586static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300587get_modifier(char *modifier)
588{
589 if (!modifier)
590 return MODIFIER_SUPER;
591
592 if (!strcmp("ctrl", modifier))
593 return MODIFIER_CTRL;
594 else if (!strcmp("alt", modifier))
595 return MODIFIER_ALT;
596 else if (!strcmp("super", modifier))
597 return MODIFIER_SUPER;
598 else
599 return MODIFIER_SUPER;
600}
601
Juan Zhaoe10d2792012-04-25 19:09:52 +0800602static enum animation_type
603get_animation_type(char *animation)
604{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800605 if (!animation)
606 return ANIMATION_NONE;
607
Juan Zhaoe10d2792012-04-25 19:09:52 +0800608 if (!strcmp("zoom", animation))
609 return ANIMATION_ZOOM;
610 else if (!strcmp("fade", animation))
611 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100612 else if (!strcmp("dim-layer", animation))
613 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800614 else
615 return ANIMATION_NONE;
616}
617
Alex Wu4539b082012-03-01 12:57:46 +0800618static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400619shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200620{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400621 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500622 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300623 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200624
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400625 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400626 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300627 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
628 WESTON_SHELL_CLIENT);
629 if (ret < 0)
630 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400631 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500632 "client", &s, client);
633 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100634 shell->client = s;
635 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400636 "binding-modifier", &s, "super");
637 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200638 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800639
640 weston_config_section_get_string(section,
641 "exposay-modifier", &s, "none");
642 if (strcmp(s, "none") == 0)
643 shell->exposay_modifier = 0;
644 else
645 shell->exposay_modifier = get_modifier(s);
646 free(s);
647
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400648 weston_config_section_get_string(section, "animation", &s, "none");
649 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200650 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200651 weston_config_section_get_string(section, "close-animation", &s, "fade");
652 shell->win_close_animation_type = get_animation_type(s);
653 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700654 weston_config_section_get_string(section,
655 "startup-animation", &s, "fade");
656 shell->startup_animation_type = get_animation_type(s);
657 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700658 if (shell->startup_animation_type == ANIMATION_ZOOM)
659 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100660 weston_config_section_get_string(section, "focus-animation", &s, "none");
661 shell->focus_animation_type = get_animation_type(s);
662 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400663 weston_config_section_get_uint(section, "num-workspaces",
664 &shell->workspaces.num,
665 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200666}
667
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800668struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100669get_default_output(struct weston_compositor *compositor)
670{
671 return container_of(compositor->output_list.next,
672 struct weston_output, link);
673}
674
Pekka Paalanen8274d902014-08-06 19:36:51 +0300675static int
676focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
677{
678 return snprintf(buf, len, "focus highlight effect for output %s",
679 surface->output->name);
680}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681
682/* no-op func for checking focus surface */
683static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600684focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100685{
686}
687
688static struct focus_surface *
689get_focus_surface(struct weston_surface *surface)
690{
691 if (surface->configure == focus_surface_configure)
692 return surface->configure_private;
693 else
694 return NULL;
695}
696
697static bool
698is_focus_surface (struct weston_surface *es)
699{
700 return (es->configure == focus_surface_configure);
701}
702
703static bool
704is_focus_view (struct weston_view *view)
705{
706 return is_focus_surface (view->surface);
707}
708
709static struct focus_surface *
710create_focus_surface(struct weston_compositor *ec,
711 struct weston_output *output)
712{
713 struct focus_surface *fsurf = NULL;
714 struct weston_surface *surface = NULL;
715
716 fsurf = malloc(sizeof *fsurf);
717 if (!fsurf)
718 return NULL;
719
720 fsurf->surface = weston_surface_create(ec);
721 surface = fsurf->surface;
722 if (surface == NULL) {
723 free(fsurf);
724 return NULL;
725 }
726
727 surface->configure = focus_surface_configure;
728 surface->output = output;
729 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300730 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100731
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800732 fsurf->view = weston_view_create(surface);
733 if (fsurf->view == NULL) {
734 weston_surface_destroy(surface);
735 free(fsurf);
736 return NULL;
737 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100738 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100739
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600740 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600741 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100742 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
743 pixman_region32_fini(&surface->opaque);
744 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
745 output->width, output->height);
746 pixman_region32_fini(&surface->input);
747 pixman_region32_init(&surface->input);
748
749 wl_list_init(&fsurf->workspace_transform.link);
750
751 return fsurf;
752}
753
754static void
755focus_surface_destroy(struct focus_surface *fsurf)
756{
757 weston_surface_destroy(fsurf->surface);
758 free(fsurf);
759}
760
761static void
762focus_animation_done(struct weston_view_animation *animation, void *data)
763{
764 struct workspace *ws = data;
765
766 ws->focus_animation = NULL;
767}
768
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200769static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200770focus_state_destroy(struct focus_state *state)
771{
772 wl_list_remove(&state->seat_destroy_listener.link);
773 wl_list_remove(&state->surface_destroy_listener.link);
774 free(state);
775}
776
777static void
778focus_state_seat_destroy(struct wl_listener *listener, void *data)
779{
780 struct focus_state *state = container_of(listener,
781 struct focus_state,
782 seat_destroy_listener);
783
784 wl_list_remove(&state->link);
785 focus_state_destroy(state);
786}
787
788static void
789focus_state_surface_destroy(struct wl_listener *listener, void *data)
790{
791 struct focus_state *state = container_of(listener,
792 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400793 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400794 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 struct weston_surface *main_surface, *next;
796 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200797
Pekka Paalanen01388e22013-04-25 13:57:44 +0300798 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
799
Kristian Høgsberge3778222012-07-31 17:29:30 -0400800 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300801 wl_list_for_each(view,
802 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400804 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100805 if (is_focus_view(view))
806 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400807
Jason Ekstranda7af7042013-10-12 22:38:11 -0500808 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400809 break;
810 }
811
Pekka Paalanen01388e22013-04-25 13:57:44 +0300812 /* if the focus was a sub-surface, activate its main surface */
813 if (main_surface != state->keyboard_focus)
814 next = main_surface;
815
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100816 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400817 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100818 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100819 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400820 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100821 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
822 if (state->ws->focus_animation)
823 weston_view_animation_destroy(state->ws->focus_animation);
824
825 state->ws->focus_animation = weston_fade_run(
826 state->ws->fsurf_front->view,
827 state->ws->fsurf_front->view->alpha, 0.0, 300,
828 focus_animation_done, state->ws);
829 }
830
Kristian Høgsberge3778222012-07-31 17:29:30 -0400831 wl_list_remove(&state->link);
832 focus_state_destroy(state);
833 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200834}
835
836static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400837focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200838{
Jonas Ådahl04769742012-06-13 00:01:24 +0200839 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200840
841 state = malloc(sizeof *state);
842 if (state == NULL)
843 return NULL;
844
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100845 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400846 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200847 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400848 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200849
850 state->seat_destroy_listener.notify = focus_state_seat_destroy;
851 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400852 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200853 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400854 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200855
856 return state;
857}
858
Jonas Ådahl8538b222012-08-29 22:13:03 +0200859static struct focus_state *
860ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
861{
862 struct workspace *ws = get_current_workspace(shell);
863 struct focus_state *state;
864
865 wl_list_for_each(state, &ws->focus_list, link)
866 if (state->seat == seat)
867 break;
868
869 if (&state->link == &ws->focus_list)
870 state = focus_state_create(seat, ws);
871
872 return state;
873}
874
Jonas Ådahl04769742012-06-13 00:01:24 +0200875static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800876focus_state_set_focus(struct focus_state *state,
877 struct weston_surface *surface)
878{
879 if (state->keyboard_focus) {
880 wl_list_remove(&state->surface_destroy_listener.link);
881 wl_list_init(&state->surface_destroy_listener.link);
882 }
883
884 state->keyboard_focus = surface;
885 if (surface)
886 wl_signal_add(&surface->destroy_signal,
887 &state->surface_destroy_listener);
888}
889
890static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400891restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200892{
893 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400894 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100895 struct wl_list pending_seat_list;
896 struct weston_seat *seat, *next_seat;
897
898 /* Temporarily steal the list of seats so that we can keep
899 * track of the seats we've already processed */
900 wl_list_init(&pending_seat_list);
901 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
902 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200903
904 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500905 struct weston_keyboard *keyboard =
906 weston_seat_get_keyboard(state->seat);
907
Neil Roberts4237f502014-04-09 16:33:31 +0100908 wl_list_remove(&state->seat->link);
909 wl_list_insert(&shell->compositor->seat_list,
910 &state->seat->link);
911
Derek Foreman1281a362015-07-31 16:55:32 -0500912 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800913 continue;
914
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400915 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200916
Derek Foreman1281a362015-07-31 16:55:32 -0500917 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200918 }
Neil Roberts4237f502014-04-09 16:33:31 +0100919
920 /* For any remaining seats that we don't have a focus state
921 * for we'll reset the keyboard focus to NULL */
922 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500923 struct weston_keyboard *keyboard =
924 weston_seat_get_keyboard(seat);
925
Neil Roberts4237f502014-04-09 16:33:31 +0100926 wl_list_insert(&shell->compositor->seat_list, &seat->link);
927
Derek Foreman1281a362015-07-31 16:55:32 -0500928 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100929 continue;
930
Derek Foreman1281a362015-07-31 16:55:32 -0500931 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100932 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200933}
934
935static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
937 struct weston_seat *seat)
938{
Derek Foreman1281a362015-07-31 16:55:32 -0500939 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200940 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200941
942 wl_list_for_each(state, &ws->focus_list, link) {
943 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500944 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200945 return;
946 }
947 }
948}
949
950static void
951drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
952 struct weston_surface *surface)
953{
954 struct focus_state *state;
955
956 wl_list_for_each(state, &ws->focus_list, link)
957 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800958 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200959}
960
961static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100962animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
963 struct weston_view *from, struct weston_view *to)
964{
965 struct weston_output *output;
966 bool focus_surface_created = false;
967
968 /* FIXME: Only support dim animation using two layers */
969 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
970 return;
971
972 output = get_default_output(shell->compositor);
973 if (ws->fsurf_front == NULL && (from || to)) {
974 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800975 if (ws->fsurf_front == NULL)
976 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800978
979 ws->fsurf_back = create_focus_surface(shell->compositor, output);
980 if (ws->fsurf_back == NULL) {
981 focus_surface_destroy(ws->fsurf_front);
982 return;
983 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100984 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800985
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100986 focus_surface_created = true;
987 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300988 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
989 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990 }
991
992 if (ws->focus_animation) {
993 weston_view_animation_destroy(ws->focus_animation);
994 ws->focus_animation = NULL;
995 }
996
997 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300998 weston_layer_entry_insert(&to->layer_link,
999 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001001 weston_layer_entry_insert(&ws->layer.view_list,
1002 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003
1004 if (focus_surface_created) {
1005 ws->focus_animation = weston_fade_run(
1006 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001007 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001008 focus_animation_done, ws);
1009 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001010 weston_layer_entry_insert(&from->layer_link,
1011 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 ws->focus_animation = weston_stable_fade_run(
1013 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001014 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 focus_animation_done, ws);
1016 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001017 weston_layer_entry_insert(&ws->layer.view_list,
1018 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001019 ws->focus_animation = weston_stable_fade_run(
1020 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001021 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001022 focus_animation_done, ws);
1023 }
1024}
1025
1026static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001027workspace_destroy(struct workspace *ws)
1028{
Jonas Ådahl04769742012-06-13 00:01:24 +02001029 struct focus_state *state, *next;
1030
1031 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1032 focus_state_destroy(state);
1033
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 if (ws->fsurf_front)
1035 focus_surface_destroy(ws->fsurf_front);
1036 if (ws->fsurf_back)
1037 focus_surface_destroy(ws->fsurf_back);
1038
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001039 free(ws);
1040}
1041
Jonas Ådahl04769742012-06-13 00:01:24 +02001042static void
1043seat_destroyed(struct wl_listener *listener, void *data)
1044{
1045 struct weston_seat *seat = data;
1046 struct focus_state *state, *next;
1047 struct workspace *ws = container_of(listener,
1048 struct workspace,
1049 seat_destroyed_listener);
1050
1051 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1052 if (state->seat == seat)
1053 wl_list_remove(&state->link);
1054}
1055
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001056static struct workspace *
1057workspace_create(void)
1058{
1059 struct workspace *ws = malloc(sizeof *ws);
1060 if (ws == NULL)
1061 return NULL;
1062
1063 weston_layer_init(&ws->layer, NULL);
1064
Jonas Ådahl04769742012-06-13 00:01:24 +02001065 wl_list_init(&ws->focus_list);
1066 wl_list_init(&ws->seat_destroyed_listener.link);
1067 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001068 ws->fsurf_front = NULL;
1069 ws->fsurf_back = NULL;
1070 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001071
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001072 return ws;
1073}
1074
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001075static int
1076workspace_is_empty(struct workspace *ws)
1077{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001078 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001079}
1080
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001081static struct workspace *
1082get_workspace(struct desktop_shell *shell, unsigned int index)
1083{
1084 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001085 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001086 pws += index;
1087 return *pws;
1088}
1089
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001090struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001091get_current_workspace(struct desktop_shell *shell)
1092{
1093 return get_workspace(shell, shell->workspaces.current);
1094}
1095
1096static void
1097activate_workspace(struct desktop_shell *shell, unsigned int index)
1098{
1099 struct workspace *ws;
1100
1101 ws = get_workspace(shell, index);
1102 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1103
1104 shell->workspaces.current = index;
1105}
1106
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107static unsigned int
1108get_output_height(struct weston_output *output)
1109{
1110 return abs(output->region.extents.y1 - output->region.extents.y2);
1111}
1112
1113static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001114view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116 struct weston_transform *transform;
1117
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001118 if (is_focus_view(view)) {
1119 struct focus_surface *fsurf = get_focus_surface(view->surface);
1120 transform = &fsurf->workspace_transform;
1121 } else {
1122 struct shell_surface *shsurf = get_shell_surface(view->surface);
1123 transform = &shsurf->workspace_transform;
1124 }
1125
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001128 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001130 weston_matrix_init(&transform->matrix);
1131 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001132 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134}
1135
1136static void
1137workspace_translate_out(struct workspace *ws, double fraction)
1138{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140 unsigned int height;
1141 double d;
1142
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001143 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145 d = height * fraction;
1146
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001147 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148 }
1149}
1150
1151static void
1152workspace_translate_in(struct workspace *ws, double fraction)
1153{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155 unsigned int height;
1156 double d;
1157
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001158 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160
1161 if (fraction > 0)
1162 d = -(height - height * fraction);
1163 else
1164 d = height + 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
Jonas Ådahle9d22502012-08-29 22:13:01 +02001171broadcast_current_workspace_state(struct desktop_shell *shell)
1172{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001173 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001174
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001175 wl_resource_for_each(resource, &shell->workspaces.client_list)
1176 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001177 shell->workspaces.current,
1178 shell->workspaces.num);
1179}
1180
1181static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182reverse_workspace_change_animation(struct desktop_shell *shell,
1183 unsigned int index,
1184 struct workspace *from,
1185 struct workspace *to)
1186{
1187 shell->workspaces.current = index;
1188
1189 shell->workspaces.anim_to = to;
1190 shell->workspaces.anim_from = from;
1191 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1192 shell->workspaces.anim_timestamp = 0;
1193
Scott Moreau4272e632012-08-13 09:58:41 -06001194 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195}
1196
1197static void
1198workspace_deactivate_transforms(struct workspace *ws)
1199{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001201 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001203 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001204 if (is_focus_view(view)) {
1205 struct focus_surface *fsurf = get_focus_surface(view->surface);
1206 transform = &fsurf->workspace_transform;
1207 } else {
1208 struct shell_surface *shsurf = get_shell_surface(view->surface);
1209 transform = &shsurf->workspace_transform;
1210 }
1211
1212 if (!wl_list_empty(&transform->link)) {
1213 wl_list_remove(&transform->link);
1214 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001215 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001217 }
1218}
1219
1220static void
1221finish_workspace_change_animation(struct desktop_shell *shell,
1222 struct workspace *from,
1223 struct workspace *to)
1224{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001225 struct weston_view *view;
1226
Scott Moreau4272e632012-08-13 09:58:41 -06001227 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001229 /* Views that extend past the bottom of the output are still
1230 * visible after the workspace animation ends but before its layer
1231 * is hidden. In that case, we need to damage below those views so
1232 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001233 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001234 weston_view_damage_below(view);
1235
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001236 wl_list_remove(&shell->workspaces.animation.link);
1237 workspace_deactivate_transforms(from);
1238 workspace_deactivate_transforms(to);
1239 shell->workspaces.anim_to = NULL;
1240
1241 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1242}
1243
1244static void
1245animate_workspace_change_frame(struct weston_animation *animation,
1246 struct weston_output *output, uint32_t msecs)
1247{
1248 struct desktop_shell *shell =
1249 container_of(animation, struct desktop_shell,
1250 workspaces.animation);
1251 struct workspace *from = shell->workspaces.anim_from;
1252 struct workspace *to = shell->workspaces.anim_to;
1253 uint32_t t;
1254 double x, y;
1255
1256 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1257 finish_workspace_change_animation(shell, from, to);
1258 return;
1259 }
1260
1261 if (shell->workspaces.anim_timestamp == 0) {
1262 if (shell->workspaces.anim_current == 0.0)
1263 shell->workspaces.anim_timestamp = msecs;
1264 else
1265 shell->workspaces.anim_timestamp =
1266 msecs -
1267 /* Invers of movement function 'y' below. */
1268 (asin(1.0 - shell->workspaces.anim_current) *
1269 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1270 M_2_PI);
1271 }
1272
1273 t = msecs - shell->workspaces.anim_timestamp;
1274
1275 /*
1276 * x = [0, π/2]
1277 * y(x) = sin(x)
1278 */
1279 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1280 y = sin(x);
1281
1282 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001283 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001284
1285 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1286 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1287 shell->workspaces.anim_current = y;
1288
Scott Moreau4272e632012-08-13 09:58:41 -06001289 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001290 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001291 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001292 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001293}
1294
1295static void
1296animate_workspace_change(struct desktop_shell *shell,
1297 unsigned int index,
1298 struct workspace *from,
1299 struct workspace *to)
1300{
1301 struct weston_output *output;
1302
1303 int dir;
1304
1305 if (index > shell->workspaces.current)
1306 dir = -1;
1307 else
1308 dir = 1;
1309
1310 shell->workspaces.current = index;
1311
1312 shell->workspaces.anim_dir = dir;
1313 shell->workspaces.anim_from = from;
1314 shell->workspaces.anim_to = to;
1315 shell->workspaces.anim_current = 0.0;
1316 shell->workspaces.anim_timestamp = 0;
1317
1318 output = container_of(shell->compositor->output_list.next,
1319 struct weston_output, link);
1320 wl_list_insert(&output->animation_list,
1321 &shell->workspaces.animation.link);
1322
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001323 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001324
1325 workspace_translate_in(to, 0);
1326
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001327 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001328
Scott Moreau4272e632012-08-13 09:58:41 -06001329 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001330}
1331
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001332static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001333update_workspace(struct desktop_shell *shell, unsigned int index,
1334 struct workspace *from, struct workspace *to)
1335{
1336 shell->workspaces.current = index;
1337 wl_list_insert(&from->layer.link, &to->layer.link);
1338 wl_list_remove(&from->layer.link);
1339}
1340
1341static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001342change_workspace(struct desktop_shell *shell, unsigned int index)
1343{
1344 struct workspace *from;
1345 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001346 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001347
1348 if (index == shell->workspaces.current)
1349 return;
1350
1351 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001352 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001353 return;
1354
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001355 from = get_current_workspace(shell);
1356 to = get_workspace(shell, index);
1357
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001358 if (shell->workspaces.anim_from == to &&
1359 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001360 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001361 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001362 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001363 return;
1364 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001365
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001366 if (shell->workspaces.anim_to != NULL)
1367 finish_workspace_change_animation(shell,
1368 shell->workspaces.anim_from,
1369 shell->workspaces.anim_to);
1370
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001371 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001372
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001373 if (shell->focus_animation_type != ANIMATION_NONE) {
1374 wl_list_for_each(state, &from->focus_list, link)
1375 if (state->keyboard_focus)
1376 animate_focus_change(shell, from,
1377 get_default_view(state->keyboard_focus), NULL);
1378
1379 wl_list_for_each(state, &to->focus_list, link)
1380 if (state->keyboard_focus)
1381 animate_focus_change(shell, to,
1382 NULL, get_default_view(state->keyboard_focus));
1383 }
1384
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001385 if (workspace_is_empty(to) && workspace_is_empty(from))
1386 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001387 else
1388 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001389
1390 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001391}
1392
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001393static bool
1394workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1395{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001396 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001397 struct wl_list *e;
1398
1399 if (wl_list_empty(list))
1400 return false;
1401
1402 e = list->next;
1403
1404 if (e->next != list)
1405 return false;
1406
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001407 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001408}
1409
1410static void
Philip Withnall659163d2013-11-25 18:01:36 +00001411move_surface_to_workspace(struct desktop_shell *shell,
1412 struct shell_surface *shsurf,
1413 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001414{
1415 struct workspace *from;
1416 struct workspace *to;
1417 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001418 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001419 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001420
1421 if (workspace == shell->workspaces.current)
1422 return;
1423
Philip Withnall659163d2013-11-25 18:01:36 +00001424 view = get_default_view(shsurf->surface);
1425 if (!view)
1426 return;
1427
1428 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1429
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001430 if (workspace >= shell->workspaces.num)
1431 workspace = shell->workspaces.num - 1;
1432
Jonas Ådahle9d22502012-08-29 22:13:01 +02001433 from = get_current_workspace(shell);
1434 to = get_workspace(shell, workspace);
1435
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001436 weston_layer_entry_remove(&view->layer_link);
1437 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001438
Philip Withnall648a4dd2013-11-25 18:01:44 +00001439 shell_surface_update_child_surface_layers(shsurf);
1440
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001442 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001443 struct weston_keyboard *keyboard =
1444 weston_seat_get_keyboard(seat);
1445
1446 if (!keyboard)
Pekka Paalanen01388e22013-04-25 13:57:44 +03001447 continue;
1448
Derek Foreman1281a362015-07-31 16:55:32 -05001449 focus = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 if (focus == view->surface)
Derek Foreman1281a362015-07-31 16:55:32 -05001451 weston_keyboard_set_focus(keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001452 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001453
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001455}
1456
1457static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001458take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001459 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001460 unsigned int index)
1461{
Derek Foreman1281a362015-07-31 16:55:32 -05001462 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001463 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001465 struct shell_surface *shsurf;
1466 struct workspace *from;
1467 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001468 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001469
Derek Foreman1281a362015-07-31 16:55:32 -05001470 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 view = get_default_view(surface);
1472 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001473 index == shell->workspaces.current ||
1474 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001475 return;
1476
1477 from = get_current_workspace(shell);
1478 to = get_workspace(shell, index);
1479
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001480 weston_layer_entry_remove(&view->layer_link);
1481 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001482
Philip Withnall659163d2013-11-25 18:01:36 +00001483 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001484 if (shsurf != NULL)
1485 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001486
Jonas Ådahle9d22502012-08-29 22:13:01 +02001487 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001488 drop_focus_state(shell, from, surface);
1489
1490 if (shell->workspaces.anim_from == to &&
1491 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001492 wl_list_remove(&to->layer.link);
1493 wl_list_insert(from->layer.link.prev, &to->layer.link);
1494
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001495 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001496 broadcast_current_workspace_state(shell);
1497
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001498 return;
1499 }
1500
1501 if (shell->workspaces.anim_to != NULL)
1502 finish_workspace_change_animation(shell,
1503 shell->workspaces.anim_from,
1504 shell->workspaces.anim_to);
1505
1506 if (workspace_is_empty(from) &&
1507 workspace_has_only(to, surface))
1508 update_workspace(shell, index, from, to);
1509 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001510 if (shsurf != NULL &&
1511 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001512 wl_list_insert(&shell->workspaces.anim_sticky_list,
1513 &shsurf->workspace_transform.link);
1514
1515 animate_workspace_change(shell, index, from, to);
1516 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001517
1518 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001519
1520 state = ensure_focus_state(shell, seat);
1521 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001522 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001523}
1524
1525static void
1526workspace_manager_move_surface(struct wl_client *client,
1527 struct wl_resource *resource,
1528 struct wl_resource *surface_resource,
1529 uint32_t workspace)
1530{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001531 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001532 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001533 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001534 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001535 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001536
Pekka Paalanen01388e22013-04-25 13:57:44 +03001537 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001538 shell_surface = get_shell_surface(main_surface);
1539 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001541
1542 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001543}
1544
1545static const struct workspace_manager_interface workspace_manager_implementation = {
1546 workspace_manager_move_surface,
1547};
1548
1549static void
1550unbind_resource(struct wl_resource *resource)
1551{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001552 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001553}
1554
1555static void
1556bind_workspace_manager(struct wl_client *client,
1557 void *data, uint32_t version, uint32_t id)
1558{
1559 struct desktop_shell *shell = data;
1560 struct wl_resource *resource;
1561
Jason Ekstranda85118c2013-06-27 20:17:02 -05001562 resource = wl_resource_create(client,
1563 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001564
1565 if (resource == NULL) {
1566 weston_log("couldn't add workspace manager object");
1567 return;
1568 }
1569
Jason Ekstranda85118c2013-06-27 20:17:02 -05001570 wl_resource_set_implementation(resource,
1571 &workspace_manager_implementation,
1572 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001573 wl_list_insert(&shell->workspaces.client_list,
1574 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001575
1576 workspace_manager_send_state(resource,
1577 shell->workspaces.current,
1578 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001579}
1580
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001581static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001582touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001583 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001584{
1585}
1586
1587static void
1588touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1589{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001590 struct weston_touch_move_grab *move =
1591 (struct weston_touch_move_grab *) container_of(
1592 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001593
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001594 if (touch_id == 0)
1595 move->active = 0;
1596
Jonas Ådahl9484b692013-12-02 22:05:03 +01001597 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001598 shell_touch_grab_end(&move->base);
1599 free(move);
1600 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001601}
1602
1603static void
1604touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001605 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001606{
1607 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1608 struct shell_surface *shsurf = move->base.shsurf;
1609 struct weston_surface *es;
1610 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1611 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1612
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001613 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001614 return;
1615
1616 es = shsurf->surface;
1617
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001618 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001619
1620 weston_compositor_schedule_repaint(es->compositor);
1621}
1622
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001623static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001624touch_move_grab_frame(struct weston_touch_grab *grab)
1625{
1626}
1627
1628static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001629touch_move_grab_cancel(struct weston_touch_grab *grab)
1630{
1631 struct weston_touch_move_grab *move =
1632 (struct weston_touch_move_grab *) container_of(
1633 grab, struct shell_touch_grab, grab);
1634
1635 shell_touch_grab_end(&move->base);
1636 free(move);
1637}
1638
Rusty Lynch1084da52013-08-15 09:10:08 -07001639static const struct weston_touch_grab_interface touch_move_grab_interface = {
1640 touch_move_grab_down,
1641 touch_move_grab_up,
1642 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001643 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001644 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001645};
1646
1647static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001648surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001649{
1650 struct weston_touch_move_grab *move;
1651
1652 if (!shsurf)
1653 return -1;
1654
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001655 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001656 return 0;
1657
1658 move = malloc(sizeof *move);
1659 if (!move)
1660 return -1;
1661
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001662 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001664 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001666 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001667
1668 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001669 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001670
1671 return 0;
1672}
1673
1674static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001675noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001676{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001677}
1678
1679static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001680constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1681{
1682 struct shell_surface *shsurf = move->base.shsurf;
1683 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001684 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001685 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001686 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001687
1688 x = wl_fixed_to_int(pointer->x + move->dx);
1689 y = wl_fixed_to_int(pointer->y + move->dy);
1690
Jonny Lambd73c6942014-08-20 15:53:20 +02001691 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001692 get_output_work_area(shsurf->shell,
1693 shsurf->surface->output,
1694 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001695
Derek Foreman6feb0f92015-04-15 12:23:56 -05001696 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1697 if (bottom - safety < area.y)
1698 y = area.y + safety - shsurf->geometry.height
1699 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001700
1701 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001702 y + shsurf->geometry.y < area.y)
1703 y = area.y - shsurf->geometry.y;
1704
Jonny Lambd73c6942014-08-20 15:53:20 +02001705 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001706
1707 *cx = x;
1708 *cy = y;
1709}
1710
1711static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001712move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1713 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001714{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001715 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001716 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001717 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001718 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001719
1720 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001721 if (!shsurf)
1722 return;
1723
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001724 constrain_position(move, &cx, &cy);
1725
1726 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001727
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001729}
1730
1731static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001732move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001733 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001734{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001735 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1736 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001737 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001738 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001739
Daniel Stone4dbadb12012-05-30 16:31:51 +01001740 if (pointer->button_count == 0 &&
1741 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001742 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001743 free(grab);
1744 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001745}
1746
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001747static void
1748move_grab_cancel(struct weston_pointer_grab *grab)
1749{
1750 struct shell_grab *shell_grab =
1751 container_of(grab, struct shell_grab, grab);
1752
1753 shell_grab_end(shell_grab);
1754 free(grab);
1755}
1756
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001757static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001758 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001759 move_grab_motion,
1760 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001761 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001762};
1763
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001764static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001765surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001766 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767{
1768 struct weston_move_grab *move;
1769
1770 if (!shsurf)
1771 return -1;
1772
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001773 if (shsurf->grabbed ||
1774 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001775 return 0;
1776
1777 move = malloc(sizeof *move);
1778 if (!move)
1779 return -1;
1780
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001782 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001784 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001785 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786
1787 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Derek Foreman794fa0e2015-07-15 13:00:38 -05001788 pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001789
1790 return 0;
1791}
1792
1793static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001794common_surface_move(struct wl_resource *resource,
1795 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001796{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001797 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001798 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1799 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001800 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001801 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001802
Derek Foreman1281a362015-07-31 16:55:32 -05001803 if (pointer &&
1804 pointer->focus &&
1805 pointer->button_count > 0 &&
1806 pointer->grab_serial == serial) {
1807 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001808 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001809 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001810 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001811 } else if (touch &&
1812 touch->focus &&
1813 touch->grab_serial == serial) {
1814 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001815 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001816 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001817 wl_resource_post_no_memory(resource);
1818 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001819}
1820
Rafael Antognollie2a34552013-12-03 15:35:45 -02001821static void
1822shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1823 struct wl_resource *seat_resource, uint32_t serial)
1824{
1825 common_surface_move(resource, seat_resource, serial);
1826}
1827
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001828struct weston_resize_grab {
1829 struct shell_grab base;
1830 uint32_t edges;
1831 int32_t width, height;
1832};
1833
1834static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001835resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1836 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001837{
1838 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001839 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001840 struct shell_surface *shsurf = resize->base.shsurf;
1841 int32_t width, height;
1842 wl_fixed_t from_x, from_y;
1843 wl_fixed_t to_x, to_y;
1844
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001845 weston_pointer_move(pointer, x, y);
1846
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001847 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001848 return;
1849
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850 weston_view_from_global_fixed(shsurf->view,
1851 pointer->grab_x, pointer->grab_y,
1852 &from_x, &from_y);
1853 weston_view_from_global_fixed(shsurf->view,
1854 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001855
1856 width = resize->width;
1857 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1858 width += wl_fixed_to_int(from_x - to_x);
1859 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1860 width += wl_fixed_to_int(to_x - from_x);
1861 }
1862
1863 height = resize->height;
1864 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1865 height += wl_fixed_to_int(from_y - to_y);
1866 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1867 height += wl_fixed_to_int(to_y - from_y);
1868 }
1869
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001870 if (width < 1)
1871 width = 1;
1872 if (height < 1)
1873 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001874 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001875}
1876
1877static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001878send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001879{
1880 struct shell_surface *shsurf = get_shell_surface(surface);
1881
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001882 assert(shsurf);
1883
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001884 if (shsurf->resource)
1885 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001886 shsurf->resize_edges,
1887 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001888}
1889
1890static const struct weston_shell_client shell_client = {
1891 send_configure
1892};
1893
1894static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001895resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001896 uint32_t time, uint32_t button, uint32_t state_w)
1897{
1898 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001899 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001900 enum wl_pointer_button_state state = state_w;
1901
1902 if (pointer->button_count == 0 &&
1903 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1904 shell_grab_end(&resize->base);
1905 free(grab);
1906 }
1907}
1908
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001909static void
1910resize_grab_cancel(struct weston_pointer_grab *grab)
1911{
1912 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1913
1914 shell_grab_end(&resize->base);
1915 free(grab);
1916}
1917
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001918static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001919 noop_grab_focus,
1920 resize_grab_motion,
1921 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001922 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001923};
1924
Giulio Camuffob8366642013-04-25 13:57:46 +03001925/*
1926 * Returns the bounding box of a surface and all its sub-surfaces,
1927 * in the surface coordinates system. */
1928static void
1929surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1930 int32_t *y, int32_t *w, int32_t *h) {
1931 pixman_region32_t region;
1932 pixman_box32_t *box;
1933 struct weston_subsurface *subsurface;
1934
1935 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001936 surface->width,
1937 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001938
1939 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1940 pixman_region32_union_rect(&region, &region,
1941 subsurface->position.x,
1942 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943 subsurface->surface->width,
1944 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001945 }
1946
1947 box = pixman_region32_extents(&region);
1948 if (x)
1949 *x = box->x1;
1950 if (y)
1951 *y = box->y1;
1952 if (w)
1953 *w = box->x2 - box->x1;
1954 if (h)
1955 *h = box->y2 - box->y1;
1956
1957 pixman_region32_fini(&region);
1958}
1959
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001960static int
1961surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001962 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001963{
1964 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001965 const unsigned resize_topbottom =
1966 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1967 const unsigned resize_leftright =
1968 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1969 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001970
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001971 if (shsurf->grabbed ||
1972 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001973 return 0;
1974
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001975 /* Check for invalid edge combinations. */
1976 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1977 (edges & resize_topbottom) == resize_topbottom ||
1978 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001979 return 0;
1980
1981 resize = malloc(sizeof *resize);
1982 if (!resize)
1983 return -1;
1984
1985 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001986
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001987 resize->width = shsurf->geometry.width;
1988 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001989
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001990 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001991 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001992 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001993 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001994
1995 return 0;
1996}
1997
1998static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001999common_surface_resize(struct wl_resource *resource,
2000 struct wl_resource *seat_resource, uint32_t serial,
2001 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002002{
Jason Ekstrand44a38632013-06-14 10:08:00 -05002003 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05002004 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002005 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03002006 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002007
Derek Foreman1281a362015-07-31 16:55:32 -05002008 if (!pointer ||
2009 pointer->button_count == 0 ||
2010 pointer->grab_serial != serial ||
2011 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08002012 return;
2013
Derek Foreman1281a362015-07-31 16:55:32 -05002014 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08002015 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002016 return;
2017
Derek Foreman1281a362015-07-31 16:55:32 -05002018 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002019 wl_resource_post_no_memory(resource);
2020}
2021
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002022static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002023shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2024 struct wl_resource *seat_resource, uint32_t serial,
2025 uint32_t edges)
2026{
2027 common_surface_resize(resource, seat_resource, serial, edges);
2028}
2029
2030static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002031busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002032{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002033 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002034 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002036 wl_fixed_t sx, sy;
2037
Jason Ekstranda7af7042013-10-12 22:38:11 -05002038 view = weston_compositor_pick_view(pointer->seat->compositor,
2039 pointer->x, pointer->y,
2040 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002041
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002042 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2043 shell_grab_end(grab);
2044 free(grab);
2045 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002046}
2047
2048static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002049busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2050 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002051{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002052 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002053}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002054
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002055static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002056busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002057 uint32_t time, uint32_t button, uint32_t state)
2058{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002059 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002060 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002061 struct weston_pointer *pointer = grab->grab.pointer;
2062 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002063
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002064 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002065 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002066 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002067 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002068 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002069 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002070 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002071}
2072
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002073static void
2074busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2075{
2076 struct shell_grab *grab = (struct shell_grab *) base;
2077
2078 shell_grab_end(grab);
2079 free(grab);
2080}
2081
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002082static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002083 busy_cursor_grab_focus,
2084 busy_cursor_grab_motion,
2085 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002086 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002087};
2088
2089static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002090set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002091{
2092 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002093
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002094 if (pointer->grab->interface == &busy_cursor_grab_interface)
2095 return;
2096
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002097 grab = malloc(sizeof *grab);
2098 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002099 return;
2100
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002101 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2102 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002103 /* Mark the shsurf as ungrabbed so that button binding is able
2104 * to move it. */
2105 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002106}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002107
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002108static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002109end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002110{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002111 struct shell_grab *grab;
2112 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002113
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002114 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002115 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2116
2117 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002118 continue;
2119
Derek Foreman1281a362015-07-31 16:55:32 -05002120 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002121 if (grab->grab.interface == &busy_cursor_grab_interface &&
2122 wl_resource_get_client(grab->shsurf->resource) == client) {
2123 shell_grab_end(grab);
2124 free(grab);
2125 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002126 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002127}
2128
Scott Moreau9521d5e2012-04-19 13:06:17 -06002129static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002130handle_shell_client_destroy(struct wl_listener *listener, void *data);
2131
2132static int
2133xdg_ping_timeout_handler(void *data)
2134{
2135 struct shell_client *sc = data;
2136 struct weston_seat *seat;
2137 struct shell_surface *shsurf;
2138
2139 /* Client is not responding */
2140 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002141 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002142 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2143
2144 if (!pointer ||
2145 !pointer->focus ||
2146 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002147 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002148
Derek Foreman1281a362015-07-31 16:55:32 -05002149 shsurf = get_shell_surface(pointer->focus->surface);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002150 if (shsurf &&
2151 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002152 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002153 }
2154
2155 return 1;
2156}
2157
2158static void
2159handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2160{
2161 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002162 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002163 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002164 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002165
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002166 if (sc->unresponsive) {
2167 xdg_ping_timeout_handler(sc);
2168 return;
2169 }
2170
2171 sc->ping_serial = serial;
2172 loop = wl_display_get_event_loop(compositor->wl_display);
2173 if (sc->ping_timer == NULL)
2174 sc->ping_timer =
2175 wl_event_loop_add_timer(loop,
2176 xdg_ping_timeout_handler, sc);
2177 if (sc->ping_timer == NULL)
2178 return;
2179
2180 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2181
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002182 if (shell_surface_is_xdg_surface(shsurf) ||
2183 shell_surface_is_xdg_popup(shsurf))
2184 xdg_shell_send_ping(sc->resource, serial);
2185 else if (shell_surface_is_wl_shell_surface(shsurf))
2186 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002187}
2188
Scott Moreauff1db4a2012-04-17 19:06:18 -06002189static void
2190ping_handler(struct weston_surface *surface, uint32_t serial)
2191{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002192 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002193
2194 if (!shsurf)
2195 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002196 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002197 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002198 if (shsurf->surface == shsurf->shell->grab_surface)
2199 return;
2200
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002201 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002202}
2203
2204static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002205handle_pointer_focus(struct wl_listener *listener, void *data)
2206{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002207 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002208 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002209 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002210 uint32_t serial;
2211
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002213 return;
2214
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002216 serial = wl_display_next_serial(compositor->wl_display);
2217 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002218}
2219
2220static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002221shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2222{
2223 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002224 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002225}
2226
2227static void
2228shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2229{
2230 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002231 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002232}
2233
2234static void
2235handle_keyboard_focus(struct wl_listener *listener, void *data)
2236{
2237 struct weston_keyboard *keyboard = data;
2238 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2239
2240 if (seat->focused_surface) {
2241 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2242 if (shsurf)
2243 shell_surface_lose_keyboard_focus(shsurf);
2244 }
2245
2246 seat->focused_surface = keyboard->focus;
2247
2248 if (seat->focused_surface) {
2249 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2250 if (shsurf)
2251 shell_surface_gain_keyboard_focus(shsurf);
2252 }
2253}
2254
2255static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002256shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002257{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002258 if (sc->ping_serial != serial)
2259 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002260
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002261 sc->unresponsive = 0;
2262 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002263
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002264 if (sc->ping_timer) {
2265 wl_event_source_remove(sc->ping_timer);
2266 sc->ping_timer = NULL;
2267 }
2268
2269}
2270
2271static void
2272shell_surface_pong(struct wl_client *client,
2273 struct wl_resource *resource, uint32_t serial)
2274{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002275 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2276 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002277
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002278 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002279}
2280
2281static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002282set_title(struct shell_surface *shsurf, const char *title)
2283{
2284 free(shsurf->title);
2285 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002286 shsurf->surface->timeline.force_refresh = 1;
2287}
2288
2289static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002290set_pid(struct shell_surface *shsurf, pid_t pid)
2291{
2292 /* We have no use for it */
2293}
2294
2295static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002296set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2297{
2298 shsurf->type = t;
2299 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002300}
2301
2302static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002303set_window_geometry(struct shell_surface *shsurf,
2304 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002305{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002306 shsurf->next_geometry.x = x;
2307 shsurf->next_geometry.y = y;
2308 shsurf->next_geometry.width = width;
2309 shsurf->next_geometry.height = height;
2310 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002311}
2312
2313static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002314shell_surface_set_title(struct wl_client *client,
2315 struct wl_resource *resource, const char *title)
2316{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002317 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002318
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002319 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002320}
2321
2322static void
2323shell_surface_set_class(struct wl_client *client,
2324 struct wl_resource *resource, const char *class)
2325{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002326 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002327
2328 free(shsurf->class);
2329 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002330 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002331}
2332
Alex Wu4539b082012-03-01 12:57:46 +08002333static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002334restore_output_mode(struct weston_output *output)
2335{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002336 if (output->original_mode)
2337 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002338}
2339
2340static void
2341restore_all_output_modes(struct weston_compositor *compositor)
2342{
2343 struct weston_output *output;
2344
2345 wl_list_for_each(output, &compositor->output_list, link)
2346 restore_output_mode(output);
2347}
2348
Philip Withnall07926d92013-11-25 18:01:40 +00002349/* The surface will be inserted into the list immediately after the link
2350 * returned by this function (i.e. will be stacked immediately above the
2351 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002352static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002353shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2354{
2355 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002356 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002357
2358 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002359 case SHELL_SURFACE_XWAYLAND:
2360 return &shsurf->shell->fullscreen_layer.view_list;
2361
2362 case SHELL_SURFACE_NONE:
2363 return NULL;
2364
Kristian Høgsbergead52422014-01-01 13:51:52 -08002365 case SHELL_SURFACE_POPUP:
2366 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002367 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002368 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002369 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002370 /* Move the surface to its parent layer so
2371 * that surfaces which are transient for
2372 * fullscreen surfaces don't get hidden by the
2373 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002374
2375 /* TODO: Handle a parent with multiple views */
2376 parent = get_default_view(shsurf->parent);
2377 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002378 return container_of(parent->layer_link.link.prev,
2379 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002380 }
Philip Withnall07926d92013-11-25 18:01:40 +00002381
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002382 /* Move the surface to a normal workspace layer so that surfaces
2383 * which were previously fullscreen or transient are no longer
2384 * rendered on top. */
2385 ws = get_current_workspace(shsurf->shell);
2386 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002387 }
2388
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002389 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002390}
2391
Philip Withnall648a4dd2013-11-25 18:01:44 +00002392static void
2393shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2394{
2395 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002396 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002397
2398 /* Move the child layers to the same workspace as shsurf. They will be
2399 * stacked above shsurf. */
2400 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002401 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002402 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002403 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002404 prev = container_of(shsurf->view->layer_link.link.prev,
2405 struct weston_layer_entry, link);
2406 weston_layer_entry_remove(&child->view->layer_link);
2407 weston_layer_entry_insert(prev,
2408 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002409 weston_view_geometry_dirty(child->view);
2410 weston_surface_damage(child->surface);
2411
2412 /* Recurse. We don’t expect this to recurse very far (if
2413 * at all) because that would imply we have transient
2414 * (or popup) children of transient surfaces, which
2415 * would be unusual. */
2416 shell_surface_update_child_surface_layers(child);
2417 }
2418 }
2419}
2420
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002421/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002422 * geometry to ensure the changes are redrawn.
2423 *
2424 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2425 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002426static void
2427shell_surface_update_layer(struct shell_surface *shsurf)
2428{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002429 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002430
2431 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2432
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002433 if (new_layer_link == NULL)
2434 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002435 if (new_layer_link == &shsurf->view->layer_link)
2436 return;
2437
2438 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002439 weston_layer_entry_remove(&shsurf->view->layer_link);
2440 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002441 weston_view_geometry_dirty(shsurf->view);
2442 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002443
2444 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002445}
2446
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002447static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002448shell_surface_set_parent(struct shell_surface *shsurf,
2449 struct weston_surface *parent)
2450{
2451 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002452
2453 wl_list_remove(&shsurf->children_link);
2454 wl_list_init(&shsurf->children_link);
2455
2456 /* Insert into the parent surface’s child list. */
2457 if (parent != NULL) {
2458 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2459 if (parent_shsurf != NULL)
2460 wl_list_insert(&parent_shsurf->children_list,
2461 &shsurf->children_link);
2462 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002463}
2464
2465static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002466shell_surface_set_output(struct shell_surface *shsurf,
2467 struct weston_output *output)
2468{
2469 struct weston_surface *es = shsurf->surface;
2470
2471 /* get the default output, if the client set it as NULL
2472 check whether the ouput is available */
2473 if (output)
2474 shsurf->output = output;
2475 else if (es->output)
2476 shsurf->output = es->output;
2477 else
2478 shsurf->output = get_default_output(es->compositor);
2479}
2480
2481static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002482surface_clear_next_states(struct shell_surface *shsurf)
2483{
2484 shsurf->next_state.maximized = false;
2485 shsurf->next_state.fullscreen = false;
2486
2487 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2488 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2489 shsurf->state_changed = true;
2490}
2491
2492static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002493set_toplevel(struct shell_surface *shsurf)
2494{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002495 shell_surface_set_parent(shsurf, NULL);
2496 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002497 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002498
2499 /* The layer_link is updated in set_surface_type(),
2500 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002501}
2502
2503static void
2504shell_surface_set_toplevel(struct wl_client *client,
2505 struct wl_resource *resource)
2506{
2507 struct shell_surface *surface = wl_resource_get_user_data(resource);
2508
2509 set_toplevel(surface);
2510}
2511
2512static void
2513set_transient(struct shell_surface *shsurf,
2514 struct weston_surface *parent, int x, int y, uint32_t flags)
2515{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002516 assert(parent != NULL);
2517
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002518 shell_surface_set_parent(shsurf, parent);
2519
2520 surface_clear_next_states(shsurf);
2521
Philip Withnallbecb77e2013-11-25 18:01:30 +00002522 shsurf->transient.x = x;
2523 shsurf->transient.y = y;
2524 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002525
Rafael Antognollied207b42013-12-03 15:35:43 -02002526 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002527 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002528 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002529
2530 /* The layer_link is updated in set_surface_type(),
2531 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002532}
2533
2534static void
2535shell_surface_set_transient(struct wl_client *client,
2536 struct wl_resource *resource,
2537 struct wl_resource *parent_resource,
2538 int x, int y, uint32_t flags)
2539{
2540 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2541 struct weston_surface *parent =
2542 wl_resource_get_user_data(parent_resource);
2543
2544 set_transient(shsurf, parent, x, y, flags);
2545}
2546
2547static void
2548set_fullscreen(struct shell_surface *shsurf,
2549 uint32_t method,
2550 uint32_t framerate,
2551 struct weston_output *output)
2552{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002553 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002554 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002555
2556 shsurf->fullscreen_output = shsurf->output;
2557 shsurf->fullscreen.type = method;
2558 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002559
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002560 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002561}
2562
2563static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002564weston_view_set_initial_position(struct weston_view *view,
2565 struct desktop_shell *shell);
2566
2567static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002568unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002569{
Philip Withnallf85fe842013-11-25 18:01:37 +00002570 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002571 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2572 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002573 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002574 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002575
Alex Wu4539b082012-03-01 12:57:46 +08002576 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2577 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002578
Alex Wu4539b082012-03-01 12:57:46 +08002579 wl_list_remove(&shsurf->fullscreen.transform.link);
2580 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002581
Jason Ekstranda7af7042013-10-12 22:38:11 -05002582 if (shsurf->fullscreen.black_view)
2583 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2584 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002585
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002586 if (shsurf->saved_position_valid)
2587 weston_view_set_position(shsurf->view,
2588 shsurf->saved_x, shsurf->saved_y);
2589 else
2590 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2591
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002592 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002594 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002595 shsurf->saved_rotation_valid = false;
2596 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002597
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002598 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002599}
2600
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002601static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002602shell_surface_set_fullscreen(struct wl_client *client,
2603 struct wl_resource *resource,
2604 uint32_t method,
2605 uint32_t framerate,
2606 struct wl_resource *output_resource)
2607{
2608 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2609 struct weston_output *output;
2610
2611 if (output_resource)
2612 output = wl_resource_get_user_data(output_resource);
2613 else
2614 output = NULL;
2615
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002616 shell_surface_set_parent(shsurf, NULL);
2617
Rafael Antognolli03b16592013-12-03 15:35:42 -02002618 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002619 shsurf->next_state.fullscreen = true;
2620 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002621 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002622}
2623
2624static void
2625set_popup(struct shell_surface *shsurf,
2626 struct weston_surface *parent,
2627 struct weston_seat *seat,
2628 uint32_t serial,
2629 int32_t x,
2630 int32_t y)
2631{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002632 assert(parent != NULL);
2633
Philip Withnallbecb77e2013-11-25 18:01:30 +00002634 shsurf->popup.shseat = get_shell_seat(seat);
2635 shsurf->popup.serial = serial;
2636 shsurf->popup.x = x;
2637 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002638
Pekka Paalanenb5026542014-11-12 15:09:24 +02002639 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002640}
2641
2642static void
2643shell_surface_set_popup(struct wl_client *client,
2644 struct wl_resource *resource,
2645 struct wl_resource *seat_resource,
2646 uint32_t serial,
2647 struct wl_resource *parent_resource,
2648 int32_t x, int32_t y, uint32_t flags)
2649{
2650 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002651 struct weston_surface *parent =
2652 wl_resource_get_user_data(parent_resource);
2653
2654 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002655
Rafael Antognolli03b16592013-12-03 15:35:42 -02002656 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002657 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002658 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002659 wl_resource_get_user_data(seat_resource),
2660 serial, x, y);
2661}
2662
2663static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002664unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002665{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002666 /* undo all maximized things here */
2667 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002668
2669 if (shsurf->saved_position_valid)
2670 weston_view_set_position(shsurf->view,
2671 shsurf->saved_x, shsurf->saved_y);
2672 else
2673 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002674
2675 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002676 wl_list_insert(&shsurf->view->geometry.transformation_list,
2677 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002678 shsurf->saved_rotation_valid = false;
2679 }
2680
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002681 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002682}
2683
Philip Withnallbecb77e2013-11-25 18:01:30 +00002684static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002685set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002686{
2687 struct shell_surface *shsurf;
2688 struct workspace *current_ws;
2689 struct weston_seat *seat;
2690 struct weston_surface *focus;
2691 struct weston_view *view;
2692
2693 view = get_default_view(surface);
2694 if (!view)
2695 return;
2696
2697 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2698
2699 shsurf = get_shell_surface(surface);
2700 current_ws = get_current_workspace(shsurf->shell);
2701
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002702 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002703 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002704
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002705 drop_focus_state(shsurf->shell, current_ws, view->surface);
2706 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002707 struct weston_keyboard *keyboard =
2708 weston_seat_get_keyboard(seat);
2709
2710 if (!keyboard)
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002711 continue;
Derek Foreman1281a362015-07-31 16:55:32 -05002712 focus = weston_surface_get_main_surface(keyboard->focus);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002713 if (focus == view->surface)
Derek Foreman1281a362015-07-31 16:55:32 -05002714 weston_keyboard_set_focus(keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002715 }
2716
2717 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002718 weston_view_damage_below(view);
2719}
2720
2721static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002722shell_surface_set_maximized(struct wl_client *client,
2723 struct wl_resource *resource,
2724 struct wl_resource *output_resource)
2725{
2726 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2727 struct weston_output *output;
2728
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002729 surface_clear_next_states(shsurf);
2730 shsurf->next_state.maximized = true;
2731 shsurf->state_changed = true;
2732
Pekka Paalanenb5026542014-11-12 15:09:24 +02002733 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002734 shell_surface_set_parent(shsurf, NULL);
2735
Philip Withnallbecb77e2013-11-25 18:01:30 +00002736 if (output_resource)
2737 output = wl_resource_get_user_data(output_resource);
2738 else
2739 output = NULL;
2740
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002741 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002742
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002743 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002744}
2745
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002746/* This is only ever called from set_surface_type(), so there’s no need to
2747 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002748static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002749reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002750{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002751 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002752 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002753 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002754 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002755
Pekka Paalanen98262232011-12-01 10:42:22 +02002756 return 0;
2757}
2758
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002759static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002760set_full_output(struct shell_surface *shsurf)
2761{
2762 shsurf->saved_x = shsurf->view->geometry.x;
2763 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002764 shsurf->saved_width = shsurf->surface->width;
2765 shsurf->saved_height = shsurf->surface->height;
2766 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002767 shsurf->saved_position_valid = true;
2768
2769 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2770 wl_list_remove(&shsurf->rotation.transform.link);
2771 wl_list_init(&shsurf->rotation.transform.link);
2772 weston_view_geometry_dirty(shsurf->view);
2773 shsurf->saved_rotation_valid = true;
2774 }
2775}
2776
2777static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002778set_surface_type(struct shell_surface *shsurf)
2779{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002780 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002782
Philip Withnallbecb77e2013-11-25 18:01:30 +00002783 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002784
Rafael Antognolli03b16592013-12-03 15:35:42 -02002785 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002786 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002787
2788 switch (shsurf->type) {
2789 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002790 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002791 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002792 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002793 weston_view_set_position(shsurf->view,
2794 pev->geometry.x + shsurf->transient.x,
2795 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002796 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002797 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002798
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002799 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002800 weston_view_set_position(shsurf->view, shsurf->transient.x,
2801 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002802 break;
2803
Philip Withnall0f640e22013-11-25 18:01:31 +00002804 case SHELL_SURFACE_POPUP:
2805 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002806 default:
2807 break;
2808 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002809
2810 /* Update the surface’s layer. */
2811 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002812}
2813
Tiago Vignattibe143262012-04-16 17:31:41 +03002814static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002815shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002816{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002817 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002818}
2819
Pekka Paalanen8274d902014-08-06 19:36:51 +03002820static int
2821black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2822{
2823 struct weston_surface *fs_surface = surface->configure_private;
2824 int n;
2825 int rem;
2826 int ret;
2827
2828 n = snprintf(buf, len, "black background surface for ");
2829 if (n < 0)
2830 return n;
2831
2832 rem = (int)len - n;
2833 if (rem < 0)
2834 rem = 0;
2835
2836 if (fs_surface->get_label)
2837 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2838 else
2839 ret = snprintf(buf + n, rem, "<unknown>");
2840
2841 if (ret < 0)
2842 return n;
2843
2844 return n + ret;
2845}
2846
Alex Wu21858432012-04-01 20:13:08 +08002847static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002848black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002849
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002851create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002852 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002853 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002854{
2855 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002857
2858 surface = weston_surface_create(ec);
2859 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002860 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002861 return NULL;
2862 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002863 view = weston_view_create(surface);
2864 if (surface == NULL) {
2865 weston_log("no memory\n");
2866 weston_surface_destroy(surface);
2867 return NULL;
2868 }
Alex Wu4539b082012-03-01 12:57:46 +08002869
Alex Wu21858432012-04-01 20:13:08 +08002870 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002871 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002872 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002873 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002874 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002875 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002876 pixman_region32_fini(&surface->input);
2877 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002878
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002879 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002880 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002881
2882 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002883}
2884
Philip Withnalled8f1a92013-11-25 18:01:43 +00002885static void
2886shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2887{
2888 struct weston_output *output = shsurf->fullscreen_output;
2889
Rafael Antognolli03b16592013-12-03 15:35:42 -02002890 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002891
2892 if (!shsurf->fullscreen.black_view)
2893 shsurf->fullscreen.black_view =
2894 create_black_surface(shsurf->surface->compositor,
2895 shsurf->surface,
2896 output->x, output->y,
2897 output->width,
2898 output->height);
2899
2900 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002901 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2902 weston_layer_entry_insert(&shsurf->view->layer_link,
2903 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002904 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2905 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002906
2907 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002908}
2909
Alex Wu4539b082012-03-01 12:57:46 +08002910/* Create black surface and append it to the associated fullscreen surface.
2911 * Handle size dismatch and positioning according to the method. */
2912static void
2913shell_configure_fullscreen(struct shell_surface *shsurf)
2914{
2915 struct weston_output *output = shsurf->fullscreen_output;
2916 struct weston_surface *surface = shsurf->surface;
2917 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002918 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002919 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002920
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002921 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2922 restore_output_mode(output);
2923
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002924 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002925 weston_layer_entry_remove(&shsurf->view->layer_link);
2926 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002927
Philip Withnalled8f1a92013-11-25 18:01:43 +00002928 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002929
Jason Ekstranda7af7042013-10-12 22:38:11 -05002930 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002931 &surf_width, &surf_height);
2932
Alex Wu4539b082012-03-01 12:57:46 +08002933 switch (shsurf->fullscreen.type) {
2934 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002935 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002937 break;
2938 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002939 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002940 if (output->width == surf_width &&
2941 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 weston_view_set_position(shsurf->view,
2943 output->x - surf_x,
2944 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002945 break;
2946 }
2947
Alex Wu4539b082012-03-01 12:57:46 +08002948 matrix = &shsurf->fullscreen.transform.matrix;
2949 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002950
Scott Moreau1bad5db2012-08-18 01:04:05 -06002951 output_aspect = (float) output->width /
2952 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002953 /* XXX: Use surf_width and surf_height here? */
2954 surface_aspect = (float) surface->width /
2955 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002956 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002957 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002958 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002959 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002960 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002961 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002962
Alex Wu4539b082012-03-01 12:57:46 +08002963 weston_matrix_scale(matrix, scale, scale, 1);
2964 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002965 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002966 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002967 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2968 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002970
Alex Wu4539b082012-03-01 12:57:46 +08002971 break;
2972 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002973 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002974 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002975 surf_width * surface->buffer_viewport.buffer.scale,
2976 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002977 shsurf->fullscreen.framerate};
2978
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002979 if (weston_output_mode_switch_to_temporary(output, &mode,
2980 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002981 weston_view_set_position(shsurf->view,
2982 output->x - surf_x,
2983 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002984 shsurf->fullscreen.black_view->surface->width = output->width;
2985 shsurf->fullscreen.black_view->surface->height = output->height;
2986 weston_view_set_position(shsurf->fullscreen.black_view,
2987 output->x - surf_x,
2988 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002989 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002990 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002991 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002992 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002993 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002994 }
Alex Wubd3354b2012-04-17 17:20:49 +08002995 }
Alex Wu4539b082012-03-01 12:57:46 +08002996 break;
2997 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002998 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002999 break;
3000 default:
3001 break;
3002 }
3003}
3004
Alex Wu4539b082012-03-01 12:57:46 +08003005static void
3006shell_map_fullscreen(struct shell_surface *shsurf)
3007{
Alex Wubd3354b2012-04-17 17:20:49 +08003008 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003009}
3010
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003011static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003012set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
3013{
3014 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02003015 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003016 shsurf->transient.x = x;
3017 shsurf->transient.y = y;
3018 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00003019
3020 shell_surface_set_parent(shsurf, NULL);
3021
Pekka Paalanenb5026542014-11-12 15:09:24 +02003022 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08003023 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003024}
3025
Kristian Høgsberg47792412014-05-04 13:47:06 -07003026static void
3027shell_interface_set_fullscreen(struct shell_surface *shsurf,
3028 uint32_t method,
3029 uint32_t framerate,
3030 struct weston_output *output)
3031{
3032 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003033 shsurf->next_state.fullscreen = true;
3034 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003035
3036 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003037}
3038
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003039static struct weston_output *
3040get_focused_output(struct weston_compositor *compositor)
3041{
3042 struct weston_seat *seat;
3043 struct weston_output *output = NULL;
3044
3045 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003046 struct weston_touch *touch = weston_seat_get_touch(seat);
3047 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3048 struct weston_keyboard *keyboard =
3049 weston_seat_get_keyboard(seat);
3050
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003051 /* Priority has touch focus, then pointer and
3052 * then keyboard focus. We should probably have
3053 * three for loops and check frist for touch,
3054 * then for pointer, etc. but unless somebody has some
3055 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003056 if (touch && touch->focus)
3057 output = touch->focus->output;
3058 else if (pointer && pointer->focus)
3059 output = pointer->focus->output;
3060 else if (keyboard && keyboard->focus)
3061 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003062
3063 if (output)
3064 break;
3065 }
3066
3067 return output;
3068}
3069
3070static void
3071shell_interface_set_maximized(struct shell_surface *shsurf)
3072{
3073 struct weston_output *output;
3074
3075 surface_clear_next_states(shsurf);
3076 shsurf->next_state.maximized = true;
3077 shsurf->state_changed = true;
3078 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3079
3080 if (!weston_surface_is_mapped(shsurf->surface))
3081 output = get_focused_output(shsurf->surface->compositor);
3082 else
3083 output = shsurf->surface->output;
3084
3085 shell_surface_set_output(shsurf, output);
3086 send_configure_for_surface(shsurf);
3087}
3088
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003089static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003090shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003091{
Derek Foremane4d6c832015-08-05 14:48:11 -05003092 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003093}
3094
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003095static int
3096shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003097 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003098 uint32_t edges)
3099{
Derek Foremane4d6c832015-08-05 14:48:11 -05003100 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003101}
3102
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003103static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003104
3105static void
3106destroy_shell_seat(struct wl_listener *listener, void *data)
3107{
3108 struct shell_seat *shseat =
3109 container_of(listener,
3110 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003111 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003112
3113 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003114 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003115 shseat->popup_grab.client = NULL;
3116
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003117 wl_list_for_each_safe(shsurf, next,
3118 &shseat->popup_grab.surfaces_list,
3119 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003120 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003121 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003122 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003123 }
3124
3125 wl_list_remove(&shseat->seat_destroy_listener.link);
3126 free(shseat);
3127}
3128
Jason Ekstrand024177c2014-04-21 19:42:58 -05003129static void
3130shell_seat_caps_changed(struct wl_listener *listener, void *data)
3131{
Derek Foreman1281a362015-07-31 16:55:32 -05003132 struct weston_keyboard *keyboard;
3133 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003134 struct shell_seat *seat;
3135
3136 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003137 keyboard = weston_seat_get_keyboard(seat->seat);
3138 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003139
Derek Foreman1281a362015-07-31 16:55:32 -05003140 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003141 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003142 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003143 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003144 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003145 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003146 wl_list_init(&seat->keyboard_focus_listener.link);
3147 }
3148
Derek Foreman1281a362015-07-31 16:55:32 -05003149 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003150 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003151 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003152 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003153 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003154 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003155 wl_list_init(&seat->pointer_focus_listener.link);
3156 }
3157}
3158
Giulio Camuffo5085a752013-03-25 21:42:45 +01003159static struct shell_seat *
3160create_shell_seat(struct weston_seat *seat)
3161{
3162 struct shell_seat *shseat;
3163
3164 shseat = calloc(1, sizeof *shseat);
3165 if (!shseat) {
3166 weston_log("no memory to allocate shell seat\n");
3167 return NULL;
3168 }
3169
3170 shseat->seat = seat;
3171 wl_list_init(&shseat->popup_grab.surfaces_list);
3172
3173 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3174 wl_signal_add(&seat->destroy_signal,
3175 &shseat->seat_destroy_listener);
3176
Jason Ekstrand024177c2014-04-21 19:42:58 -05003177 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3178 wl_list_init(&shseat->keyboard_focus_listener.link);
3179
3180 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3181 wl_list_init(&shseat->pointer_focus_listener.link);
3182
3183 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3184 wl_signal_add(&seat->updated_caps_signal,
3185 &shseat->caps_changed_listener);
3186 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3187
Giulio Camuffo5085a752013-03-25 21:42:45 +01003188 return shseat;
3189}
3190
3191static struct shell_seat *
3192get_shell_seat(struct weston_seat *seat)
3193{
3194 struct wl_listener *listener;
3195
3196 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003197 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003198
3199 return container_of(listener,
3200 struct shell_seat, seat_destroy_listener);
3201}
3202
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003203static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003204popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003205{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003206 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003207 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003208 struct shell_seat *shseat =
3209 container_of(grab, struct shell_seat, popup_grab.grab);
3210 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003211 wl_fixed_t sx, sy;
3212
Jason Ekstranda7af7042013-10-12 22:38:11 -05003213 view = weston_compositor_pick_view(pointer->seat->compositor,
3214 pointer->x, pointer->y,
3215 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003216
Jason Ekstranda7af7042013-10-12 22:38:11 -05003217 if (view && view->surface->resource &&
3218 wl_resource_get_client(view->surface->resource) == client) {
3219 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003220 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003221 weston_pointer_set_focus(pointer, NULL,
3222 wl_fixed_from_int(0),
3223 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003224 }
3225}
3226
3227static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003228popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3229 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003230{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003231 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003232 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003233 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003234
Jonas Ådahl61917c82014-08-11 22:44:02 +02003235 if (pointer->focus) {
3236 weston_view_from_global_fixed(pointer->focus, x, y,
3237 &pointer->sx, &pointer->sy);
3238 }
3239
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003240 weston_pointer_move(pointer, x, y);
3241
Neil Roberts96d790e2013-09-19 17:32:00 +01003242 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003243 weston_view_from_global_fixed(pointer->focus,
3244 pointer->x, pointer->y,
3245 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003246 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003247 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003248}
3249
3250static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003251popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003252 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003253{
3254 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003255 struct shell_seat *shseat =
3256 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003257 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003258 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003259 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003260 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003261
Neil Roberts96d790e2013-09-19 17:32:00 +01003262 resource_list = &grab->pointer->focus_resource_list;
3263 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003264 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003265 wl_resource_for_each(resource, resource_list) {
3266 wl_pointer_send_button(resource, serial,
3267 time, button, state);
3268 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003269 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003270 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003271 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003272 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003273 }
3274
Daniel Stone4dbadb12012-05-30 16:31:51 +01003275 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003276 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003277}
3278
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003279static void
3280popup_grab_cancel(struct weston_pointer_grab *grab)
3281{
3282 popup_grab_end(grab->pointer);
3283}
3284
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003285static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003286 popup_grab_focus,
3287 popup_grab_motion,
3288 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003289 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003290};
3291
3292static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003293touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003294 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003295{
3296 struct wl_resource *resource;
3297 struct shell_seat *shseat =
3298 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3299 struct wl_display *display = shseat->seat->compositor->wl_display;
3300 uint32_t serial;
3301 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003302 wl_fixed_t sx, sy;
3303
3304 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003305
3306 resource_list = &grab->touch->focus_resource_list;
3307 if (!wl_list_empty(resource_list)) {
3308 serial = wl_display_get_serial(display);
3309 wl_resource_for_each(resource, resource_list) {
3310 wl_touch_send_down(resource, serial, time,
3311 grab->touch->focus->surface->resource,
3312 touch_id, sx, sy);
3313 }
3314 }
3315}
3316
3317static void
3318touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3319{
3320 struct wl_resource *resource;
3321 struct shell_seat *shseat =
3322 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3323 struct wl_display *display = shseat->seat->compositor->wl_display;
3324 uint32_t serial;
3325 struct wl_list *resource_list;
3326
3327 resource_list = &grab->touch->focus_resource_list;
3328 if (!wl_list_empty(resource_list)) {
3329 serial = wl_display_get_serial(display);
3330 wl_resource_for_each(resource, resource_list) {
3331 wl_touch_send_up(resource, serial, time, touch_id);
3332 }
3333 }
3334}
3335
3336static void
3337touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003338 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003339{
3340 struct wl_resource *resource;
3341 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003342 wl_fixed_t sx, sy;
3343
3344 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003345
3346 resource_list = &grab->touch->focus_resource_list;
3347 if (!wl_list_empty(resource_list)) {
3348 wl_resource_for_each(resource, resource_list) {
3349 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3350 }
3351 }
3352}
3353
3354static void
3355touch_popup_grab_frame(struct weston_touch_grab *grab)
3356{
3357}
3358
3359static void
3360touch_popup_grab_cancel(struct weston_touch_grab *grab)
3361{
3362 touch_popup_grab_end(grab->touch);
3363}
3364
3365static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3366 touch_popup_grab_down,
3367 touch_popup_grab_up,
3368 touch_popup_grab_motion,
3369 touch_popup_grab_frame,
3370 touch_popup_grab_cancel,
3371};
3372
3373static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003374shell_surface_send_popup_done(struct shell_surface *shsurf)
3375{
3376 if (shell_surface_is_wl_shell_surface(shsurf))
3377 wl_shell_surface_send_popup_done(shsurf->resource);
3378 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003379 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003380}
3381
3382static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003383popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003384{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003385 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003386 struct shell_seat *shseat =
3387 container_of(grab, struct shell_seat, popup_grab.grab);
3388 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003389 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003390
3391 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003392 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003393 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003394 shseat->popup_grab.grab.interface = NULL;
3395 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003396 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003397 wl_list_for_each_safe(shsurf, next,
3398 &shseat->popup_grab.surfaces_list,
3399 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003400 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003401 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003402 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003403 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003404 wl_list_init(&shseat->popup_grab.surfaces_list);
3405 }
3406}
3407
3408static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003409touch_popup_grab_end(struct weston_touch *touch)
3410{
3411 struct weston_touch_grab *grab = touch->grab;
3412 struct shell_seat *shseat =
3413 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3414 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003415 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003416
3417 if (touch->grab->interface == &touch_popup_grab_interface) {
3418 weston_touch_end_grab(grab->touch);
3419 shseat->popup_grab.client = NULL;
3420 shseat->popup_grab.touch_grab.interface = NULL;
3421 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3422 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003423 wl_list_for_each_safe(shsurf, next,
3424 &shseat->popup_grab.surfaces_list,
3425 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003426 shell_surface_send_popup_done(shsurf);
3427 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003428 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003429 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003430 wl_list_init(&shseat->popup_grab.surfaces_list);
3431 }
3432}
3433
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003434static struct shell_surface *
3435get_top_popup(struct shell_seat *shseat)
3436{
3437 struct shell_surface *shsurf;
3438
3439 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3440 return NULL;
3441 } else {
3442 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3443 struct shell_surface,
3444 popup.grab_link);
3445 return shsurf;
3446 }
3447}
3448
3449static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003450add_popup_grab(struct shell_surface *shsurf,
3451 struct shell_seat *shseat,
3452 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003453{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003454 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003455 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003456 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3457 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003458
3459 parent = get_shell_surface(shsurf->parent);
3460 top_surface = get_top_popup(shseat);
3461 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003462 (!parent ||
3463 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003464 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003465 wl_resource_post_error(shsurf->owner_resource,
3466 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003467 "xdg_popup was not created on the "
3468 "topmost popup");
3469 return -1;
3470 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003471
3472 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003473 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003474 shseat->popup_grab.client =
3475 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003476
3477 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003478 shseat->popup_grab.grab.interface =
3479 &popup_grab_interface;
3480
3481 /* We must make sure here that this popup was opened
3482 * after a mouse press, and not just by moving around
3483 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003484 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003485 shseat->popup_grab.initial_up = 0;
3486 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003487 shseat->popup_grab.touch_grab.interface =
3488 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003489 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003490
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003491 wl_list_insert(&shseat->popup_grab.surfaces_list,
3492 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003493
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003494 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003495 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003496 &shseat->popup_grab.grab);
3497 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003498 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003499 &shseat->popup_grab.touch_grab);
3500 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003501 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003502 wl_list_insert(&shseat->popup_grab.surfaces_list,
3503 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003504 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003505
3506 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003507}
3508
3509static void
3510remove_popup_grab(struct shell_surface *shsurf)
3511{
3512 struct shell_seat *shseat = shsurf->popup.shseat;
3513
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003514 if (shell_surface_is_xdg_popup(shsurf) &&
3515 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003516 wl_resource_post_error(shsurf->owner_resource,
3517 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003518 "xdg_popup was destroyed while it was "
3519 "not the topmost popup.");
3520 return;
3521 }
3522
Giulio Camuffo5085a752013-03-25 21:42:45 +01003523 wl_list_remove(&shsurf->popup.grab_link);
3524 wl_list_init(&shsurf->popup.grab_link);
3525 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003526 if (shseat->popup_grab.type == POINTER) {
3527 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3528 shseat->popup_grab.grab.interface = NULL;
3529 } else if (shseat->popup_grab.type == TOUCH) {
3530 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3531 shseat->popup_grab.touch_grab.interface = NULL;
3532 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003533 }
3534}
3535
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003536static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003537shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003538{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003539 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003540 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003541 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3542 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003543
Jason Ekstranda7af7042013-10-12 22:38:11 -05003544 shsurf->surface->output = parent_view->output;
3545 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003546
Jason Ekstranda7af7042013-10-12 22:38:11 -05003547 weston_view_set_transform_parent(shsurf->view, parent_view);
3548 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3549 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003550
Derek Foreman1281a362015-07-31 16:55:32 -05003551 if (pointer &&
3552 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003553 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3554 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003555 } else if (touch &&
3556 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003557 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3558 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003559 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003560 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003561 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003562 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003563
3564 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003565}
3566
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003567static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003568 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003569 shell_surface_move,
3570 shell_surface_resize,
3571 shell_surface_set_toplevel,
3572 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003573 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003574 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003575 shell_surface_set_maximized,
3576 shell_surface_set_title,
3577 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003578};
3579
3580static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003581destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003582{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003583 struct shell_surface *child, *next;
3584
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003585 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3586
Giulio Camuffo5085a752013-03-25 21:42:45 +01003587 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3588 remove_popup_grab(shsurf);
3589 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003590
Alex Wubd3354b2012-04-17 17:20:49 +08003591 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003592 shell_surface_is_top_fullscreen(shsurf))
3593 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003594
Jason Ekstranda7af7042013-10-12 22:38:11 -05003595 if (shsurf->fullscreen.black_view)
3596 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003597
Alex Wubd3354b2012-04-17 17:20:49 +08003598 /* As destroy_resource() use wl_list_for_each_safe(),
3599 * we can always remove the listener.
3600 */
3601 wl_list_remove(&shsurf->surface_destroy_listener.link);
3602 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003603 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003604 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003605
Jason Ekstranda7af7042013-10-12 22:38:11 -05003606 weston_view_destroy(shsurf->view);
3607
Philip Withnall648a4dd2013-11-25 18:01:44 +00003608 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003609 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3610 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003611
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003612 wl_list_remove(&shsurf->link);
3613 free(shsurf);
3614}
3615
3616static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003617shell_destroy_shell_surface(struct wl_resource *resource)
3618{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003619 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003620
Bryan Caina46b9462014-04-04 17:41:24 -05003621 if (!wl_list_empty(&shsurf->popup.grab_link))
3622 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003623 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003624 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003625}
3626
3627static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003628shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003629{
3630 struct shell_surface *shsurf = container_of(listener,
3631 struct shell_surface,
3632 surface_destroy_listener);
3633
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003634 if (shsurf->resource)
3635 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003636
3637 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003638}
3639
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003640static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003641fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003642{
3643 struct shell_surface *shsurf = data;
3644
3645 weston_surface_destroy(shsurf->surface);
3646}
3647
3648static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003649fade_out_done(struct weston_view_animation *animation, void *data)
3650{
3651 struct shell_surface *shsurf = data;
3652 struct wl_event_loop *loop;
3653
3654 loop = wl_display_get_event_loop(
3655 shsurf->surface->compositor->wl_display);
3656
3657 if (!shsurf->destroying) {
3658 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3659 shsurf->destroying = true;
3660 }
3661}
3662
3663static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003664handle_resource_destroy(struct wl_listener *listener, void *data)
3665{
3666 struct shell_surface *shsurf =
3667 container_of(listener, struct shell_surface,
3668 resource_destroy_listener);
3669
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003670 if (!weston_surface_is_mapped(shsurf->surface))
3671 return;
3672
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003673 shsurf->surface->ref_count++;
3674
3675 pixman_region32_fini(&shsurf->surface->pending.input);
3676 pixman_region32_init(&shsurf->surface->pending.input);
3677 pixman_region32_fini(&shsurf->surface->input);
3678 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003679 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3680 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3681 fade_out_done, shsurf);
3682 } else {
3683 weston_surface_destroy(shsurf->surface);
3684 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003685}
3686
3687static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003688shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003689
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003690struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003691get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003692{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003693 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003694 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003695 else
3696 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003697}
3698
Rafael Antognollie2a34552013-12-03 15:35:45 -02003699static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003700create_common_surface(struct shell_client *owner, void *shell,
3701 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003702 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003703{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003704 struct shell_surface *shsurf;
3705
Pekka Paalanen50b67472014-10-01 15:02:41 +03003706 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003707
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003708 shsurf = calloc(1, sizeof *shsurf);
3709 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003710 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003711 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003712 }
3713
Jason Ekstranda7af7042013-10-12 22:38:11 -05003714 shsurf->view = weston_view_create(surface);
3715 if (!shsurf->view) {
3716 weston_log("no memory to allocate shell surface\n");
3717 free(shsurf);
3718 return NULL;
3719 }
3720
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003721 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003722 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003723 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003724
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003725 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3726 wl_resource_add_destroy_listener(surface->resource,
3727 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003728 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003729
Tiago Vignattibc052c92012-04-19 16:18:18 +03003730 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003731 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003732 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003733 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003734 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003735 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003736 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3737 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003738 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003739 wl_list_init(&shsurf->fullscreen.transform.link);
3740
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003741 shsurf->output = get_default_output(shsurf->shell->compositor);
3742
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003743 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003744 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003745 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003746 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003747
3748 /* init link so its safe to always remove it in destroy_shell_surface */
3749 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003750 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003751
Pekka Paalanen460099f2012-01-20 16:48:25 +02003752 /* empty when not in use */
3753 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003754 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003755
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003756 wl_list_init(&shsurf->workspace_transform.link);
3757
Philip Withnall648a4dd2013-11-25 18:01:44 +00003758 wl_list_init(&shsurf->children_link);
3759 wl_list_init(&shsurf->children_list);
3760 shsurf->parent = NULL;
3761
Pekka Paalanenb5026542014-11-12 15:09:24 +02003762 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003763
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003764 shsurf->client = client;
3765
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003766 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003767}
3768
Rafael Antognollie2a34552013-12-03 15:35:45 -02003769static struct shell_surface *
3770create_shell_surface(void *shell, struct weston_surface *surface,
3771 const struct weston_shell_client *client)
3772{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003773 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003774}
3775
Jason Ekstranda7af7042013-10-12 22:38:11 -05003776static struct weston_view *
3777get_primary_view(void *shell, struct shell_surface *shsurf)
3778{
3779 return shsurf->view;
3780}
3781
Tiago Vignattibc052c92012-04-19 16:18:18 +03003782static void
3783shell_get_shell_surface(struct wl_client *client,
3784 struct wl_resource *resource,
3785 uint32_t id,
3786 struct wl_resource *surface_resource)
3787{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003788 struct weston_surface *surface =
3789 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003790 struct shell_client *sc = wl_resource_get_user_data(resource);
3791 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003792 struct shell_surface *shsurf;
3793
Pekka Paalanen50b67472014-10-01 15:02:41 +03003794 if (weston_surface_set_role(surface, "wl_shell_surface",
3795 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003796 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003797
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003798 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003799 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003800 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003801 return;
3802 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003803
Jason Ekstranda85118c2013-06-27 20:17:02 -05003804 shsurf->resource =
3805 wl_resource_create(client,
3806 &wl_shell_surface_interface, 1, id);
3807 wl_resource_set_implementation(shsurf->resource,
3808 &shell_surface_implementation,
3809 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003810 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003811}
3812
Rafael Antognollie2a34552013-12-03 15:35:45 -02003813static bool
3814shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3815{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003816 /* A shell surface without a resource is created from xwayland
3817 * and is considered a wl_shell surface for now. */
3818
3819 return shsurf->resource == NULL ||
3820 wl_resource_instance_of(shsurf->resource,
3821 &wl_shell_surface_interface,
3822 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003823}
3824
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003825static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003826 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003827};
3828
Rafael Antognollie2a34552013-12-03 15:35:45 -02003829/****************************
3830 * xdg-shell implementation */
3831
3832static void
3833xdg_surface_destroy(struct wl_client *client,
3834 struct wl_resource *resource)
3835{
3836 wl_resource_destroy(resource);
3837}
3838
3839static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003840xdg_surface_set_parent(struct wl_client *client,
3841 struct wl_resource *resource,
3842 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003843{
3844 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003845 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003846
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003847 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003848 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003849 shell_surface_set_parent(shsurf, parent->surface);
3850 } else {
3851 shell_surface_set_parent(shsurf, NULL);
3852 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003853}
3854
3855static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003856xdg_surface_set_app_id(struct wl_client *client,
3857 struct wl_resource *resource,
3858 const char *app_id)
3859{
3860 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3861
3862 free(shsurf->class);
3863 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003864 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003865}
3866
3867static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003868xdg_surface_show_window_menu(struct wl_client *client,
3869 struct wl_resource *surface_resource,
3870 struct wl_resource *seat_resource,
3871 uint32_t serial,
3872 int32_t x,
3873 int32_t y)
3874{
3875 /* TODO */
3876}
3877
3878static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003879xdg_surface_set_title(struct wl_client *client,
3880 struct wl_resource *resource, const char *title)
3881{
3882 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3883
3884 set_title(shsurf, title);
3885}
3886
3887static void
3888xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3889 struct wl_resource *seat_resource, uint32_t serial)
3890{
3891 common_surface_move(resource, seat_resource, serial);
3892}
3893
3894static void
3895xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3896 struct wl_resource *seat_resource, uint32_t serial,
3897 uint32_t edges)
3898{
3899 common_surface_resize(resource, seat_resource, serial, edges);
3900}
3901
3902static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003903xdg_surface_ack_configure(struct wl_client *client,
3904 struct wl_resource *resource,
3905 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003906{
3907 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3908
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003909 if (shsurf->state_requested) {
3910 shsurf->next_state = shsurf->requested_state;
3911 shsurf->state_changed = true;
3912 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003913 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003914}
3915
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003916static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003917xdg_surface_set_window_geometry(struct wl_client *client,
3918 struct wl_resource *resource,
3919 int32_t x,
3920 int32_t y,
3921 int32_t width,
3922 int32_t height)
3923{
3924 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3925
3926 set_window_geometry(shsurf, x, y, width, height);
3927}
3928
3929static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003930xdg_surface_set_maximized(struct wl_client *client,
3931 struct wl_resource *resource)
3932{
3933 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003934 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003935
3936 shsurf->state_requested = true;
3937 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003938
3939 if (!weston_surface_is_mapped(shsurf->surface))
3940 output = get_focused_output(shsurf->surface->compositor);
3941 else
3942 output = shsurf->surface->output;
3943
3944 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003945 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003946}
3947
3948static void
3949xdg_surface_unset_maximized(struct wl_client *client,
3950 struct wl_resource *resource)
3951{
3952 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3953
3954 shsurf->state_requested = true;
3955 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003956 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003957}
3958
3959static void
3960xdg_surface_set_fullscreen(struct wl_client *client,
3961 struct wl_resource *resource,
3962 struct wl_resource *output_resource)
3963{
3964 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3965 struct weston_output *output;
3966
3967 shsurf->state_requested = true;
3968 shsurf->requested_state.fullscreen = true;
3969
3970 if (output_resource)
3971 output = wl_resource_get_user_data(output_resource);
3972 else
3973 output = NULL;
3974
Marek Chalupa052917a2014-09-02 11:35:12 +02003975 /* handle clients launching in fullscreen */
3976 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3977 /* Set the output to the one that has focus currently. */
3978 assert(shsurf->surface);
3979 output = get_focused_output(shsurf->surface->compositor);
3980 }
3981
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003982 shell_surface_set_output(shsurf, output);
3983 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003984
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003985 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003986}
3987
3988static void
3989xdg_surface_unset_fullscreen(struct wl_client *client,
3990 struct wl_resource *resource)
3991{
3992 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3993
3994 shsurf->state_requested = true;
3995 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003996 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003997}
3998
3999static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004000xdg_surface_set_minimized(struct wl_client *client,
4001 struct wl_resource *resource)
4002{
4003 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
4004
4005 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
4006 return;
4007
4008 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01004009 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004010}
4011
Rafael Antognollie2a34552013-12-03 15:35:45 -02004012static const struct xdg_surface_interface xdg_surface_implementation = {
4013 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004014 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004015 xdg_surface_set_title,
4016 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004017 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004018 xdg_surface_move,
4019 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004020 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004021 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004022 xdg_surface_set_maximized,
4023 xdg_surface_unset_maximized,
4024 xdg_surface_set_fullscreen,
4025 xdg_surface_unset_fullscreen,
4026 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004027};
4028
4029static void
4030xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004031 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004032{
4033 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004034 uint32_t *s;
4035 struct wl_array states;
4036 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004037
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004038 assert(shsurf);
4039
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004040 if (!shsurf->resource)
4041 return;
4042
4043 wl_array_init(&states);
4044 if (shsurf->requested_state.fullscreen) {
4045 s = wl_array_add(&states, sizeof *s);
4046 *s = XDG_SURFACE_STATE_FULLSCREEN;
4047 } else if (shsurf->requested_state.maximized) {
4048 s = wl_array_add(&states, sizeof *s);
4049 *s = XDG_SURFACE_STATE_MAXIMIZED;
4050 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004051 if (shsurf->resize_edges != 0) {
4052 s = wl_array_add(&states, sizeof *s);
4053 *s = XDG_SURFACE_STATE_RESIZING;
4054 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004055 if (shsurf->focus_count > 0) {
4056 s = wl_array_add(&states, sizeof *s);
4057 *s = XDG_SURFACE_STATE_ACTIVATED;
4058 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004059
4060 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4061 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4062
4063 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004064}
4065
4066static const struct weston_shell_client xdg_client = {
4067 xdg_send_configure
4068};
4069
4070static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004071xdg_shell_destroy(struct wl_client *client,
4072 struct wl_resource *resource)
4073{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004074 struct shell_client *sc = wl_resource_get_user_data(resource);
4075 struct wl_resource *shsurf_resource;
4076 struct shell_surface *shsurf;
4077
4078 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4079 shsurf = wl_resource_get_user_data(shsurf_resource);
4080 if (shsurf->owner_resource == resource) {
4081 wl_resource_post_error(
4082 resource,
4083 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4084 "not all child surface objects destroyed");
4085 return;
4086 }
4087 }
4088
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004089 wl_resource_destroy(resource);
4090}
4091
4092static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004093xdg_use_unstable_version(struct wl_client *client,
4094 struct wl_resource *resource,
4095 int32_t version)
4096{
4097 if (version > 1) {
4098 wl_resource_post_error(resource,
4099 1,
4100 "xdg-shell:: version not implemented yet.");
4101 return;
4102 }
4103}
4104
4105static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004106create_xdg_surface(struct shell_client *owner, void *shell,
4107 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004108 const struct weston_shell_client *client)
4109{
4110 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004111
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004112 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004113 if (!shsurf)
4114 return NULL;
4115
Pekka Paalanenb5026542014-11-12 15:09:24 +02004116 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004117
4118 return shsurf;
4119}
4120
4121static void
4122xdg_get_xdg_surface(struct wl_client *client,
4123 struct wl_resource *resource,
4124 uint32_t id,
4125 struct wl_resource *surface_resource)
4126{
4127 struct weston_surface *surface =
4128 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004129 struct shell_client *sc = wl_resource_get_user_data(resource);
4130 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004131 struct shell_surface *shsurf;
4132
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004133 shsurf = get_shell_surface(surface);
4134 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4135 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4136 "This wl_surface is already an "
4137 "xdg_surface");
4138 return;
4139 }
4140
Pekka Paalanen50b67472014-10-01 15:02:41 +03004141 if (weston_surface_set_role(surface, "xdg_surface",
4142 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004143 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004144
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004145 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004146 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004147 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004148 return;
4149 }
4150
4151 shsurf->resource =
4152 wl_resource_create(client,
4153 &xdg_surface_interface, 1, id);
4154 wl_resource_set_implementation(shsurf->resource,
4155 &xdg_surface_implementation,
4156 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004157 shsurf->owner_resource = resource;
4158 wl_list_insert(&sc->surface_list,
4159 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004160}
4161
4162static bool
4163shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4164{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004165 return shsurf->resource &&
4166 wl_resource_instance_of(shsurf->resource,
4167 &xdg_surface_interface,
4168 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004169}
4170
4171/* xdg-popup implementation */
4172
4173static void
4174xdg_popup_destroy(struct wl_client *client,
4175 struct wl_resource *resource)
4176{
4177 wl_resource_destroy(resource);
4178}
4179
Rafael Antognollie2a34552013-12-03 15:35:45 -02004180static const struct xdg_popup_interface xdg_popup_implementation = {
4181 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004182};
4183
4184static void
4185xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004186 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004187{
4188}
4189
4190static const struct weston_shell_client xdg_popup_client = {
4191 xdg_popup_send_configure
4192};
4193
4194static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004195create_xdg_popup(struct shell_client *owner, void *shell,
4196 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004197 const struct weston_shell_client *client,
4198 struct weston_surface *parent,
4199 struct shell_seat *seat,
4200 uint32_t serial,
4201 int32_t x, int32_t y)
4202{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004203 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004204
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004205 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004206 if (!shsurf)
4207 return NULL;
4208
Pekka Paalanenb5026542014-11-12 15:09:24 +02004209 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004210 shsurf->popup.shseat = seat;
4211 shsurf->popup.serial = serial;
4212 shsurf->popup.x = x;
4213 shsurf->popup.y = y;
4214 shell_surface_set_parent(shsurf, parent);
4215
4216 return shsurf;
4217}
4218
4219static void
4220xdg_get_xdg_popup(struct wl_client *client,
4221 struct wl_resource *resource,
4222 uint32_t id,
4223 struct wl_resource *surface_resource,
4224 struct wl_resource *parent_resource,
4225 struct wl_resource *seat_resource,
4226 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004227 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004228{
4229 struct weston_surface *surface =
4230 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004231 struct shell_client *sc = wl_resource_get_user_data(resource);
4232 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004233 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004234 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004235 struct weston_surface *parent;
4236 struct shell_seat *seat;
4237
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004238 shsurf = get_shell_surface(surface);
4239 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4240 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4241 "This wl_surface is already an "
4242 "xdg_popup");
4243 return;
4244 }
4245
Pekka Paalanen50b67472014-10-01 15:02:41 +03004246 if (weston_surface_set_role(surface, "xdg_popup",
4247 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004248 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004249
4250 if (!parent_resource) {
4251 wl_resource_post_error(surface_resource,
4252 WL_DISPLAY_ERROR_INVALID_OBJECT,
4253 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004254 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255 }
4256
4257 parent = wl_resource_get_user_data(parent_resource);
4258 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4259
Jonas Ådahlee45a552015-03-18 16:37:47 +08004260 /* Verify that we are creating the top most popup when mapping,
4261 * as it's not until then we know whether it was mapped as most
4262 * top level or not. */
4263
4264 parent_shsurf = get_shell_surface(parent);
4265 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4266 !shell_surface_is_xdg_surface(parent_shsurf)) {
4267 wl_resource_post_error(resource,
4268 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4269 "xdg_popup parent was invalid");
4270 return;
4271 }
4272
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004273 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004274 parent, seat, serial, x, y);
4275 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004276 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004277 return;
4278 }
4279
4280 shsurf->resource =
4281 wl_resource_create(client,
4282 &xdg_popup_interface, 1, id);
4283 wl_resource_set_implementation(shsurf->resource,
4284 &xdg_popup_implementation,
4285 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004286 shsurf->owner_resource = resource;
4287 wl_list_insert(&sc->surface_list,
4288 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004289}
4290
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004291static void
4292xdg_pong(struct wl_client *client,
4293 struct wl_resource *resource, uint32_t serial)
4294{
4295 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004296
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004297 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004298}
4299
Rafael Antognollie2a34552013-12-03 15:35:45 -02004300static bool
4301shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4302{
4303 return wl_resource_instance_of(shsurf->resource,
4304 &xdg_popup_interface,
4305 &xdg_popup_implementation);
4306}
4307
4308static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004309 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004310 xdg_use_unstable_version,
4311 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004312 xdg_get_xdg_popup,
4313 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004314};
4315
4316static int
4317xdg_shell_unversioned_dispatch(const void *implementation,
4318 void *_target, uint32_t opcode,
4319 const struct wl_message *message,
4320 union wl_argument *args)
4321{
4322 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004323 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004324
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004325 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004326 wl_resource_post_error(resource,
4327 WL_DISPLAY_ERROR_INVALID_OBJECT,
4328 "must call use_unstable_version first");
4329 return 0;
4330 }
4331
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004332#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004333
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004334 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4335 "shell implementation doesn't match protocol version");
4336
Rafael Antognollie2a34552013-12-03 15:35:45 -02004337 if (args[0].i != XDG_SERVER_VERSION) {
4338 wl_resource_post_error(resource,
4339 WL_DISPLAY_ERROR_INVALID_OBJECT,
4340 "incompatible version, server is %d "
4341 "client wants %d",
4342 XDG_SERVER_VERSION, args[0].i);
4343 return 0;
4344 }
4345
4346 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004347 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004348
4349 return 1;
4350}
4351
4352/* end of xdg-shell implementation */
4353/***********************************/
4354
Kristian Høgsberg07937562011-04-12 17:25:42 -04004355static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004356shell_fade(struct desktop_shell *shell, enum fade_type type);
4357
Pekka Paalanen77346a62011-11-30 16:26:35 +02004358static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004359configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004360{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004362
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004363 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004364 if (v->output == ev->output && v != ev) {
4365 weston_view_unmap(v);
4366 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004367 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004368 }
4369 }
4370
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004371 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004372
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004373 if (wl_list_empty(&ev->layer_link.link)) {
4374 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004376 }
4377}
4378
Pekka Paalanen8274d902014-08-06 19:36:51 +03004379static int
4380background_get_label(struct weston_surface *surface, char *buf, size_t len)
4381{
4382 return snprintf(buf, len, "background for output %s",
4383 surface->output->name);
4384}
4385
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004386static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004387background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004388{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004389 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004391
Jason Ekstranda7af7042013-10-12 22:38:11 -05004392 view = container_of(es->views.next, struct weston_view, surface_link);
4393
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004394 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004395}
4396
4397static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004398desktop_shell_set_background(struct wl_client *client,
4399 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004400 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004401 struct wl_resource *surface_resource)
4402{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004403 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004404 struct weston_surface *surface =
4405 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004407
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004408 if (surface->configure) {
4409 wl_resource_post_error(surface_resource,
4410 WL_DISPLAY_ERROR_INVALID_OBJECT,
4411 "surface role already assigned");
4412 return;
4413 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004414
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4416 weston_view_destroy(view);
4417 view = weston_view_create(surface);
4418
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004419 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004420 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004421 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004422 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004424 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004425 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004426 surface->output->width,
4427 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004428}
4429
Pekka Paalanen8274d902014-08-06 19:36:51 +03004430static int
4431panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4432{
4433 return snprintf(buf, len, "panel for output %s",
4434 surface->output->name);
4435}
4436
Kristian Høgsberg75840622011-09-06 13:48:16 -04004437static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004438panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004439{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004440 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004441 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004442
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 view = container_of(es->views.next, struct weston_view, surface_link);
4444
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004445 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004446}
4447
4448static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004449desktop_shell_set_panel(struct wl_client *client,
4450 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004451 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004452 struct wl_resource *surface_resource)
4453{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004454 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004455 struct weston_surface *surface =
4456 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004458
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004459 if (surface->configure) {
4460 wl_resource_post_error(surface_resource,
4461 WL_DISPLAY_ERROR_INVALID_OBJECT,
4462 "surface role already assigned");
4463 return;
4464 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004465
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4467 weston_view_destroy(view);
4468 view = weston_view_create(surface);
4469
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004470 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004471 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004472 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004473 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004474 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004475 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004476 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004477 surface->output->width,
4478 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004479}
4480
Pekka Paalanen8274d902014-08-06 19:36:51 +03004481static int
4482lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4483{
4484 return snprintf(buf, len, "lock window");
4485}
4486
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004487static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004488lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004489{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004490 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004491 struct weston_view *view;
4492
4493 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004494
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004495 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004496 return;
4497
Jason Ekstranda7af7042013-10-12 22:38:11 -05004498 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004499
4500 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004501 weston_layer_entry_insert(&shell->lock_layer.view_list,
4502 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004503 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004504 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004505 }
4506}
4507
4508static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004509handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004510{
Tiago Vignattibe143262012-04-16 17:31:41 +03004511 struct desktop_shell *shell =
4512 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004513
Martin Minarik6d118362012-06-07 18:01:59 +02004514 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004515 shell->lock_surface = NULL;
4516}
4517
4518static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004519desktop_shell_set_lock_surface(struct wl_client *client,
4520 struct wl_resource *resource,
4521 struct wl_resource *surface_resource)
4522{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004523 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004524 struct weston_surface *surface =
4525 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004526
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004527 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004528
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004529 if (!shell->locked)
4530 return;
4531
Pekka Paalanen98262232011-12-01 10:42:22 +02004532 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004533
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004534 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004535 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004536 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004537
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004538 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004539 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004540 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004541 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004542}
4543
4544static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004545resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004546{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004547 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004548
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004549 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004550 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004551 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004552 &shell->input_panel_layer.link);
4553 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004554 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004555 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004556 wl_list_insert(&shell->compositor->cursor_layer.link,
4557 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004558 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004559 wl_list_insert(&shell->fullscreen_layer.link,
4560 &shell->panel_layer.link);
4561 wl_list_insert(&shell->panel_layer.link,
4562 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004563
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004564 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004565
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004566 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004567 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004568 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004569}
4570
4571static void
4572desktop_shell_unlock(struct wl_client *client,
4573 struct wl_resource *resource)
4574{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004575 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004576
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004577 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004578
4579 if (shell->locked)
4580 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004581}
4582
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004583static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004584desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004585 struct wl_resource *resource,
4586 struct wl_resource *surface_resource)
4587{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004588 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004589
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004590 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004591 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004592}
4593
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004594static void
4595desktop_shell_desktop_ready(struct wl_client *client,
4596 struct wl_resource *resource)
4597{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004598 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004599
4600 shell_fade_startup(shell);
4601}
4602
Jonny Lamb765760d2014-08-20 15:53:19 +02004603static void
4604desktop_shell_set_panel_position(struct wl_client *client,
4605 struct wl_resource *resource,
4606 uint32_t position)
4607{
4608 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4609
4610 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4611 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4612 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4613 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4614 wl_resource_post_error(resource,
4615 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4616 "bad position argument");
4617 return;
4618 }
4619
4620 shell->panel_position = position;
4621}
4622
Kristian Høgsberg75840622011-09-06 13:48:16 -04004623static const struct desktop_shell_interface desktop_shell_implementation = {
4624 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004625 desktop_shell_set_panel,
4626 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004627 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004628 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004629 desktop_shell_desktop_ready,
4630 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004631};
4632
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004633static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004634get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004635{
4636 struct shell_surface *shsurf;
4637
4638 shsurf = get_shell_surface(surface);
4639 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004640 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004641 return shsurf->type;
4642}
4643
Kristian Høgsberg75840622011-09-06 13:48:16 -04004644static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004645move_binding(struct weston_pointer *pointer, uint32_t time,
4646 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004647{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004648 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004649 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004650 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004651
Derek Foreman8ae2db52015-07-15 13:00:36 -05004652 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004653 return;
4654
Derek Foreman8ae2db52015-07-15 13:00:36 -05004655 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004656
Pekka Paalanen01388e22013-04-25 13:57:44 +03004657 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004658 if (surface == NULL)
4659 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004660
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004661 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004662 if (shsurf == NULL || shsurf->state.fullscreen ||
4663 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004664 return;
4665
Derek Foreman794fa0e2015-07-15 13:00:38 -05004666 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004667}
4668
4669static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004670maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4671 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004672{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004673 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004674 struct weston_surface *surface;
4675 struct shell_surface *shsurf;
4676
4677 surface = weston_surface_get_main_surface(focus);
4678 if (surface == NULL)
4679 return;
4680
4681 shsurf = get_shell_surface(surface);
4682 if (shsurf == NULL)
4683 return;
4684
4685 if (!shell_surface_is_xdg_surface(shsurf))
4686 return;
4687
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004688 shsurf->state_requested = true;
4689 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004690 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004691}
4692
4693static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004694fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4695 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004696{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004697 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004698 struct weston_surface *surface;
4699 struct shell_surface *shsurf;
4700
4701 surface = weston_surface_get_main_surface(focus);
4702 if (surface == NULL)
4703 return;
4704
4705 shsurf = get_shell_surface(surface);
4706 if (shsurf == NULL)
4707 return;
4708
4709 if (!shell_surface_is_xdg_surface(shsurf))
4710 return;
4711
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004712 shsurf->state_requested = true;
4713 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004714 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004715 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004716}
4717
4718static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004719touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004720{
Derek Foreman362656b2014-09-04 10:23:05 -05004721 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004722 struct weston_surface *surface;
4723 struct shell_surface *shsurf;
4724
Derek Foreman8ae2db52015-07-15 13:00:36 -05004725 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004726 return;
4727
Derek Foreman8ae2db52015-07-15 13:00:36 -05004728 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004729 surface = weston_surface_get_main_surface(focus);
4730 if (surface == NULL)
4731 return;
4732
4733 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004734 if (shsurf == NULL || shsurf->state.fullscreen ||
4735 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004736 return;
4737
Derek Foremanb7674ae2015-07-15 13:00:37 -05004738 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004739}
4740
4741static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004742resize_binding(struct weston_pointer *pointer, uint32_t time,
4743 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004744{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004745 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004746 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004747 uint32_t edges = 0;
4748 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004749 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004750
Derek Foreman8ae2db52015-07-15 13:00:36 -05004751 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004752 return;
4753
Derek Foreman8ae2db52015-07-15 13:00:36 -05004754 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004755
Pekka Paalanen01388e22013-04-25 13:57:44 +03004756 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004757 if (surface == NULL)
4758 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004759
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004760 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004761 if (shsurf == NULL || shsurf->state.fullscreen ||
4762 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004763 return;
4764
Jason Ekstranda7af7042013-10-12 22:38:11 -05004765 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004766 wl_fixed_to_int(pointer->grab_x),
4767 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004768 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004769
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004770 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004771 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004772 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004773 edges |= 0;
4774 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004775 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004776
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004777 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004778 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004779 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004780 edges |= 0;
4781 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004782 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004783
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004784 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004785}
4786
4787static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004788surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4789 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004790{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004791 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004792 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004793 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004794 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004795
Pekka Paalanen01388e22013-04-25 13:57:44 +03004796 /* XXX: broken for windows containing sub-surfaces */
4797 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004798 if (surface == NULL)
4799 return;
4800
4801 shsurf = get_shell_surface(surface);
4802 if (!shsurf)
4803 return;
4804
Jason Ekstranda7af7042013-10-12 22:38:11 -05004805 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004806
Jason Ekstranda7af7042013-10-12 22:38:11 -05004807 if (shsurf->view->alpha > 1.0)
4808 shsurf->view->alpha = 1.0;
4809 if (shsurf->view->alpha < step)
4810 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004811
Jason Ekstranda7af7042013-10-12 22:38:11 -05004812 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004813 weston_surface_damage(surface);
4814}
4815
4816static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004817do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004818 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004819{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004820 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004821 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004822 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004823 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004824
Derek Foreman1281a362015-07-31 16:55:32 -05004825 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004826 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4827 return;
4828 }
4829
Scott Moreauccbf29d2012-02-22 14:21:41 -07004830 wl_list_for_each(output, &compositor->output_list, link) {
4831 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004832 wl_fixed_to_double(pointer->x),
4833 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004834 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004835 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004836 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004837 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004838 increment = -output->zoom.increment;
4839 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004840 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004841 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004842 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004843 else
4844 increment = 0;
4845
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004846 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004847
Scott Moreaue6603982012-06-11 13:07:51 -06004848 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004849 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004850 else if (output->zoom.level > output->zoom.max_level)
4851 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004852
4853 if (!output->zoom.active) {
4854 if (output->zoom.level <= 0.0)
4855 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004856 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004857 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004858
Scott Moreaue6603982012-06-11 13:07:51 -06004859 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004860
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004862 }
4863 }
4864}
4865
Scott Moreauccbf29d2012-02-22 14:21:41 -07004866static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004867zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4868 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004869{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004870 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004871}
4872
4873static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004874zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4875 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004876{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004877 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004878}
4879
4880static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004881terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4882 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004883{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004884 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004885
Daniel Stone325fc2d2012-05-30 16:31:58 +01004886 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004887}
4888
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004889static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004890rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4891 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004892{
4893 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004894 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004895 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004896 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004897 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004898
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004899 weston_pointer_move(pointer, x, y);
4900
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004901 if (!shsurf)
4902 return;
4903
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004904 cx = 0.5f * shsurf->surface->width;
4905 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004906
Daniel Stone37816df2012-05-16 18:45:18 +01004907 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4908 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004909 r = sqrtf(dx * dx + dy * dy);
4910
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004911 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004912 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004913
4914 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004915 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004916 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004917
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004918 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004919 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004920
4921 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004922 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004923 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004924 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004925 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004926
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004927 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004928 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004929 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004930 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004931 wl_list_init(&shsurf->rotation.transform.link);
4932 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004933 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004934 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004935
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004936 /* We need to adjust the position of the surface
4937 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004938 cposx = shsurf->view->geometry.x + cx;
4939 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004940 dposx = rotate->center.x - cposx;
4941 dposy = rotate->center.y - cposy;
4942 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004943 weston_view_set_position(shsurf->view,
4944 shsurf->view->geometry.x + dposx,
4945 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004946 }
4947
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004948 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004949 * lazily applies the damage due to rotation update.
4950 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004951 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004952}
4953
4954static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004955rotate_grab_button(struct weston_pointer_grab *grab,
4956 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004957{
4958 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004959 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004960 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004961 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004962 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963
Daniel Stone4dbadb12012-05-30 16:31:51 +01004964 if (pointer->button_count == 0 &&
4965 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004966 if (shsurf)
4967 weston_matrix_multiply(&shsurf->rotation.rotation,
4968 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004969 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004970 free(rotate);
4971 }
4972}
4973
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004974static void
4975rotate_grab_cancel(struct weston_pointer_grab *grab)
4976{
4977 struct rotate_grab *rotate =
4978 container_of(grab, struct rotate_grab, base.grab);
4979
4980 shell_grab_end(&rotate->base);
4981 free(rotate);
4982}
4983
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004984static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004985 noop_grab_focus,
4986 rotate_grab_motion,
4987 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004988 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004989};
4990
4991static void
Derek Foreman74de4692015-07-15 13:00:39 -05004992surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004993{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004994 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004995 float dx, dy;
4996 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004997
Pekka Paalanen460099f2012-01-20 16:48:25 +02004998 rotate = malloc(sizeof *rotate);
4999 if (!rotate)
5000 return;
5001
Jason Ekstranda7af7042013-10-12 22:38:11 -05005002 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005003 surface->surface->width * 0.5f,
5004 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005005 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005006
Derek Foreman74de4692015-07-15 13:00:39 -05005007 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5008 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005009 r = sqrtf(dx * dx + dy * dy);
5010 if (r > 20.0f) {
5011 struct weston_matrix inverse;
5012
5013 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005014 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005015 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005016
5017 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005018 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005019 } else {
5020 weston_matrix_init(&surface->rotation.rotation);
5021 weston_matrix_init(&rotate->rotation);
5022 }
5023
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005024 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Derek Foreman74de4692015-07-15 13:00:39 -05005025 pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005026}
5027
5028static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005029rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005030 void *data)
5031{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005032 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005033 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005034 struct shell_surface *surface;
5035
Derek Foreman8ae2db52015-07-15 13:00:36 -05005036 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005037 return;
5038
Derek Foreman8ae2db52015-07-15 13:00:36 -05005039 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005040
Pekka Paalanen01388e22013-04-25 13:57:44 +03005041 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005042 if (base_surface == NULL)
5043 return;
5044
5045 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005046 if (surface == NULL || surface->state.fullscreen ||
5047 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005048 return;
5049
Derek Foreman74de4692015-07-15 13:00:39 -05005050 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005051}
5052
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005053/* Move all fullscreen layers down to the current workspace and hide their
5054 * black views. The surfaces' state is set to both fullscreen and lowered,
5055 * and this is reversed when such a surface is re-configured, see
5056 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5057 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005058 * lowering_output = NULL - Lower on all outputs, else only lower on the
5059 * specified output.
5060 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005061 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005062 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005063void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005064lower_fullscreen_layer(struct desktop_shell *shell,
5065 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005066{
5067 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005068 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005069
5070 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005071 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005072 &shell->fullscreen_layer.view_list.link,
5073 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005074 struct shell_surface *shsurf = get_shell_surface(view->surface);
5075
5076 if (!shsurf)
5077 continue;
5078
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005079 /* Only lower surfaces which have lowering_output as their fullscreen
5080 * output, unless a NULL output asks for lowering on all outputs.
5081 */
5082 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5083 continue;
5084
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005085 /* We can have a non-fullscreen popup for a fullscreen surface
5086 * in the fullscreen layer. */
5087 if (shsurf->state.fullscreen) {
5088 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005089 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5090 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005091 weston_view_damage_below(shsurf->fullscreen.black_view);
5092
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005093 }
5094
5095 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005096 weston_layer_entry_remove(&view->layer_link);
5097 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005098 weston_view_damage_below(view);
5099 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005100
5101 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005102 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005103}
5104
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005105void
Tiago Vignattibe143262012-04-16 17:31:41 +03005106activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005107 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005108{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005109 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005110 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005111 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005112 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005113 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005114
Pekka Paalanen01388e22013-04-25 13:57:44 +03005115 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005116 shsurf = get_shell_surface(main_surface);
5117 assert(shsurf);
5118
5119 /* Only demote fullscreen surfaces on the output of activated shsurf.
5120 * Leave fullscreen surfaces on unrelated outputs alone. */
5121 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005122
Daniel Stone37816df2012-05-16 18:45:18 +01005123 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005124
Jonas Ådahl8538b222012-08-29 22:13:03 +02005125 state = ensure_focus_state(shell, seat);
5126 if (state == NULL)
5127 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005128
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005129 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005130 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005131
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005132 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005133 shell_configure_fullscreen(shsurf);
5134 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005135 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005136
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005137 /* Update the surface’s layer. This brings it to the top of the stacking
5138 * order as appropriate. */
5139 shell_surface_update_layer(shsurf);
5140
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005141 if (shell->focus_animation_type != ANIMATION_NONE) {
5142 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005143 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005144 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005145}
5146
Alex Wu21858432012-04-01 20:13:08 +08005147/* no-op func for checking black surface */
5148static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005149black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005150{
5151}
5152
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005153static bool
Alex Wu21858432012-04-01 20:13:08 +08005154is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5155{
5156 if (es->configure == black_surface_configure) {
5157 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005158 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005159 return true;
5160 }
5161 return false;
5162}
5163
Kristian Høgsberg75840622011-09-06 13:48:16 -04005164static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005165activate_binding(struct weston_seat *seat,
5166 struct desktop_shell *shell,
5167 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005168{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005169 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005170
Alex Wu9c35e6b2012-03-05 14:13:13 +08005171 if (!focus)
5172 return;
5173
Pekka Paalanen01388e22013-04-25 13:57:44 +03005174 if (is_black_surface(focus, &main_surface))
5175 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005176
Pekka Paalanen01388e22013-04-25 13:57:44 +03005177 main_surface = weston_surface_get_main_surface(focus);
5178 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005179 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005180
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005181 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005182}
5183
5184static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005185click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5186 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005187{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005188 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005189 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005190 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005191 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005192
Derek Foreman8ae2db52015-07-15 13:00:36 -05005193 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005194}
5195
5196static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005197touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5198 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005199{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005200 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005201 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005202 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005203 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005204
Derek Foreman8ae2db52015-07-15 13:00:36 -05005205 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005206}
5207
5208static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005209unfocus_all_seats(struct desktop_shell *shell)
5210{
5211 struct weston_seat *seat, *next;
5212
5213 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005214 struct weston_keyboard *keyboard =
5215 weston_seat_get_keyboard(seat);
5216
5217 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005218 continue;
5219
Derek Foreman1281a362015-07-31 16:55:32 -05005220 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005221 }
5222}
5223
5224static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005225lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005226{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005227 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005228
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005229 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005230 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005231 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005232 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005233
5234 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005235
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005236 /* Hide all surfaces by removing the fullscreen, panel and
5237 * toplevel layers. This way nothing else can show or receive
5238 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005239
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005240 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005241 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005242 if (shell->showing_input_panels)
5243 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005244 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005245 wl_list_insert(&shell->compositor->cursor_layer.link,
5246 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005247
Derek Foreman004b4a12015-07-20 16:28:13 -05005248 weston_compositor_sleep(shell->compositor);
5249
Neil Robertsb4a91702014-04-09 16:33:32 +01005250 /* Remove the keyboard focus on all seats. This will be
5251 * restored to the workspace's saved state via
5252 * restore_focus_state when the compositor is unlocked */
5253 unfocus_all_seats(shell);
5254
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005255 /* TODO: disable bindings that should not work while locked. */
5256
5257 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005258}
5259
5260static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005261unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005262{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005263 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005264 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005265 return;
5266 }
5267
5268 /* If desktop-shell client has gone away, unlock immediately. */
5269 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005270 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005271 return;
5272 }
5273
5274 if (shell->prepare_event_sent)
5275 return;
5276
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005277 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005278 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005279}
5280
5281static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005282shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005283{
5284 struct desktop_shell *shell = data;
5285
5286 shell->fade.animation = NULL;
5287
5288 switch (shell->fade.type) {
5289 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 weston_surface_destroy(shell->fade.view->surface);
5291 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005292 break;
5293 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005294 lock(shell);
5295 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005296 default:
5297 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005298 }
5299}
5300
Jason Ekstranda7af7042013-10-12 22:38:11 -05005301static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005302shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005303{
5304 struct weston_compositor *compositor = shell->compositor;
5305 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005306 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005307
5308 surface = weston_surface_create(compositor);
5309 if (!surface)
5310 return NULL;
5311
Jason Ekstranda7af7042013-10-12 22:38:11 -05005312 view = weston_view_create(surface);
5313 if (!view) {
5314 weston_surface_destroy(surface);
5315 return NULL;
5316 }
5317
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005318 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005319 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005320 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005321 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5322 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005323 pixman_region32_init(&surface->input);
5324
Jason Ekstranda7af7042013-10-12 22:38:11 -05005325 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005326}
5327
5328static void
5329shell_fade(struct desktop_shell *shell, enum fade_type type)
5330{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005331 float tint;
5332
5333 switch (type) {
5334 case FADE_IN:
5335 tint = 0.0;
5336 break;
5337 case FADE_OUT:
5338 tint = 1.0;
5339 break;
5340 default:
5341 weston_log("shell: invalid fade type\n");
5342 return;
5343 }
5344
5345 shell->fade.type = type;
5346
Jason Ekstranda7af7042013-10-12 22:38:11 -05005347 if (shell->fade.view == NULL) {
5348 shell->fade.view = shell_fade_create_surface(shell);
5349 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005350 return;
5351
Jason Ekstranda7af7042013-10-12 22:38:11 -05005352 shell->fade.view->alpha = 1.0 - tint;
5353 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005354 }
5355
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005356 if (shell->fade.view->output == NULL) {
5357 /* If the black view gets a NULL output, we lost the
5358 * last output and we'll just cancel the fade. This
5359 * happens when you close the last window under the
5360 * X11 or Wayland backends. */
5361 shell->locked = false;
5362 weston_surface_destroy(shell->fade.view->surface);
5363 shell->fade.view = NULL;
5364 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005365 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005366 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005367 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005368 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005369 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005370 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005371 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005372}
5373
5374static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005375do_shell_fade_startup(void *data)
5376{
5377 struct desktop_shell *shell = data;
5378
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005379 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005380 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005381 } else {
5382 weston_log("desktop shell: "
5383 "unexpected fade-in animation type %d\n",
5384 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005385 weston_surface_destroy(shell->fade.view->surface);
5386 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005387 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005388}
5389
5390static void
5391shell_fade_startup(struct desktop_shell *shell)
5392{
5393 struct wl_event_loop *loop;
5394
5395 if (!shell->fade.startup_timer)
5396 return;
5397
5398 wl_event_source_remove(shell->fade.startup_timer);
5399 shell->fade.startup_timer = NULL;
5400
5401 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5402 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5403}
5404
5405static int
5406fade_startup_timeout(void *data)
5407{
5408 struct desktop_shell *shell = data;
5409
5410 shell_fade_startup(shell);
5411 return 0;
5412}
5413
5414static void
5415shell_fade_init(struct desktop_shell *shell)
5416{
5417 /* Make compositor output all black, and wait for the desktop-shell
5418 * client to signal it is ready, then fade in. The timer triggers a
5419 * fade-in, in case the desktop-shell client takes too long.
5420 */
5421
5422 struct wl_event_loop *loop;
5423
Jason Ekstranda7af7042013-10-12 22:38:11 -05005424 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005425 weston_log("%s: warning: fade surface already exists\n",
5426 __func__);
5427 return;
5428 }
5429
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005430 if (shell->startup_animation_type == ANIMATION_NONE)
5431 return;
5432
Jason Ekstranda7af7042013-10-12 22:38:11 -05005433 shell->fade.view = shell_fade_create_surface(shell);
5434 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005435 return;
5436
Jason Ekstranda7af7042013-10-12 22:38:11 -05005437 weston_view_update_transform(shell->fade.view);
5438 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005439
5440 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5441 shell->fade.startup_timer =
5442 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5443 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5444}
5445
5446static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005447idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005448{
5449 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005450 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005451 struct weston_seat *seat;
5452
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005453 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005454 struct weston_touch *touch = weston_seat_get_touch(seat);
5455 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5456
5457 if (pointer)
5458 popup_grab_end(pointer);
5459 if (touch)
5460 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005461 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005462
5463 shell_fade(shell, FADE_OUT);
5464 /* lock() is called from shell_fade_done() */
5465}
5466
5467static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005468wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005469{
5470 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005471 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005472
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005473 unlock(shell);
5474}
5475
5476static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005477center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005478{
Giulio Camuffob8366642013-04-25 13:57:46 +03005479 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005480 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005481
Jason Ekstranda7af7042013-10-12 22:38:11 -05005482 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005483
5484 x = output->x + (output->width - width) / 2 - surf_x / 2;
5485 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005486
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005487 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005488}
5489
5490static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005491weston_view_set_initial_position(struct weston_view *view,
5492 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005493{
5494 struct weston_compositor *compositor = shell->compositor;
5495 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005496 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005497 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005498 struct weston_output *output, *target_output = NULL;
5499 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005500 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005501
5502 /* As a heuristic place the new window on the same output as the
5503 * pointer. Falling back to the output containing 0, 0.
5504 *
5505 * TODO: Do something clever for touch too?
5506 */
5507 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005508 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5509
5510 if (pointer) {
5511 ix = wl_fixed_to_int(pointer->x);
5512 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005513 break;
5514 }
5515 }
5516
5517 wl_list_for_each(output, &compositor->output_list, link) {
5518 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5519 target_output = output;
5520 break;
5521 }
5522 }
5523
5524 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005525 weston_view_set_position(view, 10 + random() % 400,
5526 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005527 return;
5528 }
5529
5530 /* Valid range within output where the surface will still be onscreen.
5531 * If this is negative it means that the surface is bigger than
5532 * output.
5533 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005534 get_output_work_area(shell, target_output, &area);
5535
Derek Foremanf814c5d2015-04-15 12:23:54 -05005536 x = area.x;
5537 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005538 range_x = area.width - view->surface->width;
5539 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005540
Rob Bradford4cb88c72012-08-13 15:18:44 +01005541 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005542 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005543
5544 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005545 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005546
Jason Ekstranda7af7042013-10-12 22:38:11 -05005547 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005548}
5549
5550static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005551set_maximized_position(struct desktop_shell *shell,
5552 struct shell_surface *shsurf)
5553{
5554 int32_t surf_x, surf_y;
5555 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005556 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005557
Jonny Lambd73c6942014-08-20 15:53:20 +02005558 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005559 if (shsurf->has_set_geometry) {
5560 surf_x = shsurf->geometry.x;
5561 surf_y = shsurf->geometry.y;
5562 } else {
5563 surface_subsurfaces_boundingbox(shsurf->surface,
5564 &surf_x, &surf_y, NULL, NULL);
5565 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005566 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005567
5568 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005569 e->x1 + area.x - surf_x,
5570 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005571}
5572
5573static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005574map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005575 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005576{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005577 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005578 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005579
Pekka Paalanen77346a62011-11-30 16:26:35 +02005580 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005581 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005582 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005583 if (shsurf->state.fullscreen) {
5584 center_on_output(shsurf->view, shsurf->fullscreen_output);
5585 shell_map_fullscreen(shsurf);
5586 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005587 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005588 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005589 weston_view_set_initial_position(shsurf->view, shell);
5590 }
Juan Zhao96879df2012-02-07 08:45:41 +08005591 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005592 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005593 if (shell_map_popup(shsurf) != 0)
5594 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005595 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005596 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005597 weston_view_set_position(shsurf->view,
5598 shsurf->view->geometry.x + sx,
5599 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005600 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005601 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005602 default:
5603 ;
5604 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005605
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005606 /* Surface stacking order, see also activate(). */
5607 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005608
Jason Ekstranda7af7042013-10-12 22:38:11 -05005609 if (shsurf->type != SHELL_SURFACE_NONE) {
5610 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005611 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005612 shsurf->surface->output = shsurf->output;
5613 shsurf->view->output = shsurf->output;
5614 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005615 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005616
Jason Ekstranda7af7042013-10-12 22:38:11 -05005617 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005618 /* XXX: xwayland's using the same fields for transient type */
5619 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005620 if (shsurf->transient.flags ==
5621 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5622 break;
5623 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005624 if (shsurf->state.relative &&
5625 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5626 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005627 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005628 break;
5629 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005630 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005631 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005632 case SHELL_SURFACE_POPUP:
5633 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005634 default:
5635 break;
5636 }
5637
Rafael Antognolli03b16592013-12-03 15:35:42 -02005638 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5639 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005640 {
5641 switch (shell->win_animation_type) {
5642 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005643 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005644 break;
5645 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005646 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005647 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005648 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005649 default:
5650 break;
5651 }
5652 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005653}
5654
5655static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005656configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005657 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005658{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005659 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005660 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005661
Pekka Paalanen77346a62011-11-30 16:26:35 +02005662 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005663
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005664 assert(shsurf);
5665
Rafael Antognolli03b16592013-12-03 15:35:42 -02005666 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005667 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005668 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005669 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005670 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005671 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005672 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005673
Alex Wu4539b082012-03-01 12:57:46 +08005674 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005675 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005676 wl_list_for_each(view, &surface->views, surface_link)
5677 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005678
Rafael Antognolli03b16592013-12-03 15:35:42 -02005679 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005680 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005681 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005682}
5683
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005684static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005685shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005686{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005687 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005688 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005689 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005690
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005691 assert(shsurf);
5692
5693 shell = shsurf->shell;
5694
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005695 if (!weston_surface_is_mapped(es) &&
5696 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005697 remove_popup_grab(shsurf);
5698 }
5699
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005700 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005701 return;
5702
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005703 if (shsurf->has_next_geometry) {
5704 shsurf->geometry = shsurf->next_geometry;
5705 shsurf->has_next_geometry = false;
5706 shsurf->has_set_geometry = true;
5707 } else if (!shsurf->has_set_geometry) {
5708 surface_subsurfaces_boundingbox(shsurf->surface,
5709 &shsurf->geometry.x,
5710 &shsurf->geometry.y,
5711 &shsurf->geometry.width,
5712 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005713 }
5714
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005715 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005716 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005717 type_changed = 1;
5718 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005719
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005720 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005721 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005722 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005723 shsurf->last_width != es->width ||
5724 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005725 float from_x, from_y;
5726 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005727
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005728 if (shsurf->resize_edges) {
5729 sx = 0;
5730 sy = 0;
5731 }
5732
5733 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5734 sx = shsurf->last_width - es->width;
5735 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5736 sy = shsurf->last_height - es->height;
5737
5738 shsurf->last_width = es->width;
5739 shsurf->last_height = es->height;
5740
Jason Ekstranda7af7042013-10-12 22:38:11 -05005741 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5742 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005743 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005744 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005745 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005746 }
5747}
5748
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005749static bool
5750check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5751{
5752 struct timespec now;
5753
5754 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5755 return false;
5756
5757 /*
5758 * If the shell helper client dies before the session has been
5759 * up for roughly 30 seconds, better just make Weston shut down,
5760 * because the user likely has no way to interact with the desktop
5761 * anyway.
5762 */
5763 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5764 weston_log("Error: %s apparently cannot run at all.\n",
5765 shell->client);
5766 weston_log_continue(STAMP_SPACE "Quitting...");
5767 wl_display_terminate(shell->compositor->wl_display);
5768
5769 return true;
5770 }
5771
5772 return false;
5773}
5774
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005775static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005776
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005777static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005778respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005779{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005780 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005781
5782 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5783 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005784 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005785 shell->child.deathstamp = time;
5786 shell->child.deathcount = 0;
5787 }
5788
5789 shell->child.deathcount++;
5790 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005791 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005792 return;
5793 }
5794
Pekka Paalanen826dc142014-08-27 12:11:53 +03005795 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005796 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005797}
5798
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005799static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005800desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5801{
5802 struct desktop_shell *shell;
5803
5804 shell = container_of(listener, struct desktop_shell,
5805 child.client_destroy_listener);
5806
Pekka Paalanen826dc142014-08-27 12:11:53 +03005807 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005808 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005809 /*
5810 * unbind_desktop_shell() will reset shell->child.desktop_shell
5811 * before the respawned process has a chance to create a new
5812 * desktop_shell object, because we are being called from the
5813 * wl_client destructor which destroys all wl_resources before
5814 * returning.
5815 */
5816
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005817 if (!check_desktop_shell_crash_too_early(shell))
5818 respawn_desktop_shell_process(shell);
5819
Pekka Paalanen826dc142014-08-27 12:11:53 +03005820 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005821}
5822
5823static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005824launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005825{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005826 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005827
Pekka Paalanen826dc142014-08-27 12:11:53 +03005828 shell->child.client = weston_client_start(shell->compositor,
5829 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005830
Arnaud Vrac42631192014-08-25 20:56:46 +02005831 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005832 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005833 return;
5834 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005835
5836 shell->child.client_destroy_listener.notify =
5837 desktop_shell_client_destroy;
5838 wl_client_add_destroy_listener(shell->child.client,
5839 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005840}
5841
5842static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005843handle_shell_client_destroy(struct wl_listener *listener, void *data)
5844{
5845 struct shell_client *sc =
5846 container_of(listener, struct shell_client, destroy_listener);
5847
5848 if (sc->ping_timer)
5849 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005850
5851 /* Since we're about to free shell_client, we remove it from the
5852 * head of the surface list so we don't use that freed list node
5853 * during surface clean up later on.
5854 */
5855 wl_list_remove(&sc->surface_list);
5856
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005857 free(sc);
5858}
5859
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005860static struct shell_client *
5861shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5862 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005863{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005864 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005865
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005866 sc = zalloc(sizeof *sc);
5867 if (sc == NULL) {
5868 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005869 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005870 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005871
5872 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005873 if (sc->resource == NULL) {
5874 free(sc);
5875 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005876 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005877 }
5878
5879 sc->client = client;
5880 sc->shell = shell;
5881 sc->destroy_listener.notify = handle_shell_client_destroy;
5882 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005883 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005884
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005885 return sc;
5886}
5887
5888static void
5889bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5890{
5891 struct desktop_shell *shell = data;
5892 struct shell_client *sc;
5893
5894 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5895 if (sc)
5896 wl_resource_set_implementation(sc->resource,
5897 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005898 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005899}
5900
5901static void
5902bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5903{
5904 struct desktop_shell *shell = data;
5905 struct shell_client *sc;
5906
5907 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5908 if (sc)
5909 wl_resource_set_dispatcher(sc->resource,
5910 xdg_shell_unversioned_dispatch,
5911 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005912}
5913
5914static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005915unbind_desktop_shell(struct wl_resource *resource)
5916{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005917 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005918
5919 if (shell->locked)
5920 resume_desktop(shell);
5921
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005922 shell->child.desktop_shell = NULL;
5923 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005924}
5925
5926static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005927bind_desktop_shell(struct wl_client *client,
5928 void *data, uint32_t version, uint32_t id)
5929{
Tiago Vignattibe143262012-04-16 17:31:41 +03005930 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005931 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005932
Jason Ekstranda85118c2013-06-27 20:17:02 -05005933 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005934 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005935
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005936 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005937 wl_resource_set_implementation(resource,
5938 &desktop_shell_implementation,
5939 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005940 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005941
5942 if (version < 2)
5943 shell_fade_startup(shell);
5944
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005945 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005946 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005947
5948 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5949 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005950}
5951
Kristian Høgsberg07045392012-02-19 18:52:44 -05005952struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005953 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005954 struct weston_surface *current;
5955 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005956 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005957 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005958};
5959
5960static void
5961switcher_next(struct switcher *switcher)
5962{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005963 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005964 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005965 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005966 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005967
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005968 /* temporary re-display minimized surfaces */
5969 struct weston_view *tmp;
5970 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005971 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5972 weston_layer_entry_remove(&view->layer_link);
5973 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005974 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5975 *minimized = view;
5976 }
5977
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005978 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005979 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005980 if (shsurf &&
5981 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5982 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005983 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005984 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005985 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005986 next = view->surface;
5987 prev = view->surface;
5988 view->alpha = 0.25;
5989 weston_view_geometry_dirty(view);
5990 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005991 }
Alex Wu1659daa2012-04-01 20:13:09 +08005992
Jason Ekstranda7af7042013-10-12 22:38:11 -05005993 if (is_black_surface(view->surface, NULL)) {
5994 view->alpha = 0.25;
5995 weston_view_geometry_dirty(view);
5996 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005997 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005998 }
5999
6000 if (next == NULL)
6001 next = first;
6002
Alex Wu07b26062012-03-12 16:06:01 +08006003 if (next == NULL)
6004 return;
6005
Kristian Høgsberg07045392012-02-19 18:52:44 -05006006 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006007 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006008
6009 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006010 wl_list_for_each(view, &next->views, surface_link)
6011 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006012
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006013 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006014 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006015 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006016}
6017
6018static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006019switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006020{
6021 struct switcher *switcher =
6022 container_of(listener, struct switcher, listener);
6023
6024 switcher_next(switcher);
6025}
6026
6027static void
Daniel Stone351eb612012-05-31 15:27:47 -04006028switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006029{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006030 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006031 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006032 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006033
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006034 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006035 if (is_focus_view(view))
6036 continue;
6037
Jason Ekstranda7af7042013-10-12 22:38:11 -05006038 view->alpha = 1.0;
6039 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006040 }
6041
Alex Wu07b26062012-03-12 16:06:01 +08006042 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006043 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006044 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006045 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006046 weston_keyboard_end_grab(keyboard);
6047 if (keyboard->input_method_resource)
6048 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006049
6050 /* re-hide surfaces that were temporary shown during the switch */
6051 struct weston_view **minimized;
6052 wl_array_for_each(minimized, &switcher->minimized_array) {
6053 /* with the exception of the current selected */
6054 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006055 weston_layer_entry_remove(&(*minimized)->layer_link);
6056 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006057 weston_view_damage_below(*minimized);
6058 }
6059 }
6060 wl_array_release(&switcher->minimized_array);
6061
Kristian Høgsberg07045392012-02-19 18:52:44 -05006062 free(switcher);
6063}
6064
6065static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006066switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006067 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006068{
6069 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006070 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006071
Daniel Stonec9785ea2012-05-30 16:31:52 +01006072 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006073 switcher_next(switcher);
6074}
6075
6076static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006077switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006078 uint32_t mods_depressed, uint32_t mods_latched,
6079 uint32_t mods_locked, uint32_t group)
6080{
6081 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006082 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006083
Daniel Stone351eb612012-05-31 15:27:47 -04006084 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6085 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006086}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006087
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006088static void
6089switcher_cancel(struct weston_keyboard_grab *grab)
6090{
6091 struct switcher *switcher = container_of(grab, struct switcher, grab);
6092
6093 switcher_destroy(switcher);
6094}
6095
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006096static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006097 switcher_key,
6098 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006099 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006100};
6101
6102static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006103switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6104 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006105{
Tiago Vignattibe143262012-04-16 17:31:41 +03006106 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006107 struct switcher *switcher;
6108
6109 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006110 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006111 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006112 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006113 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006114 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006115
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006116 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006117 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006118 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006119 weston_keyboard_start_grab(keyboard, &switcher->grab);
6120 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006121 switcher_next(switcher);
6122}
6123
Pekka Paalanen3c647232011-12-22 13:43:43 +02006124static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006125backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6126 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006127{
6128 struct weston_compositor *compositor = data;
6129 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006130 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006131
6132 /* TODO: we're limiting to simple use cases, where we assume just
6133 * control on the primary display. We'd have to extend later if we
6134 * ever get support for setting backlights on random desktop LCD
6135 * panels though */
6136 output = get_default_output(compositor);
6137 if (!output)
6138 return;
6139
6140 if (!output->set_backlight)
6141 return;
6142
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006143 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6144 backlight_new = output->backlight_current - 25;
6145 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6146 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006147
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006148 if (backlight_new < 5)
6149 backlight_new = 5;
6150 if (backlight_new > 255)
6151 backlight_new = 255;
6152
6153 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006154 output->set_backlight(output, output->backlight_current);
6155}
6156
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006157static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006158force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6159 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006160{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006161 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006162 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006163 struct desktop_shell *shell = data;
6164 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006165 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006166
Derek Foreman8ae2db52015-07-15 13:00:36 -05006167 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006168 if (!focus_surface)
6169 return;
6170
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006171 wl_signal_emit(&compositor->kill_signal, focus_surface);
6172
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006173 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006174 wl_client_get_credentials(client, &pid, NULL, NULL);
6175
6176 /* Skip clients that we launched ourselves (the credentials of
6177 * the socketpair is ours) */
6178 if (pid == getpid())
6179 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006180
Daniel Stone325fc2d2012-05-30 16:31:58 +01006181 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006182}
6183
6184static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006185workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006186 uint32_t key, void *data)
6187{
6188 struct desktop_shell *shell = data;
6189 unsigned int new_index = shell->workspaces.current;
6190
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006191 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006192 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006193 if (new_index != 0)
6194 new_index--;
6195
6196 change_workspace(shell, new_index);
6197}
6198
6199static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006200workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006201 uint32_t key, void *data)
6202{
6203 struct desktop_shell *shell = data;
6204 unsigned int new_index = shell->workspaces.current;
6205
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006206 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006207 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006208 if (new_index < shell->workspaces.num - 1)
6209 new_index++;
6210
6211 change_workspace(shell, new_index);
6212}
6213
6214static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006215workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006216 uint32_t key, void *data)
6217{
6218 struct desktop_shell *shell = data;
6219 unsigned int new_index;
6220
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006221 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006222 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006223 new_index = key - KEY_F1;
6224 if (new_index >= shell->workspaces.num)
6225 new_index = shell->workspaces.num - 1;
6226
6227 change_workspace(shell, new_index);
6228}
6229
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006230static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006231workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6232 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006233{
6234 struct desktop_shell *shell = data;
6235 unsigned int new_index = shell->workspaces.current;
6236
6237 if (shell->locked)
6238 return;
6239
6240 if (new_index != 0)
6241 new_index--;
6242
Derek Foreman8ae2db52015-07-15 13:00:36 -05006243 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006244}
6245
6246static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006247workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6248 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006249{
6250 struct desktop_shell *shell = data;
6251 unsigned int new_index = shell->workspaces.current;
6252
6253 if (shell->locked)
6254 return;
6255
6256 if (new_index < shell->workspaces.num - 1)
6257 new_index++;
6258
Derek Foreman8ae2db52015-07-15 13:00:36 -05006259 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006260}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006261
6262static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006263shell_reposition_view_on_output_destroy(struct weston_view *view)
6264{
6265 struct weston_output *output, *first_output;
6266 struct weston_compositor *ec = view->surface->compositor;
6267 struct shell_surface *shsurf;
6268 float x, y;
6269 int visible;
6270
6271 x = view->geometry.x;
6272 y = view->geometry.y;
6273
6274 /* At this point the destroyed output is not in the list anymore.
6275 * If the view is still visible somewhere, we leave where it is,
6276 * otherwise, move it to the first output. */
6277 visible = 0;
6278 wl_list_for_each(output, &ec->output_list, link) {
6279 if (pixman_region32_contains_point(&output->region,
6280 x, y, NULL)) {
6281 visible = 1;
6282 break;
6283 }
6284 }
6285
6286 if (!visible) {
6287 first_output = container_of(ec->output_list.next,
6288 struct weston_output, link);
6289
6290 x = first_output->x + first_output->width / 4;
6291 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006292
6293 weston_view_set_position(view, x, y);
6294 } else {
6295 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006296 }
6297
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006298
6299 shsurf = get_shell_surface(view->surface);
6300
6301 if (shsurf) {
6302 shsurf->saved_position_valid = false;
6303 shsurf->next_state.maximized = false;
6304 shsurf->next_state.fullscreen = false;
6305 shsurf->state_changed = true;
6306 }
6307}
6308
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006309void
6310shell_for_each_layer(struct desktop_shell *shell,
6311 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006312{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006313 struct workspace **ws;
6314
6315 func(shell, &shell->fullscreen_layer, data);
6316 func(shell, &shell->panel_layer, data);
6317 func(shell, &shell->background_layer, data);
6318 func(shell, &shell->lock_layer, data);
6319 func(shell, &shell->input_panel_layer, data);
6320
6321 wl_array_for_each(ws, &shell->workspaces.array)
6322 func(shell, &(*ws)->layer, data);
6323}
6324
6325static void
6326shell_output_destroy_move_layer(struct desktop_shell *shell,
6327 struct weston_layer *layer,
6328 void *data)
6329{
6330 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006331 struct weston_view *view;
6332
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006333 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006334 if (view->output != output)
6335 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006336
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006337 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006338 }
6339}
6340
6341static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006342handle_output_destroy(struct wl_listener *listener, void *data)
6343{
6344 struct shell_output *output_listener =
6345 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006346 struct weston_output *output = output_listener->output;
6347 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006348
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006349 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006350
Xiong Zhang6b481422013-10-23 13:58:32 +08006351 wl_list_remove(&output_listener->destroy_listener.link);
6352 wl_list_remove(&output_listener->link);
6353 free(output_listener);
6354}
6355
6356static void
6357create_shell_output(struct desktop_shell *shell,
6358 struct weston_output *output)
6359{
6360 struct shell_output *shell_output;
6361
6362 shell_output = zalloc(sizeof *shell_output);
6363 if (shell_output == NULL)
6364 return;
6365
6366 shell_output->output = output;
6367 shell_output->shell = shell;
6368 shell_output->destroy_listener.notify = handle_output_destroy;
6369 wl_signal_add(&output->destroy_signal,
6370 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006371 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006372}
6373
6374static void
6375handle_output_create(struct wl_listener *listener, void *data)
6376{
6377 struct desktop_shell *shell =
6378 container_of(listener, struct desktop_shell, output_create_listener);
6379 struct weston_output *output = (struct weston_output *)data;
6380
6381 create_shell_output(shell, output);
6382}
6383
6384static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006385handle_output_move_layer(struct desktop_shell *shell,
6386 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006387{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006388 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006389 struct weston_view *view;
6390 float x, y;
6391
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006392 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006393 if (view->output != output)
6394 continue;
6395
6396 x = view->geometry.x + output->move_x;
6397 y = view->geometry.y + output->move_y;
6398 weston_view_set_position(view, x, y);
6399 }
6400}
6401
6402static void
6403handle_output_move(struct wl_listener *listener, void *data)
6404{
6405 struct desktop_shell *shell;
6406
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006407 shell = container_of(listener, struct desktop_shell,
6408 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006409
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006410 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006411}
6412
6413static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006414setup_output_destroy_handler(struct weston_compositor *ec,
6415 struct desktop_shell *shell)
6416{
6417 struct weston_output *output;
6418
6419 wl_list_init(&shell->output_list);
6420 wl_list_for_each(output, &ec->output_list, link)
6421 create_shell_output(shell, output);
6422
6423 shell->output_create_listener.notify = handle_output_create;
6424 wl_signal_add(&ec->output_created_signal,
6425 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006426
6427 shell->output_move_listener.notify = handle_output_move;
6428 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006429}
6430
6431static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006432shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006433{
Tiago Vignattibe143262012-04-16 17:31:41 +03006434 struct desktop_shell *shell =
6435 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006436 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006437 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006438
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006439 /* Force state to unlocked so we don't try to fade */
6440 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006441
6442 if (shell->child.client) {
6443 /* disable respawn */
6444 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006445 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006446 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006447
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006448 wl_list_remove(&shell->idle_listener.link);
6449 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006450
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006451 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006452 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006453
Xiong Zhang6b481422013-10-23 13:58:32 +08006454 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6455 wl_list_remove(&shell_output->destroy_listener.link);
6456 wl_list_remove(&shell_output->link);
6457 free(shell_output);
6458 }
6459
6460 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006461 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006462
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006463 wl_array_for_each(ws, &shell->workspaces.array)
6464 workspace_destroy(*ws);
6465 wl_array_release(&shell->workspaces.array);
6466
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006467 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006468 free(shell);
6469}
6470
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006471static void
6472shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6473{
6474 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006475 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006476
6477 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006478 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6479 MODIFIER_CTRL | MODIFIER_ALT,
6480 terminate_binding, ec);
6481 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6482 click_to_activate_binding,
6483 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006484 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6485 click_to_activate_binding,
6486 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006487 weston_compositor_add_touch_binding(ec, 0,
6488 touch_to_activate_binding,
6489 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006490 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6491 MODIFIER_SUPER | MODIFIER_ALT,
6492 surface_opacity_binding, NULL);
6493 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6494 MODIFIER_SUPER, zoom_axis_binding,
6495 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006496
6497 /* configurable bindings */
6498 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006499 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6500 zoom_key_binding, NULL);
6501 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6502 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006503 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6504 maximize_binding, NULL);
6505 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6506 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006507 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6508 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006509 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006510 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006511 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006512 weston_compositor_add_button_binding(ec, BTN_LEFT,
6513 mod | MODIFIER_SHIFT,
6514 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006515
6516 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006517 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006518 rotate_binding, NULL);
6519
Daniel Stone325fc2d2012-05-30 16:31:58 +01006520 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6521 shell);
6522 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6523 ec);
6524 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6525 backlight_binding, ec);
6526 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6527 ec);
6528 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6529 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006530 weston_compositor_add_key_binding(ec, KEY_K, mod,
6531 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006532 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6533 workspace_up_binding, shell);
6534 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6535 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006536 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6537 workspace_move_surface_up_binding,
6538 shell);
6539 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6540 workspace_move_surface_down_binding,
6541 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006542
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006543 if (shell->exposay_modifier)
6544 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6545 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006546
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006547 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6548 if (shell->workspaces.num > 1) {
6549 num_workspace_bindings = shell->workspaces.num;
6550 if (num_workspace_bindings > 6)
6551 num_workspace_bindings = 6;
6552 for (i = 0; i < num_workspace_bindings; i++)
6553 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6554 workspace_f_binding,
6555 shell);
6556 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006557
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006558 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006559}
6560
Jason Ekstrand024177c2014-04-21 19:42:58 -05006561static void
6562handle_seat_created(struct wl_listener *listener, void *data)
6563{
6564 struct weston_seat *seat = data;
6565
6566 create_shell_seat(seat);
6567}
6568
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006569WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006570module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006571 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006572{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006573 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006574 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006575 struct workspace **pws;
6576 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006577 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006578
Peter Huttererf3d62272013-08-08 11:57:05 +10006579 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006580 if (shell == NULL)
6581 return -1;
6582
Kristian Høgsberg75840622011-09-06 13:48:16 -04006583 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006584
6585 shell->destroy_listener.notify = shell_destroy;
6586 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006587 shell->idle_listener.notify = idle_handler;
6588 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6589 shell->wake_listener.notify = wake_handler;
6590 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006591
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006592 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006593 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006594 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006595 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006596 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006597 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006598 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006599 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006600 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006601 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006602 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006603 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006604 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006605
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006606 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6607 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006608 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6609 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006610 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006611
6612 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006613 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006614
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006615 if (input_panel_setup(shell) < 0)
6616 return -1;
6617
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006618 shell->text_backend = text_backend_init(ec);
6619 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006620 return -1;
6621
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006622 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006623
Daniel Stonedf8133b2013-11-19 11:37:14 +01006624 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6625 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6626
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006627 for (i = 0; i < shell->workspaces.num; i++) {
6628 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6629 if (pws == NULL)
6630 return -1;
6631
6632 *pws = workspace_create();
6633 if (*pws == NULL)
6634 return -1;
6635 }
6636 activate_workspace(shell, 0);
6637
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006638 weston_layer_init(&shell->minimized_layer, NULL);
6639
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006640 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006641 wl_list_init(&shell->workspaces.animation.link);
6642 shell->workspaces.animation.frame = animate_workspace_change_frame;
6643
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006644 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006645 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006646 return -1;
6647
Rafael Antognollie2a34552013-12-03 15:35:45 -02006648 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6649 shell, bind_xdg_shell) == NULL)
6650 return -1;
6651
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006652 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006653 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006654 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006655 return -1;
6656
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006657 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6658 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006659 return -1;
6660
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006661 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006662
Jonny Lamb765760d2014-08-20 15:53:19 +02006663 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6664
Xiong Zhang6b481422013-10-23 13:58:32 +08006665 setup_output_destroy_handler(ec, shell);
6666
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006667 loop = wl_display_get_event_loop(ec->wl_display);
6668 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006669
Jason Ekstrand024177c2014-04-21 19:42:58 -05006670 wl_list_for_each(seat, &ec->seat_list, link)
6671 handle_seat_created(NULL, seat);
6672 shell->seat_create_listener.notify = handle_seat_created;
6673 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006674
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006675 screenshooter_create(ec);
6676
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006677 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006678
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006679 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006680
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006681 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6682
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006683 return 0;
6684}