blob: 64979cdb96687852f6834d18fe533dfa49b2c88a [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040030#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031#include <string.h>
32#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040033#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020034#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020035#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020036#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040037#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030040#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080041#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080044#include "xdg-shell-unstable-v5-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050045
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020047#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010049#ifndef static_assert
50#define static_assert(cond, msg)
51#endif
52
Jonas Ådahl04769742012-06-13 00:01:24 +020053struct focus_state {
54 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040055 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020056 struct weston_surface *keyboard_focus;
57 struct wl_list link;
58 struct wl_listener seat_destroy_listener;
59 struct wl_listener surface_destroy_listener;
60};
61
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050062enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020063 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050064 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030065 SHELL_SURFACE_POPUP,
66 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020067};
68
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050069struct shell_client;
70
Philip Withnall648a4dd2013-11-25 18:01:44 +000071/*
72 * Surface stacking and ordering.
73 *
74 * This is handled using several linked lists of surfaces, organised into
75 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
76 * above all of the surfaces in the layer below. The set of layers is static and
77 * in the following order (top-most first):
78 * • Lock layer (only ever displayed on its own)
79 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010080 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000081 * • Fullscreen layer
82 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000083 * • Workspace layers
84 * • Background layer
85 *
86 * The list of layers may be manipulated to remove whole layers of surfaces from
87 * display. For example, when locking the screen, all layers except the lock
88 * layer are removed.
89 *
90 * A surface’s layer is modified on configuring the surface, in
91 * set_surface_type() (which is only called when the surface’s type change is
92 * _committed_). If a surface’s type changes (e.g. when making a window
93 * fullscreen) its layer changes too.
94 *
95 * In order to allow popup and transient surfaces to be correctly stacked above
96 * their parent surfaces, each surface tracks both its parent surface, and a
97 * linked list of its children. When a surface’s layer is updated, so are the
98 * layers of its children. Note that child surfaces are *not* the same as
99 * subsurfaces — child/parent surfaces are purely for maintaining stacking
100 * order.
101 *
102 * The children_link list of siblings of a surface (i.e. those surfaces which
103 * have the same parent) only contains weston_surfaces which have a
104 * shell_surface. Stacking is not implemented for non-shell_surface
105 * weston_surfaces. This means that the following implication does *not* hold:
106 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
107 */
108
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200109struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500110 struct wl_resource *resource;
111 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500112 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800113 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500115 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500116 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600117 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200118 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700119 struct wl_listener resource_destroy_listener;
120
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400121 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000122 struct wl_list children_list; /* child surfaces of this one */
123 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300124 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200125
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800126 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400127 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500128 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800129 bool saved_position_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;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500201 bool 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
Derek Foreman74de4692015-07-15 13:00:39 -0500253surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
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;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200311 struct weston_view *view;
312 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800313
314 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100315
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300316 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800317 return false;
318
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200319 /* Find topmost shsurf on the same fullscreen output on which shsurf
320 * is displaying. We don't care about other outputs.
321 */
322 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
323 layer_link.link) {
324 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
325
326 if (cand_shsurf &&
327 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
328 top_fs_shsurf = cand_shsurf;
329 break;
330 }
331 }
332
333 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800334}
335
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500336static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400337destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338{
339 struct shell_grab *grab;
340
341 grab = container_of(listener, struct shell_grab,
342 shsurf_destroy_listener);
343
344 grab->shsurf = NULL;
345}
346
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800347struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348get_default_view(struct weston_surface *surface)
349{
350 struct shell_surface *shsurf;
351 struct weston_view *view;
352
353 if (!surface || wl_list_empty(&surface->views))
354 return NULL;
355
356 shsurf = get_shell_surface(surface);
357 if (shsurf)
358 return shsurf->view;
359
360 wl_list_for_each(view, &surface->views, surface_link)
361 if (weston_view_is_mapped(view))
362 return view;
363
364 return container_of(surface->views.next, struct weston_view, surface_link);
365}
366
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400368popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200369static void
370touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400371
372static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300373shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400374 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300375 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400376 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800377 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300379 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500380 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300381
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400382 popup_grab_end(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -0500383 if (touch)
384 touch_popup_grab_end(touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400385
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300386 grab->grab.interface = interface;
387 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400388 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500389 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400390 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300391
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700392 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400393 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400394 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800395 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400396 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397 weston_pointer_set_focus(pointer,
398 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400399 wl_fixed_from_int(0),
400 wl_fixed_from_int(0));
401 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300402}
403
Jonny Lambd73c6942014-08-20 15:53:20 +0200404static void
405get_output_panel_size(struct desktop_shell *shell,
406 struct weston_output *output,
407 int *width,
408 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700409{
410 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200411
412 *width = 0;
413 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700414
415 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200416 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700417
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300418 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200419 float x, y;
420
421 if (view->surface->output != output)
422 continue;
423
424 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800425 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
426 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200427 weston_view_to_global_float(view,
428 view->surface->width, 0,
429 &x, &y);
430
Derek Foreman612341f2015-04-15 12:23:55 -0500431 *width = (int)x - output->x;
432 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200433 return;
434
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800435 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
436 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200437 weston_view_to_global_float(view,
438 0, view->surface->height,
439 &x, &y);
440
Derek Foreman612341f2015-04-15 12:23:55 -0500441 *width = view->surface->width + (int)x - output->x;
442 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200443 return;
444
445 default:
446 /* we've already set width and height to
447 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700448 break;
449 }
450 }
451
Jonny Lambd73c6942014-08-20 15:53:20 +0200452 /* the correct view wasn't found */
453}
454
455static void
Quentin Glidic5c201952016-03-23 13:50:49 +0100456get_output_work_area(void *data,
Jonny Lambd73c6942014-08-20 15:53:20 +0200457 struct weston_output *output,
458 pixman_rectangle32_t *area)
459{
Quentin Glidic5c201952016-03-23 13:50:49 +0100460 struct desktop_shell *shell = data;
Jonny Lambd73c6942014-08-20 15:53:20 +0200461 int32_t panel_width = 0, panel_height = 0;
462
Derek Foremanf814c5d2015-04-15 12:23:54 -0500463 area->x = output->x;
464 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200465
466 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200467 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800468 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200469 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500470 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800471 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200472 area->width = output->width;
473 area->height = output->height - panel_height;
474 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800475 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500476 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800477 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200478 area->width = output->width - panel_width;
479 area->height = output->height;
480 break;
481 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700482}
483
Derek Foreman45a7c272015-09-11 14:27:40 -0500484static struct shell_surface *
485find_toplevel_surface(struct shell_surface *in_surface)
486{
487 struct shell_surface *surface = in_surface;
488
Ben Hummon445e44c2015-10-06 11:48:14 -0500489 while (surface) {
490 if (surface->type == SHELL_SURFACE_TOPLEVEL)
491 return surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500492 surface = get_shell_surface(surface->parent);
Ben Hummon445e44c2015-10-06 11:48:14 -0500493 }
Derek Foreman45a7c272015-09-11 14:27:40 -0500494
495 /* If no top level surface was found, just use whatever surface was
496 originally provided. */
Ben Hummon445e44c2015-10-06 11:48:14 -0500497 return in_surface;
Derek Foreman45a7c272015-09-11 14:27:40 -0500498}
499
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700500static void
501send_configure_for_surface(struct shell_surface *shsurf)
502{
503 int32_t width, height;
504 struct surface_state *state;
505
506 if (shsurf->state_requested)
507 state = &shsurf->requested_state;
508 else if (shsurf->state_changed)
509 state = &shsurf->next_state;
510 else
511 state = &shsurf->state;
512
513 if (state->fullscreen) {
514 width = shsurf->output->width;
515 height = shsurf->output->height;
516 } else if (state->maximized) {
517 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200518 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700519
520 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200521 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700522
Jonny Lambd73c6942014-08-20 15:53:20 +0200523 width = area.width;
524 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900525 } else if (shsurf->resize_edges) {
526 width = shsurf->geometry.width;
527 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700528 } else {
529 width = 0;
530 height = 0;
531 }
532
533 shsurf->client->send_configure(shsurf->surface, width, height);
534}
535
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300536static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400537shell_surface_state_changed(struct shell_surface *shsurf)
538{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700539 if (shell_surface_is_xdg_surface(shsurf))
540 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400541}
542
543static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300544shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300545{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700546 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400547 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700548 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400549
550 if (grab->shsurf->resize_edges) {
551 grab->shsurf->resize_edges = 0;
552 shell_surface_state_changed(grab->shsurf);
553 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700554 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300555
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700556 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300557}
558
559static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700560shell_touch_grab_start(struct shell_touch_grab *grab,
561 const struct weston_touch_grab_interface *interface,
562 struct shell_surface *shsurf,
563 struct weston_touch *touch)
564{
565 struct desktop_shell *shell = shsurf->shell;
Derek Foreman1281a362015-07-31 16:55:32 -0500566 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800567
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200568 touch_popup_grab_end(touch);
Derek Foreman1281a362015-07-31 16:55:32 -0500569 if (pointer)
570 popup_grab_end(pointer);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700571
Rusty Lynch1084da52013-08-15 09:10:08 -0700572 grab->grab.interface = interface;
573 grab->shsurf = shsurf;
574 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
575 wl_signal_add(&shsurf->destroy_signal,
576 &grab->shsurf_destroy_listener);
577
578 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700579 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700580
581 weston_touch_start_grab(touch, &grab->grab);
582 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500583 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700585}
586
587static void
588shell_touch_grab_end(struct shell_touch_grab *grab)
589{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700590 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700591 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700592 grab->shsurf->grabbed = 0;
593 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700594
595 weston_touch_end_grab(grab->touch);
596}
597
598static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500599center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800600 struct weston_output *output);
601
Daniel Stone496ca172012-05-30 16:31:42 +0100602static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300603get_modifier(char *modifier)
604{
605 if (!modifier)
606 return MODIFIER_SUPER;
607
608 if (!strcmp("ctrl", modifier))
609 return MODIFIER_CTRL;
610 else if (!strcmp("alt", modifier))
611 return MODIFIER_ALT;
612 else if (!strcmp("super", modifier))
613 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000614 else if (!strcmp("none", modifier))
615 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300616 else
617 return MODIFIER_SUPER;
618}
619
Juan Zhaoe10d2792012-04-25 19:09:52 +0800620static enum animation_type
621get_animation_type(char *animation)
622{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800623 if (!animation)
624 return ANIMATION_NONE;
625
Juan Zhaoe10d2792012-04-25 19:09:52 +0800626 if (!strcmp("zoom", animation))
627 return ANIMATION_ZOOM;
628 else if (!strcmp("fade", animation))
629 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100630 else if (!strcmp("dim-layer", animation))
631 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800632 else
633 return ANIMATION_NONE;
634}
635
Alex Wu4539b082012-03-01 12:57:46 +0800636static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400637shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200638{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400639 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500640 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300641 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000642 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200643
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300644 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400645 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300646 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
647 WESTON_SHELL_CLIENT);
648 if (ret < 0)
649 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400650 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500651 "client", &s, client);
652 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100653 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000654
655 weston_config_section_get_bool(section,
656 "allow-zap", &allow_zap, true);
657 shell->allow_zap = allow_zap;
658
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100659 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400660 "binding-modifier", &s, "super");
661 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200662 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800663
664 weston_config_section_get_string(section,
665 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000666 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800667 free(s);
668
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400669 weston_config_section_get_string(section, "animation", &s, "none");
670 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200671 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200672 weston_config_section_get_string(section, "close-animation", &s, "fade");
673 shell->win_close_animation_type = get_animation_type(s);
674 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700675 weston_config_section_get_string(section,
676 "startup-animation", &s, "fade");
677 shell->startup_animation_type = get_animation_type(s);
678 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700679 if (shell->startup_animation_type == ANIMATION_ZOOM)
680 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681 weston_config_section_get_string(section, "focus-animation", &s, "none");
682 shell->focus_animation_type = get_animation_type(s);
683 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400684 weston_config_section_get_uint(section, "num-workspaces",
685 &shell->workspaces.num,
686 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200687}
688
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800689struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100690get_default_output(struct weston_compositor *compositor)
691{
Armin Krezović10b06182016-06-23 11:59:30 +0200692 if (wl_list_empty(&compositor->output_list))
693 return NULL;
694
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100695 return container_of(compositor->output_list.next,
696 struct weston_output, link);
697}
698
Pekka Paalanen8274d902014-08-06 19:36:51 +0300699static int
700focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
701{
702 return snprintf(buf, len, "focus highlight effect for output %s",
703 surface->output->name);
704}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100705
706/* no-op func for checking focus surface */
707static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600708focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100709{
710}
711
712static struct focus_surface *
713get_focus_surface(struct weston_surface *surface)
714{
715 if (surface->configure == focus_surface_configure)
716 return surface->configure_private;
717 else
718 return NULL;
719}
720
721static bool
722is_focus_surface (struct weston_surface *es)
723{
724 return (es->configure == focus_surface_configure);
725}
726
727static bool
728is_focus_view (struct weston_view *view)
729{
730 return is_focus_surface (view->surface);
731}
732
733static struct focus_surface *
734create_focus_surface(struct weston_compositor *ec,
735 struct weston_output *output)
736{
737 struct focus_surface *fsurf = NULL;
738 struct weston_surface *surface = NULL;
739
740 fsurf = malloc(sizeof *fsurf);
741 if (!fsurf)
742 return NULL;
743
744 fsurf->surface = weston_surface_create(ec);
745 surface = fsurf->surface;
746 if (surface == NULL) {
747 free(fsurf);
748 return NULL;
749 }
750
751 surface->configure = focus_surface_configure;
752 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200753 surface->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100754 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300755 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100756
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800757 fsurf->view = weston_view_create(surface);
758 if (fsurf->view == NULL) {
759 weston_surface_destroy(surface);
760 free(fsurf);
761 return NULL;
762 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100763 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200764 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100765
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600766 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600767 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100768 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
769 pixman_region32_fini(&surface->opaque);
770 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
771 output->width, output->height);
772 pixman_region32_fini(&surface->input);
773 pixman_region32_init(&surface->input);
774
775 wl_list_init(&fsurf->workspace_transform.link);
776
777 return fsurf;
778}
779
780static void
781focus_surface_destroy(struct focus_surface *fsurf)
782{
783 weston_surface_destroy(fsurf->surface);
784 free(fsurf);
785}
786
787static void
788focus_animation_done(struct weston_view_animation *animation, void *data)
789{
790 struct workspace *ws = data;
791
792 ws->focus_animation = NULL;
793}
794
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200795static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200796focus_state_destroy(struct focus_state *state)
797{
798 wl_list_remove(&state->seat_destroy_listener.link);
799 wl_list_remove(&state->surface_destroy_listener.link);
800 free(state);
801}
802
803static void
804focus_state_seat_destroy(struct wl_listener *listener, void *data)
805{
806 struct focus_state *state = container_of(listener,
807 struct focus_state,
808 seat_destroy_listener);
809
810 wl_list_remove(&state->link);
811 focus_state_destroy(state);
812}
813
814static void
815focus_state_surface_destroy(struct wl_listener *listener, void *data)
816{
817 struct focus_state *state = container_of(listener,
818 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400819 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400820 struct desktop_shell *shell;
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200821 struct weston_surface *main_surface;
822 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500823 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200824
Pekka Paalanen01388e22013-04-25 13:57:44 +0300825 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
826
Kristian Høgsberge3778222012-07-31 17:29:30 -0400827 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300828 wl_list_for_each(view,
829 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500830 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400831 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 if (is_focus_view(view))
833 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400834
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200835 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400836 break;
837 }
838
Pekka Paalanen01388e22013-04-25 13:57:44 +0300839 /* if the focus was a sub-surface, activate its main surface */
840 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200841 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300842
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400844 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100845 state->keyboard_focus = NULL;
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200846 activate(shell, next, state->seat,
847 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400848 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
850 if (state->ws->focus_animation)
851 weston_view_animation_destroy(state->ws->focus_animation);
852
853 state->ws->focus_animation = weston_fade_run(
854 state->ws->fsurf_front->view,
855 state->ws->fsurf_front->view->alpha, 0.0, 300,
856 focus_animation_done, state->ws);
857 }
858
Kristian Høgsberge3778222012-07-31 17:29:30 -0400859 wl_list_remove(&state->link);
860 focus_state_destroy(state);
861 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200862}
863
864static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400865focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200866{
Jonas Ådahl04769742012-06-13 00:01:24 +0200867 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200868
869 state = malloc(sizeof *state);
870 if (state == NULL)
871 return NULL;
872
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400874 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200875 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400876 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200877
878 state->seat_destroy_listener.notify = focus_state_seat_destroy;
879 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400880 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200881 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400882 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200883
884 return state;
885}
886
Jonas Ådahl8538b222012-08-29 22:13:03 +0200887static struct focus_state *
888ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
889{
890 struct workspace *ws = get_current_workspace(shell);
891 struct focus_state *state;
892
893 wl_list_for_each(state, &ws->focus_list, link)
894 if (state->seat == seat)
895 break;
896
897 if (&state->link == &ws->focus_list)
898 state = focus_state_create(seat, ws);
899
900 return state;
901}
902
Jonas Ådahl04769742012-06-13 00:01:24 +0200903static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800904focus_state_set_focus(struct focus_state *state,
905 struct weston_surface *surface)
906{
907 if (state->keyboard_focus) {
908 wl_list_remove(&state->surface_destroy_listener.link);
909 wl_list_init(&state->surface_destroy_listener.link);
910 }
911
912 state->keyboard_focus = surface;
913 if (surface)
914 wl_signal_add(&surface->destroy_signal,
915 &state->surface_destroy_listener);
916}
917
918static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400919restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200920{
921 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400922 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100923 struct wl_list pending_seat_list;
924 struct weston_seat *seat, *next_seat;
925
926 /* Temporarily steal the list of seats so that we can keep
927 * track of the seats we've already processed */
928 wl_list_init(&pending_seat_list);
929 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
930 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200931
932 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500933 struct weston_keyboard *keyboard =
934 weston_seat_get_keyboard(state->seat);
935
Neil Roberts4237f502014-04-09 16:33:31 +0100936 wl_list_remove(&state->seat->link);
937 wl_list_insert(&shell->compositor->seat_list,
938 &state->seat->link);
939
Derek Foreman1281a362015-07-31 16:55:32 -0500940 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800941 continue;
942
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400943 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200944
Derek Foreman1281a362015-07-31 16:55:32 -0500945 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200946 }
Neil Roberts4237f502014-04-09 16:33:31 +0100947
948 /* For any remaining seats that we don't have a focus state
949 * for we'll reset the keyboard focus to NULL */
950 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500951 struct weston_keyboard *keyboard =
952 weston_seat_get_keyboard(seat);
953
Neil Roberts4237f502014-04-09 16:33:31 +0100954 wl_list_insert(&shell->compositor->seat_list, &seat->link);
955
Derek Foreman1281a362015-07-31 16:55:32 -0500956 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100957 continue;
958
Derek Foreman1281a362015-07-31 16:55:32 -0500959 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100960 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200961}
962
963static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200964replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
965 struct weston_seat *seat)
966{
Derek Foreman1281a362015-07-31 16:55:32 -0500967 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200968 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200969
970 wl_list_for_each(state, &ws->focus_list, link) {
971 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500972 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200973 return;
974 }
975 }
976}
977
978static void
979drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
980 struct weston_surface *surface)
981{
982 struct focus_state *state;
983
984 wl_list_for_each(state, &ws->focus_list, link)
985 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800986 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200987}
988
989static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
991 struct weston_view *from, struct weston_view *to)
992{
993 struct weston_output *output;
994 bool focus_surface_created = false;
995
996 /* FIXME: Only support dim animation using two layers */
997 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
998 return;
999
1000 output = get_default_output(shell->compositor);
1001 if (ws->fsurf_front == NULL && (from || to)) {
1002 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001003 if (ws->fsurf_front == NULL)
1004 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001005 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001006
1007 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1008 if (ws->fsurf_back == NULL) {
1009 focus_surface_destroy(ws->fsurf_front);
1010 return;
1011 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -08001013
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001014 focus_surface_created = true;
1015 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001016 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1017 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001018 }
1019
1020 if (ws->focus_animation) {
1021 weston_view_animation_destroy(ws->focus_animation);
1022 ws->focus_animation = NULL;
1023 }
1024
1025 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001026 weston_layer_entry_insert(&to->layer_link,
1027 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001029 weston_layer_entry_insert(&ws->layer.view_list,
1030 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031
1032 if (focus_surface_created) {
1033 ws->focus_animation = weston_fade_run(
1034 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001035 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001036 focus_animation_done, ws);
1037 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001038 weston_layer_entry_insert(&from->layer_link,
1039 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001040 ws->focus_animation = weston_stable_fade_run(
1041 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001042 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001043 focus_animation_done, ws);
1044 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001045 weston_layer_entry_insert(&ws->layer.view_list,
1046 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001047 ws->focus_animation = weston_stable_fade_run(
1048 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001049 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001050 focus_animation_done, ws);
1051 }
1052}
1053
1054static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001055workspace_destroy(struct workspace *ws)
1056{
Jonas Ådahl04769742012-06-13 00:01:24 +02001057 struct focus_state *state, *next;
1058
1059 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1060 focus_state_destroy(state);
1061
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001062 if (ws->fsurf_front)
1063 focus_surface_destroy(ws->fsurf_front);
1064 if (ws->fsurf_back)
1065 focus_surface_destroy(ws->fsurf_back);
1066
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001067 free(ws);
1068}
1069
Jonas Ådahl04769742012-06-13 00:01:24 +02001070static void
1071seat_destroyed(struct wl_listener *listener, void *data)
1072{
1073 struct weston_seat *seat = data;
1074 struct focus_state *state, *next;
1075 struct workspace *ws = container_of(listener,
1076 struct workspace,
1077 seat_destroyed_listener);
1078
1079 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1080 if (state->seat == seat)
1081 wl_list_remove(&state->link);
1082}
1083
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001084static struct workspace *
1085workspace_create(void)
1086{
1087 struct workspace *ws = malloc(sizeof *ws);
1088 if (ws == NULL)
1089 return NULL;
1090
1091 weston_layer_init(&ws->layer, NULL);
1092
Jonas Ådahl04769742012-06-13 00:01:24 +02001093 wl_list_init(&ws->focus_list);
1094 wl_list_init(&ws->seat_destroyed_listener.link);
1095 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001096 ws->fsurf_front = NULL;
1097 ws->fsurf_back = NULL;
1098 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001099
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001100 return ws;
1101}
1102
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001103static int
1104workspace_is_empty(struct workspace *ws)
1105{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001106 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107}
1108
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001109static struct workspace *
1110get_workspace(struct desktop_shell *shell, unsigned int index)
1111{
1112 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001113 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001114 pws += index;
1115 return *pws;
1116}
1117
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001118struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001119get_current_workspace(struct desktop_shell *shell)
1120{
1121 return get_workspace(shell, shell->workspaces.current);
1122}
1123
1124static void
1125activate_workspace(struct desktop_shell *shell, unsigned int index)
1126{
1127 struct workspace *ws;
1128
1129 ws = get_workspace(shell, index);
1130 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1131
1132 shell->workspaces.current = index;
1133}
1134
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135static unsigned int
1136get_output_height(struct weston_output *output)
1137{
1138 return abs(output->region.extents.y1 - output->region.extents.y2);
1139}
1140
1141static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001142view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144 struct weston_transform *transform;
1145
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001146 if (is_focus_view(view)) {
1147 struct focus_surface *fsurf = get_focus_surface(view->surface);
1148 transform = &fsurf->workspace_transform;
1149 } else {
1150 struct shell_surface *shsurf = get_shell_surface(view->surface);
1151 transform = &shsurf->workspace_transform;
1152 }
1153
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001156 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001158 weston_matrix_init(&transform->matrix);
1159 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162}
1163
1164static void
1165workspace_translate_out(struct workspace *ws, double fraction)
1166{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168 unsigned int height;
1169 double d;
1170
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001171 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173 d = height * fraction;
1174
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001175 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176 }
1177}
1178
1179static void
1180workspace_translate_in(struct workspace *ws, double fraction)
1181{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001183 unsigned int height;
1184 double d;
1185
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001186 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001188
1189 if (fraction > 0)
1190 d = -(height - height * fraction);
1191 else
1192 d = height + height * fraction;
1193
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001194 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195 }
1196}
1197
1198static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001199reverse_workspace_change_animation(struct desktop_shell *shell,
1200 unsigned int index,
1201 struct workspace *from,
1202 struct workspace *to)
1203{
1204 shell->workspaces.current = index;
1205
1206 shell->workspaces.anim_to = to;
1207 shell->workspaces.anim_from = from;
1208 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1209 shell->workspaces.anim_timestamp = 0;
1210
Scott Moreau4272e632012-08-13 09:58:41 -06001211 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001212}
1213
1214static void
1215workspace_deactivate_transforms(struct workspace *ws)
1216{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001218 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001220 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001221 if (is_focus_view(view)) {
1222 struct focus_surface *fsurf = get_focus_surface(view->surface);
1223 transform = &fsurf->workspace_transform;
1224 } else {
1225 struct shell_surface *shsurf = get_shell_surface(view->surface);
1226 transform = &shsurf->workspace_transform;
1227 }
1228
1229 if (!wl_list_empty(&transform->link)) {
1230 wl_list_remove(&transform->link);
1231 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001234 }
1235}
1236
1237static void
1238finish_workspace_change_animation(struct desktop_shell *shell,
1239 struct workspace *from,
1240 struct workspace *to)
1241{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001242 struct weston_view *view;
1243
Scott Moreau4272e632012-08-13 09:58:41 -06001244 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001245
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001246 /* Views that extend past the bottom of the output are still
1247 * visible after the workspace animation ends but before its layer
1248 * is hidden. In that case, we need to damage below those views so
1249 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001250 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001251 weston_view_damage_below(view);
1252
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001253 wl_list_remove(&shell->workspaces.animation.link);
1254 workspace_deactivate_transforms(from);
1255 workspace_deactivate_transforms(to);
1256 shell->workspaces.anim_to = NULL;
1257
1258 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1259}
1260
1261static void
1262animate_workspace_change_frame(struct weston_animation *animation,
1263 struct weston_output *output, uint32_t msecs)
1264{
1265 struct desktop_shell *shell =
1266 container_of(animation, struct desktop_shell,
1267 workspaces.animation);
1268 struct workspace *from = shell->workspaces.anim_from;
1269 struct workspace *to = shell->workspaces.anim_to;
1270 uint32_t t;
1271 double x, y;
1272
1273 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1274 finish_workspace_change_animation(shell, from, to);
1275 return;
1276 }
1277
1278 if (shell->workspaces.anim_timestamp == 0) {
1279 if (shell->workspaces.anim_current == 0.0)
1280 shell->workspaces.anim_timestamp = msecs;
1281 else
1282 shell->workspaces.anim_timestamp =
1283 msecs -
1284 /* Invers of movement function 'y' below. */
1285 (asin(1.0 - shell->workspaces.anim_current) *
1286 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1287 M_2_PI);
1288 }
1289
1290 t = msecs - shell->workspaces.anim_timestamp;
1291
1292 /*
1293 * x = [0, π/2]
1294 * y(x) = sin(x)
1295 */
1296 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1297 y = sin(x);
1298
1299 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001300 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001301
1302 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1303 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1304 shell->workspaces.anim_current = y;
1305
Scott Moreau4272e632012-08-13 09:58:41 -06001306 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001307 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001308 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001309 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001310}
1311
1312static void
1313animate_workspace_change(struct desktop_shell *shell,
1314 unsigned int index,
1315 struct workspace *from,
1316 struct workspace *to)
1317{
1318 struct weston_output *output;
1319
1320 int dir;
1321
1322 if (index > shell->workspaces.current)
1323 dir = -1;
1324 else
1325 dir = 1;
1326
1327 shell->workspaces.current = index;
1328
1329 shell->workspaces.anim_dir = dir;
1330 shell->workspaces.anim_from = from;
1331 shell->workspaces.anim_to = to;
1332 shell->workspaces.anim_current = 0.0;
1333 shell->workspaces.anim_timestamp = 0;
1334
1335 output = container_of(shell->compositor->output_list.next,
1336 struct weston_output, link);
1337 wl_list_insert(&output->animation_list,
1338 &shell->workspaces.animation.link);
1339
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001340 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001341
1342 workspace_translate_in(to, 0);
1343
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001344 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001345
Scott Moreau4272e632012-08-13 09:58:41 -06001346 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001347}
1348
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001349static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001350update_workspace(struct desktop_shell *shell, unsigned int index,
1351 struct workspace *from, struct workspace *to)
1352{
1353 shell->workspaces.current = index;
1354 wl_list_insert(&from->layer.link, &to->layer.link);
1355 wl_list_remove(&from->layer.link);
1356}
1357
1358static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001359change_workspace(struct desktop_shell *shell, unsigned int index)
1360{
1361 struct workspace *from;
1362 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001363 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001364
1365 if (index == shell->workspaces.current)
1366 return;
1367
1368 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001369 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001370 return;
1371
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001372 from = get_current_workspace(shell);
1373 to = get_workspace(shell, index);
1374
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001375 if (shell->workspaces.anim_from == to &&
1376 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001377 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001378 reverse_workspace_change_animation(shell, index, from, to);
1379 return;
1380 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001381
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001382 if (shell->workspaces.anim_to != NULL)
1383 finish_workspace_change_animation(shell,
1384 shell->workspaces.anim_from,
1385 shell->workspaces.anim_to);
1386
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001387 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001388
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001389 if (shell->focus_animation_type != ANIMATION_NONE) {
1390 wl_list_for_each(state, &from->focus_list, link)
1391 if (state->keyboard_focus)
1392 animate_focus_change(shell, from,
1393 get_default_view(state->keyboard_focus), NULL);
1394
1395 wl_list_for_each(state, &to->focus_list, link)
1396 if (state->keyboard_focus)
1397 animate_focus_change(shell, to,
1398 NULL, get_default_view(state->keyboard_focus));
1399 }
1400
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001401 if (workspace_is_empty(to) && workspace_is_empty(from))
1402 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001403 else
1404 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001405}
1406
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001407static bool
1408workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1409{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001410 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001411 struct wl_list *e;
1412
1413 if (wl_list_empty(list))
1414 return false;
1415
1416 e = list->next;
1417
1418 if (e->next != list)
1419 return false;
1420
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001421 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001422}
1423
1424static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001425surface_keyboard_focus_lost(struct weston_surface *surface)
1426{
1427 struct weston_compositor *compositor = surface->compositor;
1428 struct weston_seat *seat;
1429 struct weston_surface *focus;
1430
1431 wl_list_for_each(seat, &compositor->seat_list, link) {
1432 struct weston_keyboard *keyboard =
1433 weston_seat_get_keyboard(seat);
1434
1435 if (!keyboard)
1436 continue;
1437
1438 focus = weston_surface_get_main_surface(keyboard->focus);
1439 if (focus == surface)
1440 weston_keyboard_set_focus(keyboard, NULL);
1441 }
1442}
1443
1444static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001445take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001446 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001447 unsigned int index)
1448{
Derek Foreman1281a362015-07-31 16:55:32 -05001449 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001450 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001452 struct shell_surface *shsurf;
1453 struct workspace *from;
1454 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001455 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001456
Derek Foreman1281a362015-07-31 16:55:32 -05001457 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 view = get_default_view(surface);
1459 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001460 index == shell->workspaces.current ||
1461 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001462 return;
1463
1464 from = get_current_workspace(shell);
1465 to = get_workspace(shell, index);
1466
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001467 weston_layer_entry_remove(&view->layer_link);
1468 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001469
Philip Withnall659163d2013-11-25 18:01:36 +00001470 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001471 if (shsurf != NULL)
1472 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001473
Jonas Ådahle9d22502012-08-29 22:13:01 +02001474 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001475 drop_focus_state(shell, from, surface);
1476
1477 if (shell->workspaces.anim_from == to &&
1478 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001479 wl_list_remove(&to->layer.link);
1480 wl_list_insert(from->layer.link.prev, &to->layer.link);
1481
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001482 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001483
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001484 return;
1485 }
1486
1487 if (shell->workspaces.anim_to != NULL)
1488 finish_workspace_change_animation(shell,
1489 shell->workspaces.anim_from,
1490 shell->workspaces.anim_to);
1491
1492 if (workspace_is_empty(from) &&
1493 workspace_has_only(to, surface))
1494 update_workspace(shell, index, from, to);
1495 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001496 if (shsurf != NULL &&
1497 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001498 wl_list_insert(&shell->workspaces.anim_sticky_list,
1499 &shsurf->workspace_transform.link);
1500
1501 animate_workspace_change(shell, index, from, to);
1502 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001503
Jonas Ådahl8538b222012-08-29 22:13:03 +02001504 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
Rusty Lynch1084da52013-08-15 09:10:08 -07001510touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001511 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001512{
1513}
1514
1515static void
1516touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1517{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001518 struct weston_touch_move_grab *move =
1519 (struct weston_touch_move_grab *) container_of(
1520 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001521
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001522 if (touch_id == 0)
1523 move->active = 0;
1524
Jonas Ådahl9484b692013-12-02 22:05:03 +01001525 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001526 shell_touch_grab_end(&move->base);
1527 free(move);
1528 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001529}
1530
1531static void
1532touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001533 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001534{
1535 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1536 struct shell_surface *shsurf = move->base.shsurf;
1537 struct weston_surface *es;
1538 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1539 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1540
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001541 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001542 return;
1543
1544 es = shsurf->surface;
1545
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001546 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001547
1548 weston_compositor_schedule_repaint(es->compositor);
1549}
1550
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001551static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001552touch_move_grab_frame(struct weston_touch_grab *grab)
1553{
1554}
1555
1556static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001557touch_move_grab_cancel(struct weston_touch_grab *grab)
1558{
1559 struct weston_touch_move_grab *move =
1560 (struct weston_touch_move_grab *) container_of(
1561 grab, struct shell_touch_grab, grab);
1562
1563 shell_touch_grab_end(&move->base);
1564 free(move);
1565}
1566
Rusty Lynch1084da52013-08-15 09:10:08 -07001567static const struct weston_touch_grab_interface touch_move_grab_interface = {
1568 touch_move_grab_down,
1569 touch_move_grab_up,
1570 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001571 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001572 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001573};
1574
1575static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001576surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001577{
1578 struct weston_touch_move_grab *move;
1579
1580 if (!shsurf)
1581 return -1;
1582
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001583 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001584 return 0;
1585
1586 move = malloc(sizeof *move);
1587 if (!move)
1588 return -1;
1589
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001590 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001592 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001594 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001595
1596 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001597 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001598
1599 return 0;
1600}
1601
1602static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001603noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001604{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001605}
1606
1607static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001608noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001609 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001610{
1611}
1612
1613static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001614noop_grab_axis_source(struct weston_pointer_grab *grab,
1615 uint32_t source)
1616{
1617}
1618
1619static void
1620noop_grab_frame(struct weston_pointer_grab *grab)
1621{
1622}
1623
1624static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001625constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1626{
1627 struct shell_surface *shsurf = move->base.shsurf;
1628 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001629 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001630 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001631 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001632
1633 x = wl_fixed_to_int(pointer->x + move->dx);
1634 y = wl_fixed_to_int(pointer->y + move->dy);
1635
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001636 if (shsurf->shell->panel_position ==
1637 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001638 get_output_work_area(shsurf->shell,
1639 shsurf->surface->output,
1640 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001641
Derek Foreman6feb0f92015-04-15 12:23:56 -05001642 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1643 if (bottom - safety < area.y)
1644 y = area.y + safety - shsurf->geometry.height
1645 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001646
1647 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001648 y + shsurf->geometry.y < area.y)
1649 y = area.y - shsurf->geometry.y;
1650
Jonny Lambd73c6942014-08-20 15:53:20 +02001651 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001652
1653 *cx = x;
1654 *cy = y;
1655}
1656
1657static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001658move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001659 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001660{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001661 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001662 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001663 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001664 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001665
Jonas Ådahld2510102014-10-05 21:39:14 +02001666 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001667 if (!shsurf)
1668 return;
1669
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001670 constrain_position(move, &cx, &cy);
1671
1672 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001673
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001675}
1676
1677static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001678move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001679 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001680{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001681 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1682 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001683 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001684 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001685
Daniel Stone4dbadb12012-05-30 16:31:51 +01001686 if (pointer->button_count == 0 &&
1687 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001688 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001689 free(grab);
1690 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001691}
1692
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001693static void
1694move_grab_cancel(struct weston_pointer_grab *grab)
1695{
1696 struct shell_grab *shell_grab =
1697 container_of(grab, struct shell_grab, grab);
1698
1699 shell_grab_end(shell_grab);
1700 free(grab);
1701}
1702
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001703static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001704 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001705 move_grab_motion,
1706 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001707 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001708 noop_grab_axis_source,
1709 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001710 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001711};
1712
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001714surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001715 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716{
1717 struct weston_move_grab *move;
1718
1719 if (!shsurf)
1720 return -1;
1721
Derek Foreman45a7c272015-09-11 14:27:40 -05001722 shsurf = find_toplevel_surface(shsurf);
1723
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001724 if (shsurf->grabbed ||
1725 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726 return 0;
1727
1728 move = malloc(sizeof *move);
1729 if (!move)
1730 return -1;
1731
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001733 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001734 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001735 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001736 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737
1738 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001739 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740
1741 return 0;
1742}
1743
1744static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001745common_surface_move(struct wl_resource *resource,
1746 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001747{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001748 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001749 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1750 struct weston_touch *touch = weston_seat_get_touch(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001751 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001752 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753
Derek Foreman1281a362015-07-31 16:55:32 -05001754 if (pointer &&
1755 pointer->focus &&
1756 pointer->button_count > 0 &&
1757 pointer->grab_serial == serial) {
1758 surface = weston_surface_get_main_surface(pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001759 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001760 (surface_move(shsurf, pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001761 wl_resource_post_no_memory(resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001762 } else if (touch &&
1763 touch->focus &&
1764 touch->grab_serial == serial) {
1765 surface = weston_surface_get_main_surface(touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001766 if ((surface == shsurf->surface) &&
Derek Foreman1281a362015-07-31 16:55:32 -05001767 (surface_touch_move(shsurf, touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001768 wl_resource_post_no_memory(resource);
1769 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770}
1771
Rafael Antognollie2a34552013-12-03 15:35:45 -02001772static void
1773shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1774 struct wl_resource *seat_resource, uint32_t serial)
1775{
1776 common_surface_move(resource, seat_resource, serial);
1777}
1778
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001779struct weston_resize_grab {
1780 struct shell_grab base;
1781 uint32_t edges;
1782 int32_t width, height;
1783};
1784
1785static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001786resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001787 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001788{
1789 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001790 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001791 struct shell_surface *shsurf = resize->base.shsurf;
1792 int32_t width, height;
1793 wl_fixed_t from_x, from_y;
1794 wl_fixed_t to_x, to_y;
1795
Jonas Ådahld2510102014-10-05 21:39:14 +02001796 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001797
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001798 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001799 return;
1800
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801 weston_view_from_global_fixed(shsurf->view,
1802 pointer->grab_x, pointer->grab_y,
1803 &from_x, &from_y);
1804 weston_view_from_global_fixed(shsurf->view,
1805 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001806
1807 width = resize->width;
1808 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1809 width += wl_fixed_to_int(from_x - to_x);
1810 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1811 width += wl_fixed_to_int(to_x - from_x);
1812 }
1813
1814 height = resize->height;
1815 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1816 height += wl_fixed_to_int(from_y - to_y);
1817 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1818 height += wl_fixed_to_int(to_y - from_y);
1819 }
1820
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001821 if (width < 1)
1822 width = 1;
1823 if (height < 1)
1824 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001825 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001826}
1827
1828static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001829send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001830{
1831 struct shell_surface *shsurf = get_shell_surface(surface);
1832
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001833 assert(shsurf);
1834
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001835 if (shsurf->resource)
1836 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001837 shsurf->resize_edges,
1838 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001839}
1840
1841static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001842 send_configure,
1843 NULL
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001844};
1845
1846static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001847resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001848 uint32_t time, uint32_t button, uint32_t state_w)
1849{
1850 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001851 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001852 enum wl_pointer_button_state state = state_w;
1853
1854 if (pointer->button_count == 0 &&
1855 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1856 shell_grab_end(&resize->base);
1857 free(grab);
1858 }
1859}
1860
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001861static void
1862resize_grab_cancel(struct weston_pointer_grab *grab)
1863{
1864 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1865
1866 shell_grab_end(&resize->base);
1867 free(grab);
1868}
1869
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001870static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001871 noop_grab_focus,
1872 resize_grab_motion,
1873 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001874 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001875 noop_grab_axis_source,
1876 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001877 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001878};
1879
Giulio Camuffob8366642013-04-25 13:57:46 +03001880/*
1881 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001882 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001883static void
1884surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1885 int32_t *y, int32_t *w, int32_t *h) {
1886 pixman_region32_t region;
1887 pixman_box32_t *box;
1888 struct weston_subsurface *subsurface;
1889
1890 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891 surface->width,
1892 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001893
1894 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1895 pixman_region32_union_rect(&region, &region,
1896 subsurface->position.x,
1897 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 subsurface->surface->width,
1899 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001900 }
1901
1902 box = pixman_region32_extents(&region);
1903 if (x)
1904 *x = box->x1;
1905 if (y)
1906 *y = box->y1;
1907 if (w)
1908 *w = box->x2 - box->x1;
1909 if (h)
1910 *h = box->y2 - box->y1;
1911
1912 pixman_region32_fini(&region);
1913}
1914
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001915static int
1916surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001917 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001918{
1919 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001920 const unsigned resize_topbottom =
1921 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1922 const unsigned resize_leftright =
1923 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1924 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001925
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001926 if (shsurf->grabbed ||
1927 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001928 return 0;
1929
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001930 /* Check for invalid edge combinations. */
1931 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1932 (edges & resize_topbottom) == resize_topbottom ||
1933 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001934 return 0;
1935
1936 resize = malloc(sizeof *resize);
1937 if (!resize)
1938 return -1;
1939
1940 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001941
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001942 resize->width = shsurf->geometry.width;
1943 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001944
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001945 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001946 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001947 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001948 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001949
1950 return 0;
1951}
1952
1953static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001954common_surface_resize(struct wl_resource *resource,
1955 struct wl_resource *seat_resource, uint32_t serial,
1956 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001957{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001958 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Derek Foreman1281a362015-07-31 16:55:32 -05001959 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001960 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001961 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001962
Derek Foreman1281a362015-07-31 16:55:32 -05001963 if (!pointer ||
1964 pointer->button_count == 0 ||
1965 pointer->grab_serial != serial ||
1966 pointer->focus == NULL)
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001967 return;
1968
Derek Foreman1281a362015-07-31 16:55:32 -05001969 surface = weston_surface_get_main_surface(pointer->focus->surface);
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001970 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001971 return;
1972
Derek Foreman1281a362015-07-31 16:55:32 -05001973 if (surface_resize(shsurf, pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001974 wl_resource_post_no_memory(resource);
1975}
1976
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001977static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001978shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1979 struct wl_resource *seat_resource, uint32_t serial,
1980 uint32_t edges)
1981{
1982 common_surface_resize(resource, seat_resource, serial, edges);
1983}
1984
1985static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001986busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001987{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001988 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001989 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001991 wl_fixed_t sx, sy;
1992
Jason Ekstranda7af7042013-10-12 22:38:11 -05001993 view = weston_compositor_pick_view(pointer->seat->compositor,
1994 pointer->x, pointer->y,
1995 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001996
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001997 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1998 shell_grab_end(grab);
1999 free(grab);
2000 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002001}
2002
2003static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002004busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02002005 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002006{
Jonas Ådahld2510102014-10-05 21:39:14 +02002007 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002008}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002009
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002010static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002011busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002012 uint32_t time, uint32_t button, uint32_t state)
2013{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002014 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002015 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002016 struct weston_pointer *pointer = grab->grab.pointer;
2017 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002018
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002019 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02002020 activate(shsurf->shell, shsurf->view, seat,
2021 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002022 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002023 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02002024 activate(shsurf->shell, shsurf->view, seat,
2025 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05002026 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002027 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002028}
2029
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002030static void
2031busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2032{
2033 struct shell_grab *grab = (struct shell_grab *) base;
2034
2035 shell_grab_end(grab);
2036 free(grab);
2037}
2038
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002039static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040 busy_cursor_grab_focus,
2041 busy_cursor_grab_motion,
2042 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02002043 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002044 noop_grab_axis_source,
2045 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002046 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047};
2048
2049static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002050set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002051{
2052 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002053
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002054 if (pointer->grab->interface == &busy_cursor_grab_interface)
2055 return;
2056
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002057 grab = malloc(sizeof *grab);
2058 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002059 return;
2060
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002061 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002062 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002063 /* Mark the shsurf as ungrabbed so that button binding is able
2064 * to move it. */
2065 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002066}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002067
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002068static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002069end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002070{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002071 struct shell_grab *grab;
2072 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002073
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002074 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002075 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2076
2077 if (!pointer)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002078 continue;
2079
Derek Foreman1281a362015-07-31 16:55:32 -05002080 grab = (struct shell_grab *) pointer->grab;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002081 if (grab->grab.interface == &busy_cursor_grab_interface &&
Derek Foremanc0c14972015-09-11 14:30:39 -05002082 grab->shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002083 wl_resource_get_client(grab->shsurf->resource) == client) {
2084 shell_grab_end(grab);
2085 free(grab);
2086 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002087 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002088}
2089
Scott Moreau9521d5e2012-04-19 13:06:17 -06002090static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002091handle_shell_client_destroy(struct wl_listener *listener, void *data);
2092
2093static int
2094xdg_ping_timeout_handler(void *data)
2095{
2096 struct shell_client *sc = data;
2097 struct weston_seat *seat;
2098 struct shell_surface *shsurf;
2099
2100 /* Client is not responding */
2101 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002102 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002103 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2104
2105 if (!pointer ||
2106 !pointer->focus ||
2107 !pointer->focus->surface->resource)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002108 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002109
Derek Foreman1281a362015-07-31 16:55:32 -05002110 shsurf = get_shell_surface(pointer->focus->surface);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002111 if (shsurf && shsurf->resource &&
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002112 wl_resource_get_client(shsurf->resource) == sc->client)
Derek Foreman1281a362015-07-31 16:55:32 -05002113 set_busy_cursor(shsurf, pointer);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002114 }
2115
2116 return 1;
2117}
2118
2119static void
2120handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2121{
2122 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002123 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002124 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002125 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002126
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002127 if (sc->unresponsive) {
2128 xdg_ping_timeout_handler(sc);
2129 return;
2130 }
2131
2132 sc->ping_serial = serial;
2133 loop = wl_display_get_event_loop(compositor->wl_display);
2134 if (sc->ping_timer == NULL)
2135 sc->ping_timer =
2136 wl_event_loop_add_timer(loop,
2137 xdg_ping_timeout_handler, sc);
2138 if (sc->ping_timer == NULL)
2139 return;
2140
2141 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2142
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002143 if (shell_surface_is_xdg_surface(shsurf) ||
2144 shell_surface_is_xdg_popup(shsurf))
2145 xdg_shell_send_ping(sc->resource, serial);
Bryce Harrington22b1f932015-09-23 17:30:43 -07002146 else if (shell_surface_is_wl_shell_surface(shsurf)
2147 && shsurf->resource)
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002148 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002149}
2150
Scott Moreauff1db4a2012-04-17 19:06:18 -06002151static void
2152ping_handler(struct weston_surface *surface, uint32_t serial)
2153{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002154 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002155
2156 if (!shsurf)
2157 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002158 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002159 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002160 if (shsurf->surface == shsurf->shell->grab_surface)
2161 return;
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08002162 if (!shsurf->owner)
2163 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002164
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002165 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002166}
2167
2168static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002169handle_pointer_focus(struct wl_listener *listener, void *data)
2170{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002171 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002173 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002174 uint32_t serial;
2175
Jason Ekstranda7af7042013-10-12 22:38:11 -05002176 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002177 return;
2178
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002180 serial = wl_display_next_serial(compositor->wl_display);
2181 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002182}
2183
2184static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002185shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2186{
2187 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002188 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002189}
2190
2191static void
2192shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2193{
2194 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002195 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002196}
2197
2198static void
2199handle_keyboard_focus(struct wl_listener *listener, void *data)
2200{
2201 struct weston_keyboard *keyboard = data;
2202 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2203
2204 if (seat->focused_surface) {
2205 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2206 if (shsurf)
2207 shell_surface_lose_keyboard_focus(shsurf);
2208 }
2209
2210 seat->focused_surface = keyboard->focus;
2211
2212 if (seat->focused_surface) {
2213 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2214 if (shsurf)
2215 shell_surface_gain_keyboard_focus(shsurf);
2216 }
2217}
2218
2219static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002220shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002221{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002222 if (sc->ping_serial != serial)
2223 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002224
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002225 sc->unresponsive = 0;
2226 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002227
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002228 if (sc->ping_timer) {
2229 wl_event_source_remove(sc->ping_timer);
2230 sc->ping_timer = NULL;
2231 }
2232
2233}
2234
2235static void
2236shell_surface_pong(struct wl_client *client,
2237 struct wl_resource *resource, uint32_t serial)
2238{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002239 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2240 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002241
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002242 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002243}
2244
2245static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002246set_title(struct shell_surface *shsurf, const char *title)
2247{
2248 free(shsurf->title);
2249 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002250 shsurf->surface->timeline.force_refresh = 1;
2251}
2252
2253static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002254set_pid(struct shell_surface *shsurf, pid_t pid)
2255{
2256 /* We have no use for it */
2257}
2258
2259static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002260set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2261{
2262 shsurf->type = t;
2263 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002264}
2265
2266static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002267set_window_geometry(struct shell_surface *shsurf,
2268 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002269{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002270 shsurf->next_geometry.x = x;
2271 shsurf->next_geometry.y = y;
2272 shsurf->next_geometry.width = width;
2273 shsurf->next_geometry.height = height;
2274 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002275}
2276
2277static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002278shell_surface_set_title(struct wl_client *client,
2279 struct wl_resource *resource, const char *title)
2280{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002281 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002282
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002283 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002284}
2285
2286static void
2287shell_surface_set_class(struct wl_client *client,
2288 struct wl_resource *resource, const char *class)
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
2292 free(shsurf->class);
2293 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002294 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002295}
2296
Alex Wu4539b082012-03-01 12:57:46 +08002297static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002298restore_output_mode(struct weston_output *output)
2299{
Armin Krezovićc77f2582016-06-23 11:59:32 +02002300 if (output && output->original_mode)
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002301 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002302}
2303
2304static void
2305restore_all_output_modes(struct weston_compositor *compositor)
2306{
2307 struct weston_output *output;
2308
2309 wl_list_for_each(output, &compositor->output_list, link)
2310 restore_output_mode(output);
2311}
2312
Philip Withnall07926d92013-11-25 18:01:40 +00002313/* The surface will be inserted into the list immediately after the link
2314 * returned by this function (i.e. will be stacked immediately above the
2315 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002316static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002317shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2318{
2319 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002320 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002321
2322 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002323 case SHELL_SURFACE_XWAYLAND:
2324 return &shsurf->shell->fullscreen_layer.view_list;
2325
2326 case SHELL_SURFACE_NONE:
2327 return NULL;
2328
Kristian Høgsbergead52422014-01-01 13:51:52 -08002329 case SHELL_SURFACE_POPUP:
2330 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002331 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002332 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002333 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002334 /* Move the surface to its parent layer so
2335 * that surfaces which are transient for
2336 * fullscreen surfaces don't get hidden by the
2337 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002338
2339 /* TODO: Handle a parent with multiple views */
2340 parent = get_default_view(shsurf->parent);
2341 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002342 return container_of(parent->layer_link.link.prev,
2343 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002344 }
Philip Withnall07926d92013-11-25 18:01:40 +00002345
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002346 /* Move the surface to a normal workspace layer so that surfaces
2347 * which were previously fullscreen or transient are no longer
2348 * rendered on top. */
2349 ws = get_current_workspace(shsurf->shell);
2350 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002351 }
2352
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002353 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002354}
2355
Philip Withnall648a4dd2013-11-25 18:01:44 +00002356static void
2357shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2358{
2359 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002360 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002361
2362 /* Move the child layers to the same workspace as shsurf. They will be
2363 * stacked above shsurf. */
2364 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002365 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002366 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002367 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002368 prev = container_of(shsurf->view->layer_link.link.prev,
2369 struct weston_layer_entry, link);
2370 weston_layer_entry_remove(&child->view->layer_link);
2371 weston_layer_entry_insert(prev,
2372 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002373 weston_view_geometry_dirty(child->view);
2374 weston_surface_damage(child->surface);
2375
2376 /* Recurse. We don’t expect this to recurse very far (if
2377 * at all) because that would imply we have transient
2378 * (or popup) children of transient surfaces, which
2379 * would be unusual. */
2380 shell_surface_update_child_surface_layers(child);
2381 }
2382 }
2383}
2384
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002385/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002386 * geometry to ensure the changes are redrawn.
2387 *
2388 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2389 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002390static void
2391shell_surface_update_layer(struct shell_surface *shsurf)
2392{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002393 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002394
2395 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2396
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002397 if (new_layer_link == NULL)
2398 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002399 if (new_layer_link == &shsurf->view->layer_link)
2400 return;
2401
2402 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002403 weston_layer_entry_remove(&shsurf->view->layer_link);
2404 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002405 weston_view_geometry_dirty(shsurf->view);
2406 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002407
2408 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002409}
2410
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002411static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002412shell_surface_set_parent(struct shell_surface *shsurf,
2413 struct weston_surface *parent)
2414{
2415 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002416
2417 wl_list_remove(&shsurf->children_link);
2418 wl_list_init(&shsurf->children_link);
2419
2420 /* Insert into the parent surface’s child list. */
2421 if (parent != NULL) {
2422 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2423 if (parent_shsurf != NULL)
2424 wl_list_insert(&parent_shsurf->children_list,
2425 &shsurf->children_link);
2426 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002427}
2428
2429static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002430shell_surface_set_output(struct shell_surface *shsurf,
2431 struct weston_output *output)
2432{
2433 struct weston_surface *es = shsurf->surface;
2434
2435 /* get the default output, if the client set it as NULL
2436 check whether the ouput is available */
2437 if (output)
2438 shsurf->output = output;
2439 else if (es->output)
2440 shsurf->output = es->output;
2441 else
2442 shsurf->output = get_default_output(es->compositor);
2443}
2444
2445static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002446surface_clear_next_states(struct shell_surface *shsurf)
2447{
2448 shsurf->next_state.maximized = false;
2449 shsurf->next_state.fullscreen = false;
2450
2451 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2452 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2453 shsurf->state_changed = true;
2454}
2455
2456static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002457set_toplevel(struct shell_surface *shsurf)
2458{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002459 shell_surface_set_parent(shsurf, NULL);
2460 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002461 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002462
2463 /* The layer_link is updated in set_surface_type(),
2464 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002465}
2466
2467static void
2468shell_surface_set_toplevel(struct wl_client *client,
2469 struct wl_resource *resource)
2470{
2471 struct shell_surface *surface = wl_resource_get_user_data(resource);
2472
2473 set_toplevel(surface);
2474}
2475
2476static void
2477set_transient(struct shell_surface *shsurf,
2478 struct weston_surface *parent, int x, int y, uint32_t flags)
2479{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002480 assert(parent != NULL);
2481
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002482 shell_surface_set_parent(shsurf, parent);
2483
2484 surface_clear_next_states(shsurf);
2485
Philip Withnallbecb77e2013-11-25 18:01:30 +00002486 shsurf->transient.x = x;
2487 shsurf->transient.y = y;
2488 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002489
Rafael Antognollied207b42013-12-03 15:35:43 -02002490 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002491 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002492 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002493
2494 /* The layer_link is updated in set_surface_type(),
2495 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002496}
2497
2498static void
2499shell_surface_set_transient(struct wl_client *client,
2500 struct wl_resource *resource,
2501 struct wl_resource *parent_resource,
2502 int x, int y, uint32_t flags)
2503{
2504 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2505 struct weston_surface *parent =
2506 wl_resource_get_user_data(parent_resource);
2507
2508 set_transient(shsurf, parent, x, y, flags);
2509}
2510
2511static void
2512set_fullscreen(struct shell_surface *shsurf,
2513 uint32_t method,
2514 uint32_t framerate,
2515 struct weston_output *output)
2516{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002517 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002518 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002519
2520 shsurf->fullscreen_output = shsurf->output;
2521 shsurf->fullscreen.type = method;
2522 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002523
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002524 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002525}
2526
2527static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002528weston_view_set_initial_position(struct weston_view *view,
2529 struct desktop_shell *shell);
2530
2531static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002532unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002533{
Philip Withnallf85fe842013-11-25 18:01:37 +00002534 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002535 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2536 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002537 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002538 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002539
Alex Wu4539b082012-03-01 12:57:46 +08002540 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2541 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002542
Alex Wu4539b082012-03-01 12:57:46 +08002543 wl_list_remove(&shsurf->fullscreen.transform.link);
2544 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002545
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546 if (shsurf->fullscreen.black_view)
2547 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2548 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002549
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002550 if (shsurf->saved_position_valid)
2551 weston_view_set_position(shsurf->view,
2552 shsurf->saved_x, shsurf->saved_y);
2553 else
2554 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2555
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002556 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002558 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002559 shsurf->saved_rotation_valid = false;
2560 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002561
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002562 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002563}
2564
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002565static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002566shell_surface_set_fullscreen(struct wl_client *client,
2567 struct wl_resource *resource,
2568 uint32_t method,
2569 uint32_t framerate,
2570 struct wl_resource *output_resource)
2571{
2572 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2573 struct weston_output *output;
2574
Emmanuel Gil Peyrot28834b62016-05-10 03:22:43 +02002575 if (shsurf->fullscreen_output == shsurf->output &&
2576 shsurf->fullscreen.type == method &&
2577 shsurf->fullscreen.framerate == framerate) {
2578 send_configure_for_surface(shsurf);
2579 return;
2580 }
2581
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582 if (output_resource)
2583 output = wl_resource_get_user_data(output_resource);
2584 else
2585 output = NULL;
2586
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002587 shell_surface_set_parent(shsurf, NULL);
2588
Rafael Antognolli03b16592013-12-03 15:35:42 -02002589 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002590 shsurf->next_state.fullscreen = true;
2591 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002592 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002593}
2594
2595static void
2596set_popup(struct shell_surface *shsurf,
2597 struct weston_surface *parent,
2598 struct weston_seat *seat,
2599 uint32_t serial,
2600 int32_t x,
2601 int32_t y)
2602{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002603 assert(parent != NULL);
2604
Philip Withnallbecb77e2013-11-25 18:01:30 +00002605 shsurf->popup.shseat = get_shell_seat(seat);
2606 shsurf->popup.serial = serial;
2607 shsurf->popup.x = x;
2608 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002609
Pekka Paalanenb5026542014-11-12 15:09:24 +02002610 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002611}
2612
2613static void
2614shell_surface_set_popup(struct wl_client *client,
2615 struct wl_resource *resource,
2616 struct wl_resource *seat_resource,
2617 uint32_t serial,
2618 struct wl_resource *parent_resource,
2619 int32_t x, int32_t y, uint32_t flags)
2620{
2621 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002622 struct weston_surface *parent =
2623 wl_resource_get_user_data(parent_resource);
2624
2625 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002626
Rafael Antognolli03b16592013-12-03 15:35:42 -02002627 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002628 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002629 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002630 wl_resource_get_user_data(seat_resource),
2631 serial, x, y);
2632}
2633
2634static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002635unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002636{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002637 /* undo all maximized things here */
2638 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002639
2640 if (shsurf->saved_position_valid)
2641 weston_view_set_position(shsurf->view,
2642 shsurf->saved_x, shsurf->saved_y);
2643 else
2644 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002645
2646 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002647 wl_list_insert(&shsurf->view->geometry.transformation_list,
2648 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002649 shsurf->saved_rotation_valid = false;
2650 }
2651
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002652 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002653}
2654
Philip Withnallbecb77e2013-11-25 18:01:30 +00002655static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002656set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002657{
2658 struct shell_surface *shsurf;
2659 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002660 struct weston_view *view;
2661
2662 view = get_default_view(surface);
2663 if (!view)
2664 return;
2665
2666 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2667
2668 shsurf = get_shell_surface(surface);
2669 current_ws = get_current_workspace(shsurf->shell);
2670
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002671 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002672 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002673
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002674 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002675 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002676
2677 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002678 weston_view_damage_below(view);
2679}
2680
2681static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002682shell_surface_set_maximized(struct wl_client *client,
2683 struct wl_resource *resource,
2684 struct wl_resource *output_resource)
2685{
2686 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2687 struct weston_output *output;
2688
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002689 surface_clear_next_states(shsurf);
2690 shsurf->next_state.maximized = true;
2691 shsurf->state_changed = true;
2692
Pekka Paalanenb5026542014-11-12 15:09:24 +02002693 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002694 shell_surface_set_parent(shsurf, NULL);
2695
Philip Withnallbecb77e2013-11-25 18:01:30 +00002696 if (output_resource)
2697 output = wl_resource_get_user_data(output_resource);
2698 else
2699 output = NULL;
2700
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002701 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002702
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002703 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002704}
2705
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002706/* This is only ever called from set_surface_type(), so there’s no need to
2707 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002708static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002709reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002710{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002711 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002712 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002713 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002714 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002715
Pekka Paalanen98262232011-12-01 10:42:22 +02002716 return 0;
2717}
2718
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002719static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002720set_full_output(struct shell_surface *shsurf)
2721{
2722 shsurf->saved_x = shsurf->view->geometry.x;
2723 shsurf->saved_y = shsurf->view->geometry.y;
2724 shsurf->saved_position_valid = true;
2725
2726 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2727 wl_list_remove(&shsurf->rotation.transform.link);
2728 wl_list_init(&shsurf->rotation.transform.link);
2729 weston_view_geometry_dirty(shsurf->view);
2730 shsurf->saved_rotation_valid = true;
2731 }
2732}
2733
2734static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002735set_surface_type(struct shell_surface *shsurf)
2736{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002737 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002739
Philip Withnallbecb77e2013-11-25 18:01:30 +00002740 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002741
Rafael Antognolli03b16592013-12-03 15:35:42 -02002742 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002743 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002744
2745 switch (shsurf->type) {
2746 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002747 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002748 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002749 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 weston_view_set_position(shsurf->view,
2751 pev->geometry.x + shsurf->transient.x,
2752 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002753 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002754 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002755
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002756 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002757 weston_view_set_position(shsurf->view, shsurf->transient.x,
2758 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002759 break;
2760
Philip Withnall0f640e22013-11-25 18:01:31 +00002761 case SHELL_SURFACE_POPUP:
2762 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002763 default:
2764 break;
2765 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002766
2767 /* Update the surface’s layer. */
2768 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002769}
2770
Tiago Vignattibe143262012-04-16 17:31:41 +03002771static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002772shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002773{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002774 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002775}
2776
Pekka Paalanen8274d902014-08-06 19:36:51 +03002777static int
2778black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2779{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002780 struct weston_view *fs_view = surface->configure_private;
2781 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002782 int n;
2783 int rem;
2784 int ret;
2785
2786 n = snprintf(buf, len, "black background surface for ");
2787 if (n < 0)
2788 return n;
2789
2790 rem = (int)len - n;
2791 if (rem < 0)
2792 rem = 0;
2793
2794 if (fs_surface->get_label)
2795 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2796 else
2797 ret = snprintf(buf + n, rem, "<unknown>");
2798
2799 if (ret < 0)
2800 return n;
2801
2802 return n + ret;
2803}
2804
Alex Wu21858432012-04-01 20:13:08 +08002805static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002806black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002807
Jason Ekstranda7af7042013-10-12 22:38:11 -05002808static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002809create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002810 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002811 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002812{
2813 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002814 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002815
2816 surface = weston_surface_create(ec);
2817 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002818 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002819 return NULL;
2820 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821 view = weston_view_create(surface);
2822 if (surface == NULL) {
2823 weston_log("no memory\n");
2824 weston_surface_destroy(surface);
2825 return NULL;
2826 }
Alex Wu4539b082012-03-01 12:57:46 +08002827
Alex Wu21858432012-04-01 20:13:08 +08002828 surface->configure = black_surface_configure;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002829 surface->configure_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002830 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002831 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002832 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002833 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002834 pixman_region32_fini(&surface->input);
2835 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002836
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002837 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002838 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002839
2840 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002841}
2842
Philip Withnalled8f1a92013-11-25 18:01:43 +00002843static void
2844shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2845{
2846 struct weston_output *output = shsurf->fullscreen_output;
2847
Rafael Antognolli03b16592013-12-03 15:35:42 -02002848 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002849
2850 if (!shsurf->fullscreen.black_view)
2851 shsurf->fullscreen.black_view =
2852 create_black_surface(shsurf->surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002853 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002854 output->x, output->y,
2855 output->width,
2856 output->height);
2857
2858 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002859 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2860 weston_layer_entry_insert(&shsurf->view->layer_link,
2861 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002862 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2863 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002864
Armin Krezović4663aca2016-06-30 06:04:29 +02002865 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002866 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002867}
2868
Alex Wu4539b082012-03-01 12:57:46 +08002869/* Create black surface and append it to the associated fullscreen surface.
2870 * Handle size dismatch and positioning according to the method. */
2871static void
2872shell_configure_fullscreen(struct shell_surface *shsurf)
2873{
2874 struct weston_output *output = shsurf->fullscreen_output;
2875 struct weston_surface *surface = shsurf->surface;
2876 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002877 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002878 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002879
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002880 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2881 restore_output_mode(output);
2882
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002883 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002884 weston_layer_entry_remove(&shsurf->view->layer_link);
2885 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002886
Philip Withnalled8f1a92013-11-25 18:01:43 +00002887 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002888
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002890 &surf_width, &surf_height);
2891
Alex Wu4539b082012-03-01 12:57:46 +08002892 switch (shsurf->fullscreen.type) {
2893 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002894 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002895 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002896 break;
2897 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002898 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002899 if (output->width == surf_width &&
2900 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 weston_view_set_position(shsurf->view,
2902 output->x - surf_x,
2903 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002904 break;
2905 }
2906
Alex Wu4539b082012-03-01 12:57:46 +08002907 matrix = &shsurf->fullscreen.transform.matrix;
2908 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002909
Scott Moreau1bad5db2012-08-18 01:04:05 -06002910 output_aspect = (float) output->width /
2911 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 /* XXX: Use surf_width and surf_height here? */
2913 surface_aspect = (float) surface->width /
2914 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002915 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002916 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002917 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002918 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002919 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002920 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002921
Alex Wu4539b082012-03-01 12:57:46 +08002922 weston_matrix_scale(matrix, scale, scale, 1);
2923 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002924 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002925 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002926 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2927 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002929
Alex Wu4539b082012-03-01 12:57:46 +08002930 break;
2931 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002932 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002933 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002934 surf_width * surface->buffer_viewport.buffer.scale,
2935 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002936 shsurf->fullscreen.framerate};
2937
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002938 if (weston_output_mode_switch_to_temporary(output, &mode,
2939 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 weston_view_set_position(shsurf->view,
2941 output->x - surf_x,
2942 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002943 shsurf->fullscreen.black_view->surface->width = output->width;
2944 shsurf->fullscreen.black_view->surface->height = output->height;
2945 weston_view_set_position(shsurf->fullscreen.black_view,
2946 output->x - surf_x,
2947 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002948 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002949 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002950 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002951 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002953 }
Alex Wubd3354b2012-04-17 17:20:49 +08002954 }
Alex Wu4539b082012-03-01 12:57:46 +08002955 break;
2956 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002957 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002958 break;
2959 default:
2960 break;
2961 }
2962}
2963
Alex Wu4539b082012-03-01 12:57:46 +08002964static void
2965shell_map_fullscreen(struct shell_surface *shsurf)
2966{
Alex Wubd3354b2012-04-17 17:20:49 +08002967 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002968}
2969
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002970static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002971set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2972{
2973 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002974 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002975 shsurf->transient.x = x;
2976 shsurf->transient.y = y;
2977 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002978
2979 shell_surface_set_parent(shsurf, NULL);
2980
Pekka Paalanenb5026542014-11-12 15:09:24 +02002981 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002982 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002983}
2984
Kristian Høgsberg47792412014-05-04 13:47:06 -07002985static void
2986shell_interface_set_fullscreen(struct shell_surface *shsurf,
2987 uint32_t method,
2988 uint32_t framerate,
2989 struct weston_output *output)
2990{
2991 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002992 shsurf->next_state.fullscreen = true;
2993 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01002994
2995 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07002996}
2997
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002998static struct weston_output *
2999get_focused_output(struct weston_compositor *compositor)
3000{
3001 struct weston_seat *seat;
3002 struct weston_output *output = NULL;
3003
3004 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003005 struct weston_touch *touch = weston_seat_get_touch(seat);
3006 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3007 struct weston_keyboard *keyboard =
3008 weston_seat_get_keyboard(seat);
3009
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003010 /* Priority has touch focus, then pointer and
3011 * then keyboard focus. We should probably have
3012 * three for loops and check frist for touch,
3013 * then for pointer, etc. but unless somebody has some
3014 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05003015 if (touch && touch->focus)
3016 output = touch->focus->output;
3017 else if (pointer && pointer->focus)
3018 output = pointer->focus->output;
3019 else if (keyboard && keyboard->focus)
3020 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003021
3022 if (output)
3023 break;
3024 }
3025
3026 return output;
3027}
3028
3029static void
3030shell_interface_set_maximized(struct shell_surface *shsurf)
3031{
3032 struct weston_output *output;
3033
3034 surface_clear_next_states(shsurf);
3035 shsurf->next_state.maximized = true;
3036 shsurf->state_changed = true;
3037 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3038
3039 if (!weston_surface_is_mapped(shsurf->surface))
3040 output = get_focused_output(shsurf->surface->compositor);
3041 else
3042 output = shsurf->surface->output;
3043
3044 shell_surface_set_output(shsurf, output);
3045 send_configure_for_surface(shsurf);
3046}
3047
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003048static int
Derek Foremane4d6c832015-08-05 14:48:11 -05003049shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003050{
Derek Foremane4d6c832015-08-05 14:48:11 -05003051 return surface_move(shsurf, pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003052}
3053
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003054static int
3055shell_interface_resize(struct shell_surface *shsurf,
Derek Foremane4d6c832015-08-05 14:48:11 -05003056 struct weston_pointer *pointer,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003057 uint32_t edges)
3058{
Derek Foremane4d6c832015-08-05 14:48:11 -05003059 return surface_resize(shsurf, pointer, edges);
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003060}
3061
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003062static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003063
3064static void
3065destroy_shell_seat(struct wl_listener *listener, void *data)
3066{
3067 struct shell_seat *shseat =
3068 container_of(listener,
3069 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003070 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003071
3072 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003073 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074 shseat->popup_grab.client = NULL;
3075
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003076 wl_list_for_each_safe(shsurf, next,
3077 &shseat->popup_grab.surfaces_list,
3078 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003079 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003080 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003081 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003082 }
3083
3084 wl_list_remove(&shseat->seat_destroy_listener.link);
3085 free(shseat);
3086}
3087
Jason Ekstrand024177c2014-04-21 19:42:58 -05003088static void
3089shell_seat_caps_changed(struct wl_listener *listener, void *data)
3090{
Derek Foreman1281a362015-07-31 16:55:32 -05003091 struct weston_keyboard *keyboard;
3092 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05003093 struct shell_seat *seat;
3094
3095 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003096 keyboard = weston_seat_get_keyboard(seat->seat);
3097 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003098
Derek Foreman1281a362015-07-31 16:55:32 -05003099 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003100 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003101 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003102 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003103 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003104 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003105 wl_list_init(&seat->keyboard_focus_listener.link);
3106 }
3107
Derek Foreman1281a362015-07-31 16:55:32 -05003108 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05003109 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05003110 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05003111 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05003112 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003113 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003114 wl_list_init(&seat->pointer_focus_listener.link);
3115 }
3116}
3117
Giulio Camuffo5085a752013-03-25 21:42:45 +01003118static struct shell_seat *
3119create_shell_seat(struct weston_seat *seat)
3120{
3121 struct shell_seat *shseat;
3122
3123 shseat = calloc(1, sizeof *shseat);
3124 if (!shseat) {
3125 weston_log("no memory to allocate shell seat\n");
3126 return NULL;
3127 }
3128
3129 shseat->seat = seat;
3130 wl_list_init(&shseat->popup_grab.surfaces_list);
3131
3132 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3133 wl_signal_add(&seat->destroy_signal,
3134 &shseat->seat_destroy_listener);
3135
Jason Ekstrand024177c2014-04-21 19:42:58 -05003136 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3137 wl_list_init(&shseat->keyboard_focus_listener.link);
3138
3139 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3140 wl_list_init(&shseat->pointer_focus_listener.link);
3141
3142 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3143 wl_signal_add(&seat->updated_caps_signal,
3144 &shseat->caps_changed_listener);
3145 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3146
Giulio Camuffo5085a752013-03-25 21:42:45 +01003147 return shseat;
3148}
3149
3150static struct shell_seat *
3151get_shell_seat(struct weston_seat *seat)
3152{
3153 struct wl_listener *listener;
3154
3155 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003156 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003157
3158 return container_of(listener,
3159 struct shell_seat, seat_destroy_listener);
3160}
3161
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003162static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003163popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003164{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003165 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003166 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003167 struct shell_seat *shseat =
3168 container_of(grab, struct shell_seat, popup_grab.grab);
3169 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003170 wl_fixed_t sx, sy;
3171
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 view = weston_compositor_pick_view(pointer->seat->compositor,
3173 pointer->x, pointer->y,
3174 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175
Jason Ekstranda7af7042013-10-12 22:38:11 -05003176 if (view && view->surface->resource &&
3177 wl_resource_get_client(view->surface->resource) == client) {
3178 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003179 } else {
Derek Foremanf9318d12015-05-11 15:40:11 -05003180 weston_pointer_clear_focus(pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181 }
3182}
3183
3184static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003185popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003186 struct weston_pointer_motion_event *event)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003187{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003188 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003189 struct wl_resource *resource;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003190 struct wl_list *resource_list;
Jonas Ådahld2510102014-10-05 21:39:14 +02003191 wl_fixed_t x, y;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003192 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003193
Jonas Ådahl61917c82014-08-11 22:44:02 +02003194 if (pointer->focus) {
Jonas Ådahld2510102014-10-05 21:39:14 +02003195 weston_pointer_motion_to_abs(pointer, event, &x, &y);
Jonas Ådahl61917c82014-08-11 22:44:02 +02003196 weston_view_from_global_fixed(pointer->focus, x, y,
3197 &pointer->sx, &pointer->sy);
3198 }
3199
Jonas Ådahld2510102014-10-05 21:39:14 +02003200 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003201
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003202 if (!pointer->focus_client)
3203 return;
3204
3205 resource_list = &pointer->focus_client->pointer_resources;
3206 wl_resource_for_each(resource, resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003207 weston_view_from_global_fixed(pointer->focus,
3208 pointer->x, pointer->y,
3209 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003210 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003211 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212}
3213
3214static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003215popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003216 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003217{
3218 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003219 struct shell_seat *shseat =
3220 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003221 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003222 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003223 uint32_t serial;
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003224 struct wl_list *resource_list = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003225
Jonas Ådahl2cbf2932015-07-22 12:05:38 +08003226 if (grab->pointer->focus_client)
3227 resource_list = &grab->pointer->focus_client->pointer_resources;
3228 if (resource_list && !wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003229 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003230 wl_resource_for_each(resource, resource_list) {
3231 wl_pointer_send_button(resource, serial,
3232 time, button, state);
3233 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003234 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003235 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003236 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003237 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003238 }
3239
Daniel Stone4dbadb12012-05-30 16:31:51 +01003240 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003241 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003242}
3243
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003244static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003245popup_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003246 uint32_t time,
3247 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003248{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003249 weston_pointer_send_axis(grab->pointer, time, event);
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003250}
3251
3252static void
Peter Hutterer87743e92016-01-18 16:38:22 +10003253popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3254{
3255 weston_pointer_send_axis_source(grab->pointer, source);
3256}
3257
3258static void
3259popup_grab_frame(struct weston_pointer_grab *grab)
3260{
3261 weston_pointer_send_frame(grab->pointer);
3262}
3263
3264static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003265popup_grab_cancel(struct weston_pointer_grab *grab)
3266{
3267 popup_grab_end(grab->pointer);
3268}
3269
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003270static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003271 popup_grab_focus,
3272 popup_grab_motion,
3273 popup_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003274 popup_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003275 popup_grab_axis_source,
3276 popup_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003277 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003278};
3279
3280static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003281touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003282 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003283{
3284 struct wl_resource *resource;
3285 struct shell_seat *shseat =
3286 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3287 struct wl_display *display = shseat->seat->compositor->wl_display;
3288 uint32_t serial;
3289 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003290 wl_fixed_t sx, sy;
3291
3292 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003293
3294 resource_list = &grab->touch->focus_resource_list;
3295 if (!wl_list_empty(resource_list)) {
3296 serial = wl_display_get_serial(display);
3297 wl_resource_for_each(resource, resource_list) {
3298 wl_touch_send_down(resource, serial, time,
3299 grab->touch->focus->surface->resource,
3300 touch_id, sx, sy);
3301 }
3302 }
3303}
3304
3305static void
3306touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3307{
3308 struct wl_resource *resource;
3309 struct shell_seat *shseat =
3310 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3311 struct wl_display *display = shseat->seat->compositor->wl_display;
3312 uint32_t serial;
3313 struct wl_list *resource_list;
3314
3315 resource_list = &grab->touch->focus_resource_list;
3316 if (!wl_list_empty(resource_list)) {
3317 serial = wl_display_get_serial(display);
3318 wl_resource_for_each(resource, resource_list) {
3319 wl_touch_send_up(resource, serial, time, touch_id);
3320 }
3321 }
3322}
3323
3324static void
3325touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003326 int touch_id, wl_fixed_t x, wl_fixed_t y)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003327{
3328 struct wl_resource *resource;
3329 struct wl_list *resource_list;
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03003330 wl_fixed_t sx, sy;
3331
3332 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003333
3334 resource_list = &grab->touch->focus_resource_list;
3335 if (!wl_list_empty(resource_list)) {
3336 wl_resource_for_each(resource, resource_list) {
3337 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3338 }
3339 }
3340}
3341
3342static void
3343touch_popup_grab_frame(struct weston_touch_grab *grab)
3344{
3345}
3346
3347static void
3348touch_popup_grab_cancel(struct weston_touch_grab *grab)
3349{
3350 touch_popup_grab_end(grab->touch);
3351}
3352
3353static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3354 touch_popup_grab_down,
3355 touch_popup_grab_up,
3356 touch_popup_grab_motion,
3357 touch_popup_grab_frame,
3358 touch_popup_grab_cancel,
3359};
3360
3361static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362shell_surface_send_popup_done(struct shell_surface *shsurf)
3363{
Bryce Harrington22b1f932015-09-23 17:30:43 -07003364 if (shsurf->resource == NULL)
3365 return;
3366 else if (shell_surface_is_wl_shell_surface(shsurf))
Rafael Antognollie2a34552013-12-03 15:35:45 -02003367 wl_shell_surface_send_popup_done(shsurf->resource);
3368 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003369 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003370}
3371
3372static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003373popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003374{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003375 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003376 struct shell_seat *shseat =
3377 container_of(grab, struct shell_seat, popup_grab.grab);
3378 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003379 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003380
3381 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003382 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003383 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003384 shseat->popup_grab.grab.interface = NULL;
3385 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003386 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003387 wl_list_for_each_safe(shsurf, next,
3388 &shseat->popup_grab.surfaces_list,
3389 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003390 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003391 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003392 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003393 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003394 wl_list_init(&shseat->popup_grab.surfaces_list);
3395 }
3396}
3397
3398static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003399touch_popup_grab_end(struct weston_touch *touch)
3400{
3401 struct weston_touch_grab *grab = touch->grab;
3402 struct shell_seat *shseat =
3403 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3404 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003405 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003406
3407 if (touch->grab->interface == &touch_popup_grab_interface) {
3408 weston_touch_end_grab(grab->touch);
3409 shseat->popup_grab.client = NULL;
3410 shseat->popup_grab.touch_grab.interface = NULL;
3411 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3412 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003413 wl_list_for_each_safe(shsurf, next,
3414 &shseat->popup_grab.surfaces_list,
3415 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003416 shell_surface_send_popup_done(shsurf);
3417 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003418 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003419 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003420 wl_list_init(&shseat->popup_grab.surfaces_list);
3421 }
3422}
3423
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003424static struct shell_surface *
3425get_top_popup(struct shell_seat *shseat)
3426{
3427 struct shell_surface *shsurf;
3428
3429 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3430 return NULL;
3431 } else {
3432 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3433 struct shell_surface,
3434 popup.grab_link);
3435 return shsurf;
3436 }
3437}
3438
3439static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003440add_popup_grab(struct shell_surface *shsurf,
3441 struct shell_seat *shseat,
3442 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003443{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003444 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003445 struct shell_surface *parent, *top_surface;
Derek Foreman1281a362015-07-31 16:55:32 -05003446 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3447 struct weston_touch *touch = weston_seat_get_touch(seat);
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003448
3449 parent = get_shell_surface(shsurf->parent);
3450 top_surface = get_top_popup(shseat);
3451 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003452 (!parent ||
3453 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003454 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003455 wl_resource_post_error(shsurf->owner_resource,
3456 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003457 "xdg_popup was not created on the "
3458 "topmost popup");
3459 return -1;
3460 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003461
3462 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003463 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003464 shseat->popup_grab.client =
3465 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003466
3467 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003468 shseat->popup_grab.grab.interface =
3469 &popup_grab_interface;
3470
3471 /* We must make sure here that this popup was opened
3472 * after a mouse press, and not just by moving around
3473 * with other popups already open. */
Derek Foreman1281a362015-07-31 16:55:32 -05003474 if (pointer->button_count > 0)
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003475 shseat->popup_grab.initial_up = 0;
3476 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003477 shseat->popup_grab.touch_grab.interface =
3478 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003479 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003480
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003481 wl_list_insert(&shseat->popup_grab.surfaces_list,
3482 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003483
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003484 if (type == POINTER) {
Derek Foreman1281a362015-07-31 16:55:32 -05003485 weston_pointer_start_grab(pointer,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003486 &shseat->popup_grab.grab);
3487 } else if (type == TOUCH) {
Derek Foreman1281a362015-07-31 16:55:32 -05003488 weston_touch_start_grab(touch,
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003489 &shseat->popup_grab.touch_grab);
3490 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003491 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003492 wl_list_insert(&shseat->popup_grab.surfaces_list,
3493 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003494 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003495
3496 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003497}
3498
3499static void
3500remove_popup_grab(struct shell_surface *shsurf)
3501{
3502 struct shell_seat *shseat = shsurf->popup.shseat;
3503
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003504 if (shell_surface_is_xdg_popup(shsurf) &&
3505 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003506 wl_resource_post_error(shsurf->owner_resource,
3507 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003508 "xdg_popup was destroyed while it was "
3509 "not the topmost popup.");
3510 return;
3511 }
3512
Giulio Camuffo5085a752013-03-25 21:42:45 +01003513 wl_list_remove(&shsurf->popup.grab_link);
3514 wl_list_init(&shsurf->popup.grab_link);
3515 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003516 if (shseat->popup_grab.type == POINTER) {
3517 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3518 shseat->popup_grab.grab.interface = NULL;
3519 } else if (shseat->popup_grab.type == TOUCH) {
3520 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3521 shseat->popup_grab.touch_grab.interface = NULL;
3522 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003523 }
3524}
3525
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003526static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003527shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003528{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003529 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003530 struct weston_view *parent_view = get_default_view(shsurf->parent);
Derek Foreman1281a362015-07-31 16:55:32 -05003531 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3532 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003533
Jason Ekstranda7af7042013-10-12 22:38:11 -05003534 shsurf->surface->output = parent_view->output;
Armin Krezović4663aca2016-06-30 06:04:29 +02003535 shsurf->surface->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003536 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003537
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 weston_view_set_transform_parent(shsurf->view, parent_view);
3539 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3540 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003541 shsurf->view->is_mapped = true;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003542
Derek Foreman1281a362015-07-31 16:55:32 -05003543 if (pointer &&
3544 pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003545 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3546 return -1;
Derek Foreman1281a362015-07-31 16:55:32 -05003547 } else if (touch &&
3548 touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003549 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3550 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003551 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003552 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003553 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003554 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003555
3556 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003557}
3558
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003559static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003560 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003561 shell_surface_move,
3562 shell_surface_resize,
3563 shell_surface_set_toplevel,
3564 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003565 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003566 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003567 shell_surface_set_maximized,
3568 shell_surface_set_title,
3569 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003570};
3571
3572static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003573destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003574{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003575 struct shell_surface *child, *next;
3576
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003577 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3578
Giulio Camuffo5085a752013-03-25 21:42:45 +01003579 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3580 remove_popup_grab(shsurf);
3581 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003582
Alex Wubd3354b2012-04-17 17:20:49 +08003583 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003584 shell_surface_is_top_fullscreen(shsurf))
3585 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003586
Jason Ekstranda7af7042013-10-12 22:38:11 -05003587 if (shsurf->fullscreen.black_view)
3588 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003589
Alex Wubd3354b2012-04-17 17:20:49 +08003590 /* As destroy_resource() use wl_list_for_each_safe(),
3591 * we can always remove the listener.
3592 */
3593 wl_list_remove(&shsurf->surface_destroy_listener.link);
3594 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003595 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003596 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003597
Jason Ekstranda7af7042013-10-12 22:38:11 -05003598 weston_view_destroy(shsurf->view);
3599
Philip Withnall648a4dd2013-11-25 18:01:44 +00003600 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003601 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3602 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003603
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003604 wl_list_remove(&shsurf->link);
3605 free(shsurf);
3606}
3607
3608static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003609shell_destroy_shell_surface(struct wl_resource *resource)
3610{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003611 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003612
Bryan Caina46b9462014-04-04 17:41:24 -05003613 if (!wl_list_empty(&shsurf->popup.grab_link))
3614 remove_popup_grab(shsurf);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003615 if (shsurf->resource)
3616 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003617 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003618}
3619
3620static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003621shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003622{
3623 struct shell_surface *shsurf = container_of(listener,
3624 struct shell_surface,
3625 surface_destroy_listener);
3626
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003627 if (shsurf->resource)
3628 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003629
3630 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003631}
3632
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003633static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003634fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003635{
3636 struct shell_surface *shsurf = data;
3637
3638 weston_surface_destroy(shsurf->surface);
3639}
3640
3641static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003642fade_out_done(struct weston_view_animation *animation, void *data)
3643{
3644 struct shell_surface *shsurf = data;
3645 struct wl_event_loop *loop;
3646
3647 loop = wl_display_get_event_loop(
3648 shsurf->surface->compositor->wl_display);
3649
3650 if (!shsurf->destroying) {
3651 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3652 shsurf->destroying = true;
3653 }
3654}
3655
3656static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003657handle_resource_destroy(struct wl_listener *listener, void *data)
3658{
3659 struct shell_surface *shsurf =
3660 container_of(listener, struct shell_surface,
3661 resource_destroy_listener);
3662
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003663 if (!weston_surface_is_mapped(shsurf->surface))
3664 return;
3665
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003666 shsurf->surface->ref_count++;
3667
3668 pixman_region32_fini(&shsurf->surface->pending.input);
3669 pixman_region32_init(&shsurf->surface->pending.input);
3670 pixman_region32_fini(&shsurf->surface->input);
3671 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003672 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3673 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3674 fade_out_done, shsurf);
3675 } else {
3676 weston_surface_destroy(shsurf->surface);
3677 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003678}
3679
3680static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003681shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003682
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003683struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003684get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003685{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003686 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003687 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003688 else
3689 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003690}
3691
Rafael Antognollie2a34552013-12-03 15:35:45 -02003692static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003693create_common_surface(struct shell_client *owner, void *shell,
3694 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003695 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003696{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003697 struct shell_surface *shsurf;
3698
Pekka Paalanen50b67472014-10-01 15:02:41 +03003699 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003700
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003701 shsurf = calloc(1, sizeof *shsurf);
3702 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003703 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003704 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003705 }
3706
Jason Ekstranda7af7042013-10-12 22:38:11 -05003707 shsurf->view = weston_view_create(surface);
3708 if (!shsurf->view) {
3709 weston_log("no memory to allocate shell surface\n");
3710 free(shsurf);
3711 return NULL;
3712 }
3713
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003714 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003715 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003716 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003717
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003718 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3719 wl_resource_add_destroy_listener(surface->resource,
3720 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003721 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003722
Tiago Vignattibc052c92012-04-19 16:18:18 +03003723 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003724 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003725 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003726 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003727 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003728 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3729 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003730 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003731 wl_list_init(&shsurf->fullscreen.transform.link);
3732
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003733 shsurf->output = get_default_output(shsurf->shell->compositor);
3734
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003735 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003736 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003737 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003738 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003739
3740 /* init link so its safe to always remove it in destroy_shell_surface */
3741 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003742 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003743
Pekka Paalanen460099f2012-01-20 16:48:25 +02003744 /* empty when not in use */
3745 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003746 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003747
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003748 wl_list_init(&shsurf->workspace_transform.link);
3749
Philip Withnall648a4dd2013-11-25 18:01:44 +00003750 wl_list_init(&shsurf->children_link);
3751 wl_list_init(&shsurf->children_list);
3752 shsurf->parent = NULL;
3753
Pekka Paalanenb5026542014-11-12 15:09:24 +02003754 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003755
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003756 shsurf->client = client;
3757
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003758 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003759}
3760
Rafael Antognollie2a34552013-12-03 15:35:45 -02003761static struct shell_surface *
3762create_shell_surface(void *shell, struct weston_surface *surface,
3763 const struct weston_shell_client *client)
3764{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003765 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003766}
3767
Tiago Vignattibc052c92012-04-19 16:18:18 +03003768static void
3769shell_get_shell_surface(struct wl_client *client,
3770 struct wl_resource *resource,
3771 uint32_t id,
3772 struct wl_resource *surface_resource)
3773{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003774 struct weston_surface *surface =
3775 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003776 struct shell_client *sc = wl_resource_get_user_data(resource);
3777 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003778 struct shell_surface *shsurf;
3779
Pekka Paalanen50b67472014-10-01 15:02:41 +03003780 if (weston_surface_set_role(surface, "wl_shell_surface",
3781 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003782 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003783
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003784 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003785 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003786 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003787 return;
3788 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003789
Jason Ekstranda85118c2013-06-27 20:17:02 -05003790 shsurf->resource =
3791 wl_resource_create(client,
3792 &wl_shell_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07003793 if (!shsurf->resource) {
3794 wl_resource_post_no_memory(surface_resource);
3795 return;
3796 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003797 wl_resource_set_implementation(shsurf->resource,
3798 &shell_surface_implementation,
3799 shsurf, shell_destroy_shell_surface);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08003800 wl_list_insert(&sc->surface_list,
3801 wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003802}
3803
Rafael Antognollie2a34552013-12-03 15:35:45 -02003804static bool
3805shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3806{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003807 /* A shell surface without a resource is created from xwayland
3808 * and is considered a wl_shell surface for now. */
3809
3810 return shsurf->resource == NULL ||
3811 wl_resource_instance_of(shsurf->resource,
3812 &wl_shell_surface_interface,
3813 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003814}
3815
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003816static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003817 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003818};
3819
Rafael Antognollie2a34552013-12-03 15:35:45 -02003820/****************************
3821 * xdg-shell implementation */
3822
3823static void
3824xdg_surface_destroy(struct wl_client *client,
3825 struct wl_resource *resource)
3826{
3827 wl_resource_destroy(resource);
3828}
3829
3830static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003831xdg_surface_set_parent(struct wl_client *client,
3832 struct wl_resource *resource,
3833 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003834{
3835 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003836 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003837
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003838 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003839 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003840 shell_surface_set_parent(shsurf, parent->surface);
3841 } else {
3842 shell_surface_set_parent(shsurf, NULL);
3843 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003844}
3845
3846static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003847xdg_surface_set_app_id(struct wl_client *client,
3848 struct wl_resource *resource,
3849 const char *app_id)
3850{
3851 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3852
3853 free(shsurf->class);
3854 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003855 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003856}
3857
3858static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003859xdg_surface_show_window_menu(struct wl_client *client,
3860 struct wl_resource *surface_resource,
3861 struct wl_resource *seat_resource,
3862 uint32_t serial,
3863 int32_t x,
3864 int32_t y)
3865{
3866 /* TODO */
3867}
3868
3869static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003870xdg_surface_set_title(struct wl_client *client,
3871 struct wl_resource *resource, const char *title)
3872{
3873 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3874
3875 set_title(shsurf, title);
3876}
3877
3878static void
3879xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3880 struct wl_resource *seat_resource, uint32_t serial)
3881{
3882 common_surface_move(resource, seat_resource, serial);
3883}
3884
3885static void
3886xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3887 struct wl_resource *seat_resource, uint32_t serial,
3888 uint32_t edges)
3889{
3890 common_surface_resize(resource, seat_resource, serial, edges);
3891}
3892
3893static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003894xdg_surface_ack_configure(struct wl_client *client,
3895 struct wl_resource *resource,
3896 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003897{
3898 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3899
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003900 if (shsurf->state_requested) {
3901 shsurf->next_state = shsurf->requested_state;
3902 shsurf->state_changed = true;
3903 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003904 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003905}
3906
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003907static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003908xdg_surface_set_window_geometry(struct wl_client *client,
3909 struct wl_resource *resource,
3910 int32_t x,
3911 int32_t y,
3912 int32_t width,
3913 int32_t height)
3914{
3915 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3916
3917 set_window_geometry(shsurf, x, y, width, height);
3918}
3919
3920static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003921xdg_surface_set_maximized(struct wl_client *client,
3922 struct wl_resource *resource)
3923{
3924 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003925 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003926
3927 shsurf->state_requested = true;
3928 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003929
3930 if (!weston_surface_is_mapped(shsurf->surface))
3931 output = get_focused_output(shsurf->surface->compositor);
3932 else
3933 output = shsurf->surface->output;
3934
3935 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003936 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003937}
3938
3939static void
3940xdg_surface_unset_maximized(struct wl_client *client,
3941 struct wl_resource *resource)
3942{
3943 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3944
3945 shsurf->state_requested = true;
3946 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003947 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003948}
3949
3950static void
3951xdg_surface_set_fullscreen(struct wl_client *client,
3952 struct wl_resource *resource,
3953 struct wl_resource *output_resource)
3954{
3955 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3956 struct weston_output *output;
3957
3958 shsurf->state_requested = true;
3959 shsurf->requested_state.fullscreen = true;
3960
3961 if (output_resource)
3962 output = wl_resource_get_user_data(output_resource);
3963 else
3964 output = NULL;
3965
Marek Chalupa052917a2014-09-02 11:35:12 +02003966 /* handle clients launching in fullscreen */
3967 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3968 /* Set the output to the one that has focus currently. */
3969 assert(shsurf->surface);
3970 output = get_focused_output(shsurf->surface->compositor);
3971 }
3972
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003973 shell_surface_set_output(shsurf, output);
3974 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003975
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003976 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003977}
3978
3979static void
3980xdg_surface_unset_fullscreen(struct wl_client *client,
3981 struct wl_resource *resource)
3982{
3983 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3984
3985 shsurf->state_requested = true;
3986 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003987 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003988}
3989
3990static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003991xdg_surface_set_minimized(struct wl_client *client,
3992 struct wl_resource *resource)
3993{
3994 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3995
3996 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3997 return;
3998
3999 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01004000 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004001}
4002
Rafael Antognollie2a34552013-12-03 15:35:45 -02004003static const struct xdg_surface_interface xdg_surface_implementation = {
4004 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004005 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004006 xdg_surface_set_title,
4007 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04004008 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004009 xdg_surface_move,
4010 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004011 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004012 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004013 xdg_surface_set_maximized,
4014 xdg_surface_unset_maximized,
4015 xdg_surface_set_fullscreen,
4016 xdg_surface_unset_fullscreen,
4017 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004018};
4019
4020static void
4021xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004022 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004023{
4024 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004025 uint32_t *s;
4026 struct wl_array states;
4027 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004028
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004029 assert(shsurf);
4030
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004031 if (!shsurf->resource)
4032 return;
4033
4034 wl_array_init(&states);
4035 if (shsurf->requested_state.fullscreen) {
4036 s = wl_array_add(&states, sizeof *s);
4037 *s = XDG_SURFACE_STATE_FULLSCREEN;
4038 } else if (shsurf->requested_state.maximized) {
4039 s = wl_array_add(&states, sizeof *s);
4040 *s = XDG_SURFACE_STATE_MAXIMIZED;
4041 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004042 if (shsurf->resize_edges != 0) {
4043 s = wl_array_add(&states, sizeof *s);
4044 *s = XDG_SURFACE_STATE_RESIZING;
4045 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004046 if (shsurf->focus_count > 0) {
4047 s = wl_array_add(&states, sizeof *s);
4048 *s = XDG_SURFACE_STATE_ACTIVATED;
4049 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004050
4051 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4052 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4053
4054 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004055}
4056
4057static const struct weston_shell_client xdg_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004058 xdg_send_configure,
4059 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004060};
4061
4062static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004063xdg_shell_destroy(struct wl_client *client,
4064 struct wl_resource *resource)
4065{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004066 struct shell_client *sc = wl_resource_get_user_data(resource);
4067 struct wl_resource *shsurf_resource;
4068 struct shell_surface *shsurf;
4069
4070 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4071 shsurf = wl_resource_get_user_data(shsurf_resource);
4072 if (shsurf->owner_resource == resource) {
4073 wl_resource_post_error(
4074 resource,
4075 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4076 "not all child surface objects destroyed");
4077 return;
4078 }
4079 }
4080
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004081 wl_resource_destroy(resource);
4082}
4083
4084static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004085xdg_use_unstable_version(struct wl_client *client,
4086 struct wl_resource *resource,
4087 int32_t version)
4088{
4089 if (version > 1) {
4090 wl_resource_post_error(resource,
4091 1,
4092 "xdg-shell:: version not implemented yet.");
4093 return;
4094 }
4095}
4096
4097static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004098create_xdg_surface(struct shell_client *owner, void *shell,
4099 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004100 const struct weston_shell_client *client)
4101{
4102 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004103
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004104 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004105 if (!shsurf)
4106 return NULL;
4107
Pekka Paalanenb5026542014-11-12 15:09:24 +02004108 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004109
4110 return shsurf;
4111}
4112
4113static void
4114xdg_get_xdg_surface(struct wl_client *client,
4115 struct wl_resource *resource,
4116 uint32_t id,
4117 struct wl_resource *surface_resource)
4118{
4119 struct weston_surface *surface =
4120 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004121 struct shell_client *sc = wl_resource_get_user_data(resource);
4122 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004123 struct shell_surface *shsurf;
4124
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004125 shsurf = get_shell_surface(surface);
4126 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4127 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4128 "This wl_surface is already an "
4129 "xdg_surface");
4130 return;
4131 }
4132
Pekka Paalanen50b67472014-10-01 15:02:41 +03004133 if (weston_surface_set_role(surface, "xdg_surface",
4134 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004135 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004136
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004137 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004138 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004139 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004140 return;
4141 }
4142
4143 shsurf->resource =
4144 wl_resource_create(client,
4145 &xdg_surface_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004146 if (!shsurf->resource) {
4147 wl_resource_post_no_memory(surface_resource);
4148 return;
4149 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004150 wl_resource_set_implementation(shsurf->resource,
4151 &xdg_surface_implementation,
4152 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004153 shsurf->owner_resource = resource;
4154 wl_list_insert(&sc->surface_list,
4155 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004156}
4157
4158static bool
4159shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4160{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004161 return shsurf->resource &&
4162 wl_resource_instance_of(shsurf->resource,
4163 &xdg_surface_interface,
4164 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004165}
4166
4167/* xdg-popup implementation */
4168
4169static void
4170xdg_popup_destroy(struct wl_client *client,
4171 struct wl_resource *resource)
4172{
4173 wl_resource_destroy(resource);
4174}
4175
Rafael Antognollie2a34552013-12-03 15:35:45 -02004176static const struct xdg_popup_interface xdg_popup_implementation = {
4177 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004178};
4179
4180static void
4181xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004182 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004183{
4184}
4185
4186static const struct weston_shell_client xdg_popup_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004187 xdg_popup_send_configure,
4188 NULL
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189};
4190
4191static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004192create_xdg_popup(struct shell_client *owner, void *shell,
4193 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004194 const struct weston_shell_client *client,
4195 struct weston_surface *parent,
4196 struct shell_seat *seat,
4197 uint32_t serial,
4198 int32_t x, int32_t y)
4199{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004200 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004201
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004202 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004203 if (!shsurf)
4204 return NULL;
4205
Pekka Paalanenb5026542014-11-12 15:09:24 +02004206 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004207 shsurf->popup.shseat = seat;
4208 shsurf->popup.serial = serial;
4209 shsurf->popup.x = x;
4210 shsurf->popup.y = y;
4211 shell_surface_set_parent(shsurf, parent);
4212
4213 return shsurf;
4214}
4215
4216static void
4217xdg_get_xdg_popup(struct wl_client *client,
4218 struct wl_resource *resource,
4219 uint32_t id,
4220 struct wl_resource *surface_resource,
4221 struct wl_resource *parent_resource,
4222 struct wl_resource *seat_resource,
4223 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004224 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004225{
4226 struct weston_surface *surface =
4227 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004228 struct shell_client *sc = wl_resource_get_user_data(resource);
4229 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004230 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004231 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004232 struct weston_surface *parent;
4233 struct shell_seat *seat;
4234
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004235 shsurf = get_shell_surface(surface);
4236 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4237 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4238 "This wl_surface is already an "
4239 "xdg_popup");
4240 return;
4241 }
4242
Pekka Paalanen50b67472014-10-01 15:02:41 +03004243 if (weston_surface_set_role(surface, "xdg_popup",
4244 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004245 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004246
4247 if (!parent_resource) {
4248 wl_resource_post_error(surface_resource,
4249 WL_DISPLAY_ERROR_INVALID_OBJECT,
4250 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004251 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004252 }
4253
4254 parent = wl_resource_get_user_data(parent_resource);
Derek Foremanbdc8c722015-10-07 11:51:29 -05004255 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004256
Jonas Ådahlee45a552015-03-18 16:37:47 +08004257 /* Verify that we are creating the top most popup when mapping,
4258 * as it's not until then we know whether it was mapped as most
4259 * top level or not. */
4260
4261 parent_shsurf = get_shell_surface(parent);
Jonas Ådahlbc5d8492015-10-07 14:44:50 +08004262 if (!parent_shsurf ||
4263 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4264 !shell_surface_is_xdg_surface(parent_shsurf))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08004265 wl_resource_post_error(resource,
4266 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4267 "xdg_popup parent was invalid");
4268 return;
4269 }
4270
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004271 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004272 parent, seat, serial, x, y);
4273 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004274 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004275 return;
4276 }
4277
4278 shsurf->resource =
4279 wl_resource_create(client,
4280 &xdg_popup_interface, 1, id);
Bryce Harrington22b1f932015-09-23 17:30:43 -07004281 if (!shsurf->resource) {
4282 wl_resource_post_no_memory(surface_resource);
4283 return;
4284 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02004285 wl_resource_set_implementation(shsurf->resource,
4286 &xdg_popup_implementation,
4287 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004288 shsurf->owner_resource = resource;
4289 wl_list_insert(&sc->surface_list,
4290 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004291}
4292
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004293static void
4294xdg_pong(struct wl_client *client,
4295 struct wl_resource *resource, uint32_t serial)
4296{
4297 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004298
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004299 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004300}
4301
Rafael Antognollie2a34552013-12-03 15:35:45 -02004302static bool
4303shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4304{
Bryce Harrington22b1f932015-09-23 17:30:43 -07004305 return (shsurf->resource &&
4306 wl_resource_instance_of(shsurf->resource,
4307 &xdg_popup_interface,
4308 &xdg_popup_implementation));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004309}
4310
4311static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004312 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004313 xdg_use_unstable_version,
4314 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004315 xdg_get_xdg_popup,
4316 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004317};
4318
4319static int
4320xdg_shell_unversioned_dispatch(const void *implementation,
4321 void *_target, uint32_t opcode,
4322 const struct wl_message *message,
4323 union wl_argument *args)
4324{
4325 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004326 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004327
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004328 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004329 wl_resource_post_error(resource,
4330 WL_DISPLAY_ERROR_INVALID_OBJECT,
4331 "must call use_unstable_version first");
4332 return 0;
4333 }
4334
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004335#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004336
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004337 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4338 "shell implementation doesn't match protocol version");
4339
Rafael Antognollie2a34552013-12-03 15:35:45 -02004340 if (args[0].i != XDG_SERVER_VERSION) {
4341 wl_resource_post_error(resource,
4342 WL_DISPLAY_ERROR_INVALID_OBJECT,
4343 "incompatible version, server is %d "
4344 "client wants %d",
4345 XDG_SERVER_VERSION, args[0].i);
4346 return 0;
4347 }
4348
4349 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004350 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004351
4352 return 1;
4353}
4354
4355/* end of xdg-shell implementation */
4356/***********************************/
4357
Kristian Høgsberg07937562011-04-12 17:25:42 -04004358static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004359shell_fade(struct desktop_shell *shell, enum fade_type type);
4360
Pekka Paalanen77346a62011-11-30 16:26:35 +02004361static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004362configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004363{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004364 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004365
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004366 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004367 if (v->output == ev->output && v != ev) {
4368 weston_view_unmap(v);
4369 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004370 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004371 }
4372 }
4373
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004374 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02004375 ev->surface->is_mapped = true;
4376 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004377
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004378 if (wl_list_empty(&ev->layer_link.link)) {
4379 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004381 }
4382}
4383
David Fort50d962f2016-06-09 17:55:52 +02004384
4385static struct shell_output *
4386find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4387{
4388 struct shell_output *shell_output;
4389
4390 wl_list_for_each(shell_output, &shell->output_list, link) {
4391 if (shell_output->output == output)
4392 return shell_output;
4393 }
4394
4395 return NULL;
4396}
4397
Pekka Paalanen8274d902014-08-06 19:36:51 +03004398static int
4399background_get_label(struct weston_surface *surface, char *buf, size_t len)
4400{
4401 return snprintf(buf, len, "background for output %s",
4402 surface->output->name);
4403}
4404
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004405static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004406background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004407{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004408 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004409 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004410
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 view = container_of(es->views.next, struct weston_view, surface_link);
4412
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004413 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004414}
4415
4416static void
David Fort50d962f2016-06-09 17:55:52 +02004417handle_background_surface_destroy(struct wl_listener *listener, void *data)
4418{
4419 struct shell_output *output =
4420 container_of(listener, struct shell_output, background_surface_listener);
4421
4422 weston_log("background surface gone\n");
4423 output->background_surface = NULL;
4424}
4425
4426static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004427desktop_shell_set_background(struct wl_client *client,
4428 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004429 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004430 struct wl_resource *surface_resource)
4431{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004432 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004433 struct weston_surface *surface =
4434 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02004435 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004437
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004438 if (surface->configure) {
4439 wl_resource_post_error(surface_resource,
4440 WL_DISPLAY_ERROR_INVALID_OBJECT,
4441 "surface role already assigned");
4442 return;
4443 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004444
Jason Ekstranda7af7042013-10-12 22:38:11 -05004445 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4446 weston_view_destroy(view);
4447 view = weston_view_create(surface);
4448
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004449 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004450 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004451 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004452 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004453 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004454 weston_desktop_shell_send_configure(resource, 0,
4455 surface_resource,
4456 surface->output->width,
4457 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004458
4459 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4460 sh_output->background_surface = surface;
4461
4462 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4463 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004464}
4465
Pekka Paalanen8274d902014-08-06 19:36:51 +03004466static int
4467panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4468{
4469 return snprintf(buf, len, "panel for output %s",
4470 surface->output->name);
4471}
4472
Kristian Høgsberg75840622011-09-06 13:48:16 -04004473static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004474panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004475{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004476 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004477 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004478
Jason Ekstranda7af7042013-10-12 22:38:11 -05004479 view = container_of(es->views.next, struct weston_view, surface_link);
4480
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004481 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004482}
4483
4484static void
David Fort50d962f2016-06-09 17:55:52 +02004485handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4486{
4487 struct shell_output *output =
4488 container_of(listener, struct shell_output, panel_surface_listener);
4489
4490 weston_log("panel surface gone\n");
4491 output->panel_surface = NULL;
4492}
4493
4494
4495static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004496desktop_shell_set_panel(struct wl_client *client,
4497 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004498 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004499 struct wl_resource *surface_resource)
4500{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004501 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004502 struct weston_surface *surface =
4503 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004504 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02004505 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004506
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004507 if (surface->configure) {
4508 wl_resource_post_error(surface_resource,
4509 WL_DISPLAY_ERROR_INVALID_OBJECT,
4510 "surface role already assigned");
4511 return;
4512 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004513
Jason Ekstranda7af7042013-10-12 22:38:11 -05004514 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4515 weston_view_destroy(view);
4516 view = weston_view_create(surface);
4517
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004518 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004519 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004520 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004521 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004522 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004523 weston_desktop_shell_send_configure(resource, 0,
4524 surface_resource,
4525 surface->output->width,
4526 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02004527
4528 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4529 sh_output->panel_surface = surface;
4530
4531 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4532 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004533}
4534
Pekka Paalanen8274d902014-08-06 19:36:51 +03004535static int
4536lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4537{
4538 return snprintf(buf, len, "lock window");
4539}
4540
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004541static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004542lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004543{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004544 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004545 struct weston_view *view;
4546
4547 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004548
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004549 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004550 return;
4551
Jason Ekstranda7af7042013-10-12 22:38:11 -05004552 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004553
4554 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004555 weston_layer_entry_insert(&shell->lock_layer.view_list,
4556 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004557 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02004558 surface->is_mapped = true;
4559 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004560 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004561 }
4562}
4563
4564static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004565handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004566{
Tiago Vignattibe143262012-04-16 17:31:41 +03004567 struct desktop_shell *shell =
4568 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004569
Martin Minarik6d118362012-06-07 18:01:59 +02004570 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004571 shell->lock_surface = NULL;
4572}
4573
4574static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004575desktop_shell_set_lock_surface(struct wl_client *client,
4576 struct wl_resource *resource,
4577 struct wl_resource *surface_resource)
4578{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004579 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004580 struct weston_surface *surface =
4581 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004582
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004583 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004584
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004585 if (!shell->locked)
4586 return;
4587
Pekka Paalanen98262232011-12-01 10:42:22 +02004588 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004589
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004590 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004591 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004592 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004593
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004594 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004595 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004596 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004597 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004598}
4599
4600static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004601resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004602{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004603 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004604
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004605 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004606 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004607 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004608 &shell->input_panel_layer.link);
4609 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004610 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004611 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004612 wl_list_insert(&shell->compositor->cursor_layer.link,
4613 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004614 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004615 wl_list_insert(&shell->fullscreen_layer.link,
4616 &shell->panel_layer.link);
4617 wl_list_insert(&shell->panel_layer.link,
4618 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004619
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004620 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004621
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004622 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004623 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004624 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004625}
4626
4627static void
4628desktop_shell_unlock(struct wl_client *client,
4629 struct wl_resource *resource)
4630{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004631 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004632
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004633 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004634
4635 if (shell->locked)
4636 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004637}
4638
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004639static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004640desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004641 struct wl_resource *resource,
4642 struct wl_resource *surface_resource)
4643{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004644 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004645
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004646 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004647 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004648}
4649
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004650static void
4651desktop_shell_desktop_ready(struct wl_client *client,
4652 struct wl_resource *resource)
4653{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004654 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004655
4656 shell_fade_startup(shell);
4657}
4658
Jonny Lamb765760d2014-08-20 15:53:19 +02004659static void
4660desktop_shell_set_panel_position(struct wl_client *client,
4661 struct wl_resource *resource,
4662 uint32_t position)
4663{
4664 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4665
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004666 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4667 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4668 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4669 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02004670 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004671 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02004672 "bad position argument");
4673 return;
4674 }
4675
4676 shell->panel_position = position;
4677}
4678
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004679static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04004680 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004681 desktop_shell_set_panel,
4682 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004683 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004684 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004685 desktop_shell_desktop_ready,
4686 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004687};
4688
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004689static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004690get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004691{
4692 struct shell_surface *shsurf;
4693
4694 shsurf = get_shell_surface(surface);
4695 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004696 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004697 return shsurf->type;
4698}
4699
Kristian Høgsberg75840622011-09-06 13:48:16 -04004700static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004701move_binding(struct weston_pointer *pointer, uint32_t time,
4702 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004703{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004704 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004705 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004706 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004707
Derek Foreman8ae2db52015-07-15 13:00:36 -05004708 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004709 return;
4710
Derek Foreman8ae2db52015-07-15 13:00:36 -05004711 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004712
Pekka Paalanen01388e22013-04-25 13:57:44 +03004713 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004714 if (surface == NULL)
4715 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004716
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004717 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004718 if (shsurf == NULL || shsurf->state.fullscreen ||
4719 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004720 return;
4721
Derek Foreman794fa0e2015-07-15 13:00:38 -05004722 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004723}
4724
4725static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004726maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4727 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004728{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004729 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004730 struct weston_surface *surface;
4731 struct shell_surface *shsurf;
4732
4733 surface = weston_surface_get_main_surface(focus);
4734 if (surface == NULL)
4735 return;
4736
4737 shsurf = get_shell_surface(surface);
4738 if (shsurf == NULL)
4739 return;
4740
4741 if (!shell_surface_is_xdg_surface(shsurf))
4742 return;
4743
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004744 shsurf->state_requested = true;
4745 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004746 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004747}
4748
4749static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004750fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4751 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004752{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004753 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004754 struct weston_surface *surface;
4755 struct shell_surface *shsurf;
4756
4757 surface = weston_surface_get_main_surface(focus);
4758 if (surface == NULL)
4759 return;
4760
4761 shsurf = get_shell_surface(surface);
4762 if (shsurf == NULL)
4763 return;
4764
4765 if (!shell_surface_is_xdg_surface(shsurf))
4766 return;
4767
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004768 shsurf->state_requested = true;
4769 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004770 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004771 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004772}
4773
4774static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004775touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004776{
Derek Foreman362656b2014-09-04 10:23:05 -05004777 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004778 struct weston_surface *surface;
4779 struct shell_surface *shsurf;
4780
Derek Foreman8ae2db52015-07-15 13:00:36 -05004781 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004782 return;
4783
Derek Foreman8ae2db52015-07-15 13:00:36 -05004784 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004785 surface = weston_surface_get_main_surface(focus);
4786 if (surface == NULL)
4787 return;
4788
4789 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004790 if (shsurf == NULL || shsurf->state.fullscreen ||
4791 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004792 return;
4793
Derek Foremanb7674ae2015-07-15 13:00:37 -05004794 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004795}
4796
4797static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004798resize_binding(struct weston_pointer *pointer, uint32_t time,
4799 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004800{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004801 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004802 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004803 uint32_t edges = 0;
4804 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004805 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004806
Derek Foreman8ae2db52015-07-15 13:00:36 -05004807 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004808 return;
4809
Derek Foreman8ae2db52015-07-15 13:00:36 -05004810 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004811
Pekka Paalanen01388e22013-04-25 13:57:44 +03004812 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004813 if (surface == NULL)
4814 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004815
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004816 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004817 if (shsurf == NULL || shsurf->state.fullscreen ||
4818 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004819 return;
4820
Jason Ekstranda7af7042013-10-12 22:38:11 -05004821 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004822 wl_fixed_to_int(pointer->grab_x),
4823 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004824 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004825
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004826 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004827 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004828 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004829 edges |= 0;
4830 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004831 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004832
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004833 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004834 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004835 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004836 edges |= 0;
4837 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004838 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004839
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004840 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004841}
4842
4843static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004844surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004845 struct weston_pointer_axis_event *event,
4846 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004847{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004848 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004849 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004850 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004851 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004852
Pekka Paalanen01388e22013-04-25 13:57:44 +03004853 /* XXX: broken for windows containing sub-surfaces */
4854 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004855 if (surface == NULL)
4856 return;
4857
4858 shsurf = get_shell_surface(surface);
4859 if (!shsurf)
4860 return;
4861
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004862 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004863
Jason Ekstranda7af7042013-10-12 22:38:11 -05004864 if (shsurf->view->alpha > 1.0)
4865 shsurf->view->alpha = 1.0;
4866 if (shsurf->view->alpha < step)
4867 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004868
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004870 weston_surface_damage(surface);
4871}
4872
4873static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004874do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004875 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004876{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004877 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05004878 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004879 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004880 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004881
Derek Foreman1281a362015-07-31 16:55:32 -05004882 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004883 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4884 return;
4885 }
4886
Scott Moreauccbf29d2012-02-22 14:21:41 -07004887 wl_list_for_each(output, &compositor->output_list, link) {
4888 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05004889 wl_fixed_to_double(pointer->x),
4890 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004891 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004892 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004893 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004894 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004895 increment = -output->zoom.increment;
4896 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004897 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004898 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004899 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004900 else
4901 increment = 0;
4902
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004903 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004904
Scott Moreaue6603982012-06-11 13:07:51 -06004905 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004906 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004907 else if (output->zoom.level > output->zoom.max_level)
4908 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004909
4910 if (!output->zoom.active) {
4911 if (output->zoom.level <= 0.0)
4912 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004913 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004914 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004915
Scott Moreaue6603982012-06-11 13:07:51 -06004916 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004917
Jason Ekstranda7af7042013-10-12 22:38:11 -05004918 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004919 }
4920 }
4921}
4922
Scott Moreauccbf29d2012-02-22 14:21:41 -07004923static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004924zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10004925 struct weston_pointer_axis_event *event,
4926 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004927{
Peter Hutterer89b6a492016-01-18 15:58:17 +10004928 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004929}
4930
4931static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004932zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4933 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004934{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004935 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004936}
4937
4938static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004939terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4940 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004941{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004942 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004943
Daniel Stone325fc2d2012-05-30 16:31:58 +01004944 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004945}
4946
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004947static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004948rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02004949 struct weston_pointer_motion_event *event)
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;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004955 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004956
Jonas Ådahld2510102014-10-05 21:39:14 +02004957 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004958
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004959 if (!shsurf)
4960 return;
4961
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004962 cx = 0.5f * shsurf->surface->width;
4963 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004964
Daniel Stone37816df2012-05-16 18:45:18 +01004965 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4966 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004967 r = sqrtf(dx * dx + dy * dy);
4968
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004969 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004970 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004971
4972 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004973 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004974 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004975
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004976 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004977 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004978
4979 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004980 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004981 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004982 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004983 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004984
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004985 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004986 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004987 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004988 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004989 wl_list_init(&shsurf->rotation.transform.link);
4990 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004991 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004992 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004993
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004994 /* We need to adjust the position of the surface
4995 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004996 cposx = shsurf->view->geometry.x + cx;
4997 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004998 dposx = rotate->center.x - cposx;
4999 dposy = rotate->center.y - cposy;
5000 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005001 weston_view_set_position(shsurf->view,
5002 shsurf->view->geometry.x + dposx,
5003 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005004 }
5005
Derek Foreman4b1a0a12014-09-10 15:37:33 -05005006 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02005007 * lazily applies the damage due to rotation update.
5008 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005009 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005010}
5011
5012static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005013rotate_grab_button(struct weston_pointer_grab *grab,
5014 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005015{
5016 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005017 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005018 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005019 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01005020 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005021
Daniel Stone4dbadb12012-05-30 16:31:51 +01005022 if (pointer->button_count == 0 &&
5023 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03005024 if (shsurf)
5025 weston_matrix_multiply(&shsurf->rotation.rotation,
5026 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005027 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005028 free(rotate);
5029 }
5030}
5031
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005032static void
5033rotate_grab_cancel(struct weston_pointer_grab *grab)
5034{
5035 struct rotate_grab *rotate =
5036 container_of(grab, struct rotate_grab, base.grab);
5037
5038 shell_grab_end(&rotate->base);
5039 free(rotate);
5040}
5041
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04005042static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02005043 noop_grab_focus,
5044 rotate_grab_motion,
5045 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02005046 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10005047 noop_grab_axis_source,
5048 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005049 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02005050};
5051
5052static void
Derek Foreman74de4692015-07-15 13:00:39 -05005053surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02005054{
Pekka Paalanen460099f2012-01-20 16:48:25 +02005055 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005056 float dx, dy;
5057 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02005058
Derek Foreman45a7c272015-09-11 14:27:40 -05005059 surface = find_toplevel_surface(surface);
5060
Pekka Paalanen460099f2012-01-20 16:48:25 +02005061 rotate = malloc(sizeof *rotate);
5062 if (!rotate)
5063 return;
5064
Jason Ekstranda7af7042013-10-12 22:38:11 -05005065 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005066 surface->surface->width * 0.5f,
5067 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005068 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005069
Derek Foreman74de4692015-07-15 13:00:39 -05005070 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5071 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005072 r = sqrtf(dx * dx + dy * dy);
5073 if (r > 20.0f) {
5074 struct weston_matrix inverse;
5075
5076 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005077 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005078 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01005079
5080 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03005081 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05005082 } else {
5083 weston_matrix_init(&surface->rotation.rotation);
5084 weston_matrix_init(&rotate->rotation);
5085 }
5086
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03005087 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005088 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02005089}
5090
5091static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005092rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005093 void *data)
5094{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005095 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005096 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005097 struct shell_surface *surface;
5098
Derek Foreman8ae2db52015-07-15 13:00:36 -05005099 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005100 return;
5101
Derek Foreman8ae2db52015-07-15 13:00:36 -05005102 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01005103
Pekka Paalanen01388e22013-04-25 13:57:44 +03005104 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005105 if (base_surface == NULL)
5106 return;
5107
5108 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005109 if (surface == NULL || surface->state.fullscreen ||
5110 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005111 return;
5112
Derek Foreman74de4692015-07-15 13:00:39 -05005113 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005114}
5115
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005116/* Move all fullscreen layers down to the current workspace and hide their
5117 * black views. The surfaces' state is set to both fullscreen and lowered,
5118 * and this is reversed when such a surface is re-configured, see
5119 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5120 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005121 * lowering_output = NULL - Lower on all outputs, else only lower on the
5122 * specified output.
5123 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005124 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005125 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005126void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005127lower_fullscreen_layer(struct desktop_shell *shell,
5128 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005129{
5130 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005131 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005132
5133 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005134 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005135 &shell->fullscreen_layer.view_list.link,
5136 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005137 struct shell_surface *shsurf = get_shell_surface(view->surface);
5138
5139 if (!shsurf)
5140 continue;
5141
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005142 /* Only lower surfaces which have lowering_output as their fullscreen
5143 * output, unless a NULL output asks for lowering on all outputs.
5144 */
5145 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5146 continue;
5147
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005148 /* We can have a non-fullscreen popup for a fullscreen surface
5149 * in the fullscreen layer. */
5150 if (shsurf->state.fullscreen) {
5151 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005152 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5153 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005154 weston_view_damage_below(shsurf->fullscreen.black_view);
5155
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005156 }
5157
5158 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005159 weston_layer_entry_remove(&view->layer_link);
5160 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005161 weston_view_damage_below(view);
5162 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005163
5164 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005165 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005166}
5167
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005168void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005169activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005170 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005171{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02005172 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005173 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005174 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005175 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005176 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005177 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005178
Pekka Paalanen01388e22013-04-25 13:57:44 +03005179 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005180 shsurf = get_shell_surface(main_surface);
5181 assert(shsurf);
5182
5183 /* Only demote fullscreen surfaces on the output of activated shsurf.
5184 * Leave fullscreen surfaces on unrelated outputs alone. */
5185 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005186
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005187 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005188
Jonas Ådahl8538b222012-08-29 22:13:03 +02005189 state = ensure_focus_state(shell, seat);
5190 if (state == NULL)
5191 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005192
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005193 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005194 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005195
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005196 if (shsurf->state.fullscreen && flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005197 shell_configure_fullscreen(shsurf);
5198 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005199 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005200
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005201 /* Update the surface’s layer. This brings it to the top of the stacking
5202 * order as appropriate. */
5203 shell_surface_update_layer(shsurf);
5204
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005205 if (shell->focus_animation_type != ANIMATION_NONE) {
5206 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005207 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005208 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005209}
5210
Alex Wu21858432012-04-01 20:13:08 +08005211/* no-op func for checking black surface */
5212static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005213black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005214{
5215}
5216
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005217static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005218is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08005219{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005220 struct weston_surface *surface = view->surface;
5221
5222 if (surface->configure == black_surface_configure) {
5223 if (fs_view)
5224 *fs_view = surface->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005225 return true;
5226 }
5227 return false;
5228}
5229
Kristian Høgsberg75840622011-09-06 13:48:16 -04005230static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005231activate_binding(struct weston_seat *seat,
5232 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005233 struct weston_view *focus_view,
5234 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005235{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005236 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03005237 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005238
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005239 if (!focus_view)
5240 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08005241
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005242 if (is_black_surface_view(focus_view, &main_view))
5243 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08005244
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02005245 main_surface = weston_surface_get_main_surface(focus_view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005246 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005247 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005248
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005249 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01005250}
5251
5252static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005253click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5254 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005255{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005256 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005257 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005258 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005259 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005260
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005261 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005262 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005263 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01005264}
5265
5266static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005267touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5268 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005269{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005270 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005271 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005272 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005273 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005274
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005275 activate_binding(touch->seat, data, touch->focus,
5276 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005277}
5278
5279static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005280unfocus_all_seats(struct desktop_shell *shell)
5281{
5282 struct weston_seat *seat, *next;
5283
5284 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005285 struct weston_keyboard *keyboard =
5286 weston_seat_get_keyboard(seat);
5287
5288 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01005289 continue;
5290
Derek Foreman1281a362015-07-31 16:55:32 -05005291 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01005292 }
5293}
5294
5295static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005296lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005297{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005298 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005299
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005300 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005301 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005302 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005303 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005304
5305 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005306
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005307 /* Hide all surfaces by removing the fullscreen, panel and
5308 * toplevel layers. This way nothing else can show or receive
5309 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005310
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005311 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005312 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005313 if (shell->showing_input_panels)
5314 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005315 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005316 wl_list_insert(&shell->compositor->cursor_layer.link,
5317 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005318
Derek Foreman004b4a12015-07-20 16:28:13 -05005319 weston_compositor_sleep(shell->compositor);
5320
Neil Robertsb4a91702014-04-09 16:33:32 +01005321 /* Remove the keyboard focus on all seats. This will be
5322 * restored to the workspace's saved state via
5323 * restore_focus_state when the compositor is unlocked */
5324 unfocus_all_seats(shell);
5325
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005326 /* TODO: disable bindings that should not work while locked. */
5327
5328 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005329}
5330
5331static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005332unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005333{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005334 struct wl_resource *shell_resource;
5335
Pekka Paalanend81c2162011-11-16 13:47:34 +02005336 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005337 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005338 return;
5339 }
5340
5341 /* If desktop-shell client has gone away, unlock immediately. */
5342 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005343 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005344 return;
5345 }
5346
5347 if (shell->prepare_event_sent)
5348 return;
5349
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005350 shell_resource = shell->child.desktop_shell;
5351 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005352 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005353}
5354
5355static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005356shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005357{
5358 struct desktop_shell *shell = data;
5359
5360 shell->fade.animation = NULL;
5361
5362 switch (shell->fade.type) {
5363 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005364 weston_surface_destroy(shell->fade.view->surface);
5365 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005366 break;
5367 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005368 lock(shell);
5369 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005370 default:
5371 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005372 }
5373}
5374
Jason Ekstranda7af7042013-10-12 22:38:11 -05005375static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005376shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005377{
5378 struct weston_compositor *compositor = shell->compositor;
5379 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005380 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005381
5382 surface = weston_surface_create(compositor);
5383 if (!surface)
5384 return NULL;
5385
Jason Ekstranda7af7042013-10-12 22:38:11 -05005386 view = weston_view_create(surface);
5387 if (!view) {
5388 weston_surface_destroy(surface);
5389 return NULL;
5390 }
5391
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005392 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005393 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005394 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005395 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5396 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005397 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02005398 surface->is_mapped = true;
5399 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005400
Jason Ekstranda7af7042013-10-12 22:38:11 -05005401 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005402}
5403
5404static void
5405shell_fade(struct desktop_shell *shell, enum fade_type type)
5406{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005407 float tint;
5408
5409 switch (type) {
5410 case FADE_IN:
5411 tint = 0.0;
5412 break;
5413 case FADE_OUT:
5414 tint = 1.0;
5415 break;
5416 default:
5417 weston_log("shell: invalid fade type\n");
5418 return;
5419 }
5420
5421 shell->fade.type = type;
5422
Jason Ekstranda7af7042013-10-12 22:38:11 -05005423 if (shell->fade.view == NULL) {
5424 shell->fade.view = shell_fade_create_surface(shell);
5425 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005426 return;
5427
Jason Ekstranda7af7042013-10-12 22:38:11 -05005428 shell->fade.view->alpha = 1.0 - tint;
5429 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005430 }
5431
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005432 if (shell->fade.view->output == NULL) {
5433 /* If the black view gets a NULL output, we lost the
5434 * last output and we'll just cancel the fade. This
5435 * happens when you close the last window under the
5436 * X11 or Wayland backends. */
5437 shell->locked = false;
5438 weston_surface_destroy(shell->fade.view->surface);
5439 shell->fade.view = NULL;
5440 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005441 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005442 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005443 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005444 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005445 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005446 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005447 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005448}
5449
5450static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005451do_shell_fade_startup(void *data)
5452{
5453 struct desktop_shell *shell = data;
5454
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005455 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005456 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005457 } else {
5458 weston_log("desktop shell: "
5459 "unexpected fade-in animation type %d\n",
5460 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005461 weston_surface_destroy(shell->fade.view->surface);
5462 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005463 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005464}
5465
5466static void
5467shell_fade_startup(struct desktop_shell *shell)
5468{
5469 struct wl_event_loop *loop;
5470
5471 if (!shell->fade.startup_timer)
5472 return;
5473
5474 wl_event_source_remove(shell->fade.startup_timer);
5475 shell->fade.startup_timer = NULL;
5476
5477 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5478 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5479}
5480
5481static int
5482fade_startup_timeout(void *data)
5483{
5484 struct desktop_shell *shell = data;
5485
5486 shell_fade_startup(shell);
5487 return 0;
5488}
5489
5490static void
5491shell_fade_init(struct desktop_shell *shell)
5492{
5493 /* Make compositor output all black, and wait for the desktop-shell
5494 * client to signal it is ready, then fade in. The timer triggers a
5495 * fade-in, in case the desktop-shell client takes too long.
5496 */
5497
5498 struct wl_event_loop *loop;
5499
Jason Ekstranda7af7042013-10-12 22:38:11 -05005500 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005501 weston_log("%s: warning: fade surface already exists\n",
5502 __func__);
5503 return;
5504 }
5505
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005506 if (shell->startup_animation_type == ANIMATION_NONE)
5507 return;
5508
Jason Ekstranda7af7042013-10-12 22:38:11 -05005509 shell->fade.view = shell_fade_create_surface(shell);
5510 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005511 return;
5512
Jason Ekstranda7af7042013-10-12 22:38:11 -05005513 weston_view_update_transform(shell->fade.view);
5514 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005515
5516 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5517 shell->fade.startup_timer =
5518 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5519 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5520}
5521
5522static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005523idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005524{
5525 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005526 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005527 struct weston_seat *seat;
5528
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005529 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005530 struct weston_touch *touch = weston_seat_get_touch(seat);
5531 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5532
5533 if (pointer)
5534 popup_grab_end(pointer);
5535 if (touch)
5536 touch_popup_grab_end(touch);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005537 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005538
5539 shell_fade(shell, FADE_OUT);
5540 /* lock() is called from shell_fade_done() */
5541}
5542
5543static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005544wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005545{
5546 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005547 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005548
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005549 unlock(shell);
5550}
5551
5552static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005553transform_handler(struct wl_listener *listener, void *data)
5554{
5555 struct weston_surface *surface = data;
5556 struct shell_surface *shsurf = get_shell_surface(surface);
5557 struct weston_view *view;;
5558 int x, y;
5559
5560 if (!shsurf || !shsurf->client->send_position)
5561 return;
5562
5563 view = shsurf->view;
5564 if (!view || !weston_view_is_mapped(view))
5565 return;
5566
5567 x = view->geometry.x;
5568 y = view->geometry.y;
5569
5570 shsurf->client->send_position(surface, x, y);
5571}
5572
5573static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005574center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005575{
Giulio Camuffob8366642013-04-25 13:57:46 +03005576 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005577 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005578
Jason Ekstranda7af7042013-10-12 22:38:11 -05005579 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005580
5581 x = output->x + (output->width - width) / 2 - surf_x / 2;
5582 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005583
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005584 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005585}
5586
5587static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005588weston_view_set_initial_position(struct weston_view *view,
5589 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005590{
5591 struct weston_compositor *compositor = shell->compositor;
5592 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005593 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005594 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005595 struct weston_output *output, *target_output = NULL;
5596 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005597 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005598
5599 /* As a heuristic place the new window on the same output as the
5600 * pointer. Falling back to the output containing 0, 0.
5601 *
5602 * TODO: Do something clever for touch too?
5603 */
5604 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005605 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5606
5607 if (pointer) {
5608 ix = wl_fixed_to_int(pointer->x);
5609 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005610 break;
5611 }
5612 }
5613
5614 wl_list_for_each(output, &compositor->output_list, link) {
5615 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5616 target_output = output;
5617 break;
5618 }
5619 }
5620
5621 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005622 weston_view_set_position(view, 10 + random() % 400,
5623 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005624 return;
5625 }
5626
5627 /* Valid range within output where the surface will still be onscreen.
5628 * If this is negative it means that the surface is bigger than
5629 * output.
5630 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005631 get_output_work_area(shell, target_output, &area);
5632
Derek Foremanf814c5d2015-04-15 12:23:54 -05005633 x = area.x;
5634 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005635 range_x = area.width - view->surface->width;
5636 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005637
Rob Bradford4cb88c72012-08-13 15:18:44 +01005638 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005639 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005640
5641 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005642 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005643
Jason Ekstranda7af7042013-10-12 22:38:11 -05005644 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005645}
5646
5647static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005648set_maximized_position(struct desktop_shell *shell,
5649 struct shell_surface *shsurf)
5650{
5651 int32_t surf_x, surf_y;
5652 pixman_rectangle32_t area;
5653
Jonny Lambd73c6942014-08-20 15:53:20 +02005654 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005655 if (shsurf->has_set_geometry) {
5656 surf_x = shsurf->geometry.x;
5657 surf_y = shsurf->geometry.y;
5658 } else {
5659 surface_subsurfaces_boundingbox(shsurf->surface,
5660 &surf_x, &surf_y, NULL, NULL);
5661 }
Jonny Lambd73c6942014-08-20 15:53:20 +02005662
5663 weston_view_set_position(shsurf->view,
Marek Chalupa6ce78992015-10-09 18:17:07 +02005664 area.x - surf_x,
5665 area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005666}
5667
5668static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005669map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005670 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005671{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005672 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005673 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005674
Pekka Paalanen77346a62011-11-30 16:26:35 +02005675 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005676 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005677 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005678 if (shsurf->state.fullscreen) {
5679 center_on_output(shsurf->view, shsurf->fullscreen_output);
5680 shell_map_fullscreen(shsurf);
5681 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005682 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005683 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005684 weston_view_set_initial_position(shsurf->view, shell);
5685 }
Juan Zhao96879df2012-02-07 08:45:41 +08005686 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005687 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005688 if (shell_map_popup(shsurf) != 0)
5689 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005690 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005691 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005692 weston_view_set_position(shsurf->view,
5693 shsurf->view->geometry.x + sx,
5694 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005695 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005696 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005697 default:
5698 ;
5699 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005700
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005701 /* Surface stacking order, see also activate(). */
5702 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005703
Jason Ekstranda7af7042013-10-12 22:38:11 -05005704 if (shsurf->type != SHELL_SURFACE_NONE) {
5705 weston_view_update_transform(shsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +02005706 shsurf->view->is_mapped = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005707 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005708 shsurf->surface->output = shsurf->output;
5709 shsurf->view->output = shsurf->output;
5710 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005711 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005712
Jason Ekstranda7af7042013-10-12 22:38:11 -05005713 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005714 /* XXX: xwayland's using the same fields for transient type */
5715 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005716 if (shsurf->transient.flags ==
5717 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5718 break;
5719 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005720 if (shsurf->state.relative &&
5721 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5722 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005723 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005724 break;
5725 wl_list_for_each(seat, &compositor->seat_list, link)
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02005726 activate(shell, shsurf->view, seat,
5727 WESTON_ACTIVATE_FLAG_CONFIGURE);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005728 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005729 case SHELL_SURFACE_POPUP:
5730 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005731 default:
5732 break;
5733 }
5734
Rafael Antognolli03b16592013-12-03 15:35:42 -02005735 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5736 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005737 {
5738 switch (shell->win_animation_type) {
5739 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005740 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005741 break;
5742 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005743 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005744 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005745 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005746 default:
5747 break;
5748 }
5749 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005750}
5751
5752static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005753configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005754 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005755{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005756 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005757 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005758
Pekka Paalanen77346a62011-11-30 16:26:35 +02005759 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005760
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005761 assert(shsurf);
5762
Rafael Antognolli03b16592013-12-03 15:35:42 -02005763 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005764 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005765 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005766 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005767 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005768 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005769 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005770
Alex Wu4539b082012-03-01 12:57:46 +08005771 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005772 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005773 wl_list_for_each(view, &surface->views, surface_link)
5774 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005775
Rafael Antognolli03b16592013-12-03 15:35:42 -02005776 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005777 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005778 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005779}
5780
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005781static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005782shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005783{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005784 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005785 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005786 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005787
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005788 assert(shsurf);
5789
5790 shell = shsurf->shell;
5791
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005792 if (!weston_surface_is_mapped(es) &&
5793 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005794 remove_popup_grab(shsurf);
5795 }
5796
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005797 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005798 return;
5799
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005800 if (shsurf->has_next_geometry) {
5801 shsurf->geometry = shsurf->next_geometry;
5802 shsurf->has_next_geometry = false;
5803 shsurf->has_set_geometry = true;
5804 } else if (!shsurf->has_set_geometry) {
5805 surface_subsurfaces_boundingbox(shsurf->surface,
5806 &shsurf->geometry.x,
5807 &shsurf->geometry.y,
5808 &shsurf->geometry.width,
5809 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005810 }
5811
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005812 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005813 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005814 type_changed = 1;
5815 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005816
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005817 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005818 map(shell, shsurf, sx, sy);
Armin Krezović4663aca2016-06-30 06:04:29 +02005819 es->is_mapped = true;
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005820 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005821 shsurf->last_width != es->width ||
5822 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005823 float from_x, from_y;
5824 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005825
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005826 if (shsurf->resize_edges) {
5827 sx = 0;
5828 sy = 0;
5829 }
5830
5831 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5832 sx = shsurf->last_width - es->width;
5833 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5834 sy = shsurf->last_height - es->height;
5835
5836 shsurf->last_width = es->width;
5837 shsurf->last_height = es->height;
5838
Jason Ekstranda7af7042013-10-12 22:38:11 -05005839 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5840 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005841 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005842 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005843 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005844 }
5845}
5846
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005847static bool
5848check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5849{
5850 struct timespec now;
5851
5852 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5853 return false;
5854
5855 /*
5856 * If the shell helper client dies before the session has been
5857 * up for roughly 30 seconds, better just make Weston shut down,
5858 * because the user likely has no way to interact with the desktop
5859 * anyway.
5860 */
5861 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5862 weston_log("Error: %s apparently cannot run at all.\n",
5863 shell->client);
5864 weston_log_continue(STAMP_SPACE "Quitting...");
5865 wl_display_terminate(shell->compositor->wl_display);
5866
5867 return true;
5868 }
5869
5870 return false;
5871}
5872
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005873static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005874
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005875static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005876respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005877{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005878 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005879
5880 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5881 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005882 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005883 shell->child.deathstamp = time;
5884 shell->child.deathcount = 0;
5885 }
5886
5887 shell->child.deathcount++;
5888 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005889 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005890 return;
5891 }
5892
Pekka Paalanen826dc142014-08-27 12:11:53 +03005893 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005894 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005895}
5896
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005897static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005898desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5899{
5900 struct desktop_shell *shell;
5901
5902 shell = container_of(listener, struct desktop_shell,
5903 child.client_destroy_listener);
5904
Pekka Paalanen826dc142014-08-27 12:11:53 +03005905 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005906 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005907 /*
5908 * unbind_desktop_shell() will reset shell->child.desktop_shell
5909 * before the respawned process has a chance to create a new
5910 * desktop_shell object, because we are being called from the
5911 * wl_client destructor which destroys all wl_resources before
5912 * returning.
5913 */
5914
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005915 if (!check_desktop_shell_crash_too_early(shell))
5916 respawn_desktop_shell_process(shell);
5917
Pekka Paalanen826dc142014-08-27 12:11:53 +03005918 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005919}
5920
5921static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005922launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005923{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005924 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005925
Pekka Paalanen826dc142014-08-27 12:11:53 +03005926 shell->child.client = weston_client_start(shell->compositor,
5927 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005928
Arnaud Vrac42631192014-08-25 20:56:46 +02005929 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005930 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005931 return;
5932 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005933
5934 shell->child.client_destroy_listener.notify =
5935 desktop_shell_client_destroy;
5936 wl_client_add_destroy_listener(shell->child.client,
5937 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005938}
5939
5940static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005941handle_shell_client_destroy(struct wl_listener *listener, void *data)
5942{
5943 struct shell_client *sc =
5944 container_of(listener, struct shell_client, destroy_listener);
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005945 struct wl_resource *shsurf_resource;
5946 struct shell_surface *shsurf;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005947
5948 if (sc->ping_timer)
5949 wl_event_source_remove(sc->ping_timer);
Derek Foremane42d7542015-05-29 10:46:44 -05005950
5951 /* Since we're about to free shell_client, we remove it from the
5952 * head of the surface list so we don't use that freed list node
5953 * during surface clean up later on.
5954 */
Jonas Ådahld3a3ee92016-02-26 14:02:06 +08005955 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5956 shsurf = wl_resource_get_user_data(shsurf_resource);
5957 shsurf->owner = NULL;
5958 }
Derek Foremane42d7542015-05-29 10:46:44 -05005959 wl_list_remove(&sc->surface_list);
5960
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005961 free(sc);
5962}
5963
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005964static struct shell_client *
5965shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5966 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005967{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005968 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005969
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005970 sc = zalloc(sizeof *sc);
5971 if (sc == NULL) {
5972 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005973 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005974 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005975
5976 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005977 if (sc->resource == NULL) {
5978 free(sc);
5979 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005980 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005981 }
5982
5983 sc->client = client;
5984 sc->shell = shell;
5985 sc->destroy_listener.notify = handle_shell_client_destroy;
5986 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005987 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005988
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005989 return sc;
5990}
5991
5992static void
5993bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5994{
5995 struct desktop_shell *shell = data;
5996 struct shell_client *sc;
5997
5998 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5999 if (sc)
6000 wl_resource_set_implementation(sc->resource,
6001 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05006002 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08006003}
6004
6005static void
6006bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
6007{
6008 struct desktop_shell *shell = data;
6009 struct shell_client *sc;
6010
6011 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
6012 if (sc)
6013 wl_resource_set_dispatcher(sc->resource,
6014 xdg_shell_unversioned_dispatch,
6015 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02006016}
6017
6018static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006019unbind_desktop_shell(struct wl_resource *resource)
6020{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05006021 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05006022
6023 if (shell->locked)
6024 resume_desktop(shell);
6025
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006026 shell->child.desktop_shell = NULL;
6027 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006028}
6029
6030static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04006031bind_desktop_shell(struct wl_client *client,
6032 void *data, uint32_t version, uint32_t id)
6033{
Tiago Vignattibe143262012-04-16 17:31:41 +03006034 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006035 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04006036
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006037 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6038 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006039
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006040 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05006041 wl_resource_set_implementation(resource,
6042 &desktop_shell_implementation,
6043 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006044 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006045 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02006046 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02006047
6048 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6049 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04006050}
6051
Kristian Høgsberg07045392012-02-19 18:52:44 -05006052struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03006053 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006054 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006055 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006056 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006057 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006058};
6059
6060static void
6061switcher_next(struct switcher *switcher)
6062{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006063 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006064 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04006065 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006066 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006067
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006068 /* temporary re-display minimized surfaces */
6069 struct weston_view *tmp;
6070 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006071 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6072 weston_layer_entry_remove(&view->layer_link);
6073 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006074 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6075 *minimized = view;
6076 }
6077
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006078 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02006079 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02006080 if (shsurf &&
6081 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6082 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05006083 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006084 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006085 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006086 next = view;
6087 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006088 view->alpha = 0.25;
6089 weston_view_geometry_dirty(view);
6090 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006091 }
Alex Wu1659daa2012-04-01 20:13:09 +08006092
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02006093 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05006094 view->alpha = 0.25;
6095 weston_view_geometry_dirty(view);
6096 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08006097 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05006098 }
6099
6100 if (next == NULL)
6101 next = first;
6102
Alex Wu07b26062012-03-12 16:06:01 +08006103 if (next == NULL)
6104 return;
6105
Kristian Høgsberg07045392012-02-19 18:52:44 -05006106 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006107 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006108
6109 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006110 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006111 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08006112
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006113 shsurf = get_shell_surface(switcher->current->surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02006114 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05006115 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006116}
6117
6118static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006119switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006120{
6121 struct switcher *switcher =
6122 container_of(listener, struct switcher, listener);
6123
6124 switcher_next(switcher);
6125}
6126
6127static void
Daniel Stone351eb612012-05-31 15:27:47 -04006128switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05006130 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006131 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04006132 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006133
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006134 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01006135 if (is_focus_view(view))
6136 continue;
6137
Jason Ekstranda7af7042013-10-12 22:38:11 -05006138 view->alpha = 1.0;
6139 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006140 }
6141
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02006142 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02006143 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02006144 keyboard->seat,
6145 WESTON_ACTIVATE_FLAG_CONFIGURE);
6146 }
6147
Kristian Høgsberg07045392012-02-19 18:52:44 -05006148 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006149 weston_keyboard_end_grab(keyboard);
6150 if (keyboard->input_method_resource)
6151 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006152
6153 /* re-hide surfaces that were temporary shown during the switch */
6154 struct weston_view **minimized;
6155 wl_array_for_each(minimized, &switcher->minimized_array) {
6156 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006157 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006158 weston_layer_entry_remove(&(*minimized)->layer_link);
6159 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006160 weston_view_damage_below(*minimized);
6161 }
6162 }
6163 wl_array_release(&switcher->minimized_array);
6164
Kristian Høgsberg07045392012-02-19 18:52:44 -05006165 free(switcher);
6166}
6167
6168static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006169switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006170 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006171{
6172 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006173 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006174
Daniel Stonec9785ea2012-05-30 16:31:52 +01006175 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006176 switcher_next(switcher);
6177}
6178
6179static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006180switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006181 uint32_t mods_depressed, uint32_t mods_latched,
6182 uint32_t mods_locked, uint32_t group)
6183{
6184 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006185 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006186
Daniel Stone351eb612012-05-31 15:27:47 -04006187 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6188 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006189}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006190
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006191static void
6192switcher_cancel(struct weston_keyboard_grab *grab)
6193{
6194 struct switcher *switcher = container_of(grab, struct switcher, grab);
6195
6196 switcher_destroy(switcher);
6197}
6198
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006199static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006200 switcher_key,
6201 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006202 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006203};
6204
6205static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006206switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6207 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006208{
Tiago Vignattibe143262012-04-16 17:31:41 +03006209 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006210 struct switcher *switcher;
6211
6212 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006213 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006214 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02006215 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006216 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006217 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006218
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006219 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006220 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006221 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006222 weston_keyboard_start_grab(keyboard, &switcher->grab);
6223 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006224 switcher_next(switcher);
6225}
6226
Pekka Paalanen3c647232011-12-22 13:43:43 +02006227static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006228backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6229 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006230{
6231 struct weston_compositor *compositor = data;
6232 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006233 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006234
6235 /* TODO: we're limiting to simple use cases, where we assume just
6236 * control on the primary display. We'd have to extend later if we
6237 * ever get support for setting backlights on random desktop LCD
6238 * panels though */
6239 output = get_default_output(compositor);
6240 if (!output)
6241 return;
6242
6243 if (!output->set_backlight)
6244 return;
6245
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006246 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6247 backlight_new = output->backlight_current - 25;
6248 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6249 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006250
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006251 if (backlight_new < 5)
6252 backlight_new = 5;
6253 if (backlight_new > 255)
6254 backlight_new = 255;
6255
6256 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006257 output->set_backlight(output, output->backlight_current);
6258}
6259
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006260static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006261force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6262 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006263{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006264 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006265 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006266 struct desktop_shell *shell = data;
6267 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006268 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006269
Derek Foreman8ae2db52015-07-15 13:00:36 -05006270 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006271 if (!focus_surface)
6272 return;
6273
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006274 wl_signal_emit(&compositor->kill_signal, focus_surface);
6275
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006276 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006277 wl_client_get_credentials(client, &pid, NULL, NULL);
6278
6279 /* Skip clients that we launched ourselves (the credentials of
6280 * the socketpair is ours) */
6281 if (pid == getpid())
6282 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006283
Daniel Stone325fc2d2012-05-30 16:31:58 +01006284 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006285}
6286
6287static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006288workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006289 uint32_t key, void *data)
6290{
6291 struct desktop_shell *shell = data;
6292 unsigned int new_index = shell->workspaces.current;
6293
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006294 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006295 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006296 if (new_index != 0)
6297 new_index--;
6298
6299 change_workspace(shell, new_index);
6300}
6301
6302static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006303workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006304 uint32_t key, void *data)
6305{
6306 struct desktop_shell *shell = data;
6307 unsigned int new_index = shell->workspaces.current;
6308
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006309 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006310 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006311 if (new_index < shell->workspaces.num - 1)
6312 new_index++;
6313
6314 change_workspace(shell, new_index);
6315}
6316
6317static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006318workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006319 uint32_t key, void *data)
6320{
6321 struct desktop_shell *shell = data;
6322 unsigned int new_index;
6323
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006324 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006325 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006326 new_index = key - KEY_F1;
6327 if (new_index >= shell->workspaces.num)
6328 new_index = shell->workspaces.num - 1;
6329
6330 change_workspace(shell, new_index);
6331}
6332
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006333static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006334workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6335 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006336{
6337 struct desktop_shell *shell = data;
6338 unsigned int new_index = shell->workspaces.current;
6339
6340 if (shell->locked)
6341 return;
6342
6343 if (new_index != 0)
6344 new_index--;
6345
Derek Foreman8ae2db52015-07-15 13:00:36 -05006346 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006347}
6348
6349static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006350workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6351 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006352{
6353 struct desktop_shell *shell = data;
6354 unsigned int new_index = shell->workspaces.current;
6355
6356 if (shell->locked)
6357 return;
6358
6359 if (new_index < shell->workspaces.num - 1)
6360 new_index++;
6361
Derek Foreman8ae2db52015-07-15 13:00:36 -05006362 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006363}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006364
6365static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006366shell_reposition_view_on_output_destroy(struct weston_view *view)
6367{
6368 struct weston_output *output, *first_output;
6369 struct weston_compositor *ec = view->surface->compositor;
6370 struct shell_surface *shsurf;
6371 float x, y;
6372 int visible;
6373
6374 x = view->geometry.x;
6375 y = view->geometry.y;
6376
6377 /* At this point the destroyed output is not in the list anymore.
6378 * If the view is still visible somewhere, we leave where it is,
6379 * otherwise, move it to the first output. */
6380 visible = 0;
6381 wl_list_for_each(output, &ec->output_list, link) {
6382 if (pixman_region32_contains_point(&output->region,
6383 x, y, NULL)) {
6384 visible = 1;
6385 break;
6386 }
6387 }
6388
6389 if (!visible) {
6390 first_output = container_of(ec->output_list.next,
6391 struct weston_output, link);
6392
6393 x = first_output->x + first_output->width / 4;
6394 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006395
6396 weston_view_set_position(view, x, y);
6397 } else {
6398 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006399 }
6400
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006401
6402 shsurf = get_shell_surface(view->surface);
6403
6404 if (shsurf) {
6405 shsurf->saved_position_valid = false;
6406 shsurf->next_state.maximized = false;
6407 shsurf->next_state.fullscreen = false;
6408 shsurf->state_changed = true;
6409 }
6410}
6411
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006412void
6413shell_for_each_layer(struct desktop_shell *shell,
6414 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006415{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006416 struct workspace **ws;
6417
6418 func(shell, &shell->fullscreen_layer, data);
6419 func(shell, &shell->panel_layer, data);
6420 func(shell, &shell->background_layer, data);
6421 func(shell, &shell->lock_layer, data);
6422 func(shell, &shell->input_panel_layer, data);
6423
6424 wl_array_for_each(ws, &shell->workspaces.array)
6425 func(shell, &(*ws)->layer, data);
6426}
6427
6428static void
6429shell_output_destroy_move_layer(struct desktop_shell *shell,
6430 struct weston_layer *layer,
6431 void *data)
6432{
6433 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006434 struct weston_view *view;
6435
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006436 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006437 if (view->output != output)
6438 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006439
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006440 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006441 }
6442}
6443
6444static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006445handle_output_destroy(struct wl_listener *listener, void *data)
6446{
6447 struct shell_output *output_listener =
6448 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006449 struct weston_output *output = output_listener->output;
6450 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006451
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006452 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006453
Xiong Zhang6b481422013-10-23 13:58:32 +08006454 wl_list_remove(&output_listener->destroy_listener.link);
6455 wl_list_remove(&output_listener->link);
6456 free(output_listener);
6457}
6458
6459static void
David Fort50d962f2016-06-09 17:55:52 +02006460shell_resize_surface_to_output(struct desktop_shell *shell,
6461 struct weston_surface *surface,
6462 const struct weston_output *output)
6463{
6464 if (!surface)
6465 return;
6466
6467 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6468 surface->resource,
6469 output->width,
6470 output->height);
6471}
6472
6473
6474static void
6475handle_output_resized(struct wl_listener *listener, void *data)
6476{
6477 struct desktop_shell *shell =
6478 container_of(listener, struct desktop_shell, resized_listener);
6479 struct weston_output *output = (struct weston_output *)data;
6480 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6481
6482 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6483 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6484}
6485
6486static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006487create_shell_output(struct desktop_shell *shell,
6488 struct weston_output *output)
6489{
6490 struct shell_output *shell_output;
6491
6492 shell_output = zalloc(sizeof *shell_output);
6493 if (shell_output == NULL)
6494 return;
6495
6496 shell_output->output = output;
6497 shell_output->shell = shell;
6498 shell_output->destroy_listener.notify = handle_output_destroy;
6499 wl_signal_add(&output->destroy_signal,
6500 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006501 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006502}
6503
6504static void
6505handle_output_create(struct wl_listener *listener, void *data)
6506{
6507 struct desktop_shell *shell =
6508 container_of(listener, struct desktop_shell, output_create_listener);
6509 struct weston_output *output = (struct weston_output *)data;
6510
6511 create_shell_output(shell, output);
6512}
6513
6514static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006515handle_output_move_layer(struct desktop_shell *shell,
6516 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006517{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006518 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006519 struct weston_view *view;
6520 float x, y;
6521
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006522 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006523 if (view->output != output)
6524 continue;
6525
6526 x = view->geometry.x + output->move_x;
6527 y = view->geometry.y + output->move_y;
6528 weston_view_set_position(view, x, y);
6529 }
6530}
6531
6532static void
6533handle_output_move(struct wl_listener *listener, void *data)
6534{
6535 struct desktop_shell *shell;
6536
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006537 shell = container_of(listener, struct desktop_shell,
6538 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006539
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006540 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006541}
6542
6543static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006544setup_output_destroy_handler(struct weston_compositor *ec,
6545 struct desktop_shell *shell)
6546{
6547 struct weston_output *output;
6548
6549 wl_list_init(&shell->output_list);
6550 wl_list_for_each(output, &ec->output_list, link)
6551 create_shell_output(shell, output);
6552
6553 shell->output_create_listener.notify = handle_output_create;
6554 wl_signal_add(&ec->output_created_signal,
6555 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006556
6557 shell->output_move_listener.notify = handle_output_move;
6558 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006559}
6560
6561static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006562shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006563{
Tiago Vignattibe143262012-04-16 17:31:41 +03006564 struct desktop_shell *shell =
6565 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006566 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006567 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006568
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006569 /* Force state to unlocked so we don't try to fade */
6570 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006571
6572 if (shell->child.client) {
6573 /* disable respawn */
6574 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006575 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006576 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006577
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006578 wl_list_remove(&shell->idle_listener.link);
6579 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006580 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006581
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006582 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006583 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006584
Xiong Zhang6b481422013-10-23 13:58:32 +08006585 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6586 wl_list_remove(&shell_output->destroy_listener.link);
6587 wl_list_remove(&shell_output->link);
6588 free(shell_output);
6589 }
6590
6591 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006592 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02006593 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006594
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006595 wl_array_for_each(ws, &shell->workspaces.array)
6596 workspace_destroy(*ws);
6597 wl_array_release(&shell->workspaces.array);
6598
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006599 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006600 free(shell);
6601}
6602
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006603static void
6604shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6605{
6606 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006607 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006608
Bob Ham744e6532016-01-12 10:21:48 +00006609 if (shell->allow_zap)
6610 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6611 MODIFIER_CTRL | MODIFIER_ALT,
6612 terminate_binding, ec);
6613
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006614 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006615 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6616 click_to_activate_binding,
6617 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006618 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6619 click_to_activate_binding,
6620 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006621 weston_compositor_add_touch_binding(ec, 0,
6622 touch_to_activate_binding,
6623 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006624 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6625 MODIFIER_SUPER | MODIFIER_ALT,
6626 surface_opacity_binding, NULL);
6627 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6628 MODIFIER_SUPER, zoom_axis_binding,
6629 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00006630 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6631 backlight_binding, ec);
6632 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6633 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006634
6635 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00006636 if (shell->exposay_modifier)
6637 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6638 exposay_binding, shell);
6639
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006640 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00006641 if (!mod)
6642 return;
6643
Daniel Stone325fc2d2012-05-30 16:31:58 +01006644 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6645 zoom_key_binding, NULL);
6646 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6647 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006648 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6649 maximize_binding, NULL);
6650 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6651 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006652 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6653 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006654 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006655 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006656 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006657 weston_compositor_add_button_binding(ec, BTN_LEFT,
6658 mod | MODIFIER_SHIFT,
6659 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006660
6661 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006662 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006663 rotate_binding, NULL);
6664
Daniel Stone325fc2d2012-05-30 16:31:58 +01006665 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6666 shell);
6667 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6668 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006669 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6670 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006671 weston_compositor_add_key_binding(ec, KEY_K, mod,
6672 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006673 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6674 workspace_up_binding, shell);
6675 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6676 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006677 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6678 workspace_move_surface_up_binding,
6679 shell);
6680 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6681 workspace_move_surface_down_binding,
6682 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006683
6684 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6685 if (shell->workspaces.num > 1) {
6686 num_workspace_bindings = shell->workspaces.num;
6687 if (num_workspace_bindings > 6)
6688 num_workspace_bindings = 6;
6689 for (i = 0; i < num_workspace_bindings; i++)
6690 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6691 workspace_f_binding,
6692 shell);
6693 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006694
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006695 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006696}
6697
Jason Ekstrand024177c2014-04-21 19:42:58 -05006698static void
6699handle_seat_created(struct wl_listener *listener, void *data)
6700{
6701 struct weston_seat *seat = data;
6702
6703 create_shell_seat(seat);
6704}
6705
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006706WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006707module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006708 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006709{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006710 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006711 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006712 struct workspace **pws;
6713 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006714 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006715
Peter Huttererf3d62272013-08-08 11:57:05 +10006716 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006717 if (shell == NULL)
6718 return -1;
6719
Kristian Høgsberg75840622011-09-06 13:48:16 -04006720 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006721
6722 shell->destroy_listener.notify = shell_destroy;
6723 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006724 shell->idle_listener.notify = idle_handler;
6725 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6726 shell->wake_listener.notify = wake_handler;
6727 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02006728 shell->transform_listener.notify = transform_handler;
6729 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006730
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006731 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006732 ec->shell_interface.create_shell_surface = create_shell_surface;
6733 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006734 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006735 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006736 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006737 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006738 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006739 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006740 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006741 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006742 ec->shell_interface.set_pid = set_pid;
Quentin Glidic5c201952016-03-23 13:50:49 +01006743 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006744
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006745 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6746 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006747 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6748 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006749 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006750
6751 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006752 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006753
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006754 if (input_panel_setup(shell) < 0)
6755 return -1;
6756
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006757 shell->text_backend = text_backend_init(ec);
6758 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006759 return -1;
6760
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006761 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006762
Daniel Stonedf8133b2013-11-19 11:37:14 +01006763 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6764 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6765
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006766 for (i = 0; i < shell->workspaces.num; i++) {
6767 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6768 if (pws == NULL)
6769 return -1;
6770
6771 *pws = workspace_create();
6772 if (*pws == NULL)
6773 return -1;
6774 }
6775 activate_workspace(shell, 0);
6776
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006777 weston_layer_init(&shell->minimized_layer, NULL);
6778
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006779 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006780 wl_list_init(&shell->workspaces.animation.link);
6781 shell->workspaces.animation.frame = animate_workspace_change_frame;
6782
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006783 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006784 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006785 return -1;
6786
Rafael Antognollie2a34552013-12-03 15:35:45 -02006787 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6788 shell, bind_xdg_shell) == NULL)
6789 return -1;
6790
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006791 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006792 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006793 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006794 return -1;
6795
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006796 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006797
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08006798 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02006799
Xiong Zhang6b481422013-10-23 13:58:32 +08006800 setup_output_destroy_handler(ec, shell);
6801
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006802 loop = wl_display_get_event_loop(ec->wl_display);
6803 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006804
Jason Ekstrand024177c2014-04-21 19:42:58 -05006805 wl_list_for_each(seat, &ec->seat_list, link)
6806 handle_seat_created(NULL, seat);
6807 shell->seat_create_listener.notify = handle_seat_created;
6808 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006809
David Fort50d962f2016-06-09 17:55:52 +02006810 shell->resized_listener.notify = handle_output_resized;
6811 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6812
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006813 screenshooter_create(ec);
6814
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006815 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006816
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006817 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006818
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006819 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6820
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006821 return 0;
6822}