blob: 37936140bc62172fbd9fc9bbae9ff176fc7c3b9f [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"
Rafael Antognollie2a34552013-12-03 15:35:45 -020042#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010047#ifndef static_assert
48#define static_assert(cond, msg)
49#endif
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 struct weston_surface *keyboard_focus;
55 struct wl_list link;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
58};
59
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050060enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020061 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050062 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030063 SHELL_SURFACE_POPUP,
64 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020065};
66
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050067struct shell_client;
68
Philip Withnall648a4dd2013-11-25 18:01:44 +000069/*
70 * Surface stacking and ordering.
71 *
72 * This is handled using several linked lists of surfaces, organised into
73 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
74 * above all of the surfaces in the layer below. The set of layers is static and
75 * in the following order (top-most first):
76 * • Lock layer (only ever displayed on its own)
77 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010078 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000079 * • Fullscreen layer
80 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000081 * • Workspace layers
82 * • Background layer
83 *
84 * The list of layers may be manipulated to remove whole layers of surfaces from
85 * display. For example, when locking the screen, all layers except the lock
86 * layer are removed.
87 *
88 * A surface’s layer is modified on configuring the surface, in
89 * set_surface_type() (which is only called when the surface’s type change is
90 * _committed_). If a surface’s type changes (e.g. when making a window
91 * fullscreen) its layer changes too.
92 *
93 * In order to allow popup and transient surfaces to be correctly stacked above
94 * their parent surfaces, each surface tracks both its parent surface, and a
95 * linked list of its children. When a surface’s layer is updated, so are the
96 * layers of its children. Note that child surfaces are *not* the same as
97 * subsurfaces — child/parent surfaces are purely for maintaining stacking
98 * order.
99 *
100 * The children_link list of siblings of a surface (i.e. those surfaces which
101 * have the same parent) only contains weston_surfaces which have a
102 * shell_surface. Stacking is not implemented for non-shell_surface
103 * weston_surfaces. This means that the following implication does *not* hold:
104 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
105 */
106
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200107struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500108 struct wl_resource *resource;
109 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500110 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800111 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200112
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500114 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600115 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200116 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700117 struct wl_listener resource_destroy_listener;
118
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400119 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000120 struct wl_list children_list; /* child surfaces of this one */
121 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300122 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200123
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800124 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400125 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500126 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800128 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200129 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800130 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700131 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800132 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100133
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500136 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200137 } rotation;
138
139 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100142 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400143 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500144 } popup;
145
Alex Wu4539b082012-03-01 12:57:46 +0800146 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400148 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300149 } transient;
150
151 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800152 enum wl_shell_surface_fullscreen_method type;
153 struct weston_transform transform; /* matrix from x, y */
154 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500155 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800156 } fullscreen;
157
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200158 struct weston_transform workspace_transform;
159
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500160 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500161 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100162 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400163
164 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200165
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700166 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200167 bool maximized;
168 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200169 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100170 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200172 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500173 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500174
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700175 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400176 int32_t x, y, width, height;
177 } geometry, next_geometry;
178 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700179
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500180 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500181
182 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200183};
184
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300185struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400186 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300187 struct shell_surface *shsurf;
188 struct wl_listener shsurf_destroy_listener;
189};
190
Rusty Lynch1084da52013-08-15 09:10:08 -0700191struct shell_touch_grab {
192 struct weston_touch_grab grab;
193 struct shell_surface *shsurf;
194 struct wl_listener shsurf_destroy_listener;
195 struct weston_touch *touch;
196};
197
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198struct weston_move_grab {
199 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100200 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700201 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500202};
203
Rusty Lynch1084da52013-08-15 09:10:08 -0700204struct weston_touch_move_grab {
205 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800206 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700207 wl_fixed_t dx, dy;
208};
209
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300211 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500212 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200214 float x;
215 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200216 } center;
217};
218
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219struct shell_seat {
220 struct weston_seat *seat;
221 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500222 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100223
Jason Ekstrand024177c2014-04-21 19:42:58 -0500224 struct wl_listener caps_changed_listener;
225 struct wl_listener pointer_focus_listener;
226 struct wl_listener keyboard_focus_listener;
227
Giulio Camuffo5085a752013-03-25 21:42:45 +0100228 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400229 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200230 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100231 struct wl_list surfaces_list;
232 struct wl_client *client;
233 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200234 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100235 } popup_grab;
236};
237
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800238struct shell_client {
239 struct wl_resource *resource;
240 struct wl_client *client;
241 struct desktop_shell *shell;
242 struct wl_listener destroy_listener;
243 struct wl_event_source *ping_timer;
244 uint32_t ping_serial;
245 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800246 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800247};
248
Alex Wubd3354b2012-04-17 17:20:49 +0800249static struct desktop_shell *
250shell_surface_get_shell(struct shell_surface *shsurf);
251
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500252static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400253surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500254
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300255static void
256shell_fade_startup(struct desktop_shell *shell);
257
Philip Withnallbecb77e2013-11-25 18:01:30 +0000258static struct shell_seat *
259get_shell_seat(struct weston_seat *seat);
260
Jonny Lambd73c6942014-08-20 15:53:20 +0200261static void
262get_output_panel_size(struct desktop_shell *shell,
263 struct weston_output *output,
264 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700265
Philip Withnall648a4dd2013-11-25 18:01:44 +0000266static void
267shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
268
Alex Wubd3354b2012-04-17 17:20:49 +0800269static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200270shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
271
272static bool
273shell_surface_is_xdg_surface(struct shell_surface *shsurf);
274
275static bool
276shell_surface_is_xdg_popup(struct shell_surface *shsurf);
277
278static void
279shell_surface_set_parent(struct shell_surface *shsurf,
280 struct weston_surface *parent);
281
Pekka Paalanen8274d902014-08-06 19:36:51 +0300282static int
283shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
284{
285 struct shell_surface *shsurf;
286 const char *typestr[] = {
287 [SHELL_SURFACE_NONE] = "unidentified",
288 [SHELL_SURFACE_TOPLEVEL] = "top-level",
289 [SHELL_SURFACE_POPUP] = "popup",
290 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
291 };
292 const char *t, *c;
293
294 shsurf = get_shell_surface(surface);
295 if (!shsurf)
296 return snprintf(buf, len, "unidentified window");
297
298 t = shsurf->title;
299 c = shsurf->class;
300
301 return snprintf(buf, len, "%s window%s%s%s%s%s",
302 typestr[shsurf->type],
303 t ? " '" : "", t ?: "", t ? "'" : "",
304 c ? " of " : "", c ?: "");
305}
306
Rafael Antognollie2a34552013-12-03 15:35:45 -0200307static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800308shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
309{
310 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800312
313 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100314
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300315 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800316 return false;
317
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300318 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500319 struct weston_view,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300320 layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800322}
323
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500324static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400325destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300326{
327 struct shell_grab *grab;
328
329 grab = container_of(listener, struct shell_grab,
330 shsurf_destroy_listener);
331
332 grab->shsurf = NULL;
333}
334
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800335struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336get_default_view(struct weston_surface *surface)
337{
338 struct shell_surface *shsurf;
339 struct weston_view *view;
340
341 if (!surface || wl_list_empty(&surface->views))
342 return NULL;
343
344 shsurf = get_shell_surface(surface);
345 if (shsurf)
346 return shsurf->view;
347
348 wl_list_for_each(view, &surface->views, surface_link)
349 if (weston_view_is_mapped(view))
350 return view;
351
352 return container_of(surface->views.next, struct weston_view, surface_link);
353}
354
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300355static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400356popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200357static void
358touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400359
360static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300361shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400362 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300363 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400364 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300365 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300366{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300367 struct desktop_shell *shell = shsurf->shell;
368
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400369 popup_grab_end(pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200370 if (pointer->seat->touch)
371 touch_popup_grab_end(pointer->seat->touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400372
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300373 grab->grab.interface = interface;
374 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400375 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500376 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400377 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700379 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400380 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400381 if (shell->child.desktop_shell) {
382 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
383 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500384 weston_pointer_set_focus(pointer,
385 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400386 wl_fixed_from_int(0),
387 wl_fixed_from_int(0));
388 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300389}
390
Jonny Lambd73c6942014-08-20 15:53:20 +0200391static void
392get_output_panel_size(struct desktop_shell *shell,
393 struct weston_output *output,
394 int *width,
395 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700396{
397 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200398
399 *width = 0;
400 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700401
402 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200403 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700404
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300405 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200406 float x, y;
407
408 if (view->surface->output != output)
409 continue;
410
411 switch (shell->panel_position) {
412 case DESKTOP_SHELL_PANEL_POSITION_TOP:
413 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
414
415 weston_view_to_global_float(view,
416 view->surface->width, 0,
417 &x, &y);
418
419 *width = (int) x;
420 *height = view->surface->height + (int) y;
421 return;
422
423 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
424 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
425 weston_view_to_global_float(view,
426 0, view->surface->height,
427 &x, &y);
428
429 *width = view->surface->width + (int) x;
430 *height = (int) y;
431 return;
432
433 default:
434 /* we've already set width and height to
435 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700436 break;
437 }
438 }
439
Jonny Lambd73c6942014-08-20 15:53:20 +0200440 /* the correct view wasn't found */
441}
442
443static void
444get_output_work_area(struct desktop_shell *shell,
445 struct weston_output *output,
446 pixman_rectangle32_t *area)
447{
448 int32_t panel_width = 0, panel_height = 0;
449
450 area->x = 0;
451 area->y = 0;
452
453 get_output_panel_size(shell, output, &panel_width, &panel_height);
454
455 switch (shell->panel_position) {
456 case DESKTOP_SHELL_PANEL_POSITION_TOP:
457 default:
458 area->y = panel_height;
459 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
460 area->width = output->width;
461 area->height = output->height - panel_height;
462 break;
463 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
464 area->x = panel_width;
465 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
466 area->width = output->width - panel_width;
467 area->height = output->height;
468 break;
469 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700470}
471
472static void
473send_configure_for_surface(struct shell_surface *shsurf)
474{
475 int32_t width, height;
476 struct surface_state *state;
477
478 if (shsurf->state_requested)
479 state = &shsurf->requested_state;
480 else if (shsurf->state_changed)
481 state = &shsurf->next_state;
482 else
483 state = &shsurf->state;
484
485 if (state->fullscreen) {
486 width = shsurf->output->width;
487 height = shsurf->output->height;
488 } else if (state->maximized) {
489 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200490 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700491
492 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200493 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700494
Jonny Lambd73c6942014-08-20 15:53:20 +0200495 width = area.width;
496 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900497 } else if (shsurf->resize_edges) {
498 width = shsurf->geometry.width;
499 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700500 } else {
501 width = 0;
502 height = 0;
503 }
504
505 shsurf->client->send_configure(shsurf->surface, width, height);
506}
507
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300508static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400509shell_surface_state_changed(struct shell_surface *shsurf)
510{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700511 if (shell_surface_is_xdg_surface(shsurf))
512 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400513}
514
515static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300516shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300517{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700518 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400519 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700520 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400521
522 if (grab->shsurf->resize_edges) {
523 grab->shsurf->resize_edges = 0;
524 shell_surface_state_changed(grab->shsurf);
525 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700526 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300527
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700528 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300529}
530
531static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700532shell_touch_grab_start(struct shell_touch_grab *grab,
533 const struct weston_touch_grab_interface *interface,
534 struct shell_surface *shsurf,
535 struct weston_touch *touch)
536{
537 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800538
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200539 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700540 if (touch->seat->pointer)
541 popup_grab_end(touch->seat->pointer);
542
Rusty Lynch1084da52013-08-15 09:10:08 -0700543 grab->grab.interface = interface;
544 grab->shsurf = shsurf;
545 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
546 wl_signal_add(&shsurf->destroy_signal,
547 &grab->shsurf_destroy_listener);
548
549 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700550 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700551
552 weston_touch_start_grab(touch, &grab->grab);
553 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500554 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700556}
557
558static void
559shell_touch_grab_end(struct shell_touch_grab *grab)
560{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700561 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700562 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700563 grab->shsurf->grabbed = 0;
564 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700565
566 weston_touch_end_grab(grab->touch);
567}
568
569static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500570center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800571 struct weston_output *output);
572
Daniel Stone496ca172012-05-30 16:31:42 +0100573static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300574get_modifier(char *modifier)
575{
576 if (!modifier)
577 return MODIFIER_SUPER;
578
579 if (!strcmp("ctrl", modifier))
580 return MODIFIER_CTRL;
581 else if (!strcmp("alt", modifier))
582 return MODIFIER_ALT;
583 else if (!strcmp("super", modifier))
584 return MODIFIER_SUPER;
585 else
586 return MODIFIER_SUPER;
587}
588
Juan Zhaoe10d2792012-04-25 19:09:52 +0800589static enum animation_type
590get_animation_type(char *animation)
591{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800592 if (!animation)
593 return ANIMATION_NONE;
594
Juan Zhaoe10d2792012-04-25 19:09:52 +0800595 if (!strcmp("zoom", animation))
596 return ANIMATION_ZOOM;
597 else if (!strcmp("fade", animation))
598 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100599 else if (!strcmp("dim-layer", animation))
600 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800601 else
602 return ANIMATION_NONE;
603}
604
Alex Wu4539b082012-03-01 12:57:46 +0800605static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400606shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200607{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400608 struct weston_config_section *section;
609 int duration;
Derek Foremanc7210432014-08-21 11:32:38 -0500610 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300611 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200612
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400613 section = weston_config_get_section(shell->compositor->config,
614 "screensaver", NULL, NULL);
615 weston_config_section_get_string(section,
616 "path", &shell->screensaver.path, NULL);
617 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200618 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400619
620 section = weston_config_get_section(shell->compositor->config,
621 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300622 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
623 WESTON_SHELL_CLIENT);
624 if (ret < 0)
625 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400626 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500627 "client", &s, client);
628 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100629 shell->client = s;
630 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400631 "binding-modifier", &s, "super");
632 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200633 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800634
635 weston_config_section_get_string(section,
636 "exposay-modifier", &s, "none");
637 if (strcmp(s, "none") == 0)
638 shell->exposay_modifier = 0;
639 else
640 shell->exposay_modifier = get_modifier(s);
641 free(s);
642
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400643 weston_config_section_get_string(section, "animation", &s, "none");
644 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200645 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200646 weston_config_section_get_string(section, "close-animation", &s, "fade");
647 shell->win_close_animation_type = get_animation_type(s);
648 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700649 weston_config_section_get_string(section,
650 "startup-animation", &s, "fade");
651 shell->startup_animation_type = get_animation_type(s);
652 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700653 if (shell->startup_animation_type == ANIMATION_ZOOM)
654 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100655 weston_config_section_get_string(section, "focus-animation", &s, "none");
656 shell->focus_animation_type = get_animation_type(s);
657 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400658 weston_config_section_get_uint(section, "num-workspaces",
659 &shell->workspaces.num,
660 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200661}
662
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800663struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100664get_default_output(struct weston_compositor *compositor)
665{
666 return container_of(compositor->output_list.next,
667 struct weston_output, link);
668}
669
Pekka Paalanen8274d902014-08-06 19:36:51 +0300670static int
671focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
672{
673 return snprintf(buf, len, "focus highlight effect for output %s",
674 surface->output->name);
675}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100676
677/* no-op func for checking focus surface */
678static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600679focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100680{
681}
682
683static struct focus_surface *
684get_focus_surface(struct weston_surface *surface)
685{
686 if (surface->configure == focus_surface_configure)
687 return surface->configure_private;
688 else
689 return NULL;
690}
691
692static bool
693is_focus_surface (struct weston_surface *es)
694{
695 return (es->configure == focus_surface_configure);
696}
697
698static bool
699is_focus_view (struct weston_view *view)
700{
701 return is_focus_surface (view->surface);
702}
703
704static struct focus_surface *
705create_focus_surface(struct weston_compositor *ec,
706 struct weston_output *output)
707{
708 struct focus_surface *fsurf = NULL;
709 struct weston_surface *surface = NULL;
710
711 fsurf = malloc(sizeof *fsurf);
712 if (!fsurf)
713 return NULL;
714
715 fsurf->surface = weston_surface_create(ec);
716 surface = fsurf->surface;
717 if (surface == NULL) {
718 free(fsurf);
719 return NULL;
720 }
721
722 surface->configure = focus_surface_configure;
723 surface->output = output;
724 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300725 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100726
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800727 fsurf->view = weston_view_create(surface);
728 if (fsurf->view == NULL) {
729 weston_surface_destroy(surface);
730 free(fsurf);
731 return NULL;
732 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100733 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100734
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600735 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600736 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100737 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
738 pixman_region32_fini(&surface->opaque);
739 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
740 output->width, output->height);
741 pixman_region32_fini(&surface->input);
742 pixman_region32_init(&surface->input);
743
744 wl_list_init(&fsurf->workspace_transform.link);
745
746 return fsurf;
747}
748
749static void
750focus_surface_destroy(struct focus_surface *fsurf)
751{
752 weston_surface_destroy(fsurf->surface);
753 free(fsurf);
754}
755
756static void
757focus_animation_done(struct weston_view_animation *animation, void *data)
758{
759 struct workspace *ws = data;
760
761 ws->focus_animation = NULL;
762}
763
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200764static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200765focus_state_destroy(struct focus_state *state)
766{
767 wl_list_remove(&state->seat_destroy_listener.link);
768 wl_list_remove(&state->surface_destroy_listener.link);
769 free(state);
770}
771
772static void
773focus_state_seat_destroy(struct wl_listener *listener, void *data)
774{
775 struct focus_state *state = container_of(listener,
776 struct focus_state,
777 seat_destroy_listener);
778
779 wl_list_remove(&state->link);
780 focus_state_destroy(state);
781}
782
783static void
784focus_state_surface_destroy(struct wl_listener *listener, void *data)
785{
786 struct focus_state *state = container_of(listener,
787 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400788 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400789 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500790 struct weston_surface *main_surface, *next;
791 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200792
Pekka Paalanen01388e22013-04-25 13:57:44 +0300793 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
794
Kristian Høgsberge3778222012-07-31 17:29:30 -0400795 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300796 wl_list_for_each(view,
797 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500798 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400799 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100800 if (is_focus_view(view))
801 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400802
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400804 break;
805 }
806
Pekka Paalanen01388e22013-04-25 13:57:44 +0300807 /* if the focus was a sub-surface, activate its main surface */
808 if (main_surface != state->keyboard_focus)
809 next = main_surface;
810
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100811 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400812 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100813 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100814 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400815 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100816 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
817 if (state->ws->focus_animation)
818 weston_view_animation_destroy(state->ws->focus_animation);
819
820 state->ws->focus_animation = weston_fade_run(
821 state->ws->fsurf_front->view,
822 state->ws->fsurf_front->view->alpha, 0.0, 300,
823 focus_animation_done, state->ws);
824 }
825
Kristian Høgsberge3778222012-07-31 17:29:30 -0400826 wl_list_remove(&state->link);
827 focus_state_destroy(state);
828 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200829}
830
831static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400832focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200833{
Jonas Ådahl04769742012-06-13 00:01:24 +0200834 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200835
836 state = malloc(sizeof *state);
837 if (state == NULL)
838 return NULL;
839
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100840 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400841 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200842 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400843 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200844
845 state->seat_destroy_listener.notify = focus_state_seat_destroy;
846 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400847 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200848 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400849 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200850
851 return state;
852}
853
Jonas Ådahl8538b222012-08-29 22:13:03 +0200854static struct focus_state *
855ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
856{
857 struct workspace *ws = get_current_workspace(shell);
858 struct focus_state *state;
859
860 wl_list_for_each(state, &ws->focus_list, link)
861 if (state->seat == seat)
862 break;
863
864 if (&state->link == &ws->focus_list)
865 state = focus_state_create(seat, ws);
866
867 return state;
868}
869
Jonas Ådahl04769742012-06-13 00:01:24 +0200870static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800871focus_state_set_focus(struct focus_state *state,
872 struct weston_surface *surface)
873{
874 if (state->keyboard_focus) {
875 wl_list_remove(&state->surface_destroy_listener.link);
876 wl_list_init(&state->surface_destroy_listener.link);
877 }
878
879 state->keyboard_focus = surface;
880 if (surface)
881 wl_signal_add(&surface->destroy_signal,
882 &state->surface_destroy_listener);
883}
884
885static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400886restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200887{
888 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400889 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100890 struct wl_list pending_seat_list;
891 struct weston_seat *seat, *next_seat;
892
893 /* Temporarily steal the list of seats so that we can keep
894 * track of the seats we've already processed */
895 wl_list_init(&pending_seat_list);
896 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
897 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200898
899 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100900 wl_list_remove(&state->seat->link);
901 wl_list_insert(&shell->compositor->seat_list,
902 &state->seat->link);
903
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800904 if (state->seat->keyboard == NULL)
905 continue;
906
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400907 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200908
Kristian Høgsberge3148752013-05-06 23:19:49 -0400909 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200910 }
Neil Roberts4237f502014-04-09 16:33:31 +0100911
912 /* For any remaining seats that we don't have a focus state
913 * for we'll reset the keyboard focus to NULL */
914 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
915 wl_list_insert(&shell->compositor->seat_list, &seat->link);
916
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300917 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100918 continue;
919
920 weston_keyboard_set_focus(seat->keyboard, NULL);
921 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200922}
923
924static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200925replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
926 struct weston_seat *seat)
927{
928 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929
930 wl_list_for_each(state, &ws->focus_list, link) {
931 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800932 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200933 return;
934 }
935 }
936}
937
938static void
939drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
940 struct weston_surface *surface)
941{
942 struct focus_state *state;
943
944 wl_list_for_each(state, &ws->focus_list, link)
945 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800946 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200947}
948
949static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100950animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
951 struct weston_view *from, struct weston_view *to)
952{
953 struct weston_output *output;
954 bool focus_surface_created = false;
955
956 /* FIXME: Only support dim animation using two layers */
957 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
958 return;
959
960 output = get_default_output(shell->compositor);
961 if (ws->fsurf_front == NULL && (from || to)) {
962 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800963 if (ws->fsurf_front == NULL)
964 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100965 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800966
967 ws->fsurf_back = create_focus_surface(shell->compositor, output);
968 if (ws->fsurf_back == NULL) {
969 focus_surface_destroy(ws->fsurf_front);
970 return;
971 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100972 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800973
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100974 focus_surface_created = true;
975 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300976 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
977 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978 }
979
980 if (ws->focus_animation) {
981 weston_view_animation_destroy(ws->focus_animation);
982 ws->focus_animation = NULL;
983 }
984
985 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300986 weston_layer_entry_insert(&to->layer_link,
987 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100988 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300989 weston_layer_entry_insert(&ws->layer.view_list,
990 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100991
992 if (focus_surface_created) {
993 ws->focus_animation = weston_fade_run(
994 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200995 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 focus_animation_done, ws);
997 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300998 weston_layer_entry_insert(&from->layer_link,
999 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 ws->focus_animation = weston_stable_fade_run(
1001 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001002 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 focus_animation_done, ws);
1004 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001005 weston_layer_entry_insert(&ws->layer.view_list,
1006 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001007 ws->focus_animation = weston_stable_fade_run(
1008 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001009 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001010 focus_animation_done, ws);
1011 }
1012}
1013
1014static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001015workspace_destroy(struct workspace *ws)
1016{
Jonas Ådahl04769742012-06-13 00:01:24 +02001017 struct focus_state *state, *next;
1018
1019 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1020 focus_state_destroy(state);
1021
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001022 if (ws->fsurf_front)
1023 focus_surface_destroy(ws->fsurf_front);
1024 if (ws->fsurf_back)
1025 focus_surface_destroy(ws->fsurf_back);
1026
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001027 free(ws);
1028}
1029
Jonas Ådahl04769742012-06-13 00:01:24 +02001030static void
1031seat_destroyed(struct wl_listener *listener, void *data)
1032{
1033 struct weston_seat *seat = data;
1034 struct focus_state *state, *next;
1035 struct workspace *ws = container_of(listener,
1036 struct workspace,
1037 seat_destroyed_listener);
1038
1039 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1040 if (state->seat == seat)
1041 wl_list_remove(&state->link);
1042}
1043
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001044static struct workspace *
1045workspace_create(void)
1046{
1047 struct workspace *ws = malloc(sizeof *ws);
1048 if (ws == NULL)
1049 return NULL;
1050
1051 weston_layer_init(&ws->layer, NULL);
1052
Jonas Ådahl04769742012-06-13 00:01:24 +02001053 wl_list_init(&ws->focus_list);
1054 wl_list_init(&ws->seat_destroyed_listener.link);
1055 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001056 ws->fsurf_front = NULL;
1057 ws->fsurf_back = NULL;
1058 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001059
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001060 return ws;
1061}
1062
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063static int
1064workspace_is_empty(struct workspace *ws)
1065{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001066 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067}
1068
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001069static struct workspace *
1070get_workspace(struct desktop_shell *shell, unsigned int index)
1071{
1072 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001073 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001074 pws += index;
1075 return *pws;
1076}
1077
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001078struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001079get_current_workspace(struct desktop_shell *shell)
1080{
1081 return get_workspace(shell, shell->workspaces.current);
1082}
1083
1084static void
1085activate_workspace(struct desktop_shell *shell, unsigned int index)
1086{
1087 struct workspace *ws;
1088
1089 ws = get_workspace(shell, index);
1090 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1091
1092 shell->workspaces.current = index;
1093}
1094
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095static unsigned int
1096get_output_height(struct weston_output *output)
1097{
1098 return abs(output->region.extents.y1 - output->region.extents.y2);
1099}
1100
1101static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001102view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001103{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001104 struct weston_transform *transform;
1105
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001106 if (is_focus_view(view)) {
1107 struct focus_surface *fsurf = get_focus_surface(view->surface);
1108 transform = &fsurf->workspace_transform;
1109 } else {
1110 struct shell_surface *shsurf = get_shell_surface(view->surface);
1111 transform = &shsurf->workspace_transform;
1112 }
1113
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001116 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001118 weston_matrix_init(&transform->matrix);
1119 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122}
1123
1124static void
1125workspace_translate_out(struct workspace *ws, double fraction)
1126{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 unsigned int height;
1129 double d;
1130
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001131 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 d = height * fraction;
1134
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001135 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136 }
1137}
1138
1139static void
1140workspace_translate_in(struct workspace *ws, double fraction)
1141{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 unsigned int height;
1144 double d;
1145
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001146 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148
1149 if (fraction > 0)
1150 d = -(height - height * fraction);
1151 else
1152 d = height + height * fraction;
1153
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001154 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155 }
1156}
1157
1158static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001159broadcast_current_workspace_state(struct desktop_shell *shell)
1160{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001161 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001162
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001163 wl_resource_for_each(resource, &shell->workspaces.client_list)
1164 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001165 shell->workspaces.current,
1166 shell->workspaces.num);
1167}
1168
1169static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001170reverse_workspace_change_animation(struct desktop_shell *shell,
1171 unsigned int index,
1172 struct workspace *from,
1173 struct workspace *to)
1174{
1175 shell->workspaces.current = index;
1176
1177 shell->workspaces.anim_to = to;
1178 shell->workspaces.anim_from = from;
1179 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1180 shell->workspaces.anim_timestamp = 0;
1181
Scott Moreau4272e632012-08-13 09:58:41 -06001182 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001183}
1184
1185static void
1186workspace_deactivate_transforms(struct workspace *ws)
1187{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001189 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001190
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001191 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001192 if (is_focus_view(view)) {
1193 struct focus_surface *fsurf = get_focus_surface(view->surface);
1194 transform = &fsurf->workspace_transform;
1195 } else {
1196 struct shell_surface *shsurf = get_shell_surface(view->surface);
1197 transform = &shsurf->workspace_transform;
1198 }
1199
1200 if (!wl_list_empty(&transform->link)) {
1201 wl_list_remove(&transform->link);
1202 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001203 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001205 }
1206}
1207
1208static void
1209finish_workspace_change_animation(struct desktop_shell *shell,
1210 struct workspace *from,
1211 struct workspace *to)
1212{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001213 struct weston_view *view;
1214
Scott Moreau4272e632012-08-13 09:58:41 -06001215 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001216
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001217 /* Views that extend past the bottom of the output are still
1218 * visible after the workspace animation ends but before its layer
1219 * is hidden. In that case, we need to damage below those views so
1220 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001221 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001222 weston_view_damage_below(view);
1223
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001224 wl_list_remove(&shell->workspaces.animation.link);
1225 workspace_deactivate_transforms(from);
1226 workspace_deactivate_transforms(to);
1227 shell->workspaces.anim_to = NULL;
1228
1229 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1230}
1231
1232static void
1233animate_workspace_change_frame(struct weston_animation *animation,
1234 struct weston_output *output, uint32_t msecs)
1235{
1236 struct desktop_shell *shell =
1237 container_of(animation, struct desktop_shell,
1238 workspaces.animation);
1239 struct workspace *from = shell->workspaces.anim_from;
1240 struct workspace *to = shell->workspaces.anim_to;
1241 uint32_t t;
1242 double x, y;
1243
1244 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1245 finish_workspace_change_animation(shell, from, to);
1246 return;
1247 }
1248
1249 if (shell->workspaces.anim_timestamp == 0) {
1250 if (shell->workspaces.anim_current == 0.0)
1251 shell->workspaces.anim_timestamp = msecs;
1252 else
1253 shell->workspaces.anim_timestamp =
1254 msecs -
1255 /* Invers of movement function 'y' below. */
1256 (asin(1.0 - shell->workspaces.anim_current) *
1257 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1258 M_2_PI);
1259 }
1260
1261 t = msecs - shell->workspaces.anim_timestamp;
1262
1263 /*
1264 * x = [0, π/2]
1265 * y(x) = sin(x)
1266 */
1267 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1268 y = sin(x);
1269
1270 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001271 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001272
1273 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1274 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1275 shell->workspaces.anim_current = y;
1276
Scott Moreau4272e632012-08-13 09:58:41 -06001277 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001278 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001279 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001280 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001281}
1282
1283static void
1284animate_workspace_change(struct desktop_shell *shell,
1285 unsigned int index,
1286 struct workspace *from,
1287 struct workspace *to)
1288{
1289 struct weston_output *output;
1290
1291 int dir;
1292
1293 if (index > shell->workspaces.current)
1294 dir = -1;
1295 else
1296 dir = 1;
1297
1298 shell->workspaces.current = index;
1299
1300 shell->workspaces.anim_dir = dir;
1301 shell->workspaces.anim_from = from;
1302 shell->workspaces.anim_to = to;
1303 shell->workspaces.anim_current = 0.0;
1304 shell->workspaces.anim_timestamp = 0;
1305
1306 output = container_of(shell->compositor->output_list.next,
1307 struct weston_output, link);
1308 wl_list_insert(&output->animation_list,
1309 &shell->workspaces.animation.link);
1310
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001311 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001312
1313 workspace_translate_in(to, 0);
1314
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001315 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001316
Scott Moreau4272e632012-08-13 09:58:41 -06001317 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001318}
1319
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001320static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001321update_workspace(struct desktop_shell *shell, unsigned int index,
1322 struct workspace *from, struct workspace *to)
1323{
1324 shell->workspaces.current = index;
1325 wl_list_insert(&from->layer.link, &to->layer.link);
1326 wl_list_remove(&from->layer.link);
1327}
1328
1329static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001330change_workspace(struct desktop_shell *shell, unsigned int index)
1331{
1332 struct workspace *from;
1333 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001334 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001335
1336 if (index == shell->workspaces.current)
1337 return;
1338
1339 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001340 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001341 return;
1342
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001343 from = get_current_workspace(shell);
1344 to = get_workspace(shell, index);
1345
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001346 if (shell->workspaces.anim_from == to &&
1347 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001348 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001349 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001350 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001351 return;
1352 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001353
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001354 if (shell->workspaces.anim_to != NULL)
1355 finish_workspace_change_animation(shell,
1356 shell->workspaces.anim_from,
1357 shell->workspaces.anim_to);
1358
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001359 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001360
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001361 if (shell->focus_animation_type != ANIMATION_NONE) {
1362 wl_list_for_each(state, &from->focus_list, link)
1363 if (state->keyboard_focus)
1364 animate_focus_change(shell, from,
1365 get_default_view(state->keyboard_focus), NULL);
1366
1367 wl_list_for_each(state, &to->focus_list, link)
1368 if (state->keyboard_focus)
1369 animate_focus_change(shell, to,
1370 NULL, get_default_view(state->keyboard_focus));
1371 }
1372
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001373 if (workspace_is_empty(to) && workspace_is_empty(from))
1374 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001375 else
1376 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001377
1378 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001379}
1380
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001381static bool
1382workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1383{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001384 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001385 struct wl_list *e;
1386
1387 if (wl_list_empty(list))
1388 return false;
1389
1390 e = list->next;
1391
1392 if (e->next != list)
1393 return false;
1394
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001395 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001396}
1397
1398static void
Philip Withnall659163d2013-11-25 18:01:36 +00001399move_surface_to_workspace(struct desktop_shell *shell,
1400 struct shell_surface *shsurf,
1401 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001402{
1403 struct workspace *from;
1404 struct workspace *to;
1405 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001406 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001407 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001408
1409 if (workspace == shell->workspaces.current)
1410 return;
1411
Philip Withnall659163d2013-11-25 18:01:36 +00001412 view = get_default_view(shsurf->surface);
1413 if (!view)
1414 return;
1415
1416 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1417
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001418 if (workspace >= shell->workspaces.num)
1419 workspace = shell->workspaces.num - 1;
1420
Jonas Ådahle9d22502012-08-29 22:13:01 +02001421 from = get_current_workspace(shell);
1422 to = get_workspace(shell, workspace);
1423
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001424 weston_layer_entry_remove(&view->layer_link);
1425 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001426
Philip Withnall648a4dd2013-11-25 18:01:44 +00001427 shell_surface_update_child_surface_layers(shsurf);
1428
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001430 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1431 if (!seat->keyboard)
1432 continue;
1433
1434 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001436 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001437 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001438
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001440}
1441
1442static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001443take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001444 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001445 unsigned int index)
1446{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001447 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001449 struct shell_surface *shsurf;
1450 struct workspace *from;
1451 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001452 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001453
Pekka Paalanen01388e22013-04-25 13:57:44 +03001454 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 view = get_default_view(surface);
1456 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001457 index == shell->workspaces.current ||
1458 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001459 return;
1460
1461 from = get_current_workspace(shell);
1462 to = get_workspace(shell, index);
1463
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001464 weston_layer_entry_remove(&view->layer_link);
1465 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001466
Philip Withnall659163d2013-11-25 18:01:36 +00001467 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001468 if (shsurf != NULL)
1469 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001470
Jonas Ådahle9d22502012-08-29 22:13:01 +02001471 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001472 drop_focus_state(shell, from, surface);
1473
1474 if (shell->workspaces.anim_from == to &&
1475 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001476 wl_list_remove(&to->layer.link);
1477 wl_list_insert(from->layer.link.prev, &to->layer.link);
1478
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001479 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001480 broadcast_current_workspace_state(shell);
1481
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001482 return;
1483 }
1484
1485 if (shell->workspaces.anim_to != NULL)
1486 finish_workspace_change_animation(shell,
1487 shell->workspaces.anim_from,
1488 shell->workspaces.anim_to);
1489
1490 if (workspace_is_empty(from) &&
1491 workspace_has_only(to, surface))
1492 update_workspace(shell, index, from, to);
1493 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001494 if (shsurf != NULL &&
1495 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001496 wl_list_insert(&shell->workspaces.anim_sticky_list,
1497 &shsurf->workspace_transform.link);
1498
1499 animate_workspace_change(shell, index, from, to);
1500 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001501
1502 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001503
1504 state = ensure_focus_state(shell, seat);
1505 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001506 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001507}
1508
1509static void
1510workspace_manager_move_surface(struct wl_client *client,
1511 struct wl_resource *resource,
1512 struct wl_resource *surface_resource,
1513 uint32_t workspace)
1514{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001515 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001516 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001517 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001518 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001519 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001520
Pekka Paalanen01388e22013-04-25 13:57:44 +03001521 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001522 shell_surface = get_shell_surface(main_surface);
1523 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001525
1526 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001527}
1528
1529static const struct workspace_manager_interface workspace_manager_implementation = {
1530 workspace_manager_move_surface,
1531};
1532
1533static void
1534unbind_resource(struct wl_resource *resource)
1535{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001536 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001537}
1538
1539static void
1540bind_workspace_manager(struct wl_client *client,
1541 void *data, uint32_t version, uint32_t id)
1542{
1543 struct desktop_shell *shell = data;
1544 struct wl_resource *resource;
1545
Jason Ekstranda85118c2013-06-27 20:17:02 -05001546 resource = wl_resource_create(client,
1547 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001548
1549 if (resource == NULL) {
1550 weston_log("couldn't add workspace manager object");
1551 return;
1552 }
1553
Jason Ekstranda85118c2013-06-27 20:17:02 -05001554 wl_resource_set_implementation(resource,
1555 &workspace_manager_implementation,
1556 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001557 wl_list_insert(&shell->workspaces.client_list,
1558 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001559
1560 workspace_manager_send_state(resource,
1561 shell->workspaces.current,
1562 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001563}
1564
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001565static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001566touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1567 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1568{
1569}
1570
1571static void
1572touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1573{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001574 struct weston_touch_move_grab *move =
1575 (struct weston_touch_move_grab *) container_of(
1576 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001577
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001578 if (touch_id == 0)
1579 move->active = 0;
1580
Jonas Ådahl9484b692013-12-02 22:05:03 +01001581 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001582 shell_touch_grab_end(&move->base);
1583 free(move);
1584 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001585}
1586
1587static void
1588touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1589 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1590{
1591 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1592 struct shell_surface *shsurf = move->base.shsurf;
1593 struct weston_surface *es;
1594 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1595 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1596
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001597 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001598 return;
1599
1600 es = shsurf->surface;
1601
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001602 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001603
1604 weston_compositor_schedule_repaint(es->compositor);
1605}
1606
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001607static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001608touch_move_grab_frame(struct weston_touch_grab *grab)
1609{
1610}
1611
1612static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001613touch_move_grab_cancel(struct weston_touch_grab *grab)
1614{
1615 struct weston_touch_move_grab *move =
1616 (struct weston_touch_move_grab *) container_of(
1617 grab, struct shell_touch_grab, grab);
1618
1619 shell_touch_grab_end(&move->base);
1620 free(move);
1621}
1622
Rusty Lynch1084da52013-08-15 09:10:08 -07001623static const struct weston_touch_grab_interface touch_move_grab_interface = {
1624 touch_move_grab_down,
1625 touch_move_grab_up,
1626 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001627 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001628 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001629};
1630
1631static int
1632surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1633{
1634 struct weston_touch_move_grab *move;
1635
1636 if (!shsurf)
1637 return -1;
1638
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001639 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001640 return 0;
1641
1642 move = malloc(sizeof *move);
1643 if (!move)
1644 return -1;
1645
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001646 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001647 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001648 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001650 seat->touch->grab_y;
1651
1652 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1653 seat->touch);
1654
1655 return 0;
1656}
1657
1658static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001659noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001660{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001661}
1662
1663static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001664constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1665{
1666 struct shell_surface *shsurf = move->base.shsurf;
1667 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001668 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001669 const int safety = 50;
1670
1671 x = wl_fixed_to_int(pointer->x + move->dx);
1672 y = wl_fixed_to_int(pointer->y + move->dy);
1673
Jonny Lambd73c6942014-08-20 15:53:20 +02001674 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1675 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1676 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001677
Jonny Lambd73c6942014-08-20 15:53:20 +02001678 bottom = y + shsurf->geometry.height;
1679 if (bottom - panel_height < safety)
1680 y = panel_height + safety -
1681 shsurf->geometry.height;
1682
1683 if (move->client_initiated &&
1684 y + shsurf->geometry.y < panel_height)
1685 y = panel_height - shsurf->geometry.y;
1686 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001687
1688 *cx = x;
1689 *cy = y;
1690}
1691
1692static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001693move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1694 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001695{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001696 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001697 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001698 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001699 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001700
1701 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001702 if (!shsurf)
1703 return;
1704
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001705 constrain_position(move, &cx, &cy);
1706
1707 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001708
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001710}
1711
1712static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001713move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001714 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001715{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001716 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1717 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001718 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001719 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001720
Daniel Stone4dbadb12012-05-30 16:31:51 +01001721 if (pointer->button_count == 0 &&
1722 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001723 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001724 free(grab);
1725 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001726}
1727
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001728static void
1729move_grab_cancel(struct weston_pointer_grab *grab)
1730{
1731 struct shell_grab *shell_grab =
1732 container_of(grab, struct shell_grab, grab);
1733
1734 shell_grab_end(shell_grab);
1735 free(grab);
1736}
1737
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001738static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001739 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001740 move_grab_motion,
1741 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001742 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001743};
1744
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001745static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001746surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1747 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748{
1749 struct weston_move_grab *move;
1750
1751 if (!shsurf)
1752 return -1;
1753
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001754 if (shsurf->grabbed ||
1755 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001756 return 0;
1757
1758 move = malloc(sizeof *move);
1759 if (!move)
1760 return -1;
1761
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001763 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001765 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001766 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767
1768 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001769 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770
1771 return 0;
1772}
1773
1774static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001775common_surface_move(struct wl_resource *resource,
1776 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001777{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001778 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001779 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001780 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001781
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001782 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001783 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001784 seat->pointer->button_count > 0 &&
1785 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001787 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001788 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001789 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001790 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001791 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001792 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001794 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001795 (surface_touch_move(shsurf, seat) < 0))
1796 wl_resource_post_no_memory(resource);
1797 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001798}
1799
Rafael Antognollie2a34552013-12-03 15:35:45 -02001800static void
1801shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1802 struct wl_resource *seat_resource, uint32_t serial)
1803{
1804 common_surface_move(resource, seat_resource, serial);
1805}
1806
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001807struct weston_resize_grab {
1808 struct shell_grab base;
1809 uint32_t edges;
1810 int32_t width, height;
1811};
1812
1813static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001814resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1815 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001816{
1817 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001818 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001819 struct shell_surface *shsurf = resize->base.shsurf;
1820 int32_t width, height;
1821 wl_fixed_t from_x, from_y;
1822 wl_fixed_t to_x, to_y;
1823
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001824 weston_pointer_move(pointer, x, y);
1825
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001826 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001827 return;
1828
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 weston_view_from_global_fixed(shsurf->view,
1830 pointer->grab_x, pointer->grab_y,
1831 &from_x, &from_y);
1832 weston_view_from_global_fixed(shsurf->view,
1833 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001834
1835 width = resize->width;
1836 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1837 width += wl_fixed_to_int(from_x - to_x);
1838 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1839 width += wl_fixed_to_int(to_x - from_x);
1840 }
1841
1842 height = resize->height;
1843 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1844 height += wl_fixed_to_int(from_y - to_y);
1845 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1846 height += wl_fixed_to_int(to_y - from_y);
1847 }
1848
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001849 if (width < 1)
1850 width = 1;
1851 if (height < 1)
1852 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001853 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001854}
1855
1856static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001857send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001858{
1859 struct shell_surface *shsurf = get_shell_surface(surface);
1860
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001861 assert(shsurf);
1862
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001863 if (shsurf->resource)
1864 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001865 shsurf->resize_edges,
1866 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001867}
1868
1869static const struct weston_shell_client shell_client = {
1870 send_configure
1871};
1872
1873static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001874resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001875 uint32_t time, uint32_t button, uint32_t state_w)
1876{
1877 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001878 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001879 enum wl_pointer_button_state state = state_w;
1880
1881 if (pointer->button_count == 0 &&
1882 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1883 shell_grab_end(&resize->base);
1884 free(grab);
1885 }
1886}
1887
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001888static void
1889resize_grab_cancel(struct weston_pointer_grab *grab)
1890{
1891 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1892
1893 shell_grab_end(&resize->base);
1894 free(grab);
1895}
1896
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001897static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001898 noop_grab_focus,
1899 resize_grab_motion,
1900 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001901 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001902};
1903
Giulio Camuffob8366642013-04-25 13:57:46 +03001904/*
1905 * Returns the bounding box of a surface and all its sub-surfaces,
1906 * in the surface coordinates system. */
1907static void
1908surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1909 int32_t *y, int32_t *w, int32_t *h) {
1910 pixman_region32_t region;
1911 pixman_box32_t *box;
1912 struct weston_subsurface *subsurface;
1913
1914 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915 surface->width,
1916 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001917
1918 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1919 pixman_region32_union_rect(&region, &region,
1920 subsurface->position.x,
1921 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 subsurface->surface->width,
1923 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001924 }
1925
1926 box = pixman_region32_extents(&region);
1927 if (x)
1928 *x = box->x1;
1929 if (y)
1930 *y = box->y1;
1931 if (w)
1932 *w = box->x2 - box->x1;
1933 if (h)
1934 *h = box->y2 - box->y1;
1935
1936 pixman_region32_fini(&region);
1937}
1938
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001939static int
1940surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001941 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001942{
1943 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001944 const unsigned resize_topbottom =
1945 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1946 const unsigned resize_leftright =
1947 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1948 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001949
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001950 if (shsurf->grabbed ||
1951 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001952 return 0;
1953
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001954 /* Check for invalid edge combinations. */
1955 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1956 (edges & resize_topbottom) == resize_topbottom ||
1957 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001958 return 0;
1959
1960 resize = malloc(sizeof *resize);
1961 if (!resize)
1962 return -1;
1963
1964 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001965
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001966 resize->width = shsurf->geometry.width;
1967 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001968
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001969 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001970 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001971 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001972 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001973
1974 return 0;
1975}
1976
1977static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001978common_surface_resize(struct wl_resource *resource,
1979 struct wl_resource *seat_resource, uint32_t serial,
1980 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001981{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001982 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001983 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001984 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001985
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001986 if (seat->pointer == NULL ||
1987 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001988 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001989 seat->pointer->focus == NULL)
1990 return;
1991
1992 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1993 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001994 return;
1995
Kristian Høgsberge3148752013-05-06 23:19:49 -04001996 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001997 wl_resource_post_no_memory(resource);
1998}
1999
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002000static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002001shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2002 struct wl_resource *seat_resource, uint32_t serial,
2003 uint32_t edges)
2004{
2005 common_surface_resize(resource, seat_resource, serial, edges);
2006}
2007
2008static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002009busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002010{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002011 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002012 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002014 wl_fixed_t sx, sy;
2015
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 view = weston_compositor_pick_view(pointer->seat->compositor,
2017 pointer->x, pointer->y,
2018 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002019
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002020 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2021 shell_grab_end(grab);
2022 free(grab);
2023 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002024}
2025
2026static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002027busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2028 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002029{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002030 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002031}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002032
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002033static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002034busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002035 uint32_t time, uint32_t button, uint32_t state)
2036{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002037 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002038 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002039 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002040
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002041 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002042 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002043 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002044 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002045 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002046 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002047 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002048}
2049
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002050static void
2051busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2052{
2053 struct shell_grab *grab = (struct shell_grab *) base;
2054
2055 shell_grab_end(grab);
2056 free(grab);
2057}
2058
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002059static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002060 busy_cursor_grab_focus,
2061 busy_cursor_grab_motion,
2062 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002063 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002064};
2065
2066static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002067set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002068{
2069 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002070
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002071 if (pointer->grab->interface == &busy_cursor_grab_interface)
2072 return;
2073
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002074 grab = malloc(sizeof *grab);
2075 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002076 return;
2077
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002078 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2079 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002080 /* Mark the shsurf as ungrabbed so that button binding is able
2081 * to move it. */
2082 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002083}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002084
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002085static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002086end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002087{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002088 struct shell_grab *grab;
2089 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002090
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002091 wl_list_for_each(seat, &compositor->seat_list, link) {
2092 if (seat->pointer == NULL)
2093 continue;
2094
2095 grab = (struct shell_grab *) seat->pointer->grab;
2096 if (grab->grab.interface == &busy_cursor_grab_interface &&
2097 wl_resource_get_client(grab->shsurf->resource) == client) {
2098 shell_grab_end(grab);
2099 free(grab);
2100 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002101 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002102}
2103
Scott Moreau9521d5e2012-04-19 13:06:17 -06002104static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002105handle_shell_client_destroy(struct wl_listener *listener, void *data);
2106
2107static int
2108xdg_ping_timeout_handler(void *data)
2109{
2110 struct shell_client *sc = data;
2111 struct weston_seat *seat;
2112 struct shell_surface *shsurf;
2113
2114 /* Client is not responding */
2115 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002116 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2117 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2118 continue;
2119 if (seat->pointer->focus->surface->resource == NULL)
2120 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002121
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002122 shsurf = get_shell_surface(seat->pointer->focus->surface);
2123 if (shsurf &&
2124 wl_resource_get_client(shsurf->resource) == sc->client)
2125 set_busy_cursor(shsurf, seat->pointer);
2126 }
2127
2128 return 1;
2129}
2130
2131static void
2132handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2133{
2134 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002135 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002136 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002137 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002138
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002139 if (sc->unresponsive) {
2140 xdg_ping_timeout_handler(sc);
2141 return;
2142 }
2143
2144 sc->ping_serial = serial;
2145 loop = wl_display_get_event_loop(compositor->wl_display);
2146 if (sc->ping_timer == NULL)
2147 sc->ping_timer =
2148 wl_event_loop_add_timer(loop,
2149 xdg_ping_timeout_handler, sc);
2150 if (sc->ping_timer == NULL)
2151 return;
2152
2153 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2154
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002155 if (shell_surface_is_xdg_surface(shsurf) ||
2156 shell_surface_is_xdg_popup(shsurf))
2157 xdg_shell_send_ping(sc->resource, serial);
2158 else if (shell_surface_is_wl_shell_surface(shsurf))
2159 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002160}
2161
Scott Moreauff1db4a2012-04-17 19:06:18 -06002162static void
2163ping_handler(struct weston_surface *surface, uint32_t serial)
2164{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002165 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002166
2167 if (!shsurf)
2168 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002169 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002170 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002171 if (shsurf->surface == shsurf->shell->grab_surface)
2172 return;
2173
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002174 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002175}
2176
2177static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002178handle_pointer_focus(struct wl_listener *listener, void *data)
2179{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002180 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002182 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002183 uint32_t serial;
2184
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002186 return;
2187
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002189 serial = wl_display_next_serial(compositor->wl_display);
2190 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002191}
2192
2193static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002194shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2195{
2196 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002197 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002198}
2199
2200static void
2201shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2202{
2203 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002204 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002205}
2206
2207static void
2208handle_keyboard_focus(struct wl_listener *listener, void *data)
2209{
2210 struct weston_keyboard *keyboard = data;
2211 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2212
2213 if (seat->focused_surface) {
2214 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2215 if (shsurf)
2216 shell_surface_lose_keyboard_focus(shsurf);
2217 }
2218
2219 seat->focused_surface = keyboard->focus;
2220
2221 if (seat->focused_surface) {
2222 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2223 if (shsurf)
2224 shell_surface_gain_keyboard_focus(shsurf);
2225 }
2226}
2227
2228static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002229shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002230{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002231 if (sc->ping_serial != serial)
2232 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002233
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002234 sc->unresponsive = 0;
2235 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002236
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002237 if (sc->ping_timer) {
2238 wl_event_source_remove(sc->ping_timer);
2239 sc->ping_timer = NULL;
2240 }
2241
2242}
2243
2244static void
2245shell_surface_pong(struct wl_client *client,
2246 struct wl_resource *resource, uint32_t serial)
2247{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002248 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2249 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002250
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002251 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002252}
2253
2254static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002255set_title(struct shell_surface *shsurf, const char *title)
2256{
2257 free(shsurf->title);
2258 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002259 shsurf->surface->timeline.force_refresh = 1;
2260}
2261
2262static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002263set_pid(struct shell_surface *shsurf, pid_t pid)
2264{
2265 /* We have no use for it */
2266}
2267
2268static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002269set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2270{
2271 shsurf->type = t;
2272 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002273}
2274
2275static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002276set_window_geometry(struct shell_surface *shsurf,
2277 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002278{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002279 shsurf->next_geometry.x = x;
2280 shsurf->next_geometry.y = y;
2281 shsurf->next_geometry.width = width;
2282 shsurf->next_geometry.height = height;
2283 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002284}
2285
2286static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002287shell_surface_set_title(struct wl_client *client,
2288 struct wl_resource *resource, const char *title)
2289{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002290 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002291
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002292 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002293}
2294
2295static void
2296shell_surface_set_class(struct wl_client *client,
2297 struct wl_resource *resource, const char *class)
2298{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002299 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002300
2301 free(shsurf->class);
2302 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002303 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002304}
2305
Alex Wu4539b082012-03-01 12:57:46 +08002306static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002307restore_output_mode(struct weston_output *output)
2308{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002309 if (output->original_mode)
2310 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002311}
2312
2313static void
2314restore_all_output_modes(struct weston_compositor *compositor)
2315{
2316 struct weston_output *output;
2317
2318 wl_list_for_each(output, &compositor->output_list, link)
2319 restore_output_mode(output);
2320}
2321
Philip Withnall07926d92013-11-25 18:01:40 +00002322/* The surface will be inserted into the list immediately after the link
2323 * returned by this function (i.e. will be stacked immediately above the
2324 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002325static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002326shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2327{
2328 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002329 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002330
2331 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002332 case SHELL_SURFACE_XWAYLAND:
2333 return &shsurf->shell->fullscreen_layer.view_list;
2334
2335 case SHELL_SURFACE_NONE:
2336 return NULL;
2337
Kristian Høgsbergead52422014-01-01 13:51:52 -08002338 case SHELL_SURFACE_POPUP:
2339 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002340 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002341 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002342 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002343 /* Move the surface to its parent layer so
2344 * that surfaces which are transient for
2345 * fullscreen surfaces don't get hidden by the
2346 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002347
2348 /* TODO: Handle a parent with multiple views */
2349 parent = get_default_view(shsurf->parent);
2350 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002351 return container_of(parent->layer_link.link.prev,
2352 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002353 }
Philip Withnall07926d92013-11-25 18:01:40 +00002354
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002355 /* Move the surface to a normal workspace layer so that surfaces
2356 * which were previously fullscreen or transient are no longer
2357 * rendered on top. */
2358 ws = get_current_workspace(shsurf->shell);
2359 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002360 }
2361
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002362 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002363}
2364
Philip Withnall648a4dd2013-11-25 18:01:44 +00002365static void
2366shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2367{
2368 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002369 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002370
2371 /* Move the child layers to the same workspace as shsurf. They will be
2372 * stacked above shsurf. */
2373 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002375 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002376 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002377 prev = container_of(shsurf->view->layer_link.link.prev,
2378 struct weston_layer_entry, link);
2379 weston_layer_entry_remove(&child->view->layer_link);
2380 weston_layer_entry_insert(prev,
2381 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002382 weston_view_geometry_dirty(child->view);
2383 weston_surface_damage(child->surface);
2384
2385 /* Recurse. We don’t expect this to recurse very far (if
2386 * at all) because that would imply we have transient
2387 * (or popup) children of transient surfaces, which
2388 * would be unusual. */
2389 shell_surface_update_child_surface_layers(child);
2390 }
2391 }
2392}
2393
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002394/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002395 * geometry to ensure the changes are redrawn.
2396 *
2397 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2398 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002399static void
2400shell_surface_update_layer(struct shell_surface *shsurf)
2401{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002402 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002403
2404 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2405
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002406 if (new_layer_link == NULL)
2407 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002408 if (new_layer_link == &shsurf->view->layer_link)
2409 return;
2410
2411 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002412 weston_layer_entry_remove(&shsurf->view->layer_link);
2413 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002414 weston_view_geometry_dirty(shsurf->view);
2415 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002416
2417 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002418}
2419
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002420static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002421shell_surface_set_parent(struct shell_surface *shsurf,
2422 struct weston_surface *parent)
2423{
2424 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002425
2426 wl_list_remove(&shsurf->children_link);
2427 wl_list_init(&shsurf->children_link);
2428
2429 /* Insert into the parent surface’s child list. */
2430 if (parent != NULL) {
2431 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2432 if (parent_shsurf != NULL)
2433 wl_list_insert(&parent_shsurf->children_list,
2434 &shsurf->children_link);
2435 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002436}
2437
2438static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002439shell_surface_set_output(struct shell_surface *shsurf,
2440 struct weston_output *output)
2441{
2442 struct weston_surface *es = shsurf->surface;
2443
2444 /* get the default output, if the client set it as NULL
2445 check whether the ouput is available */
2446 if (output)
2447 shsurf->output = output;
2448 else if (es->output)
2449 shsurf->output = es->output;
2450 else
2451 shsurf->output = get_default_output(es->compositor);
2452}
2453
2454static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002455surface_clear_next_states(struct shell_surface *shsurf)
2456{
2457 shsurf->next_state.maximized = false;
2458 shsurf->next_state.fullscreen = false;
2459
2460 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2461 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2462 shsurf->state_changed = true;
2463}
2464
2465static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466set_toplevel(struct shell_surface *shsurf)
2467{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002468 shell_surface_set_parent(shsurf, NULL);
2469 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002470 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002471
2472 /* The layer_link is updated in set_surface_type(),
2473 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002474}
2475
2476static void
2477shell_surface_set_toplevel(struct wl_client *client,
2478 struct wl_resource *resource)
2479{
2480 struct shell_surface *surface = wl_resource_get_user_data(resource);
2481
2482 set_toplevel(surface);
2483}
2484
2485static void
2486set_transient(struct shell_surface *shsurf,
2487 struct weston_surface *parent, int x, int y, uint32_t flags)
2488{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002489 assert(parent != NULL);
2490
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002491 shell_surface_set_parent(shsurf, parent);
2492
2493 surface_clear_next_states(shsurf);
2494
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495 shsurf->transient.x = x;
2496 shsurf->transient.y = y;
2497 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002498
Rafael Antognollied207b42013-12-03 15:35:43 -02002499 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002500 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002501 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002502
2503 /* The layer_link is updated in set_surface_type(),
2504 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002505}
2506
2507static void
2508shell_surface_set_transient(struct wl_client *client,
2509 struct wl_resource *resource,
2510 struct wl_resource *parent_resource,
2511 int x, int y, uint32_t flags)
2512{
2513 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2514 struct weston_surface *parent =
2515 wl_resource_get_user_data(parent_resource);
2516
2517 set_transient(shsurf, parent, x, y, flags);
2518}
2519
2520static void
2521set_fullscreen(struct shell_surface *shsurf,
2522 uint32_t method,
2523 uint32_t framerate,
2524 struct weston_output *output)
2525{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002526 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002527 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002528
2529 shsurf->fullscreen_output = shsurf->output;
2530 shsurf->fullscreen.type = method;
2531 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002532
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002533 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002534}
2535
2536static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002537weston_view_set_initial_position(struct weston_view *view,
2538 struct desktop_shell *shell);
2539
2540static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002542{
Philip Withnallf85fe842013-11-25 18:01:37 +00002543 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002544 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2545 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002546 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002547 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002548
Alex Wu4539b082012-03-01 12:57:46 +08002549 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2550 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002551
Alex Wu4539b082012-03-01 12:57:46 +08002552 wl_list_remove(&shsurf->fullscreen.transform.link);
2553 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002554
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 if (shsurf->fullscreen.black_view)
2556 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2557 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002558
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002559 if (shsurf->saved_position_valid)
2560 weston_view_set_position(shsurf->view,
2561 shsurf->saved_x, shsurf->saved_y);
2562 else
2563 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2564
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002565 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002567 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002568 shsurf->saved_rotation_valid = false;
2569 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002570
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002571 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002572}
2573
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002574static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002575shell_surface_set_fullscreen(struct wl_client *client,
2576 struct wl_resource *resource,
2577 uint32_t method,
2578 uint32_t framerate,
2579 struct wl_resource *output_resource)
2580{
2581 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2582 struct weston_output *output;
2583
2584 if (output_resource)
2585 output = wl_resource_get_user_data(output_resource);
2586 else
2587 output = NULL;
2588
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002589 shell_surface_set_parent(shsurf, NULL);
2590
Rafael Antognolli03b16592013-12-03 15:35:42 -02002591 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002592 shsurf->next_state.fullscreen = true;
2593 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002594 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002595}
2596
2597static void
2598set_popup(struct shell_surface *shsurf,
2599 struct weston_surface *parent,
2600 struct weston_seat *seat,
2601 uint32_t serial,
2602 int32_t x,
2603 int32_t y)
2604{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002605 assert(parent != NULL);
2606
Philip Withnallbecb77e2013-11-25 18:01:30 +00002607 shsurf->popup.shseat = get_shell_seat(seat);
2608 shsurf->popup.serial = serial;
2609 shsurf->popup.x = x;
2610 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002611
Pekka Paalanenb5026542014-11-12 15:09:24 +02002612 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002613}
2614
2615static void
2616shell_surface_set_popup(struct wl_client *client,
2617 struct wl_resource *resource,
2618 struct wl_resource *seat_resource,
2619 uint32_t serial,
2620 struct wl_resource *parent_resource,
2621 int32_t x, int32_t y, uint32_t flags)
2622{
2623 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002624 struct weston_surface *parent =
2625 wl_resource_get_user_data(parent_resource);
2626
2627 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002628
Rafael Antognolli03b16592013-12-03 15:35:42 -02002629 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002630 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002631 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002632 wl_resource_get_user_data(seat_resource),
2633 serial, x, y);
2634}
2635
2636static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002637unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002638{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002639 /* undo all maximized things here */
2640 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002641
2642 if (shsurf->saved_position_valid)
2643 weston_view_set_position(shsurf->view,
2644 shsurf->saved_x, shsurf->saved_y);
2645 else
2646 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002647
2648 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002649 wl_list_insert(&shsurf->view->geometry.transformation_list,
2650 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002651 shsurf->saved_rotation_valid = false;
2652 }
2653
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002654 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002655}
2656
Philip Withnallbecb77e2013-11-25 18:01:30 +00002657static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002658set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002659{
2660 struct shell_surface *shsurf;
2661 struct workspace *current_ws;
2662 struct weston_seat *seat;
2663 struct weston_surface *focus;
2664 struct weston_view *view;
2665
2666 view = get_default_view(surface);
2667 if (!view)
2668 return;
2669
2670 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2671
2672 shsurf = get_shell_surface(surface);
2673 current_ws = get_current_workspace(shsurf->shell);
2674
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002675 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002676 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002677
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002678 drop_focus_state(shsurf->shell, current_ws, view->surface);
2679 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2680 if (!seat->keyboard)
2681 continue;
2682 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2683 if (focus == view->surface)
2684 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002685 }
2686
2687 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002688 weston_view_damage_below(view);
2689}
2690
2691static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002692shell_surface_set_maximized(struct wl_client *client,
2693 struct wl_resource *resource,
2694 struct wl_resource *output_resource)
2695{
2696 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2697 struct weston_output *output;
2698
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002699 surface_clear_next_states(shsurf);
2700 shsurf->next_state.maximized = true;
2701 shsurf->state_changed = true;
2702
Pekka Paalanenb5026542014-11-12 15:09:24 +02002703 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002704 shell_surface_set_parent(shsurf, NULL);
2705
Philip Withnallbecb77e2013-11-25 18:01:30 +00002706 if (output_resource)
2707 output = wl_resource_get_user_data(output_resource);
2708 else
2709 output = NULL;
2710
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002711 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002712
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002713 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002714}
2715
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002716/* This is only ever called from set_surface_type(), so there’s no need to
2717 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002718static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002719reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002720{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002721 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002722 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002723 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002724 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002725
Pekka Paalanen98262232011-12-01 10:42:22 +02002726 return 0;
2727}
2728
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002729static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002730set_full_output(struct shell_surface *shsurf)
2731{
2732 shsurf->saved_x = shsurf->view->geometry.x;
2733 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002734 shsurf->saved_width = shsurf->surface->width;
2735 shsurf->saved_height = shsurf->surface->height;
2736 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002737 shsurf->saved_position_valid = true;
2738
2739 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2740 wl_list_remove(&shsurf->rotation.transform.link);
2741 wl_list_init(&shsurf->rotation.transform.link);
2742 weston_view_geometry_dirty(shsurf->view);
2743 shsurf->saved_rotation_valid = true;
2744 }
2745}
2746
2747static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002748set_surface_type(struct shell_surface *shsurf)
2749{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002750 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002752
Philip Withnallbecb77e2013-11-25 18:01:30 +00002753 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002754
Rafael Antognolli03b16592013-12-03 15:35:42 -02002755 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002756 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002757
2758 switch (shsurf->type) {
2759 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002760 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002761 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002762 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 weston_view_set_position(shsurf->view,
2764 pev->geometry.x + shsurf->transient.x,
2765 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002766 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002767 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002768
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002769 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770 weston_view_set_position(shsurf->view, shsurf->transient.x,
2771 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002772 break;
2773
Philip Withnall0f640e22013-11-25 18:01:31 +00002774 case SHELL_SURFACE_POPUP:
2775 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002776 default:
2777 break;
2778 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002779
2780 /* Update the surface’s layer. */
2781 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002782}
2783
Tiago Vignattibe143262012-04-16 17:31:41 +03002784static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002785shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002786{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002787 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002788}
2789
Pekka Paalanen8274d902014-08-06 19:36:51 +03002790static int
2791black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2792{
2793 struct weston_surface *fs_surface = surface->configure_private;
2794 int n;
2795 int rem;
2796 int ret;
2797
2798 n = snprintf(buf, len, "black background surface for ");
2799 if (n < 0)
2800 return n;
2801
2802 rem = (int)len - n;
2803 if (rem < 0)
2804 rem = 0;
2805
2806 if (fs_surface->get_label)
2807 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2808 else
2809 ret = snprintf(buf + n, rem, "<unknown>");
2810
2811 if (ret < 0)
2812 return n;
2813
2814 return n + ret;
2815}
2816
Alex Wu21858432012-04-01 20:13:08 +08002817static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002818black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002819
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002821create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002822 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002823 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002824{
2825 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002827
2828 surface = weston_surface_create(ec);
2829 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002830 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002831 return NULL;
2832 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 view = weston_view_create(surface);
2834 if (surface == NULL) {
2835 weston_log("no memory\n");
2836 weston_surface_destroy(surface);
2837 return NULL;
2838 }
Alex Wu4539b082012-03-01 12:57:46 +08002839
Alex Wu21858432012-04-01 20:13:08 +08002840 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002841 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002842 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002843 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002844 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002845 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002846 pixman_region32_fini(&surface->input);
2847 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002848
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002849 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002850 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002851
2852 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002853}
2854
Philip Withnalled8f1a92013-11-25 18:01:43 +00002855static void
2856shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2857{
2858 struct weston_output *output = shsurf->fullscreen_output;
2859
Rafael Antognolli03b16592013-12-03 15:35:42 -02002860 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002861
2862 if (!shsurf->fullscreen.black_view)
2863 shsurf->fullscreen.black_view =
2864 create_black_surface(shsurf->surface->compositor,
2865 shsurf->surface,
2866 output->x, output->y,
2867 output->width,
2868 output->height);
2869
2870 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002871 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2872 weston_layer_entry_insert(&shsurf->view->layer_link,
2873 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002874 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2875 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002876
2877 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002878}
2879
Alex Wu4539b082012-03-01 12:57:46 +08002880/* Create black surface and append it to the associated fullscreen surface.
2881 * Handle size dismatch and positioning according to the method. */
2882static void
2883shell_configure_fullscreen(struct shell_surface *shsurf)
2884{
2885 struct weston_output *output = shsurf->fullscreen_output;
2886 struct weston_surface *surface = shsurf->surface;
2887 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002888 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002889 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002890
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002891 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2892 restore_output_mode(output);
2893
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002894 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002895 weston_layer_entry_remove(&shsurf->view->layer_link);
2896 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002897
Philip Withnalled8f1a92013-11-25 18:01:43 +00002898 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002899
Jason Ekstranda7af7042013-10-12 22:38:11 -05002900 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002901 &surf_width, &surf_height);
2902
Alex Wu4539b082012-03-01 12:57:46 +08002903 switch (shsurf->fullscreen.type) {
2904 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002905 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002906 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002907 break;
2908 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002909 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002910 if (output->width == surf_width &&
2911 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 weston_view_set_position(shsurf->view,
2913 output->x - surf_x,
2914 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002915 break;
2916 }
2917
Alex Wu4539b082012-03-01 12:57:46 +08002918 matrix = &shsurf->fullscreen.transform.matrix;
2919 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002920
Scott Moreau1bad5db2012-08-18 01:04:05 -06002921 output_aspect = (float) output->width /
2922 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 /* XXX: Use surf_width and surf_height here? */
2924 surface_aspect = (float) surface->width /
2925 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002926 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002927 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002928 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002929 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002930 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002931 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002932
Alex Wu4539b082012-03-01 12:57:46 +08002933 weston_matrix_scale(matrix, scale, scale, 1);
2934 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002936 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002937 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2938 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002939 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002940
Alex Wu4539b082012-03-01 12:57:46 +08002941 break;
2942 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002943 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002944 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002945 surf_width * surface->buffer_viewport.buffer.scale,
2946 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002947 shsurf->fullscreen.framerate};
2948
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002949 if (weston_output_mode_switch_to_temporary(output, &mode,
2950 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002951 weston_view_set_position(shsurf->view,
2952 output->x - surf_x,
2953 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002954 shsurf->fullscreen.black_view->surface->width = output->width;
2955 shsurf->fullscreen.black_view->surface->height = output->height;
2956 weston_view_set_position(shsurf->fullscreen.black_view,
2957 output->x - surf_x,
2958 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002959 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002960 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002961 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002962 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002963 }
Alex Wubd3354b2012-04-17 17:20:49 +08002964 }
Alex Wu4539b082012-03-01 12:57:46 +08002965 break;
2966 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002967 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002968 break;
2969 default:
2970 break;
2971 }
2972}
2973
Alex Wu4539b082012-03-01 12:57:46 +08002974static void
2975shell_map_fullscreen(struct shell_surface *shsurf)
2976{
Alex Wubd3354b2012-04-17 17:20:49 +08002977 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002978}
2979
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002980static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002981set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2982{
2983 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002984 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002985 shsurf->transient.x = x;
2986 shsurf->transient.y = y;
2987 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002988
2989 shell_surface_set_parent(shsurf, NULL);
2990
Pekka Paalanenb5026542014-11-12 15:09:24 +02002991 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002992 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002993}
2994
Kristian Høgsberg47792412014-05-04 13:47:06 -07002995static void
2996shell_interface_set_fullscreen(struct shell_surface *shsurf,
2997 uint32_t method,
2998 uint32_t framerate,
2999 struct weston_output *output)
3000{
3001 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003002 shsurf->next_state.fullscreen = true;
3003 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003004
3005 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003006}
3007
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003008static struct weston_output *
3009get_focused_output(struct weston_compositor *compositor)
3010{
3011 struct weston_seat *seat;
3012 struct weston_output *output = NULL;
3013
3014 wl_list_for_each(seat, &compositor->seat_list, link) {
3015 /* Priority has touch focus, then pointer and
3016 * then keyboard focus. We should probably have
3017 * three for loops and check frist for touch,
3018 * then for pointer, etc. but unless somebody has some
3019 * objections, I think this is sufficient. */
3020 if (seat->touch && seat->touch->focus)
3021 output = seat->touch->focus->output;
3022 else if (seat->pointer && seat->pointer->focus)
3023 output = seat->pointer->focus->output;
3024 else if (seat->keyboard && seat->keyboard->focus)
3025 output = seat->keyboard->focus->output;
3026
3027 if (output)
3028 break;
3029 }
3030
3031 return output;
3032}
3033
3034static void
3035shell_interface_set_maximized(struct shell_surface *shsurf)
3036{
3037 struct weston_output *output;
3038
3039 surface_clear_next_states(shsurf);
3040 shsurf->next_state.maximized = true;
3041 shsurf->state_changed = true;
3042 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3043
3044 if (!weston_surface_is_mapped(shsurf->surface))
3045 output = get_focused_output(shsurf->surface->compositor);
3046 else
3047 output = shsurf->surface->output;
3048
3049 shell_surface_set_output(shsurf, output);
3050 send_configure_for_surface(shsurf);
3051}
3052
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003053static int
3054shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3055{
3056 return surface_move(shsurf, ws, 1);
3057}
3058
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003059static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003060
3061static void
3062destroy_shell_seat(struct wl_listener *listener, void *data)
3063{
3064 struct shell_seat *shseat =
3065 container_of(listener,
3066 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003067 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003068
3069 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003070 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003071 shseat->popup_grab.client = NULL;
3072
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003073 wl_list_for_each_safe(shsurf, next,
3074 &shseat->popup_grab.surfaces_list,
3075 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003077 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003078 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003079 }
3080
3081 wl_list_remove(&shseat->seat_destroy_listener.link);
3082 free(shseat);
3083}
3084
Jason Ekstrand024177c2014-04-21 19:42:58 -05003085static void
3086shell_seat_caps_changed(struct wl_listener *listener, void *data)
3087{
3088 struct shell_seat *seat;
3089
3090 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3091
3092 if (seat->seat->keyboard &&
3093 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3094 wl_signal_add(&seat->seat->keyboard->focus_signal,
3095 &seat->keyboard_focus_listener);
3096 } else if (!seat->seat->keyboard) {
3097 wl_list_init(&seat->keyboard_focus_listener.link);
3098 }
3099
3100 if (seat->seat->pointer &&
3101 wl_list_empty(&seat->pointer_focus_listener.link)) {
3102 wl_signal_add(&seat->seat->pointer->focus_signal,
3103 &seat->pointer_focus_listener);
3104 } else if (!seat->seat->pointer) {
3105 wl_list_init(&seat->pointer_focus_listener.link);
3106 }
3107}
3108
Giulio Camuffo5085a752013-03-25 21:42:45 +01003109static struct shell_seat *
3110create_shell_seat(struct weston_seat *seat)
3111{
3112 struct shell_seat *shseat;
3113
3114 shseat = calloc(1, sizeof *shseat);
3115 if (!shseat) {
3116 weston_log("no memory to allocate shell seat\n");
3117 return NULL;
3118 }
3119
3120 shseat->seat = seat;
3121 wl_list_init(&shseat->popup_grab.surfaces_list);
3122
3123 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3124 wl_signal_add(&seat->destroy_signal,
3125 &shseat->seat_destroy_listener);
3126
Jason Ekstrand024177c2014-04-21 19:42:58 -05003127 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3128 wl_list_init(&shseat->keyboard_focus_listener.link);
3129
3130 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3131 wl_list_init(&shseat->pointer_focus_listener.link);
3132
3133 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3134 wl_signal_add(&seat->updated_caps_signal,
3135 &shseat->caps_changed_listener);
3136 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3137
Giulio Camuffo5085a752013-03-25 21:42:45 +01003138 return shseat;
3139}
3140
3141static struct shell_seat *
3142get_shell_seat(struct weston_seat *seat)
3143{
3144 struct wl_listener *listener;
3145
3146 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003147 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003148
3149 return container_of(listener,
3150 struct shell_seat, seat_destroy_listener);
3151}
3152
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003153static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003154popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003155{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003156 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003157 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003158 struct shell_seat *shseat =
3159 container_of(grab, struct shell_seat, popup_grab.grab);
3160 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003161 wl_fixed_t sx, sy;
3162
Jason Ekstranda7af7042013-10-12 22:38:11 -05003163 view = weston_compositor_pick_view(pointer->seat->compositor,
3164 pointer->x, pointer->y,
3165 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003166
Jason Ekstranda7af7042013-10-12 22:38:11 -05003167 if (view && view->surface->resource &&
3168 wl_resource_get_client(view->surface->resource) == client) {
3169 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003170 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003171 weston_pointer_set_focus(pointer, NULL,
3172 wl_fixed_from_int(0),
3173 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003174 }
3175}
3176
3177static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003178popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3179 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003180{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003181 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003182 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003183 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003184
Jonas Ådahl61917c82014-08-11 22:44:02 +02003185 if (pointer->focus) {
3186 weston_view_from_global_fixed(pointer->focus, x, y,
3187 &pointer->sx, &pointer->sy);
3188 }
3189
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003190 weston_pointer_move(pointer, x, y);
3191
Neil Roberts96d790e2013-09-19 17:32:00 +01003192 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003193 weston_view_from_global_fixed(pointer->focus,
3194 pointer->x, pointer->y,
3195 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003196 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003197 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003198}
3199
3200static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003201popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003202 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003203{
3204 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003205 struct shell_seat *shseat =
3206 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003207 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003208 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003209 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003210 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003211
Neil Roberts96d790e2013-09-19 17:32:00 +01003212 resource_list = &grab->pointer->focus_resource_list;
3213 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003214 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003215 wl_resource_for_each(resource, resource_list) {
3216 wl_pointer_send_button(resource, serial,
3217 time, button, state);
3218 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003219 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003220 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003221 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003222 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003223 }
3224
Daniel Stone4dbadb12012-05-30 16:31:51 +01003225 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003226 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003227}
3228
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003229static void
3230popup_grab_cancel(struct weston_pointer_grab *grab)
3231{
3232 popup_grab_end(grab->pointer);
3233}
3234
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003235static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003236 popup_grab_focus,
3237 popup_grab_motion,
3238 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003239 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003240};
3241
3242static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003243touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3244 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3245{
3246 struct wl_resource *resource;
3247 struct shell_seat *shseat =
3248 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3249 struct wl_display *display = shseat->seat->compositor->wl_display;
3250 uint32_t serial;
3251 struct wl_list *resource_list;
3252
3253 resource_list = &grab->touch->focus_resource_list;
3254 if (!wl_list_empty(resource_list)) {
3255 serial = wl_display_get_serial(display);
3256 wl_resource_for_each(resource, resource_list) {
3257 wl_touch_send_down(resource, serial, time,
3258 grab->touch->focus->surface->resource,
3259 touch_id, sx, sy);
3260 }
3261 }
3262}
3263
3264static void
3265touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3266{
3267 struct wl_resource *resource;
3268 struct shell_seat *shseat =
3269 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3270 struct wl_display *display = shseat->seat->compositor->wl_display;
3271 uint32_t serial;
3272 struct wl_list *resource_list;
3273
3274 resource_list = &grab->touch->focus_resource_list;
3275 if (!wl_list_empty(resource_list)) {
3276 serial = wl_display_get_serial(display);
3277 wl_resource_for_each(resource, resource_list) {
3278 wl_touch_send_up(resource, serial, time, touch_id);
3279 }
3280 }
3281}
3282
3283static void
3284touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3285 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3286{
3287 struct wl_resource *resource;
3288 struct wl_list *resource_list;
3289
3290 resource_list = &grab->touch->focus_resource_list;
3291 if (!wl_list_empty(resource_list)) {
3292 wl_resource_for_each(resource, resource_list) {
3293 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3294 }
3295 }
3296}
3297
3298static void
3299touch_popup_grab_frame(struct weston_touch_grab *grab)
3300{
3301}
3302
3303static void
3304touch_popup_grab_cancel(struct weston_touch_grab *grab)
3305{
3306 touch_popup_grab_end(grab->touch);
3307}
3308
3309static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3310 touch_popup_grab_down,
3311 touch_popup_grab_up,
3312 touch_popup_grab_motion,
3313 touch_popup_grab_frame,
3314 touch_popup_grab_cancel,
3315};
3316
3317static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003318shell_surface_send_popup_done(struct shell_surface *shsurf)
3319{
3320 if (shell_surface_is_wl_shell_surface(shsurf))
3321 wl_shell_surface_send_popup_done(shsurf->resource);
3322 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003323 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003324}
3325
3326static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003327popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003328{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003329 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003330 struct shell_seat *shseat =
3331 container_of(grab, struct shell_seat, popup_grab.grab);
3332 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003333 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003334
3335 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003336 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003337 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003338 shseat->popup_grab.grab.interface = NULL;
3339 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003340 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003341 wl_list_for_each_safe(shsurf, next,
3342 &shseat->popup_grab.surfaces_list,
3343 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003344 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003345 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003346 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003347 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003348 wl_list_init(&shseat->popup_grab.surfaces_list);
3349 }
3350}
3351
3352static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003353touch_popup_grab_end(struct weston_touch *touch)
3354{
3355 struct weston_touch_grab *grab = touch->grab;
3356 struct shell_seat *shseat =
3357 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3358 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003359 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003360
3361 if (touch->grab->interface == &touch_popup_grab_interface) {
3362 weston_touch_end_grab(grab->touch);
3363 shseat->popup_grab.client = NULL;
3364 shseat->popup_grab.touch_grab.interface = NULL;
3365 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3366 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003367 wl_list_for_each_safe(shsurf, next,
3368 &shseat->popup_grab.surfaces_list,
3369 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003370 shell_surface_send_popup_done(shsurf);
3371 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003372 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003373 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003374 wl_list_init(&shseat->popup_grab.surfaces_list);
3375 }
3376}
3377
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003378static struct shell_surface *
3379get_top_popup(struct shell_seat *shseat)
3380{
3381 struct shell_surface *shsurf;
3382
3383 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3384 return NULL;
3385 } else {
3386 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3387 struct shell_surface,
3388 popup.grab_link);
3389 return shsurf;
3390 }
3391}
3392
3393static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003394add_popup_grab(struct shell_surface *shsurf,
3395 struct shell_seat *shseat,
3396 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003397{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003398 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003399 struct shell_surface *parent, *top_surface;
3400
3401 parent = get_shell_surface(shsurf->parent);
3402 top_surface = get_top_popup(shseat);
3403 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003404 (!parent ||
3405 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003406 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003407 wl_resource_post_error(shsurf->owner_resource,
3408 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003409 "xdg_popup was not created on the "
3410 "topmost popup");
3411 return -1;
3412 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003413
3414 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003415 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003416 shseat->popup_grab.client =
3417 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003418
3419 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003420 shseat->popup_grab.grab.interface =
3421 &popup_grab_interface;
3422
3423 /* We must make sure here that this popup was opened
3424 * after a mouse press, and not just by moving around
3425 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003426 if (shseat->seat->pointer->button_count > 0)
3427 shseat->popup_grab.initial_up = 0;
3428 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003429 shseat->popup_grab.touch_grab.interface =
3430 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003431 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003432
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003433 wl_list_insert(&shseat->popup_grab.surfaces_list,
3434 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003435
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003436 if (type == POINTER) {
3437 weston_pointer_start_grab(seat->pointer,
3438 &shseat->popup_grab.grab);
3439 } else if (type == TOUCH) {
3440 weston_touch_start_grab(seat->touch,
3441 &shseat->popup_grab.touch_grab);
3442 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003443 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003444 wl_list_insert(&shseat->popup_grab.surfaces_list,
3445 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003446 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003447
3448 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003449}
3450
3451static void
3452remove_popup_grab(struct shell_surface *shsurf)
3453{
3454 struct shell_seat *shseat = shsurf->popup.shseat;
3455
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003456 if (shell_surface_is_xdg_popup(shsurf) &&
3457 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003458 wl_resource_post_error(shsurf->owner_resource,
3459 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003460 "xdg_popup was destroyed while it was "
3461 "not the topmost popup.");
3462 return;
3463 }
3464
Giulio Camuffo5085a752013-03-25 21:42:45 +01003465 wl_list_remove(&shsurf->popup.grab_link);
3466 wl_list_init(&shsurf->popup.grab_link);
3467 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003468 if (shseat->popup_grab.type == POINTER) {
3469 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3470 shseat->popup_grab.grab.interface = NULL;
3471 } else if (shseat->popup_grab.type == TOUCH) {
3472 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3473 shseat->popup_grab.touch_grab.interface = NULL;
3474 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003475 }
3476}
3477
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003478static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003479shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003480{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003481 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003482 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003483
Jason Ekstranda7af7042013-10-12 22:38:11 -05003484 shsurf->surface->output = parent_view->output;
3485 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003486
Jason Ekstranda7af7042013-10-12 22:38:11 -05003487 weston_view_set_transform_parent(shsurf->view, parent_view);
3488 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3489 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003490
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003491 if (shseat->seat->pointer &&
3492 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003493 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3494 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003495 } else if (shseat->seat->touch &&
3496 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003497 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3498 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003499 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003500 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003501 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003502 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003503
3504 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003505}
3506
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003507static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003508 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003509 shell_surface_move,
3510 shell_surface_resize,
3511 shell_surface_set_toplevel,
3512 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003513 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003514 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003515 shell_surface_set_maximized,
3516 shell_surface_set_title,
3517 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003518};
3519
3520static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003521destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003522{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003523 struct shell_surface *child, *next;
3524
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003525 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3526
Giulio Camuffo5085a752013-03-25 21:42:45 +01003527 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3528 remove_popup_grab(shsurf);
3529 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003530
Alex Wubd3354b2012-04-17 17:20:49 +08003531 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003532 shell_surface_is_top_fullscreen(shsurf))
3533 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003534
Jason Ekstranda7af7042013-10-12 22:38:11 -05003535 if (shsurf->fullscreen.black_view)
3536 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003537
Alex Wubd3354b2012-04-17 17:20:49 +08003538 /* As destroy_resource() use wl_list_for_each_safe(),
3539 * we can always remove the listener.
3540 */
3541 wl_list_remove(&shsurf->surface_destroy_listener.link);
3542 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003543 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003544 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003545
Jason Ekstranda7af7042013-10-12 22:38:11 -05003546 weston_view_destroy(shsurf->view);
3547
Philip Withnall648a4dd2013-11-25 18:01:44 +00003548 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003549 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3550 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003551
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003552 wl_list_remove(&shsurf->link);
3553 free(shsurf);
3554}
3555
3556static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003557shell_destroy_shell_surface(struct wl_resource *resource)
3558{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003559 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003560
Bryan Caina46b9462014-04-04 17:41:24 -05003561 if (!wl_list_empty(&shsurf->popup.grab_link))
3562 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003563 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003564 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003565}
3566
3567static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003568shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003569{
3570 struct shell_surface *shsurf = container_of(listener,
3571 struct shell_surface,
3572 surface_destroy_listener);
3573
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003574 if (shsurf->resource)
3575 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003576
3577 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003578}
3579
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003580static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003581fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003582{
3583 struct shell_surface *shsurf = data;
3584
3585 weston_surface_destroy(shsurf->surface);
3586}
3587
3588static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003589fade_out_done(struct weston_view_animation *animation, void *data)
3590{
3591 struct shell_surface *shsurf = data;
3592 struct wl_event_loop *loop;
3593
3594 loop = wl_display_get_event_loop(
3595 shsurf->surface->compositor->wl_display);
3596
3597 if (!shsurf->destroying) {
3598 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3599 shsurf->destroying = true;
3600 }
3601}
3602
3603static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003604handle_resource_destroy(struct wl_listener *listener, void *data)
3605{
3606 struct shell_surface *shsurf =
3607 container_of(listener, struct shell_surface,
3608 resource_destroy_listener);
3609
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003610 if (!weston_surface_is_mapped(shsurf->surface))
3611 return;
3612
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003613 shsurf->surface->ref_count++;
3614
3615 pixman_region32_fini(&shsurf->surface->pending.input);
3616 pixman_region32_init(&shsurf->surface->pending.input);
3617 pixman_region32_fini(&shsurf->surface->input);
3618 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003619 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3620 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3621 fade_out_done, shsurf);
3622 } else {
3623 weston_surface_destroy(shsurf->surface);
3624 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003625}
3626
3627static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003628shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003629
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003630struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003631get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003632{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003633 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003634 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003635 else
3636 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003637}
3638
Rafael Antognollie2a34552013-12-03 15:35:45 -02003639static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003640create_common_surface(struct shell_client *owner, void *shell,
3641 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003642 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003643{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003644 struct shell_surface *shsurf;
3645
Pekka Paalanen50b67472014-10-01 15:02:41 +03003646 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003647
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003648 shsurf = calloc(1, sizeof *shsurf);
3649 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003650 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003651 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003652 }
3653
Jason Ekstranda7af7042013-10-12 22:38:11 -05003654 shsurf->view = weston_view_create(surface);
3655 if (!shsurf->view) {
3656 weston_log("no memory to allocate shell surface\n");
3657 free(shsurf);
3658 return NULL;
3659 }
3660
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003661 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003662 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003663 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003664
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003665 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3666 wl_resource_add_destroy_listener(surface->resource,
3667 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003668 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003669
Tiago Vignattibc052c92012-04-19 16:18:18 +03003670 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003671 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003672 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003673 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003674 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003675 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003676 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3677 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003678 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003679 wl_list_init(&shsurf->fullscreen.transform.link);
3680
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003681 shsurf->output = get_default_output(shsurf->shell->compositor);
3682
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003683 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003684 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003685 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003686 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003687
3688 /* init link so its safe to always remove it in destroy_shell_surface */
3689 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003690 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003691
Pekka Paalanen460099f2012-01-20 16:48:25 +02003692 /* empty when not in use */
3693 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003694 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003695
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003696 wl_list_init(&shsurf->workspace_transform.link);
3697
Philip Withnall648a4dd2013-11-25 18:01:44 +00003698 wl_list_init(&shsurf->children_link);
3699 wl_list_init(&shsurf->children_list);
3700 shsurf->parent = NULL;
3701
Pekka Paalanenb5026542014-11-12 15:09:24 +02003702 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003703
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003704 shsurf->client = client;
3705
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003706 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003707}
3708
Rafael Antognollie2a34552013-12-03 15:35:45 -02003709static struct shell_surface *
3710create_shell_surface(void *shell, struct weston_surface *surface,
3711 const struct weston_shell_client *client)
3712{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003713 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003714}
3715
Jason Ekstranda7af7042013-10-12 22:38:11 -05003716static struct weston_view *
3717get_primary_view(void *shell, struct shell_surface *shsurf)
3718{
3719 return shsurf->view;
3720}
3721
Tiago Vignattibc052c92012-04-19 16:18:18 +03003722static void
3723shell_get_shell_surface(struct wl_client *client,
3724 struct wl_resource *resource,
3725 uint32_t id,
3726 struct wl_resource *surface_resource)
3727{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003728 struct weston_surface *surface =
3729 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003730 struct shell_client *sc = wl_resource_get_user_data(resource);
3731 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003732 struct shell_surface *shsurf;
3733
Pekka Paalanen50b67472014-10-01 15:02:41 +03003734 if (weston_surface_set_role(surface, "wl_shell_surface",
3735 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003736 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003737
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003738 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003739 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003740 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003741 return;
3742 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003743
Jason Ekstranda85118c2013-06-27 20:17:02 -05003744 shsurf->resource =
3745 wl_resource_create(client,
3746 &wl_shell_surface_interface, 1, id);
3747 wl_resource_set_implementation(shsurf->resource,
3748 &shell_surface_implementation,
3749 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003750 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003751}
3752
Rafael Antognollie2a34552013-12-03 15:35:45 -02003753static bool
3754shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3755{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003756 /* A shell surface without a resource is created from xwayland
3757 * and is considered a wl_shell surface for now. */
3758
3759 return shsurf->resource == NULL ||
3760 wl_resource_instance_of(shsurf->resource,
3761 &wl_shell_surface_interface,
3762 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003763}
3764
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003765static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003766 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003767};
3768
Rafael Antognollie2a34552013-12-03 15:35:45 -02003769/****************************
3770 * xdg-shell implementation */
3771
3772static void
3773xdg_surface_destroy(struct wl_client *client,
3774 struct wl_resource *resource)
3775{
3776 wl_resource_destroy(resource);
3777}
3778
3779static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003780xdg_surface_set_parent(struct wl_client *client,
3781 struct wl_resource *resource,
3782 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003783{
3784 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003785 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003786
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003787 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003788 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003789 shell_surface_set_parent(shsurf, parent->surface);
3790 } else {
3791 shell_surface_set_parent(shsurf, NULL);
3792 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003793}
3794
3795static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003796xdg_surface_set_app_id(struct wl_client *client,
3797 struct wl_resource *resource,
3798 const char *app_id)
3799{
3800 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3801
3802 free(shsurf->class);
3803 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003804 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003805}
3806
3807static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003808xdg_surface_show_window_menu(struct wl_client *client,
3809 struct wl_resource *surface_resource,
3810 struct wl_resource *seat_resource,
3811 uint32_t serial,
3812 int32_t x,
3813 int32_t y)
3814{
3815 /* TODO */
3816}
3817
3818static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003819xdg_surface_set_title(struct wl_client *client,
3820 struct wl_resource *resource, const char *title)
3821{
3822 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3823
3824 set_title(shsurf, title);
3825}
3826
3827static void
3828xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3829 struct wl_resource *seat_resource, uint32_t serial)
3830{
3831 common_surface_move(resource, seat_resource, serial);
3832}
3833
3834static void
3835xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3836 struct wl_resource *seat_resource, uint32_t serial,
3837 uint32_t edges)
3838{
3839 common_surface_resize(resource, seat_resource, serial, edges);
3840}
3841
3842static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003843xdg_surface_ack_configure(struct wl_client *client,
3844 struct wl_resource *resource,
3845 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003846{
3847 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3848
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003849 if (shsurf->state_requested) {
3850 shsurf->next_state = shsurf->requested_state;
3851 shsurf->state_changed = true;
3852 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003853 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003854}
3855
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003856static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003857xdg_surface_set_window_geometry(struct wl_client *client,
3858 struct wl_resource *resource,
3859 int32_t x,
3860 int32_t y,
3861 int32_t width,
3862 int32_t height)
3863{
3864 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3865
3866 set_window_geometry(shsurf, x, y, width, height);
3867}
3868
3869static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003870xdg_surface_set_maximized(struct wl_client *client,
3871 struct wl_resource *resource)
3872{
3873 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003874 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003875
3876 shsurf->state_requested = true;
3877 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003878
3879 if (!weston_surface_is_mapped(shsurf->surface))
3880 output = get_focused_output(shsurf->surface->compositor);
3881 else
3882 output = shsurf->surface->output;
3883
3884 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003885 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003886}
3887
3888static void
3889xdg_surface_unset_maximized(struct wl_client *client,
3890 struct wl_resource *resource)
3891{
3892 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3893
3894 shsurf->state_requested = true;
3895 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003896 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003897}
3898
3899static void
3900xdg_surface_set_fullscreen(struct wl_client *client,
3901 struct wl_resource *resource,
3902 struct wl_resource *output_resource)
3903{
3904 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3905 struct weston_output *output;
3906
3907 shsurf->state_requested = true;
3908 shsurf->requested_state.fullscreen = true;
3909
3910 if (output_resource)
3911 output = wl_resource_get_user_data(output_resource);
3912 else
3913 output = NULL;
3914
Marek Chalupa052917a2014-09-02 11:35:12 +02003915 /* handle clients launching in fullscreen */
3916 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3917 /* Set the output to the one that has focus currently. */
3918 assert(shsurf->surface);
3919 output = get_focused_output(shsurf->surface->compositor);
3920 }
3921
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003922 shell_surface_set_output(shsurf, output);
3923 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003924
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003925 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003926}
3927
3928static void
3929xdg_surface_unset_fullscreen(struct wl_client *client,
3930 struct wl_resource *resource)
3931{
3932 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3933
3934 shsurf->state_requested = true;
3935 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003936 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003937}
3938
3939static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003940xdg_surface_set_minimized(struct wl_client *client,
3941 struct wl_resource *resource)
3942{
3943 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3944
3945 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3946 return;
3947
3948 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003949 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003950}
3951
Rafael Antognollie2a34552013-12-03 15:35:45 -02003952static const struct xdg_surface_interface xdg_surface_implementation = {
3953 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003954 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003955 xdg_surface_set_title,
3956 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003957 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003958 xdg_surface_move,
3959 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003960 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003961 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003962 xdg_surface_set_maximized,
3963 xdg_surface_unset_maximized,
3964 xdg_surface_set_fullscreen,
3965 xdg_surface_unset_fullscreen,
3966 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003967};
3968
3969static void
3970xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003971 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003972{
3973 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003974 uint32_t *s;
3975 struct wl_array states;
3976 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003977
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003978 assert(shsurf);
3979
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003980 if (!shsurf->resource)
3981 return;
3982
3983 wl_array_init(&states);
3984 if (shsurf->requested_state.fullscreen) {
3985 s = wl_array_add(&states, sizeof *s);
3986 *s = XDG_SURFACE_STATE_FULLSCREEN;
3987 } else if (shsurf->requested_state.maximized) {
3988 s = wl_array_add(&states, sizeof *s);
3989 *s = XDG_SURFACE_STATE_MAXIMIZED;
3990 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003991 if (shsurf->resize_edges != 0) {
3992 s = wl_array_add(&states, sizeof *s);
3993 *s = XDG_SURFACE_STATE_RESIZING;
3994 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003995 if (shsurf->focus_count > 0) {
3996 s = wl_array_add(&states, sizeof *s);
3997 *s = XDG_SURFACE_STATE_ACTIVATED;
3998 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003999
4000 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4001 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4002
4003 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004004}
4005
4006static const struct weston_shell_client xdg_client = {
4007 xdg_send_configure
4008};
4009
4010static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004011xdg_shell_destroy(struct wl_client *client,
4012 struct wl_resource *resource)
4013{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004014 struct shell_client *sc = wl_resource_get_user_data(resource);
4015 struct wl_resource *shsurf_resource;
4016 struct shell_surface *shsurf;
4017
4018 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4019 shsurf = wl_resource_get_user_data(shsurf_resource);
4020 if (shsurf->owner_resource == resource) {
4021 wl_resource_post_error(
4022 resource,
4023 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4024 "not all child surface objects destroyed");
4025 return;
4026 }
4027 }
4028
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004029 wl_resource_destroy(resource);
4030}
4031
4032static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004033xdg_use_unstable_version(struct wl_client *client,
4034 struct wl_resource *resource,
4035 int32_t version)
4036{
4037 if (version > 1) {
4038 wl_resource_post_error(resource,
4039 1,
4040 "xdg-shell:: version not implemented yet.");
4041 return;
4042 }
4043}
4044
4045static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004046create_xdg_surface(struct shell_client *owner, void *shell,
4047 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004048 const struct weston_shell_client *client)
4049{
4050 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004051
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004052 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004053 if (!shsurf)
4054 return NULL;
4055
Pekka Paalanenb5026542014-11-12 15:09:24 +02004056 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004057
4058 return shsurf;
4059}
4060
4061static void
4062xdg_get_xdg_surface(struct wl_client *client,
4063 struct wl_resource *resource,
4064 uint32_t id,
4065 struct wl_resource *surface_resource)
4066{
4067 struct weston_surface *surface =
4068 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004069 struct shell_client *sc = wl_resource_get_user_data(resource);
4070 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004071 struct shell_surface *shsurf;
4072
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004073 shsurf = get_shell_surface(surface);
4074 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4075 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4076 "This wl_surface is already an "
4077 "xdg_surface");
4078 return;
4079 }
4080
Pekka Paalanen50b67472014-10-01 15:02:41 +03004081 if (weston_surface_set_role(surface, "xdg_surface",
4082 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004083 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004084
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004085 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004086 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004087 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004088 return;
4089 }
4090
4091 shsurf->resource =
4092 wl_resource_create(client,
4093 &xdg_surface_interface, 1, id);
4094 wl_resource_set_implementation(shsurf->resource,
4095 &xdg_surface_implementation,
4096 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004097 shsurf->owner_resource = resource;
4098 wl_list_insert(&sc->surface_list,
4099 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004100}
4101
4102static bool
4103shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4104{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004105 return shsurf->resource &&
4106 wl_resource_instance_of(shsurf->resource,
4107 &xdg_surface_interface,
4108 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004109}
4110
4111/* xdg-popup implementation */
4112
4113static void
4114xdg_popup_destroy(struct wl_client *client,
4115 struct wl_resource *resource)
4116{
4117 wl_resource_destroy(resource);
4118}
4119
Rafael Antognollie2a34552013-12-03 15:35:45 -02004120static const struct xdg_popup_interface xdg_popup_implementation = {
4121 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004122};
4123
4124static void
4125xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004126 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004127{
4128}
4129
4130static const struct weston_shell_client xdg_popup_client = {
4131 xdg_popup_send_configure
4132};
4133
4134static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004135create_xdg_popup(struct shell_client *owner, void *shell,
4136 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004137 const struct weston_shell_client *client,
4138 struct weston_surface *parent,
4139 struct shell_seat *seat,
4140 uint32_t serial,
4141 int32_t x, int32_t y)
4142{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004143 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004144
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004145 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004146 if (!shsurf)
4147 return NULL;
4148
Pekka Paalanenb5026542014-11-12 15:09:24 +02004149 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004150 shsurf->popup.shseat = seat;
4151 shsurf->popup.serial = serial;
4152 shsurf->popup.x = x;
4153 shsurf->popup.y = y;
4154 shell_surface_set_parent(shsurf, parent);
4155
4156 return shsurf;
4157}
4158
4159static void
4160xdg_get_xdg_popup(struct wl_client *client,
4161 struct wl_resource *resource,
4162 uint32_t id,
4163 struct wl_resource *surface_resource,
4164 struct wl_resource *parent_resource,
4165 struct wl_resource *seat_resource,
4166 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004167 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004168{
4169 struct weston_surface *surface =
4170 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004171 struct shell_client *sc = wl_resource_get_user_data(resource);
4172 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004173 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004174 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004175 struct weston_surface *parent;
4176 struct shell_seat *seat;
4177
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004178 shsurf = get_shell_surface(surface);
4179 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4180 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4181 "This wl_surface is already an "
4182 "xdg_popup");
4183 return;
4184 }
4185
Pekka Paalanen50b67472014-10-01 15:02:41 +03004186 if (weston_surface_set_role(surface, "xdg_popup",
4187 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004188 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189
4190 if (!parent_resource) {
4191 wl_resource_post_error(surface_resource,
4192 WL_DISPLAY_ERROR_INVALID_OBJECT,
4193 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004194 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004195 }
4196
4197 parent = wl_resource_get_user_data(parent_resource);
4198 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4199
Jonas Ådahlee45a552015-03-18 16:37:47 +08004200 /* Verify that we are creating the top most popup when mapping,
4201 * as it's not until then we know whether it was mapped as most
4202 * top level or not. */
4203
4204 parent_shsurf = get_shell_surface(parent);
4205 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4206 !shell_surface_is_xdg_surface(parent_shsurf)) {
4207 wl_resource_post_error(resource,
4208 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4209 "xdg_popup parent was invalid");
4210 return;
4211 }
4212
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004213 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004214 parent, seat, serial, x, y);
4215 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004216 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004217 return;
4218 }
4219
4220 shsurf->resource =
4221 wl_resource_create(client,
4222 &xdg_popup_interface, 1, id);
4223 wl_resource_set_implementation(shsurf->resource,
4224 &xdg_popup_implementation,
4225 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004226 shsurf->owner_resource = resource;
4227 wl_list_insert(&sc->surface_list,
4228 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004229}
4230
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004231static void
4232xdg_pong(struct wl_client *client,
4233 struct wl_resource *resource, uint32_t serial)
4234{
4235 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004236
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004237 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004238}
4239
Rafael Antognollie2a34552013-12-03 15:35:45 -02004240static bool
4241shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4242{
4243 return wl_resource_instance_of(shsurf->resource,
4244 &xdg_popup_interface,
4245 &xdg_popup_implementation);
4246}
4247
4248static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004249 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004250 xdg_use_unstable_version,
4251 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004252 xdg_get_xdg_popup,
4253 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004254};
4255
4256static int
4257xdg_shell_unversioned_dispatch(const void *implementation,
4258 void *_target, uint32_t opcode,
4259 const struct wl_message *message,
4260 union wl_argument *args)
4261{
4262 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004263 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004264
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004265 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004266 wl_resource_post_error(resource,
4267 WL_DISPLAY_ERROR_INVALID_OBJECT,
4268 "must call use_unstable_version first");
4269 return 0;
4270 }
4271
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004272#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004273
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004274 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4275 "shell implementation doesn't match protocol version");
4276
Rafael Antognollie2a34552013-12-03 15:35:45 -02004277 if (args[0].i != XDG_SERVER_VERSION) {
4278 wl_resource_post_error(resource,
4279 WL_DISPLAY_ERROR_INVALID_OBJECT,
4280 "incompatible version, server is %d "
4281 "client wants %d",
4282 XDG_SERVER_VERSION, args[0].i);
4283 return 0;
4284 }
4285
4286 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004287 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004288
4289 return 1;
4290}
4291
4292/* end of xdg-shell implementation */
4293/***********************************/
4294
Kristian Høgsberg07937562011-04-12 17:25:42 -04004295static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004296shell_fade(struct desktop_shell *shell, enum fade_type type);
4297
4298static int
4299screensaver_timeout(void *data)
4300{
4301 struct desktop_shell *shell = data;
4302
4303 shell_fade(shell, FADE_OUT);
4304
4305 return 1;
4306}
4307
4308static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004309handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02004310{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004311 struct desktop_shell *shell =
4312 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004313
Pekka Paalanen18027e52011-12-02 16:31:49 +02004314 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02004315
4316 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02004317 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02004318}
4319
4320static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004321launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004322{
4323 if (shell->screensaver.binding)
4324 return;
4325
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004326 if (!shell->screensaver.path) {
4327 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004328 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02004329 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004330
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004331 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004332 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05004333 return;
4334 }
4335
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004336 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004337 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004338 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02004339 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004340}
4341
4342static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004343terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004344{
Pekka Paalanen18027e52011-12-02 16:31:49 +02004345 if (shell->screensaver.process.pid == 0)
4346 return;
4347
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03004348 /* Disarm the screensaver timer, otherwise it may fire when the
4349 * compositor is not in the idle state. In that case, the screen will
4350 * be locked, but the wake_signal won't fire on user input, making the
4351 * system unresponsive. */
4352 wl_event_source_timer_update(shell->screensaver.timer, 0);
4353
Pekka Paalanen18027e52011-12-02 16:31:49 +02004354 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004355}
4356
4357static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004358configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004359{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004361
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004362 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 if (v->output == ev->output && v != ev) {
4364 weston_view_unmap(v);
4365 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004366 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004367 }
4368 }
4369
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004370 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004371
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004372 if (wl_list_empty(&ev->layer_link.link)) {
4373 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004374 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004375 }
4376}
4377
Pekka Paalanen8274d902014-08-06 19:36:51 +03004378static int
4379background_get_label(struct weston_surface *surface, char *buf, size_t len)
4380{
4381 return snprintf(buf, len, "background for output %s",
4382 surface->output->name);
4383}
4384
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004385static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004386background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004387{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004388 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004389 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004390
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 view = container_of(es->views.next, struct weston_view, surface_link);
4392
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004393 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004394}
4395
4396static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004397desktop_shell_set_background(struct wl_client *client,
4398 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004399 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004400 struct wl_resource *surface_resource)
4401{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004402 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004403 struct weston_surface *surface =
4404 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004405 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004406
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004407 if (surface->configure) {
4408 wl_resource_post_error(surface_resource,
4409 WL_DISPLAY_ERROR_INVALID_OBJECT,
4410 "surface role already assigned");
4411 return;
4412 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004413
Jason Ekstranda7af7042013-10-12 22:38:11 -05004414 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4415 weston_view_destroy(view);
4416 view = weston_view_create(surface);
4417
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004418 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004419 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004420 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004421 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004423 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004424 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004425 surface->output->width,
4426 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004427}
4428
Pekka Paalanen8274d902014-08-06 19:36:51 +03004429static int
4430panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4431{
4432 return snprintf(buf, len, "panel for output %s",
4433 surface->output->name);
4434}
4435
Kristian Høgsberg75840622011-09-06 13:48:16 -04004436static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004437panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004438{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004439 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004440 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004441
Jason Ekstranda7af7042013-10-12 22:38:11 -05004442 view = container_of(es->views.next, struct weston_view, surface_link);
4443
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004444 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004445}
4446
4447static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004448desktop_shell_set_panel(struct wl_client *client,
4449 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004450 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004451 struct wl_resource *surface_resource)
4452{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004453 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004454 struct weston_surface *surface =
4455 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004456 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004457
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004458 if (surface->configure) {
4459 wl_resource_post_error(surface_resource,
4460 WL_DISPLAY_ERROR_INVALID_OBJECT,
4461 "surface role already assigned");
4462 return;
4463 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004464
Jason Ekstranda7af7042013-10-12 22:38:11 -05004465 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4466 weston_view_destroy(view);
4467 view = weston_view_create(surface);
4468
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004469 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004470 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004471 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004472 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004473 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004474 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004475 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004476 surface->output->width,
4477 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004478}
4479
Pekka Paalanen8274d902014-08-06 19:36:51 +03004480static int
4481lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4482{
4483 return snprintf(buf, len, "lock window");
4484}
4485
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004486static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004487lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004488{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004489 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004490 struct weston_view *view;
4491
4492 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004493
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004494 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004495 return;
4496
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004498
4499 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004500 weston_layer_entry_insert(&shell->lock_layer.view_list,
4501 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004502 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004503 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004504 }
4505}
4506
4507static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004508handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004509{
Tiago Vignattibe143262012-04-16 17:31:41 +03004510 struct desktop_shell *shell =
4511 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004512
Martin Minarik6d118362012-06-07 18:01:59 +02004513 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004514 shell->lock_surface = NULL;
4515}
4516
4517static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004518desktop_shell_set_lock_surface(struct wl_client *client,
4519 struct wl_resource *resource,
4520 struct wl_resource *surface_resource)
4521{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004522 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004523 struct weston_surface *surface =
4524 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004525
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004526 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004527
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004528 if (!shell->locked)
4529 return;
4530
Pekka Paalanen98262232011-12-01 10:42:22 +02004531 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004532
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004533 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004534 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004535 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004536
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004537 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004538 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004539 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004540 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004541}
4542
4543static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004544resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004545{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004546 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004547
Pekka Paalanen77346a62011-11-30 16:26:35 +02004548 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004549
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004550 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004551 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004552 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004553 &shell->input_panel_layer.link);
4554 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004555 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004556 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004557 wl_list_insert(&shell->compositor->cursor_layer.link,
4558 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004559 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004560 wl_list_insert(&shell->fullscreen_layer.link,
4561 &shell->panel_layer.link);
4562 wl_list_insert(&shell->panel_layer.link,
4563 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004564
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004565 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004566
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004567 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004568 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004569 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004570}
4571
4572static void
4573desktop_shell_unlock(struct wl_client *client,
4574 struct wl_resource *resource)
4575{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004576 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004577
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004578 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004579
4580 if (shell->locked)
4581 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004582}
4583
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004584static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004585desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004586 struct wl_resource *resource,
4587 struct wl_resource *surface_resource)
4588{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004589 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004590
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004591 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004592 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004593}
4594
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004595static void
4596desktop_shell_desktop_ready(struct wl_client *client,
4597 struct wl_resource *resource)
4598{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004599 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004600
4601 shell_fade_startup(shell);
4602}
4603
Jonny Lamb765760d2014-08-20 15:53:19 +02004604static void
4605desktop_shell_set_panel_position(struct wl_client *client,
4606 struct wl_resource *resource,
4607 uint32_t position)
4608{
4609 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4610
4611 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4612 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4613 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4614 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4615 wl_resource_post_error(resource,
4616 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4617 "bad position argument");
4618 return;
4619 }
4620
4621 shell->panel_position = position;
4622}
4623
Kristian Høgsberg75840622011-09-06 13:48:16 -04004624static const struct desktop_shell_interface desktop_shell_implementation = {
4625 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004626 desktop_shell_set_panel,
4627 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004628 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004629 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004630 desktop_shell_desktop_ready,
4631 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004632};
4633
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004634static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004635get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004636{
4637 struct shell_surface *shsurf;
4638
4639 shsurf = get_shell_surface(surface);
4640 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004641 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004642 return shsurf->type;
4643}
4644
Kristian Høgsberg75840622011-09-06 13:48:16 -04004645static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004646move_binding(struct weston_seat *seat, uint32_t time, 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
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004652 if (seat->pointer->focus == NULL)
4653 return;
4654
4655 focus = seat->pointer->focus->surface;
4656
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 Foreman8aeeac82015-01-30 13:24:36 -06004666 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004667}
4668
4669static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004670maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4671{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004672 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004673 struct weston_surface *surface;
4674 struct shell_surface *shsurf;
4675
4676 surface = weston_surface_get_main_surface(focus);
4677 if (surface == NULL)
4678 return;
4679
4680 shsurf = get_shell_surface(surface);
4681 if (shsurf == NULL)
4682 return;
4683
4684 if (!shell_surface_is_xdg_surface(shsurf))
4685 return;
4686
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004687 shsurf->state_requested = true;
4688 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004689 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004690}
4691
4692static void
4693fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4694{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004695 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004696 struct weston_surface *surface;
4697 struct shell_surface *shsurf;
4698
4699 surface = weston_surface_get_main_surface(focus);
4700 if (surface == NULL)
4701 return;
4702
4703 shsurf = get_shell_surface(surface);
4704 if (shsurf == NULL)
4705 return;
4706
4707 if (!shell_surface_is_xdg_surface(shsurf))
4708 return;
4709
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004710 shsurf->state_requested = true;
4711 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004712 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004713 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004714}
4715
4716static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004717touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4718{
Derek Foreman362656b2014-09-04 10:23:05 -05004719 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004720 struct weston_surface *surface;
4721 struct shell_surface *shsurf;
4722
Derek Foreman362656b2014-09-04 10:23:05 -05004723 if (seat->touch->focus == NULL)
4724 return;
4725
4726 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004727 surface = weston_surface_get_main_surface(focus);
4728 if (surface == NULL)
4729 return;
4730
4731 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004732 if (shsurf == NULL || shsurf->state.fullscreen ||
4733 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004734 return;
4735
Derek Foreman8aeeac82015-01-30 13:24:36 -06004736 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004737}
4738
4739static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004740resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004741{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004742 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004743 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004744 uint32_t edges = 0;
4745 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004746 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004747
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004748 if (seat->pointer->focus == NULL)
4749 return;
4750
4751 focus = seat->pointer->focus->surface;
4752
Pekka Paalanen01388e22013-04-25 13:57:44 +03004753 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004754 if (surface == NULL)
4755 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004756
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004757 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004758 if (shsurf == NULL || shsurf->state.fullscreen ||
4759 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004760 return;
4761
Jason Ekstranda7af7042013-10-12 22:38:11 -05004762 weston_view_from_global(shsurf->view,
4763 wl_fixed_to_int(seat->pointer->grab_x),
4764 wl_fixed_to_int(seat->pointer->grab_y),
4765 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004766
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004767 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004768 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004769 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004770 edges |= 0;
4771 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004772 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004773
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004774 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004775 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004776 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004777 edges |= 0;
4778 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004779 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004780
Derek Foreman8aeeac82015-01-30 13:24:36 -06004781 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004782}
4783
4784static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004785surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004786 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004787{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004788 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004789 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004790 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004791 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004792
Pekka Paalanen01388e22013-04-25 13:57:44 +03004793 /* XXX: broken for windows containing sub-surfaces */
4794 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004795 if (surface == NULL)
4796 return;
4797
4798 shsurf = get_shell_surface(surface);
4799 if (!shsurf)
4800 return;
4801
Jason Ekstranda7af7042013-10-12 22:38:11 -05004802 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004803
Jason Ekstranda7af7042013-10-12 22:38:11 -05004804 if (shsurf->view->alpha > 1.0)
4805 shsurf->view->alpha = 1.0;
4806 if (shsurf->view->alpha < step)
4807 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004808
Jason Ekstranda7af7042013-10-12 22:38:11 -05004809 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004810 weston_surface_damage(surface);
4811}
4812
4813static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004814do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004815 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004816{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004817 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004818 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004819 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004820
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004821 if (!seat->pointer) {
4822 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4823 return;
4824 }
4825
Scott Moreauccbf29d2012-02-22 14:21:41 -07004826 wl_list_for_each(output, &compositor->output_list, link) {
4827 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004828 wl_fixed_to_double(seat->pointer->x),
4829 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004830 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004831 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004832 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004833 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004834 increment = -output->zoom.increment;
4835 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004836 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004837 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004838 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004839 else
4840 increment = 0;
4841
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004842 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004843
Scott Moreaue6603982012-06-11 13:07:51 -06004844 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004845 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004846 else if (output->zoom.level > output->zoom.max_level)
4847 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004848 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004849 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004850 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004851
Scott Moreaue6603982012-06-11 13:07:51 -06004852 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004853
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004855 }
4856 }
4857}
4858
Scott Moreauccbf29d2012-02-22 14:21:41 -07004859static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004860zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004861 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004862{
4863 do_zoom(seat, time, 0, axis, value);
4864}
4865
4866static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004867zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004868 void *data)
4869{
4870 do_zoom(seat, time, key, 0, 0);
4871}
4872
4873static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004874terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004875 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004876{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004877 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004878
Daniel Stone325fc2d2012-05-30 16:31:58 +01004879 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004880}
4881
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004882static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004883rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4884 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004885{
4886 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004887 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004888 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004889 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004890 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004891
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004892 weston_pointer_move(pointer, x, y);
4893
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004894 if (!shsurf)
4895 return;
4896
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004897 cx = 0.5f * shsurf->surface->width;
4898 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004899
Daniel Stone37816df2012-05-16 18:45:18 +01004900 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4901 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004902 r = sqrtf(dx * dx + dy * dy);
4903
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004904 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004905 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004906
4907 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004908 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004909 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004910
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004911 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004912 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004913
4914 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004915 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004916 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004917 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004918 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004919
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004920 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004921 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004922 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004923 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004924 wl_list_init(&shsurf->rotation.transform.link);
4925 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004926 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004927 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004928
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004929 /* We need to adjust the position of the surface
4930 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004931 cposx = shsurf->view->geometry.x + cx;
4932 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004933 dposx = rotate->center.x - cposx;
4934 dposy = rotate->center.y - cposy;
4935 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004936 weston_view_set_position(shsurf->view,
4937 shsurf->view->geometry.x + dposx,
4938 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004939 }
4940
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004941 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004942 * lazily applies the damage due to rotation update.
4943 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004944 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004945}
4946
4947static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004948rotate_grab_button(struct weston_pointer_grab *grab,
4949 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004950{
4951 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004952 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004953 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004954 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004955 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004956
Daniel Stone4dbadb12012-05-30 16:31:51 +01004957 if (pointer->button_count == 0 &&
4958 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004959 if (shsurf)
4960 weston_matrix_multiply(&shsurf->rotation.rotation,
4961 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004962 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963 free(rotate);
4964 }
4965}
4966
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004967static void
4968rotate_grab_cancel(struct weston_pointer_grab *grab)
4969{
4970 struct rotate_grab *rotate =
4971 container_of(grab, struct rotate_grab, base.grab);
4972
4973 shell_grab_end(&rotate->base);
4974 free(rotate);
4975}
4976
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004977static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004978 noop_grab_focus,
4979 rotate_grab_motion,
4980 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004981 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004982};
4983
4984static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004985surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004986{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004987 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004988 float dx, dy;
4989 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004990
Pekka Paalanen460099f2012-01-20 16:48:25 +02004991 rotate = malloc(sizeof *rotate);
4992 if (!rotate)
4993 return;
4994
Jason Ekstranda7af7042013-10-12 22:38:11 -05004995 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004996 surface->surface->width * 0.5f,
4997 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004998 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004999
Daniel Stone37816df2012-05-16 18:45:18 +01005000 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
5001 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005002 r = sqrtf(dx * dx + dy * dy);
5003 if (r > 20.0f) {
5004 struct weston_matrix inverse;
5005
5006 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005007 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005008 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005009
5010 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005011 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005012 } else {
5013 weston_matrix_init(&surface->rotation.rotation);
5014 weston_matrix_init(&rotate->rotation);
5015 }
5016
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005017 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
5018 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005019}
5020
5021static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005022rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005023 void *data)
5024{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005025 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005026 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005027 struct shell_surface *surface;
5028
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005029 if (seat->pointer->focus == NULL)
5030 return;
5031
5032 focus = seat->pointer->focus->surface;
5033
Pekka Paalanen01388e22013-04-25 13:57:44 +03005034 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005035 if (base_surface == NULL)
5036 return;
5037
5038 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005039 if (surface == NULL || surface->state.fullscreen ||
5040 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005041 return;
5042
5043 surface_rotate(surface, seat);
5044}
5045
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005046/* Move all fullscreen layers down to the current workspace and hide their
5047 * black views. The surfaces' state is set to both fullscreen and lowered,
5048 * and this is reversed when such a surface is re-configured, see
5049 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5050 *
5051 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005052 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005053void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005054lower_fullscreen_layer(struct desktop_shell *shell)
5055{
5056 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005057 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005058
5059 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005060 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005061 &shell->fullscreen_layer.view_list.link,
5062 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005063 struct shell_surface *shsurf = get_shell_surface(view->surface);
5064
5065 if (!shsurf)
5066 continue;
5067
5068 /* We can have a non-fullscreen popup for a fullscreen surface
5069 * in the fullscreen layer. */
5070 if (shsurf->state.fullscreen) {
5071 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005072 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5073 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005074 weston_view_damage_below(shsurf->fullscreen.black_view);
5075
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005076 }
5077
5078 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005079 weston_layer_entry_remove(&view->layer_link);
5080 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005081 weston_view_damage_below(view);
5082 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005083
5084 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005085 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005086}
5087
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005088void
Tiago Vignattibe143262012-04-16 17:31:41 +03005089activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005090 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005091{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005092 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005093 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005094 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005095 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005096 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005097
Kristian Høgsberg6110d072014-04-29 15:15:45 -07005098 lower_fullscreen_layer(shell);
5099
Pekka Paalanen01388e22013-04-25 13:57:44 +03005100 main_surface = weston_surface_get_main_surface(es);
5101
Daniel Stone37816df2012-05-16 18:45:18 +01005102 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005103
Jonas Ådahl8538b222012-08-29 22:13:03 +02005104 state = ensure_focus_state(shell, seat);
5105 if (state == NULL)
5106 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005107
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005108 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005109 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005110
Rafael Antognolli03b16592013-12-03 15:35:42 -02005111 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005112 assert(shsurf);
5113
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005114 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005115 shell_configure_fullscreen(shsurf);
5116 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005117 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005118
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005119 /* Update the surface’s layer. This brings it to the top of the stacking
5120 * order as appropriate. */
5121 shell_surface_update_layer(shsurf);
5122
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005123 if (shell->focus_animation_type != ANIMATION_NONE) {
5124 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005125 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005126 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005127}
5128
Alex Wu21858432012-04-01 20:13:08 +08005129/* no-op func for checking black surface */
5130static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005131black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005132{
5133}
5134
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005135static bool
Alex Wu21858432012-04-01 20:13:08 +08005136is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5137{
5138 if (es->configure == black_surface_configure) {
5139 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005140 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005141 return true;
5142 }
5143 return false;
5144}
5145
Kristian Høgsberg75840622011-09-06 13:48:16 -04005146static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005147activate_binding(struct weston_seat *seat,
5148 struct desktop_shell *shell,
5149 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005150{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005151 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005152
Alex Wu9c35e6b2012-03-05 14:13:13 +08005153 if (!focus)
5154 return;
5155
Pekka Paalanen01388e22013-04-25 13:57:44 +03005156 if (is_black_surface(focus, &main_surface))
5157 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005158
Pekka Paalanen01388e22013-04-25 13:57:44 +03005159 main_surface = weston_surface_get_main_surface(focus);
5160 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005161 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005162
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005163 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005164}
5165
5166static void
5167click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5168 void *data)
5169{
5170 if (seat->pointer->grab != &seat->pointer->default_grab)
5171 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005172 if (seat->pointer->focus == NULL)
5173 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005174
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005175 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005176}
5177
5178static void
5179touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5180{
5181 if (seat->touch->grab != &seat->touch->default_grab)
5182 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005183 if (seat->touch->focus == NULL)
5184 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005185
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005186 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005187}
5188
5189static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005190unfocus_all_seats(struct desktop_shell *shell)
5191{
5192 struct weston_seat *seat, *next;
5193
5194 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5195 if (seat->keyboard == NULL)
5196 continue;
5197
5198 weston_keyboard_set_focus(seat->keyboard, NULL);
5199 }
5200}
5201
5202static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005203lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005204{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005205 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005206
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005207 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005208 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005209 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005210 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005211
5212 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005213
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005214 /* Hide all surfaces by removing the fullscreen, panel and
5215 * toplevel layers. This way nothing else can show or receive
5216 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005217
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005218 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005219 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005220 if (shell->showing_input_panels)
5221 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005222 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005223 wl_list_insert(&shell->compositor->cursor_layer.link,
5224 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005225
Pekka Paalanen77346a62011-11-30 16:26:35 +02005226 launch_screensaver(shell);
5227
Neil Robertsb4a91702014-04-09 16:33:32 +01005228 /* Remove the keyboard focus on all seats. This will be
5229 * restored to the workspace's saved state via
5230 * restore_focus_state when the compositor is unlocked */
5231 unfocus_all_seats(shell);
5232
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005233 /* TODO: disable bindings that should not work while locked. */
5234
5235 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005236}
5237
5238static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005239unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005240{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005241 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005242 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005243 return;
5244 }
5245
5246 /* If desktop-shell client has gone away, unlock immediately. */
5247 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005248 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005249 return;
5250 }
5251
5252 if (shell->prepare_event_sent)
5253 return;
5254
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005255 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005256 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005257}
5258
5259static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005260shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005261{
5262 struct desktop_shell *shell = data;
5263
5264 shell->fade.animation = NULL;
5265
5266 switch (shell->fade.type) {
5267 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005268 weston_surface_destroy(shell->fade.view->surface);
5269 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005270 break;
5271 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005272 lock(shell);
5273 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005274 default:
5275 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005276 }
5277}
5278
Jason Ekstranda7af7042013-10-12 22:38:11 -05005279static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005280shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005281{
5282 struct weston_compositor *compositor = shell->compositor;
5283 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005284 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005285
5286 surface = weston_surface_create(compositor);
5287 if (!surface)
5288 return NULL;
5289
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 view = weston_view_create(surface);
5291 if (!view) {
5292 weston_surface_destroy(surface);
5293 return NULL;
5294 }
5295
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005296 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005297 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005298 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005299 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5300 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005301 pixman_region32_init(&surface->input);
5302
Jason Ekstranda7af7042013-10-12 22:38:11 -05005303 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005304}
5305
5306static void
5307shell_fade(struct desktop_shell *shell, enum fade_type type)
5308{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005309 float tint;
5310
5311 switch (type) {
5312 case FADE_IN:
5313 tint = 0.0;
5314 break;
5315 case FADE_OUT:
5316 tint = 1.0;
5317 break;
5318 default:
5319 weston_log("shell: invalid fade type\n");
5320 return;
5321 }
5322
5323 shell->fade.type = type;
5324
Jason Ekstranda7af7042013-10-12 22:38:11 -05005325 if (shell->fade.view == NULL) {
5326 shell->fade.view = shell_fade_create_surface(shell);
5327 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005328 return;
5329
Jason Ekstranda7af7042013-10-12 22:38:11 -05005330 shell->fade.view->alpha = 1.0 - tint;
5331 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005332 }
5333
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005334 if (shell->fade.view->output == NULL) {
5335 /* If the black view gets a NULL output, we lost the
5336 * last output and we'll just cancel the fade. This
5337 * happens when you close the last window under the
5338 * X11 or Wayland backends. */
5339 shell->locked = false;
5340 weston_surface_destroy(shell->fade.view->surface);
5341 shell->fade.view = NULL;
5342 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005343 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005344 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005345 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005346 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005347 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005348 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005349 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005350}
5351
5352static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005353do_shell_fade_startup(void *data)
5354{
5355 struct desktop_shell *shell = data;
5356
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005357 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005358 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005359 } else {
5360 weston_log("desktop shell: "
5361 "unexpected fade-in animation type %d\n",
5362 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005363 weston_surface_destroy(shell->fade.view->surface);
5364 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005365 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005366}
5367
5368static void
5369shell_fade_startup(struct desktop_shell *shell)
5370{
5371 struct wl_event_loop *loop;
5372
5373 if (!shell->fade.startup_timer)
5374 return;
5375
5376 wl_event_source_remove(shell->fade.startup_timer);
5377 shell->fade.startup_timer = NULL;
5378
5379 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5380 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5381}
5382
5383static int
5384fade_startup_timeout(void *data)
5385{
5386 struct desktop_shell *shell = data;
5387
5388 shell_fade_startup(shell);
5389 return 0;
5390}
5391
5392static void
5393shell_fade_init(struct desktop_shell *shell)
5394{
5395 /* Make compositor output all black, and wait for the desktop-shell
5396 * client to signal it is ready, then fade in. The timer triggers a
5397 * fade-in, in case the desktop-shell client takes too long.
5398 */
5399
5400 struct wl_event_loop *loop;
5401
Jason Ekstranda7af7042013-10-12 22:38:11 -05005402 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005403 weston_log("%s: warning: fade surface already exists\n",
5404 __func__);
5405 return;
5406 }
5407
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005408 if (shell->startup_animation_type == ANIMATION_NONE)
5409 return;
5410
Jason Ekstranda7af7042013-10-12 22:38:11 -05005411 shell->fade.view = shell_fade_create_surface(shell);
5412 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005413 return;
5414
Jason Ekstranda7af7042013-10-12 22:38:11 -05005415 weston_view_update_transform(shell->fade.view);
5416 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005417
5418 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5419 shell->fade.startup_timer =
5420 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5421 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5422}
5423
5424static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005425idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005426{
5427 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005428 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005429 struct weston_seat *seat;
5430
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005431 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005432 if (seat->pointer)
5433 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005434 if (seat->touch)
5435 touch_popup_grab_end(seat->touch);
5436 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005437
5438 shell_fade(shell, FADE_OUT);
5439 /* lock() is called from shell_fade_done() */
5440}
5441
5442static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005443wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005444{
5445 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005446 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005447
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005448 unlock(shell);
5449}
5450
5451static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005452center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005453{
Giulio Camuffob8366642013-04-25 13:57:46 +03005454 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005455 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005456
Jason Ekstranda7af7042013-10-12 22:38:11 -05005457 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005458
5459 x = output->x + (output->width - width) / 2 - surf_x / 2;
5460 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005461
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005462 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005463}
5464
5465static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005466weston_view_set_initial_position(struct weston_view *view,
5467 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005468{
5469 struct weston_compositor *compositor = shell->compositor;
5470 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005471 int32_t range_x, range_y;
5472 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005473 struct weston_output *output, *target_output = NULL;
5474 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005475 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005476
5477 /* As a heuristic place the new window on the same output as the
5478 * pointer. Falling back to the output containing 0, 0.
5479 *
5480 * TODO: Do something clever for touch too?
5481 */
5482 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005483 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005484 ix = wl_fixed_to_int(seat->pointer->x);
5485 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005486 break;
5487 }
5488 }
5489
5490 wl_list_for_each(output, &compositor->output_list, link) {
5491 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5492 target_output = output;
5493 break;
5494 }
5495 }
5496
5497 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005498 weston_view_set_position(view, 10 + random() % 400,
5499 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005500 return;
5501 }
5502
5503 /* Valid range within output where the surface will still be onscreen.
5504 * If this is negative it means that the surface is bigger than
5505 * output.
5506 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005507 get_output_work_area(shell, target_output, &area);
5508
5509 dx = area.x;
5510 dy = area.y;
5511 range_x = area.width - view->surface->width;
5512 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005513
Rob Bradford4cb88c72012-08-13 15:18:44 +01005514 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005515 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005516
5517 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005518 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005519
5520 x = target_output->x + dx;
5521 y = target_output->y + dy;
5522
Jason Ekstranda7af7042013-10-12 22:38:11 -05005523 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005524}
5525
5526static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005527set_maximized_position(struct desktop_shell *shell,
5528 struct shell_surface *shsurf)
5529{
5530 int32_t surf_x, surf_y;
5531 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005532 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005533
Jonny Lambd73c6942014-08-20 15:53:20 +02005534 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005535 if (shsurf->has_set_geometry) {
5536 surf_x = shsurf->geometry.x;
5537 surf_y = shsurf->geometry.y;
5538 } else {
5539 surface_subsurfaces_boundingbox(shsurf->surface,
5540 &surf_x, &surf_y, NULL, NULL);
5541 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005542 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005543
5544 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005545 e->x1 + area.x - surf_x,
5546 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005547}
5548
5549static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005550map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005551 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005552{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005553 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005554 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005555
Pekka Paalanen77346a62011-11-30 16:26:35 +02005556 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005557 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005558 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005559 if (shsurf->state.fullscreen) {
5560 center_on_output(shsurf->view, shsurf->fullscreen_output);
5561 shell_map_fullscreen(shsurf);
5562 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005563 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005564 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005565 weston_view_set_initial_position(shsurf->view, shell);
5566 }
Juan Zhao96879df2012-02-07 08:45:41 +08005567 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005568 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005569 if (shell_map_popup(shsurf) != 0)
5570 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005571 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005572 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005573 weston_view_set_position(shsurf->view,
5574 shsurf->view->geometry.x + sx,
5575 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005576 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005577 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005578 default:
5579 ;
5580 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005581
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005582 /* Surface stacking order, see also activate(). */
5583 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005584
Jason Ekstranda7af7042013-10-12 22:38:11 -05005585 if (shsurf->type != SHELL_SURFACE_NONE) {
5586 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005587 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005588 shsurf->surface->output = shsurf->output;
5589 shsurf->view->output = shsurf->output;
5590 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005591 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005592
Jason Ekstranda7af7042013-10-12 22:38:11 -05005593 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005594 /* XXX: xwayland's using the same fields for transient type */
5595 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005596 if (shsurf->transient.flags ==
5597 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5598 break;
5599 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005600 if (shsurf->state.relative &&
5601 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5602 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005603 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005604 break;
5605 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005606 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005607 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005608 case SHELL_SURFACE_POPUP:
5609 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005610 default:
5611 break;
5612 }
5613
Rafael Antognolli03b16592013-12-03 15:35:42 -02005614 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5615 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005616 {
5617 switch (shell->win_animation_type) {
5618 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005619 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005620 break;
5621 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005622 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005623 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005624 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005625 default:
5626 break;
5627 }
5628 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005629}
5630
5631static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005632configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005633 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005634{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005635 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005636 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005637
Pekka Paalanen77346a62011-11-30 16:26:35 +02005638 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005639
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005640 assert(shsurf);
5641
Rafael Antognolli03b16592013-12-03 15:35:42 -02005642 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005643 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005644 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005645 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005646 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005647 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005648 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005649
Alex Wu4539b082012-03-01 12:57:46 +08005650 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005651 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005652 wl_list_for_each(view, &surface->views, surface_link)
5653 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005654
Rafael Antognolli03b16592013-12-03 15:35:42 -02005655 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005656 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005657 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005658}
5659
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005660static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005661shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005662{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005663 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005664 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005665 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005666
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005667 assert(shsurf);
5668
5669 shell = shsurf->shell;
5670
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005671 if (!weston_surface_is_mapped(es) &&
5672 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005673 remove_popup_grab(shsurf);
5674 }
5675
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005676 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005677 return;
5678
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005679 if (shsurf->has_next_geometry) {
5680 shsurf->geometry = shsurf->next_geometry;
5681 shsurf->has_next_geometry = false;
5682 shsurf->has_set_geometry = true;
5683 } else if (!shsurf->has_set_geometry) {
5684 surface_subsurfaces_boundingbox(shsurf->surface,
5685 &shsurf->geometry.x,
5686 &shsurf->geometry.y,
5687 &shsurf->geometry.width,
5688 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005689 }
5690
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005691 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005692 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005693 type_changed = 1;
5694 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005695
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005696 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005697 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005698 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005699 shsurf->last_width != es->width ||
5700 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005701 float from_x, from_y;
5702 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005703
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005704 if (shsurf->resize_edges) {
5705 sx = 0;
5706 sy = 0;
5707 }
5708
5709 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5710 sx = shsurf->last_width - es->width;
5711 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5712 sy = shsurf->last_height - es->height;
5713
5714 shsurf->last_width = es->width;
5715 shsurf->last_height = es->height;
5716
Jason Ekstranda7af7042013-10-12 22:38:11 -05005717 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5718 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005719 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005720 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005721 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005722 }
5723}
5724
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005725static bool
5726check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5727{
5728 struct timespec now;
5729
5730 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5731 return false;
5732
5733 /*
5734 * If the shell helper client dies before the session has been
5735 * up for roughly 30 seconds, better just make Weston shut down,
5736 * because the user likely has no way to interact with the desktop
5737 * anyway.
5738 */
5739 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5740 weston_log("Error: %s apparently cannot run at all.\n",
5741 shell->client);
5742 weston_log_continue(STAMP_SPACE "Quitting...");
5743 wl_display_terminate(shell->compositor->wl_display);
5744
5745 return true;
5746 }
5747
5748 return false;
5749}
5750
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005751static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005752
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005753static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005754respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005755{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005756 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005757
5758 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5759 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005760 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005761 shell->child.deathstamp = time;
5762 shell->child.deathcount = 0;
5763 }
5764
5765 shell->child.deathcount++;
5766 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005767 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005768 return;
5769 }
5770
Pekka Paalanen826dc142014-08-27 12:11:53 +03005771 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005772 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005773}
5774
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005775static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005776desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5777{
5778 struct desktop_shell *shell;
5779
5780 shell = container_of(listener, struct desktop_shell,
5781 child.client_destroy_listener);
5782
Pekka Paalanen826dc142014-08-27 12:11:53 +03005783 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005784 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005785 /*
5786 * unbind_desktop_shell() will reset shell->child.desktop_shell
5787 * before the respawned process has a chance to create a new
5788 * desktop_shell object, because we are being called from the
5789 * wl_client destructor which destroys all wl_resources before
5790 * returning.
5791 */
5792
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005793 if (!check_desktop_shell_crash_too_early(shell))
5794 respawn_desktop_shell_process(shell);
5795
Pekka Paalanen826dc142014-08-27 12:11:53 +03005796 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005797}
5798
5799static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005800launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005801{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005802 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005803
Pekka Paalanen826dc142014-08-27 12:11:53 +03005804 shell->child.client = weston_client_start(shell->compositor,
5805 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005806
Arnaud Vrac42631192014-08-25 20:56:46 +02005807 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005808 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005809 return;
5810 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005811
5812 shell->child.client_destroy_listener.notify =
5813 desktop_shell_client_destroy;
5814 wl_client_add_destroy_listener(shell->child.client,
5815 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005816}
5817
5818static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005819handle_shell_client_destroy(struct wl_listener *listener, void *data)
5820{
5821 struct shell_client *sc =
5822 container_of(listener, struct shell_client, destroy_listener);
5823
5824 if (sc->ping_timer)
5825 wl_event_source_remove(sc->ping_timer);
5826 free(sc);
5827}
5828
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005829static struct shell_client *
5830shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5831 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005832{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005833 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005834
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005835 sc = zalloc(sizeof *sc);
5836 if (sc == NULL) {
5837 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005838 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005839 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005840
5841 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005842 if (sc->resource == NULL) {
5843 free(sc);
5844 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005845 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005846 }
5847
5848 sc->client = client;
5849 sc->shell = shell;
5850 sc->destroy_listener.notify = handle_shell_client_destroy;
5851 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005852 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005853
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005854 return sc;
5855}
5856
5857static void
5858bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5859{
5860 struct desktop_shell *shell = data;
5861 struct shell_client *sc;
5862
5863 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5864 if (sc)
5865 wl_resource_set_implementation(sc->resource,
5866 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005867 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005868}
5869
5870static void
5871bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5872{
5873 struct desktop_shell *shell = data;
5874 struct shell_client *sc;
5875
5876 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5877 if (sc)
5878 wl_resource_set_dispatcher(sc->resource,
5879 xdg_shell_unversioned_dispatch,
5880 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005881}
5882
5883static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005884unbind_desktop_shell(struct wl_resource *resource)
5885{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005886 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005887
5888 if (shell->locked)
5889 resume_desktop(shell);
5890
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005891 shell->child.desktop_shell = NULL;
5892 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005893}
5894
5895static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005896bind_desktop_shell(struct wl_client *client,
5897 void *data, uint32_t version, uint32_t id)
5898{
Tiago Vignattibe143262012-04-16 17:31:41 +03005899 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005900 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005901
Jason Ekstranda85118c2013-06-27 20:17:02 -05005902 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005903 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005904
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005905 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005906 wl_resource_set_implementation(resource,
5907 &desktop_shell_implementation,
5908 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005909 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005910
5911 if (version < 2)
5912 shell_fade_startup(shell);
5913
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005914 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005915 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005916
5917 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5918 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005919}
5920
Pekka Paalanen8274d902014-08-06 19:36:51 +03005921static int
5922screensaver_get_label(struct weston_surface *surface, char *buf, size_t len)
5923{
5924 return snprintf(buf, len, "screensaver for output %s",
5925 surface->output->name);
5926}
5927
Pekka Paalanen6e168112011-11-24 11:34:05 +02005928static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005929screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005930{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005931 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005932 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005933 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005934
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005935 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005936 return;
5937
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005938 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005939 if (!shell->locked)
5940 return;
5941
Jason Ekstranda7af7042013-10-12 22:38:11 -05005942 view = container_of(surface->views.next, struct weston_view, surface_link);
5943 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005944
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005945 if (wl_list_empty(&view->layer_link.link)) {
5946 prev = container_of(shell->lock_layer.view_list.link.prev,
5947 struct weston_layer_entry, link);
5948 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005949 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005950 wl_event_source_timer_update(shell->screensaver.timer,
5951 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005952 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005953 }
5954}
5955
5956static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005957screensaver_set_surface(struct wl_client *client,
5958 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005959 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005960 struct wl_resource *output_resource)
5961{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005962 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005963 struct weston_surface *surface =
5964 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005965 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005966 struct weston_view *view, *next;
5967
5968 /* Make sure we only have one view */
5969 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5970 weston_view_destroy(view);
5971 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005972
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005973 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005974 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005975 weston_surface_set_label_func(surface, screensaver_get_label);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005976 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005977}
5978
5979static const struct screensaver_interface screensaver_implementation = {
5980 screensaver_set_surface
5981};
5982
5983static void
5984unbind_screensaver(struct wl_resource *resource)
5985{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005986 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005987
Pekka Paalanen77346a62011-11-30 16:26:35 +02005988 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005989}
5990
5991static void
5992bind_screensaver(struct wl_client *client,
5993 void *data, uint32_t version, uint32_t id)
5994{
Tiago Vignattibe143262012-04-16 17:31:41 +03005995 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005996 struct wl_resource *resource;
5997
Jason Ekstranda85118c2013-06-27 20:17:02 -05005998 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005999
Pekka Paalanen77346a62011-11-30 16:26:35 +02006000 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006001 wl_resource_set_implementation(resource,
6002 &screensaver_implementation,
6003 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02006004 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02006005 return;
6006 }
6007
6008 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6009 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02006010}
6011
Kristian Høgsberg07045392012-02-19 18:52:44 -05006012struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006013 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006014 struct weston_surface *current;
6015 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006016 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006017 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006018};
6019
6020static void
6021switcher_next(struct switcher *switcher)
6022{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006023 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006024 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006025 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006026 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006027
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006028 /* temporary re-display minimized surfaces */
6029 struct weston_view *tmp;
6030 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006031 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6032 weston_layer_entry_remove(&view->layer_link);
6033 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006034 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6035 *minimized = view;
6036 }
6037
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006038 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006039 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006040 if (shsurf &&
6041 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6042 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006043 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006044 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006045 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006046 next = view->surface;
6047 prev = view->surface;
6048 view->alpha = 0.25;
6049 weston_view_geometry_dirty(view);
6050 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006051 }
Alex Wu1659daa2012-04-01 20:13:09 +08006052
Jason Ekstranda7af7042013-10-12 22:38:11 -05006053 if (is_black_surface(view->surface, NULL)) {
6054 view->alpha = 0.25;
6055 weston_view_geometry_dirty(view);
6056 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006057 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006058 }
6059
6060 if (next == NULL)
6061 next = first;
6062
Alex Wu07b26062012-03-12 16:06:01 +08006063 if (next == NULL)
6064 return;
6065
Kristian Høgsberg07045392012-02-19 18:52:44 -05006066 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006067 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006068
6069 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006070 wl_list_for_each(view, &next->views, surface_link)
6071 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006072
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006073 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006074 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006075 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006076}
6077
6078static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006079switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006080{
6081 struct switcher *switcher =
6082 container_of(listener, struct switcher, listener);
6083
6084 switcher_next(switcher);
6085}
6086
6087static void
Daniel Stone351eb612012-05-31 15:27:47 -04006088switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006089{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006090 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006091 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006092 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006093
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006094 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006095 if (is_focus_view(view))
6096 continue;
6097
Jason Ekstranda7af7042013-10-12 22:38:11 -05006098 view->alpha = 1.0;
6099 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006100 }
6101
Alex Wu07b26062012-03-12 16:06:01 +08006102 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01006103 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06006104 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006105 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006106 weston_keyboard_end_grab(keyboard);
6107 if (keyboard->input_method_resource)
6108 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006109
6110 /* re-hide surfaces that were temporary shown during the switch */
6111 struct weston_view **minimized;
6112 wl_array_for_each(minimized, &switcher->minimized_array) {
6113 /* with the exception of the current selected */
6114 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006115 weston_layer_entry_remove(&(*minimized)->layer_link);
6116 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006117 weston_view_damage_below(*minimized);
6118 }
6119 }
6120 wl_array_release(&switcher->minimized_array);
6121
Kristian Høgsberg07045392012-02-19 18:52:44 -05006122 free(switcher);
6123}
6124
6125static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006126switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006127 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006128{
6129 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006130 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006131
Daniel Stonec9785ea2012-05-30 16:31:52 +01006132 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006133 switcher_next(switcher);
6134}
6135
6136static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006137switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006138 uint32_t mods_depressed, uint32_t mods_latched,
6139 uint32_t mods_locked, uint32_t group)
6140{
6141 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006142 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006143
Daniel Stone351eb612012-05-31 15:27:47 -04006144 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6145 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006146}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006147
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006148static void
6149switcher_cancel(struct weston_keyboard_grab *grab)
6150{
6151 struct switcher *switcher = container_of(grab, struct switcher, grab);
6152
6153 switcher_destroy(switcher);
6154}
6155
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006156static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006157 switcher_key,
6158 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006159 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006160};
6161
6162static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006163switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006164 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006165{
Tiago Vignattibe143262012-04-16 17:31:41 +03006166 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006167 struct switcher *switcher;
6168
6169 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006170 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006171 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006172 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006173 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006174 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006175
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006176 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006177 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006178 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006179 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6180 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006181 switcher_next(switcher);
6182}
6183
Pekka Paalanen3c647232011-12-22 13:43:43 +02006184static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006185backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006186 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006187{
6188 struct weston_compositor *compositor = data;
6189 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006190 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006191
6192 /* TODO: we're limiting to simple use cases, where we assume just
6193 * control on the primary display. We'd have to extend later if we
6194 * ever get support for setting backlights on random desktop LCD
6195 * panels though */
6196 output = get_default_output(compositor);
6197 if (!output)
6198 return;
6199
6200 if (!output->set_backlight)
6201 return;
6202
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006203 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6204 backlight_new = output->backlight_current - 25;
6205 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6206 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006207
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006208 if (backlight_new < 5)
6209 backlight_new = 5;
6210 if (backlight_new > 255)
6211 backlight_new = 255;
6212
6213 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006214 output->set_backlight(output, output->backlight_current);
6215}
6216
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006217static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006218force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006219 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006220{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006221 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006222 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006223 struct desktop_shell *shell = data;
6224 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006225 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006226
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006227 focus_surface = seat->keyboard->focus;
6228 if (!focus_surface)
6229 return;
6230
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006231 wl_signal_emit(&compositor->kill_signal, focus_surface);
6232
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006233 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006234 wl_client_get_credentials(client, &pid, NULL, NULL);
6235
6236 /* Skip clients that we launched ourselves (the credentials of
6237 * the socketpair is ours) */
6238 if (pid == getpid())
6239 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006240
Daniel Stone325fc2d2012-05-30 16:31:58 +01006241 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006242}
6243
6244static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006245workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006246 uint32_t key, void *data)
6247{
6248 struct desktop_shell *shell = data;
6249 unsigned int new_index = shell->workspaces.current;
6250
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006251 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006252 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006253 if (new_index != 0)
6254 new_index--;
6255
6256 change_workspace(shell, new_index);
6257}
6258
6259static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006260workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006261 uint32_t key, void *data)
6262{
6263 struct desktop_shell *shell = data;
6264 unsigned int new_index = shell->workspaces.current;
6265
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006266 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006267 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006268 if (new_index < shell->workspaces.num - 1)
6269 new_index++;
6270
6271 change_workspace(shell, new_index);
6272}
6273
6274static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006275workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006276 uint32_t key, void *data)
6277{
6278 struct desktop_shell *shell = data;
6279 unsigned int new_index;
6280
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006281 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006282 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006283 new_index = key - KEY_F1;
6284 if (new_index >= shell->workspaces.num)
6285 new_index = shell->workspaces.num - 1;
6286
6287 change_workspace(shell, new_index);
6288}
6289
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006290static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006291workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006292 uint32_t key, void *data)
6293{
6294 struct desktop_shell *shell = data;
6295 unsigned int new_index = shell->workspaces.current;
6296
6297 if (shell->locked)
6298 return;
6299
6300 if (new_index != 0)
6301 new_index--;
6302
6303 take_surface_to_workspace_by_seat(shell, seat, new_index);
6304}
6305
6306static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006307workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006308 uint32_t key, void *data)
6309{
6310 struct desktop_shell *shell = data;
6311 unsigned int new_index = shell->workspaces.current;
6312
6313 if (shell->locked)
6314 return;
6315
6316 if (new_index < shell->workspaces.num - 1)
6317 new_index++;
6318
6319 take_surface_to_workspace_by_seat(shell, seat, new_index);
6320}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006321
6322static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006323shell_reposition_view_on_output_destroy(struct weston_view *view)
6324{
6325 struct weston_output *output, *first_output;
6326 struct weston_compositor *ec = view->surface->compositor;
6327 struct shell_surface *shsurf;
6328 float x, y;
6329 int visible;
6330
6331 x = view->geometry.x;
6332 y = view->geometry.y;
6333
6334 /* At this point the destroyed output is not in the list anymore.
6335 * If the view is still visible somewhere, we leave where it is,
6336 * otherwise, move it to the first output. */
6337 visible = 0;
6338 wl_list_for_each(output, &ec->output_list, link) {
6339 if (pixman_region32_contains_point(&output->region,
6340 x, y, NULL)) {
6341 visible = 1;
6342 break;
6343 }
6344 }
6345
6346 if (!visible) {
6347 first_output = container_of(ec->output_list.next,
6348 struct weston_output, link);
6349
6350 x = first_output->x + first_output->width / 4;
6351 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006352
6353 weston_view_set_position(view, x, y);
6354 } else {
6355 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006356 }
6357
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006358
6359 shsurf = get_shell_surface(view->surface);
6360
6361 if (shsurf) {
6362 shsurf->saved_position_valid = false;
6363 shsurf->next_state.maximized = false;
6364 shsurf->next_state.fullscreen = false;
6365 shsurf->state_changed = true;
6366 }
6367}
6368
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006369void
6370shell_for_each_layer(struct desktop_shell *shell,
6371 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006372{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006373 struct workspace **ws;
6374
6375 func(shell, &shell->fullscreen_layer, data);
6376 func(shell, &shell->panel_layer, data);
6377 func(shell, &shell->background_layer, data);
6378 func(shell, &shell->lock_layer, data);
6379 func(shell, &shell->input_panel_layer, data);
6380
6381 wl_array_for_each(ws, &shell->workspaces.array)
6382 func(shell, &(*ws)->layer, data);
6383}
6384
6385static void
6386shell_output_destroy_move_layer(struct desktop_shell *shell,
6387 struct weston_layer *layer,
6388 void *data)
6389{
6390 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006391 struct weston_view *view;
6392
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006393 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006394 if (view->output != output)
6395 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006396
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006397 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006398 }
6399}
6400
6401static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006402handle_output_destroy(struct wl_listener *listener, void *data)
6403{
6404 struct shell_output *output_listener =
6405 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006406 struct weston_output *output = output_listener->output;
6407 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006408
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006409 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006410
Xiong Zhang6b481422013-10-23 13:58:32 +08006411 wl_list_remove(&output_listener->destroy_listener.link);
6412 wl_list_remove(&output_listener->link);
6413 free(output_listener);
6414}
6415
6416static void
6417create_shell_output(struct desktop_shell *shell,
6418 struct weston_output *output)
6419{
6420 struct shell_output *shell_output;
6421
6422 shell_output = zalloc(sizeof *shell_output);
6423 if (shell_output == NULL)
6424 return;
6425
6426 shell_output->output = output;
6427 shell_output->shell = shell;
6428 shell_output->destroy_listener.notify = handle_output_destroy;
6429 wl_signal_add(&output->destroy_signal,
6430 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006431 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006432}
6433
6434static void
6435handle_output_create(struct wl_listener *listener, void *data)
6436{
6437 struct desktop_shell *shell =
6438 container_of(listener, struct desktop_shell, output_create_listener);
6439 struct weston_output *output = (struct weston_output *)data;
6440
6441 create_shell_output(shell, output);
6442}
6443
6444static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006445handle_output_move_layer(struct desktop_shell *shell,
6446 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006447{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006448 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006449 struct weston_view *view;
6450 float x, y;
6451
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006452 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006453 if (view->output != output)
6454 continue;
6455
6456 x = view->geometry.x + output->move_x;
6457 y = view->geometry.y + output->move_y;
6458 weston_view_set_position(view, x, y);
6459 }
6460}
6461
6462static void
6463handle_output_move(struct wl_listener *listener, void *data)
6464{
6465 struct desktop_shell *shell;
6466
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006467 shell = container_of(listener, struct desktop_shell,
6468 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006469
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006470 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006471}
6472
6473static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006474setup_output_destroy_handler(struct weston_compositor *ec,
6475 struct desktop_shell *shell)
6476{
6477 struct weston_output *output;
6478
6479 wl_list_init(&shell->output_list);
6480 wl_list_for_each(output, &ec->output_list, link)
6481 create_shell_output(shell, output);
6482
6483 shell->output_create_listener.notify = handle_output_create;
6484 wl_signal_add(&ec->output_created_signal,
6485 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006486
6487 shell->output_move_listener.notify = handle_output_move;
6488 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006489}
6490
6491static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006492shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006493{
Tiago Vignattibe143262012-04-16 17:31:41 +03006494 struct desktop_shell *shell =
6495 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006496 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006497 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006498
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006499 /* Force state to unlocked so we don't try to fade */
6500 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006501
6502 if (shell->child.client) {
6503 /* disable respawn */
6504 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006505 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006506 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006507
Ryo Munakata76fb7ec2015-03-08 19:17:06 +09006508 wl_event_source_remove(shell->screensaver.timer);
6509
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006510 wl_list_remove(&shell->idle_listener.link);
6511 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006512
6513 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006514
Xiong Zhang6b481422013-10-23 13:58:32 +08006515 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6516 wl_list_remove(&shell_output->destroy_listener.link);
6517 wl_list_remove(&shell_output->link);
6518 free(shell_output);
6519 }
6520
6521 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006522 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006523
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006524 wl_array_for_each(ws, &shell->workspaces.array)
6525 workspace_destroy(*ws);
6526 wl_array_release(&shell->workspaces.array);
6527
Pekka Paalanen3c647232011-12-22 13:43:43 +02006528 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006529 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006530 free(shell);
6531}
6532
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006533static void
6534shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6535{
6536 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006537 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006538
6539 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006540 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6541 MODIFIER_CTRL | MODIFIER_ALT,
6542 terminate_binding, ec);
6543 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6544 click_to_activate_binding,
6545 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006546 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6547 click_to_activate_binding,
6548 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006549 weston_compositor_add_touch_binding(ec, 0,
6550 touch_to_activate_binding,
6551 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006552 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6553 MODIFIER_SUPER | MODIFIER_ALT,
6554 surface_opacity_binding, NULL);
6555 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6556 MODIFIER_SUPER, zoom_axis_binding,
6557 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006558
6559 /* configurable bindings */
6560 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006561 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6562 zoom_key_binding, NULL);
6563 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6564 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006565 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6566 maximize_binding, NULL);
6567 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6568 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006569 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6570 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006571 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006572 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6573 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006574 weston_compositor_add_button_binding(ec, BTN_LEFT,
6575 mod | MODIFIER_SHIFT,
6576 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006577
6578 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6579 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6580 rotate_binding, NULL);
6581
Daniel Stone325fc2d2012-05-30 16:31:58 +01006582 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6583 shell);
6584 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6585 ec);
6586 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6587 backlight_binding, ec);
6588 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6589 ec);
6590 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6591 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006592 weston_compositor_add_key_binding(ec, KEY_K, mod,
6593 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006594 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6595 workspace_up_binding, shell);
6596 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6597 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006598 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6599 workspace_move_surface_up_binding,
6600 shell);
6601 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6602 workspace_move_surface_down_binding,
6603 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006604
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006605 if (shell->exposay_modifier)
6606 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6607 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006608
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006609 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6610 if (shell->workspaces.num > 1) {
6611 num_workspace_bindings = shell->workspaces.num;
6612 if (num_workspace_bindings > 6)
6613 num_workspace_bindings = 6;
6614 for (i = 0; i < num_workspace_bindings; i++)
6615 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6616 workspace_f_binding,
6617 shell);
6618 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006619
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006620 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006621}
6622
Jason Ekstrand024177c2014-04-21 19:42:58 -05006623static void
6624handle_seat_created(struct wl_listener *listener, void *data)
6625{
6626 struct weston_seat *seat = data;
6627
6628 create_shell_seat(seat);
6629}
6630
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006631WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006632module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006633 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006634{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006635 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006636 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006637 struct workspace **pws;
6638 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006639 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006640
Peter Huttererf3d62272013-08-08 11:57:05 +10006641 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006642 if (shell == NULL)
6643 return -1;
6644
Kristian Høgsberg75840622011-09-06 13:48:16 -04006645 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006646
6647 shell->destroy_listener.notify = shell_destroy;
6648 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006649 shell->idle_listener.notify = idle_handler;
6650 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6651 shell->wake_listener.notify = wake_handler;
6652 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006653
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006654 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006655 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006656 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006657 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006658 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006659 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006660 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006661 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006662 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006663 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006664 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006665 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006666 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006667
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006668 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6669 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006670 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6671 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006672 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006673
6674 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006675 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006676
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006677 if (input_panel_setup(shell) < 0)
6678 return -1;
6679
Murray Calavera9a51cd72015-06-10 21:16:02 +00006680 if (text_backend_init(ec) < 0)
6681 return -1;
6682
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006683 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006684
Daniel Stonedf8133b2013-11-19 11:37:14 +01006685 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6686 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6687
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006688 for (i = 0; i < shell->workspaces.num; i++) {
6689 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6690 if (pws == NULL)
6691 return -1;
6692
6693 *pws = workspace_create();
6694 if (*pws == NULL)
6695 return -1;
6696 }
6697 activate_workspace(shell, 0);
6698
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006699 weston_layer_init(&shell->minimized_layer, NULL);
6700
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006701 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006702 wl_list_init(&shell->workspaces.animation.link);
6703 shell->workspaces.animation.frame = animate_workspace_change_frame;
6704
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006705 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006706 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006707 return -1;
6708
Rafael Antognollie2a34552013-12-03 15:35:45 -02006709 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6710 shell, bind_xdg_shell) == NULL)
6711 return -1;
6712
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006713 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006714 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006715 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006716 return -1;
6717
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006718 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6719 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006720 return -1;
6721
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006722 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6723 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006724 return -1;
6725
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006726 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006727
Jonny Lamb765760d2014-08-20 15:53:19 +02006728 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6729
Xiong Zhang6b481422013-10-23 13:58:32 +08006730 setup_output_destroy_handler(ec, shell);
6731
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006732 loop = wl_display_get_event_loop(ec->wl_display);
6733 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006734
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006735 shell->screensaver.timer =
6736 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6737
Jason Ekstrand024177c2014-04-21 19:42:58 -05006738 wl_list_for_each(seat, &ec->seat_list, link)
6739 handle_seat_created(NULL, seat);
6740 shell->seat_create_listener.notify = handle_seat_created;
6741 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006742
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006743 screenshooter_create(ec);
6744
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006745 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006746
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006747 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006748
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006749 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6750
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006751 return 0;
6752}