blob: dec9169ba21449f63f0e5b6a92d6b96de049e1e4 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040029#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080038#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070041#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070042#include "shared/helpers.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020043#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050044
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020046#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010048#ifndef static_assert
49#define static_assert(cond, msg)
50#endif
51
Jonas Ådahl04769742012-06-13 00:01:24 +020052struct focus_state {
53 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020062 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050063 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030064 SHELL_SURFACE_POPUP,
65 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020066};
67
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050068struct shell_client;
69
Philip Withnall648a4dd2013-11-25 18:01:44 +000070/*
71 * Surface stacking and ordering.
72 *
73 * This is handled using several linked lists of surfaces, organised into
74 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
75 * above all of the surfaces in the layer below. The set of layers is static and
76 * in the following order (top-most first):
77 * • Lock layer (only ever displayed on its own)
78 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010079 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Fullscreen layer
81 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000082 * • Workspace layers
83 * • Background layer
84 *
85 * The list of layers may be manipulated to remove whole layers of surfaces from
86 * display. For example, when locking the screen, all layers except the lock
87 * layer are removed.
88 *
89 * A surface’s layer is modified on configuring the surface, in
90 * set_surface_type() (which is only called when the surface’s type change is
91 * _committed_). If a surface’s type changes (e.g. when making a window
92 * fullscreen) its layer changes too.
93 *
94 * In order to allow popup and transient surfaces to be correctly stacked above
95 * their parent surfaces, each surface tracks both its parent surface, and a
96 * linked list of its children. When a surface’s layer is updated, so are the
97 * layers of its children. Note that child surfaces are *not* the same as
98 * subsurfaces — child/parent surfaces are purely for maintaining stacking
99 * order.
100 *
101 * The children_link list of siblings of a surface (i.e. those surfaces which
102 * have the same parent) only contains weston_surfaces which have a
103 * shell_surface. Stacking is not implemented for non-shell_surface
104 * weston_surfaces. This means that the following implication does *not* hold:
105 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
106 */
107
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200108struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500109 struct wl_resource *resource;
110 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500111 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800112 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500115 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600116 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700118 struct wl_listener resource_destroy_listener;
119
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400120 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000121 struct wl_list children_list; /* child surfaces of this one */
122 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300123 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200124
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800125 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400126 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500127 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200128 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800129 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200130 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800131 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700132 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800133 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100134
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500137 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200138 } rotation;
139
140 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100141 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100143 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400144 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 } popup;
146
Alex Wu4539b082012-03-01 12:57:46 +0800147 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300148 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400149 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300150 } transient;
151
152 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800153 enum wl_shell_surface_fullscreen_method type;
154 struct weston_transform transform; /* matrix from x, y */
155 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500156 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800157 } fullscreen;
158
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200159 struct weston_transform workspace_transform;
160
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500161 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500162 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100163 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400164
165 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700167 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200168 bool maximized;
169 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200170 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100171 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200173 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500174 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500175
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700176 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400177 int32_t x, y, width, height;
178 } geometry, next_geometry;
179 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700180
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500181 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500182
183 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400187 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188 struct shell_surface *shsurf;
189 struct wl_listener shsurf_destroy_listener;
190};
191
Rusty Lynch1084da52013-08-15 09:10:08 -0700192struct shell_touch_grab {
193 struct weston_touch_grab grab;
194 struct shell_surface *shsurf;
195 struct wl_listener shsurf_destroy_listener;
196 struct weston_touch *touch;
197};
198
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199struct weston_move_grab {
200 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100201 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500202 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203};
204
Rusty Lynch1084da52013-08-15 09:10:08 -0700205struct weston_touch_move_grab {
206 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800207 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700208 wl_fixed_t dx, dy;
209};
210
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300212 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500213 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200215 float x;
216 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200217 } center;
218};
219
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220struct shell_seat {
221 struct weston_seat *seat;
222 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500223 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224
Jason Ekstrand024177c2014-04-21 19:42:58 -0500225 struct wl_listener caps_changed_listener;
226 struct wl_listener pointer_focus_listener;
227 struct wl_listener keyboard_focus_listener;
228
Giulio Camuffo5085a752013-03-25 21:42:45 +0100229 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400230 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200231 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100232 struct wl_list surfaces_list;
233 struct wl_client *client;
234 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200235 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100236 } popup_grab;
237};
238
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800239struct shell_client {
240 struct wl_resource *resource;
241 struct wl_client *client;
242 struct desktop_shell *shell;
243 struct wl_listener destroy_listener;
244 struct wl_event_source *ping_timer;
245 uint32_t ping_serial;
246 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800247 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800248};
249
Alex Wubd3354b2012-04-17 17:20:49 +0800250static struct desktop_shell *
251shell_surface_get_shell(struct shell_surface *shsurf);
252
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500253static void
Derek Foreman74de4692015-07-15 13:00:39 -0500254surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500255
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300256static void
257shell_fade_startup(struct desktop_shell *shell);
258
Philip Withnallbecb77e2013-11-25 18:01:30 +0000259static struct shell_seat *
260get_shell_seat(struct weston_seat *seat);
261
Jonny Lambd73c6942014-08-20 15:53:20 +0200262static void
263get_output_panel_size(struct desktop_shell *shell,
264 struct weston_output *output,
265 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700266
Philip Withnall648a4dd2013-11-25 18:01:44 +0000267static void
268shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
269
Alex Wubd3354b2012-04-17 17:20:49 +0800270static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200271shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
272
273static bool
274shell_surface_is_xdg_surface(struct shell_surface *shsurf);
275
276static bool
277shell_surface_is_xdg_popup(struct shell_surface *shsurf);
278
279static void
280shell_surface_set_parent(struct shell_surface *shsurf,
281 struct weston_surface *parent);
282
Pekka Paalanen8274d902014-08-06 19:36:51 +0300283static int
284shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
285{
286 struct shell_surface *shsurf;
287 const char *typestr[] = {
288 [SHELL_SURFACE_NONE] = "unidentified",
289 [SHELL_SURFACE_TOPLEVEL] = "top-level",
290 [SHELL_SURFACE_POPUP] = "popup",
291 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
292 };
293 const char *t, *c;
294
295 shsurf = get_shell_surface(surface);
296 if (!shsurf)
297 return snprintf(buf, len, "unidentified window");
298
299 t = shsurf->title;
300 c = shsurf->class;
301
302 return snprintf(buf, len, "%s window%s%s%s%s%s",
303 typestr[shsurf->type],
304 t ? " '" : "", t ?: "", t ? "'" : "",
305 c ? " of " : "", c ?: "");
306}
307
Rafael Antognollie2a34552013-12-03 15:35:45 -0200308static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800309shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
310{
311 struct desktop_shell *shell;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200312 struct weston_view *view;
313 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800314
315 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100316
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300317 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800318 return false;
319
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200320 /* Find topmost shsurf on the same fullscreen output on which shsurf
321 * is displaying. We don't care about other outputs.
322 */
323 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
324 layer_link.link) {
325 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
326
327 if (cand_shsurf &&
328 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
329 top_fs_shsurf = cand_shsurf;
330 break;
331 }
332 }
333
334 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800335}
336
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500337static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339{
340 struct shell_grab *grab;
341
342 grab = container_of(listener, struct shell_grab,
343 shsurf_destroy_listener);
344
345 grab->shsurf = NULL;
346}
347
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800348struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349get_default_view(struct weston_surface *surface)
350{
351 struct shell_surface *shsurf;
352 struct weston_view *view;
353
354 if (!surface || wl_list_empty(&surface->views))
355 return NULL;
356
357 shsurf = get_shell_surface(surface);
358 if (shsurf)
359 return shsurf->view;
360
361 wl_list_for_each(view, &surface->views, surface_link)
362 if (weston_view_is_mapped(view))
363 return view;
364
365 return container_of(surface->views.next, struct weston_view, surface_link);
366}
367
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400369popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200370static void
371touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400372
373static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400377 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380 struct desktop_shell *shell = shsurf->shell;
381
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400382 popup_grab_end(pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200383 if (pointer->seat->touch)
384 touch_popup_grab_end(pointer->seat->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) {
395 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
396 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) {
425 case DESKTOP_SHELL_PANEL_POSITION_TOP:
426 case 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
435 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
436 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
437 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
456get_output_work_area(struct desktop_shell *shell,
457 struct weston_output *output,
458 pixman_rectangle32_t *area)
459{
460 int32_t panel_width = 0, panel_height = 0;
461
Derek Foremanf814c5d2015-04-15 12:23:54 -0500462 area->x = output->x;
463 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200464
465 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200466 switch (shell->panel_position) {
467 case DESKTOP_SHELL_PANEL_POSITION_TOP:
468 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500469 area->y += panel_height;
Jonny Lambd73c6942014-08-20 15:53:20 +0200470 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
471 area->width = output->width;
472 area->height = output->height - panel_height;
473 break;
474 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500475 area->x += panel_width;
Jonny Lambd73c6942014-08-20 15:53:20 +0200476 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
477 area->width = output->width - panel_width;
478 area->height = output->height;
479 break;
480 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700481}
482
483static void
484send_configure_for_surface(struct shell_surface *shsurf)
485{
486 int32_t width, height;
487 struct surface_state *state;
488
489 if (shsurf->state_requested)
490 state = &shsurf->requested_state;
491 else if (shsurf->state_changed)
492 state = &shsurf->next_state;
493 else
494 state = &shsurf->state;
495
496 if (state->fullscreen) {
497 width = shsurf->output->width;
498 height = shsurf->output->height;
499 } else if (state->maximized) {
500 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200501 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700502
503 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200504 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700505
Jonny Lambd73c6942014-08-20 15:53:20 +0200506 width = area.width;
507 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900508 } else if (shsurf->resize_edges) {
509 width = shsurf->geometry.width;
510 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700511 } else {
512 width = 0;
513 height = 0;
514 }
515
516 shsurf->client->send_configure(shsurf->surface, width, height);
517}
518
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300519static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400520shell_surface_state_changed(struct shell_surface *shsurf)
521{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700522 if (shell_surface_is_xdg_surface(shsurf))
523 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400524}
525
526static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300527shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300528{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700529 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400530 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700531 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400532
533 if (grab->shsurf->resize_edges) {
534 grab->shsurf->resize_edges = 0;
535 shell_surface_state_changed(grab->shsurf);
536 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700537 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300538
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700539 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300540}
541
542static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700543shell_touch_grab_start(struct shell_touch_grab *grab,
544 const struct weston_touch_grab_interface *interface,
545 struct shell_surface *shsurf,
546 struct weston_touch *touch)
547{
548 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800549
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200550 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700551 if (touch->seat->pointer)
552 popup_grab_end(touch->seat->pointer);
553
Rusty Lynch1084da52013-08-15 09:10:08 -0700554 grab->grab.interface = interface;
555 grab->shsurf = shsurf;
556 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
557 wl_signal_add(&shsurf->destroy_signal,
558 &grab->shsurf_destroy_listener);
559
560 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700561 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700562
563 weston_touch_start_grab(touch, &grab->grab);
564 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500565 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500566 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700567}
568
569static void
570shell_touch_grab_end(struct shell_touch_grab *grab)
571{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700572 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700573 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700574 grab->shsurf->grabbed = 0;
575 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700576
577 weston_touch_end_grab(grab->touch);
578}
579
580static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500581center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800582 struct weston_output *output);
583
Daniel Stone496ca172012-05-30 16:31:42 +0100584static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300585get_modifier(char *modifier)
586{
587 if (!modifier)
588 return MODIFIER_SUPER;
589
590 if (!strcmp("ctrl", modifier))
591 return MODIFIER_CTRL;
592 else if (!strcmp("alt", modifier))
593 return MODIFIER_ALT;
594 else if (!strcmp("super", modifier))
595 return MODIFIER_SUPER;
596 else
597 return MODIFIER_SUPER;
598}
599
Juan Zhaoe10d2792012-04-25 19:09:52 +0800600static enum animation_type
601get_animation_type(char *animation)
602{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800603 if (!animation)
604 return ANIMATION_NONE;
605
Juan Zhaoe10d2792012-04-25 19:09:52 +0800606 if (!strcmp("zoom", animation))
607 return ANIMATION_ZOOM;
608 else if (!strcmp("fade", animation))
609 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100610 else if (!strcmp("dim-layer", animation))
611 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800612 else
613 return ANIMATION_NONE;
614}
615
Alex Wu4539b082012-03-01 12:57:46 +0800616static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400617shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200618{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400619 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500620 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300621 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200622
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400623 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400624 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300625 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
626 WESTON_SHELL_CLIENT);
627 if (ret < 0)
628 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400629 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500630 "client", &s, client);
631 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100632 shell->client = s;
633 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400634 "binding-modifier", &s, "super");
635 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200636 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800637
638 weston_config_section_get_string(section,
639 "exposay-modifier", &s, "none");
640 if (strcmp(s, "none") == 0)
641 shell->exposay_modifier = 0;
642 else
643 shell->exposay_modifier = get_modifier(s);
644 free(s);
645
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400646 weston_config_section_get_string(section, "animation", &s, "none");
647 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200648 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200649 weston_config_section_get_string(section, "close-animation", &s, "fade");
650 shell->win_close_animation_type = get_animation_type(s);
651 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700652 weston_config_section_get_string(section,
653 "startup-animation", &s, "fade");
654 shell->startup_animation_type = get_animation_type(s);
655 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700656 if (shell->startup_animation_type == ANIMATION_ZOOM)
657 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100658 weston_config_section_get_string(section, "focus-animation", &s, "none");
659 shell->focus_animation_type = get_animation_type(s);
660 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400661 weston_config_section_get_uint(section, "num-workspaces",
662 &shell->workspaces.num,
663 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200664}
665
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800666struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100667get_default_output(struct weston_compositor *compositor)
668{
669 return container_of(compositor->output_list.next,
670 struct weston_output, link);
671}
672
Pekka Paalanen8274d902014-08-06 19:36:51 +0300673static int
674focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
675{
676 return snprintf(buf, len, "focus highlight effect for output %s",
677 surface->output->name);
678}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100679
680/* no-op func for checking focus surface */
681static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600682focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100683{
684}
685
686static struct focus_surface *
687get_focus_surface(struct weston_surface *surface)
688{
689 if (surface->configure == focus_surface_configure)
690 return surface->configure_private;
691 else
692 return NULL;
693}
694
695static bool
696is_focus_surface (struct weston_surface *es)
697{
698 return (es->configure == focus_surface_configure);
699}
700
701static bool
702is_focus_view (struct weston_view *view)
703{
704 return is_focus_surface (view->surface);
705}
706
707static struct focus_surface *
708create_focus_surface(struct weston_compositor *ec,
709 struct weston_output *output)
710{
711 struct focus_surface *fsurf = NULL;
712 struct weston_surface *surface = NULL;
713
714 fsurf = malloc(sizeof *fsurf);
715 if (!fsurf)
716 return NULL;
717
718 fsurf->surface = weston_surface_create(ec);
719 surface = fsurf->surface;
720 if (surface == NULL) {
721 free(fsurf);
722 return NULL;
723 }
724
725 surface->configure = focus_surface_configure;
726 surface->output = output;
727 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300728 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100729
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800730 fsurf->view = weston_view_create(surface);
731 if (fsurf->view == NULL) {
732 weston_surface_destroy(surface);
733 free(fsurf);
734 return NULL;
735 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100736 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100737
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600738 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600739 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100740 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
741 pixman_region32_fini(&surface->opaque);
742 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
743 output->width, output->height);
744 pixman_region32_fini(&surface->input);
745 pixman_region32_init(&surface->input);
746
747 wl_list_init(&fsurf->workspace_transform.link);
748
749 return fsurf;
750}
751
752static void
753focus_surface_destroy(struct focus_surface *fsurf)
754{
755 weston_surface_destroy(fsurf->surface);
756 free(fsurf);
757}
758
759static void
760focus_animation_done(struct weston_view_animation *animation, void *data)
761{
762 struct workspace *ws = data;
763
764 ws->focus_animation = NULL;
765}
766
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200767static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200768focus_state_destroy(struct focus_state *state)
769{
770 wl_list_remove(&state->seat_destroy_listener.link);
771 wl_list_remove(&state->surface_destroy_listener.link);
772 free(state);
773}
774
775static void
776focus_state_seat_destroy(struct wl_listener *listener, void *data)
777{
778 struct focus_state *state = container_of(listener,
779 struct focus_state,
780 seat_destroy_listener);
781
782 wl_list_remove(&state->link);
783 focus_state_destroy(state);
784}
785
786static void
787focus_state_surface_destroy(struct wl_listener *listener, void *data)
788{
789 struct focus_state *state = container_of(listener,
790 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400791 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400792 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500793 struct weston_surface *main_surface, *next;
794 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200795
Pekka Paalanen01388e22013-04-25 13:57:44 +0300796 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
797
Kristian Høgsberge3778222012-07-31 17:29:30 -0400798 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300799 wl_list_for_each(view,
800 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400802 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100803 if (is_focus_view(view))
804 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400805
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400807 break;
808 }
809
Pekka Paalanen01388e22013-04-25 13:57:44 +0300810 /* if the focus was a sub-surface, activate its main surface */
811 if (main_surface != state->keyboard_focus)
812 next = main_surface;
813
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100814 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400815 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100816 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100817 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400818 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100819 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
820 if (state->ws->focus_animation)
821 weston_view_animation_destroy(state->ws->focus_animation);
822
823 state->ws->focus_animation = weston_fade_run(
824 state->ws->fsurf_front->view,
825 state->ws->fsurf_front->view->alpha, 0.0, 300,
826 focus_animation_done, state->ws);
827 }
828
Kristian Høgsberge3778222012-07-31 17:29:30 -0400829 wl_list_remove(&state->link);
830 focus_state_destroy(state);
831 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200832}
833
834static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400835focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200836{
Jonas Ådahl04769742012-06-13 00:01:24 +0200837 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200838
839 state = malloc(sizeof *state);
840 if (state == NULL)
841 return NULL;
842
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400844 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200845 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400846 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200847
848 state->seat_destroy_listener.notify = focus_state_seat_destroy;
849 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400850 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200851 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400852 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200853
854 return state;
855}
856
Jonas Ådahl8538b222012-08-29 22:13:03 +0200857static struct focus_state *
858ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
859{
860 struct workspace *ws = get_current_workspace(shell);
861 struct focus_state *state;
862
863 wl_list_for_each(state, &ws->focus_list, link)
864 if (state->seat == seat)
865 break;
866
867 if (&state->link == &ws->focus_list)
868 state = focus_state_create(seat, ws);
869
870 return state;
871}
872
Jonas Ådahl04769742012-06-13 00:01:24 +0200873static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800874focus_state_set_focus(struct focus_state *state,
875 struct weston_surface *surface)
876{
877 if (state->keyboard_focus) {
878 wl_list_remove(&state->surface_destroy_listener.link);
879 wl_list_init(&state->surface_destroy_listener.link);
880 }
881
882 state->keyboard_focus = surface;
883 if (surface)
884 wl_signal_add(&surface->destroy_signal,
885 &state->surface_destroy_listener);
886}
887
888static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400889restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200890{
891 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400892 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100893 struct wl_list pending_seat_list;
894 struct weston_seat *seat, *next_seat;
895
896 /* Temporarily steal the list of seats so that we can keep
897 * track of the seats we've already processed */
898 wl_list_init(&pending_seat_list);
899 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
900 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200901
902 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100903 wl_list_remove(&state->seat->link);
904 wl_list_insert(&shell->compositor->seat_list,
905 &state->seat->link);
906
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800907 if (state->seat->keyboard == NULL)
908 continue;
909
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400910 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200911
Kristian Høgsberge3148752013-05-06 23:19:49 -0400912 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200913 }
Neil Roberts4237f502014-04-09 16:33:31 +0100914
915 /* For any remaining seats that we don't have a focus state
916 * for we'll reset the keyboard focus to NULL */
917 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
918 wl_list_insert(&shell->compositor->seat_list, &seat->link);
919
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300920 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100921 continue;
922
923 weston_keyboard_set_focus(seat->keyboard, NULL);
924 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200925}
926
927static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200928replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
929 struct weston_seat *seat)
930{
931 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932
933 wl_list_for_each(state, &ws->focus_list, link) {
934 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800935 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936 return;
937 }
938 }
939}
940
941static void
942drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
943 struct weston_surface *surface)
944{
945 struct focus_state *state;
946
947 wl_list_for_each(state, &ws->focus_list, link)
948 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800949 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200950}
951
952static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100953animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
954 struct weston_view *from, struct weston_view *to)
955{
956 struct weston_output *output;
957 bool focus_surface_created = false;
958
959 /* FIXME: Only support dim animation using two layers */
960 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
961 return;
962
963 output = get_default_output(shell->compositor);
964 if (ws->fsurf_front == NULL && (from || to)) {
965 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800966 if (ws->fsurf_front == NULL)
967 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100968 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800969
970 ws->fsurf_back = create_focus_surface(shell->compositor, output);
971 if (ws->fsurf_back == NULL) {
972 focus_surface_destroy(ws->fsurf_front);
973 return;
974 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100975 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800976
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 focus_surface_created = true;
978 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300979 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
980 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100981 }
982
983 if (ws->focus_animation) {
984 weston_view_animation_destroy(ws->focus_animation);
985 ws->focus_animation = NULL;
986 }
987
988 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300989 weston_layer_entry_insert(&to->layer_link,
990 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100991 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300992 weston_layer_entry_insert(&ws->layer.view_list,
993 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100994
995 if (focus_surface_created) {
996 ws->focus_animation = weston_fade_run(
997 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200998 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 focus_animation_done, ws);
1000 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001001 weston_layer_entry_insert(&from->layer_link,
1002 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 ws->focus_animation = weston_stable_fade_run(
1004 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001005 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001006 focus_animation_done, ws);
1007 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001008 weston_layer_entry_insert(&ws->layer.view_list,
1009 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001010 ws->focus_animation = weston_stable_fade_run(
1011 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001012 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001013 focus_animation_done, ws);
1014 }
1015}
1016
1017static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001018workspace_destroy(struct workspace *ws)
1019{
Jonas Ådahl04769742012-06-13 00:01:24 +02001020 struct focus_state *state, *next;
1021
1022 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1023 focus_state_destroy(state);
1024
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001025 if (ws->fsurf_front)
1026 focus_surface_destroy(ws->fsurf_front);
1027 if (ws->fsurf_back)
1028 focus_surface_destroy(ws->fsurf_back);
1029
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001030 free(ws);
1031}
1032
Jonas Ådahl04769742012-06-13 00:01:24 +02001033static void
1034seat_destroyed(struct wl_listener *listener, void *data)
1035{
1036 struct weston_seat *seat = data;
1037 struct focus_state *state, *next;
1038 struct workspace *ws = container_of(listener,
1039 struct workspace,
1040 seat_destroyed_listener);
1041
1042 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1043 if (state->seat == seat)
1044 wl_list_remove(&state->link);
1045}
1046
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001047static struct workspace *
1048workspace_create(void)
1049{
1050 struct workspace *ws = malloc(sizeof *ws);
1051 if (ws == NULL)
1052 return NULL;
1053
1054 weston_layer_init(&ws->layer, NULL);
1055
Jonas Ådahl04769742012-06-13 00:01:24 +02001056 wl_list_init(&ws->focus_list);
1057 wl_list_init(&ws->seat_destroyed_listener.link);
1058 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001059 ws->fsurf_front = NULL;
1060 ws->fsurf_back = NULL;
1061 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001062
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001063 return ws;
1064}
1065
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066static int
1067workspace_is_empty(struct workspace *ws)
1068{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001069 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070}
1071
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001072static struct workspace *
1073get_workspace(struct desktop_shell *shell, unsigned int index)
1074{
1075 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001076 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001077 pws += index;
1078 return *pws;
1079}
1080
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001081struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001082get_current_workspace(struct desktop_shell *shell)
1083{
1084 return get_workspace(shell, shell->workspaces.current);
1085}
1086
1087static void
1088activate_workspace(struct desktop_shell *shell, unsigned int index)
1089{
1090 struct workspace *ws;
1091
1092 ws = get_workspace(shell, index);
1093 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1094
1095 shell->workspaces.current = index;
1096}
1097
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098static unsigned int
1099get_output_height(struct weston_output *output)
1100{
1101 return abs(output->region.extents.y1 - output->region.extents.y2);
1102}
1103
1104static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001105view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 struct weston_transform *transform;
1108
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001109 if (is_focus_view(view)) {
1110 struct focus_surface *fsurf = get_focus_surface(view->surface);
1111 transform = &fsurf->workspace_transform;
1112 } else {
1113 struct shell_surface *shsurf = get_shell_surface(view->surface);
1114 transform = &shsurf->workspace_transform;
1115 }
1116
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001119 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001121 weston_matrix_init(&transform->matrix);
1122 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001123 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125}
1126
1127static void
1128workspace_translate_out(struct workspace *ws, double fraction)
1129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131 unsigned int height;
1132 double d;
1133
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001134 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136 d = height * fraction;
1137
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001138 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001139 }
1140}
1141
1142static void
1143workspace_translate_in(struct workspace *ws, double fraction)
1144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146 unsigned int height;
1147 double d;
1148
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001149 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151
1152 if (fraction > 0)
1153 d = -(height - height * fraction);
1154 else
1155 d = height + height * fraction;
1156
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001157 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001158 }
1159}
1160
1161static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001162broadcast_current_workspace_state(struct desktop_shell *shell)
1163{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001164 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001165
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001166 wl_resource_for_each(resource, &shell->workspaces.client_list)
1167 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001168 shell->workspaces.current,
1169 shell->workspaces.num);
1170}
1171
1172static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173reverse_workspace_change_animation(struct desktop_shell *shell,
1174 unsigned int index,
1175 struct workspace *from,
1176 struct workspace *to)
1177{
1178 shell->workspaces.current = index;
1179
1180 shell->workspaces.anim_to = to;
1181 shell->workspaces.anim_from = from;
1182 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1183 shell->workspaces.anim_timestamp = 0;
1184
Scott Moreau4272e632012-08-13 09:58:41 -06001185 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001186}
1187
1188static void
1189workspace_deactivate_transforms(struct workspace *ws)
1190{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001192 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001193
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001194 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001195 if (is_focus_view(view)) {
1196 struct focus_surface *fsurf = get_focus_surface(view->surface);
1197 transform = &fsurf->workspace_transform;
1198 } else {
1199 struct shell_surface *shsurf = get_shell_surface(view->surface);
1200 transform = &shsurf->workspace_transform;
1201 }
1202
1203 if (!wl_list_empty(&transform->link)) {
1204 wl_list_remove(&transform->link);
1205 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001206 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001208 }
1209}
1210
1211static void
1212finish_workspace_change_animation(struct desktop_shell *shell,
1213 struct workspace *from,
1214 struct workspace *to)
1215{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001216 struct weston_view *view;
1217
Scott Moreau4272e632012-08-13 09:58:41 -06001218 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001220 /* Views that extend past the bottom of the output are still
1221 * visible after the workspace animation ends but before its layer
1222 * is hidden. In that case, we need to damage below those views so
1223 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001224 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001225 weston_view_damage_below(view);
1226
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001227 wl_list_remove(&shell->workspaces.animation.link);
1228 workspace_deactivate_transforms(from);
1229 workspace_deactivate_transforms(to);
1230 shell->workspaces.anim_to = NULL;
1231
1232 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1233}
1234
1235static void
1236animate_workspace_change_frame(struct weston_animation *animation,
1237 struct weston_output *output, uint32_t msecs)
1238{
1239 struct desktop_shell *shell =
1240 container_of(animation, struct desktop_shell,
1241 workspaces.animation);
1242 struct workspace *from = shell->workspaces.anim_from;
1243 struct workspace *to = shell->workspaces.anim_to;
1244 uint32_t t;
1245 double x, y;
1246
1247 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1248 finish_workspace_change_animation(shell, from, to);
1249 return;
1250 }
1251
1252 if (shell->workspaces.anim_timestamp == 0) {
1253 if (shell->workspaces.anim_current == 0.0)
1254 shell->workspaces.anim_timestamp = msecs;
1255 else
1256 shell->workspaces.anim_timestamp =
1257 msecs -
1258 /* Invers of movement function 'y' below. */
1259 (asin(1.0 - shell->workspaces.anim_current) *
1260 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1261 M_2_PI);
1262 }
1263
1264 t = msecs - shell->workspaces.anim_timestamp;
1265
1266 /*
1267 * x = [0, π/2]
1268 * y(x) = sin(x)
1269 */
1270 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1271 y = sin(x);
1272
1273 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001274 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001275
1276 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1277 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1278 shell->workspaces.anim_current = y;
1279
Scott Moreau4272e632012-08-13 09:58:41 -06001280 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001281 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001282 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001283 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001284}
1285
1286static void
1287animate_workspace_change(struct desktop_shell *shell,
1288 unsigned int index,
1289 struct workspace *from,
1290 struct workspace *to)
1291{
1292 struct weston_output *output;
1293
1294 int dir;
1295
1296 if (index > shell->workspaces.current)
1297 dir = -1;
1298 else
1299 dir = 1;
1300
1301 shell->workspaces.current = index;
1302
1303 shell->workspaces.anim_dir = dir;
1304 shell->workspaces.anim_from = from;
1305 shell->workspaces.anim_to = to;
1306 shell->workspaces.anim_current = 0.0;
1307 shell->workspaces.anim_timestamp = 0;
1308
1309 output = container_of(shell->compositor->output_list.next,
1310 struct weston_output, link);
1311 wl_list_insert(&output->animation_list,
1312 &shell->workspaces.animation.link);
1313
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001314 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001315
1316 workspace_translate_in(to, 0);
1317
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001318 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001319
Scott Moreau4272e632012-08-13 09:58:41 -06001320 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001321}
1322
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001323static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001324update_workspace(struct desktop_shell *shell, unsigned int index,
1325 struct workspace *from, struct workspace *to)
1326{
1327 shell->workspaces.current = index;
1328 wl_list_insert(&from->layer.link, &to->layer.link);
1329 wl_list_remove(&from->layer.link);
1330}
1331
1332static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001333change_workspace(struct desktop_shell *shell, unsigned int index)
1334{
1335 struct workspace *from;
1336 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001337 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001338
1339 if (index == shell->workspaces.current)
1340 return;
1341
1342 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001343 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001344 return;
1345
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001346 from = get_current_workspace(shell);
1347 to = get_workspace(shell, index);
1348
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001349 if (shell->workspaces.anim_from == to &&
1350 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001351 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001352 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001353 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001354 return;
1355 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001356
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001357 if (shell->workspaces.anim_to != NULL)
1358 finish_workspace_change_animation(shell,
1359 shell->workspaces.anim_from,
1360 shell->workspaces.anim_to);
1361
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001362 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001363
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001364 if (shell->focus_animation_type != ANIMATION_NONE) {
1365 wl_list_for_each(state, &from->focus_list, link)
1366 if (state->keyboard_focus)
1367 animate_focus_change(shell, from,
1368 get_default_view(state->keyboard_focus), NULL);
1369
1370 wl_list_for_each(state, &to->focus_list, link)
1371 if (state->keyboard_focus)
1372 animate_focus_change(shell, to,
1373 NULL, get_default_view(state->keyboard_focus));
1374 }
1375
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001376 if (workspace_is_empty(to) && workspace_is_empty(from))
1377 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001378 else
1379 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001380
1381 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001382}
1383
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001384static bool
1385workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1386{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001387 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001388 struct wl_list *e;
1389
1390 if (wl_list_empty(list))
1391 return false;
1392
1393 e = list->next;
1394
1395 if (e->next != list)
1396 return false;
1397
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001398 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001399}
1400
1401static void
Philip Withnall659163d2013-11-25 18:01:36 +00001402move_surface_to_workspace(struct desktop_shell *shell,
1403 struct shell_surface *shsurf,
1404 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001405{
1406 struct workspace *from;
1407 struct workspace *to;
1408 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001409 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001410 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001411
1412 if (workspace == shell->workspaces.current)
1413 return;
1414
Philip Withnall659163d2013-11-25 18:01:36 +00001415 view = get_default_view(shsurf->surface);
1416 if (!view)
1417 return;
1418
1419 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1420
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001421 if (workspace >= shell->workspaces.num)
1422 workspace = shell->workspaces.num - 1;
1423
Jonas Ådahle9d22502012-08-29 22:13:01 +02001424 from = get_current_workspace(shell);
1425 to = get_workspace(shell, workspace);
1426
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001427 weston_layer_entry_remove(&view->layer_link);
1428 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001429
Philip Withnall648a4dd2013-11-25 18:01:44 +00001430 shell_surface_update_child_surface_layers(shsurf);
1431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001433 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1434 if (!seat->keyboard)
1435 continue;
1436
1437 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001439 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001440 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001441
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001443}
1444
1445static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001447 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001448 unsigned int index)
1449{
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
Pekka Paalanen01388e22013-04-25 13:57:44 +03001457 surface = weston_surface_get_main_surface(seat->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 broadcast_current_workspace_state(shell);
1484
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001485 return;
1486 }
1487
1488 if (shell->workspaces.anim_to != NULL)
1489 finish_workspace_change_animation(shell,
1490 shell->workspaces.anim_from,
1491 shell->workspaces.anim_to);
1492
1493 if (workspace_is_empty(from) &&
1494 workspace_has_only(to, surface))
1495 update_workspace(shell, index, from, to);
1496 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001497 if (shsurf != NULL &&
1498 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001499 wl_list_insert(&shell->workspaces.anim_sticky_list,
1500 &shsurf->workspace_transform.link);
1501
1502 animate_workspace_change(shell, index, from, to);
1503 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001504
1505 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001506
1507 state = ensure_focus_state(shell, seat);
1508 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001509 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001510}
1511
1512static void
1513workspace_manager_move_surface(struct wl_client *client,
1514 struct wl_resource *resource,
1515 struct wl_resource *surface_resource,
1516 uint32_t workspace)
1517{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001518 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001519 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001520 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001521 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001522 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001523
Pekka Paalanen01388e22013-04-25 13:57:44 +03001524 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001525 shell_surface = get_shell_surface(main_surface);
1526 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001528
1529 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001530}
1531
1532static const struct workspace_manager_interface workspace_manager_implementation = {
1533 workspace_manager_move_surface,
1534};
1535
1536static void
1537unbind_resource(struct wl_resource *resource)
1538{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001539 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001540}
1541
1542static void
1543bind_workspace_manager(struct wl_client *client,
1544 void *data, uint32_t version, uint32_t id)
1545{
1546 struct desktop_shell *shell = data;
1547 struct wl_resource *resource;
1548
Jason Ekstranda85118c2013-06-27 20:17:02 -05001549 resource = wl_resource_create(client,
1550 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001551
1552 if (resource == NULL) {
1553 weston_log("couldn't add workspace manager object");
1554 return;
1555 }
1556
Jason Ekstranda85118c2013-06-27 20:17:02 -05001557 wl_resource_set_implementation(resource,
1558 &workspace_manager_implementation,
1559 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001560 wl_list_insert(&shell->workspaces.client_list,
1561 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001562
1563 workspace_manager_send_state(resource,
1564 shell->workspaces.current,
1565 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001566}
1567
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001568static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001569touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1570 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1571{
1572}
1573
1574static void
1575touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1576{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001577 struct weston_touch_move_grab *move =
1578 (struct weston_touch_move_grab *) container_of(
1579 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001580
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001581 if (touch_id == 0)
1582 move->active = 0;
1583
Jonas Ådahl9484b692013-12-02 22:05:03 +01001584 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001585 shell_touch_grab_end(&move->base);
1586 free(move);
1587 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001588}
1589
1590static void
1591touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1592 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1593{
1594 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1595 struct shell_surface *shsurf = move->base.shsurf;
1596 struct weston_surface *es;
1597 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1598 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1599
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001600 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001601 return;
1602
1603 es = shsurf->surface;
1604
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001605 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001606
1607 weston_compositor_schedule_repaint(es->compositor);
1608}
1609
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001610static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001611touch_move_grab_frame(struct weston_touch_grab *grab)
1612{
1613}
1614
1615static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001616touch_move_grab_cancel(struct weston_touch_grab *grab)
1617{
1618 struct weston_touch_move_grab *move =
1619 (struct weston_touch_move_grab *) container_of(
1620 grab, struct shell_touch_grab, grab);
1621
1622 shell_touch_grab_end(&move->base);
1623 free(move);
1624}
1625
Rusty Lynch1084da52013-08-15 09:10:08 -07001626static const struct weston_touch_grab_interface touch_move_grab_interface = {
1627 touch_move_grab_down,
1628 touch_move_grab_up,
1629 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001630 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001631 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001632};
1633
1634static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001635surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001636{
1637 struct weston_touch_move_grab *move;
1638
1639 if (!shsurf)
1640 return -1;
1641
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001642 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001643 return 0;
1644
1645 move = malloc(sizeof *move);
1646 if (!move)
1647 return -1;
1648
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001649 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001651 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001653 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001654
1655 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001656 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001657
1658 return 0;
1659}
1660
1661static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001662noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001663{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001664}
1665
1666static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001667constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1668{
1669 struct shell_surface *shsurf = move->base.shsurf;
1670 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001671 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001672 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001673 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001674
1675 x = wl_fixed_to_int(pointer->x + move->dx);
1676 y = wl_fixed_to_int(pointer->y + move->dy);
1677
Jonny Lambd73c6942014-08-20 15:53:20 +02001678 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001679 get_output_work_area(shsurf->shell,
1680 shsurf->surface->output,
1681 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001682
Derek Foreman6feb0f92015-04-15 12:23:56 -05001683 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1684 if (bottom - safety < area.y)
1685 y = area.y + safety - shsurf->geometry.height
1686 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001687
1688 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001689 y + shsurf->geometry.y < area.y)
1690 y = area.y - shsurf->geometry.y;
1691
Jonny Lambd73c6942014-08-20 15:53:20 +02001692 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001693
1694 *cx = x;
1695 *cy = y;
1696}
1697
1698static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001699move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1700 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001701{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001702 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001703 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001704 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001705 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001706
1707 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001708 if (!shsurf)
1709 return;
1710
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001711 constrain_position(move, &cx, &cy);
1712
1713 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001714
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001716}
1717
1718static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001719move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001720 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001721{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001722 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1723 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001724 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001725 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001726
Daniel Stone4dbadb12012-05-30 16:31:51 +01001727 if (pointer->button_count == 0 &&
1728 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001729 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001730 free(grab);
1731 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001732}
1733
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001734static void
1735move_grab_cancel(struct weston_pointer_grab *grab)
1736{
1737 struct shell_grab *shell_grab =
1738 container_of(grab, struct shell_grab, grab);
1739
1740 shell_grab_end(shell_grab);
1741 free(grab);
1742}
1743
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001744static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001745 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001746 move_grab_motion,
1747 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001748 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001749};
1750
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001752surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001753 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754{
1755 struct weston_move_grab *move;
1756
1757 if (!shsurf)
1758 return -1;
1759
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001760 if (shsurf->grabbed ||
1761 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762 return 0;
1763
1764 move = malloc(sizeof *move);
1765 if (!move)
1766 return -1;
1767
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001769 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001771 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001772 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773
1774 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Derek Foreman794fa0e2015-07-15 13:00:38 -05001775 pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776
1777 return 0;
1778}
1779
1780static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001781common_surface_move(struct wl_resource *resource,
1782 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001783{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001784 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001785 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001786 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001787
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001788 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001789 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001790 seat->pointer->button_count > 0 &&
1791 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001793 if ((surface == shsurf->surface) &&
Derek Foreman794fa0e2015-07-15 13:00:38 -05001794 (surface_move(shsurf, seat->pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001795 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001796 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001797 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001798 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001800 if ((surface == shsurf->surface) &&
Derek Foremanb7674ae2015-07-15 13:00:37 -05001801 (surface_touch_move(shsurf, seat->touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001802 wl_resource_post_no_memory(resource);
1803 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001804}
1805
Rafael Antognollie2a34552013-12-03 15:35:45 -02001806static void
1807shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1808 struct wl_resource *seat_resource, uint32_t serial)
1809{
1810 common_surface_move(resource, seat_resource, serial);
1811}
1812
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001813struct weston_resize_grab {
1814 struct shell_grab base;
1815 uint32_t edges;
1816 int32_t width, height;
1817};
1818
1819static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001820resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1821 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001822{
1823 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001824 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001825 struct shell_surface *shsurf = resize->base.shsurf;
1826 int32_t width, height;
1827 wl_fixed_t from_x, from_y;
1828 wl_fixed_t to_x, to_y;
1829
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001830 weston_pointer_move(pointer, x, y);
1831
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001832 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001833 return;
1834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 weston_view_from_global_fixed(shsurf->view,
1836 pointer->grab_x, pointer->grab_y,
1837 &from_x, &from_y);
1838 weston_view_from_global_fixed(shsurf->view,
1839 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001840
1841 width = resize->width;
1842 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1843 width += wl_fixed_to_int(from_x - to_x);
1844 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1845 width += wl_fixed_to_int(to_x - from_x);
1846 }
1847
1848 height = resize->height;
1849 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1850 height += wl_fixed_to_int(from_y - to_y);
1851 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1852 height += wl_fixed_to_int(to_y - from_y);
1853 }
1854
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001855 if (width < 1)
1856 width = 1;
1857 if (height < 1)
1858 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001859 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001860}
1861
1862static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001863send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001864{
1865 struct shell_surface *shsurf = get_shell_surface(surface);
1866
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001867 assert(shsurf);
1868
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001869 if (shsurf->resource)
1870 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001871 shsurf->resize_edges,
1872 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001873}
1874
1875static const struct weston_shell_client shell_client = {
1876 send_configure
1877};
1878
1879static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001880resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001881 uint32_t time, uint32_t button, uint32_t state_w)
1882{
1883 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001884 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001885 enum wl_pointer_button_state state = state_w;
1886
1887 if (pointer->button_count == 0 &&
1888 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1889 shell_grab_end(&resize->base);
1890 free(grab);
1891 }
1892}
1893
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001894static void
1895resize_grab_cancel(struct weston_pointer_grab *grab)
1896{
1897 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1898
1899 shell_grab_end(&resize->base);
1900 free(grab);
1901}
1902
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001903static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001904 noop_grab_focus,
1905 resize_grab_motion,
1906 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001907 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001908};
1909
Giulio Camuffob8366642013-04-25 13:57:46 +03001910/*
1911 * Returns the bounding box of a surface and all its sub-surfaces,
1912 * in the surface coordinates system. */
1913static void
1914surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1915 int32_t *y, int32_t *w, int32_t *h) {
1916 pixman_region32_t region;
1917 pixman_box32_t *box;
1918 struct weston_subsurface *subsurface;
1919
1920 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 surface->width,
1922 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001923
1924 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1925 pixman_region32_union_rect(&region, &region,
1926 subsurface->position.x,
1927 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 subsurface->surface->width,
1929 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001930 }
1931
1932 box = pixman_region32_extents(&region);
1933 if (x)
1934 *x = box->x1;
1935 if (y)
1936 *y = box->y1;
1937 if (w)
1938 *w = box->x2 - box->x1;
1939 if (h)
1940 *h = box->y2 - box->y1;
1941
1942 pixman_region32_fini(&region);
1943}
1944
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001945static int
1946surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001947 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001948{
1949 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001950 const unsigned resize_topbottom =
1951 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1952 const unsigned resize_leftright =
1953 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1954 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001955
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001956 if (shsurf->grabbed ||
1957 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001958 return 0;
1959
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001960 /* Check for invalid edge combinations. */
1961 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1962 (edges & resize_topbottom) == resize_topbottom ||
1963 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001964 return 0;
1965
1966 resize = malloc(sizeof *resize);
1967 if (!resize)
1968 return -1;
1969
1970 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001971
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001972 resize->width = shsurf->geometry.width;
1973 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001974
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001975 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001976 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001977 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001978 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001979
1980 return 0;
1981}
1982
1983static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001984common_surface_resize(struct wl_resource *resource,
1985 struct wl_resource *seat_resource, uint32_t serial,
1986 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001987{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001988 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001989 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001990 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001991
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001992 if (seat->pointer == NULL ||
1993 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001994 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001995 seat->pointer->focus == NULL)
1996 return;
1997
1998 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1999 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002000 return;
2001
Derek Foreman8fbebbd2015-07-15 13:00:40 -05002002 if (surface_resize(shsurf, seat->pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002003 wl_resource_post_no_memory(resource);
2004}
2005
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002006static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002007shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2008 struct wl_resource *seat_resource, uint32_t serial,
2009 uint32_t edges)
2010{
2011 common_surface_resize(resource, seat_resource, serial, edges);
2012}
2013
2014static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002015busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002016{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002017 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002018 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002020 wl_fixed_t sx, sy;
2021
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 view = weston_compositor_pick_view(pointer->seat->compositor,
2023 pointer->x, pointer->y,
2024 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002025
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002026 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2027 shell_grab_end(grab);
2028 free(grab);
2029 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002030}
2031
2032static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002033busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2034 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002035{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002036 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002037}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002038
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002039static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002040busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041 uint32_t time, uint32_t button, uint32_t state)
2042{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002043 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002044 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002045 struct weston_pointer *pointer = grab->grab.pointer;
2046 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002047
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002048 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002049 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002050 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002051 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002052 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002053 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002054 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002055}
2056
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002057static void
2058busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2059{
2060 struct shell_grab *grab = (struct shell_grab *) base;
2061
2062 shell_grab_end(grab);
2063 free(grab);
2064}
2065
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002066static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002067 busy_cursor_grab_focus,
2068 busy_cursor_grab_motion,
2069 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002070 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002071};
2072
2073static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002074set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002075{
2076 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002077
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002078 if (pointer->grab->interface == &busy_cursor_grab_interface)
2079 return;
2080
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002081 grab = malloc(sizeof *grab);
2082 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002083 return;
2084
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002085 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2086 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002087 /* Mark the shsurf as ungrabbed so that button binding is able
2088 * to move it. */
2089 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002090}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002091
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002092static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002093end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002094{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002095 struct shell_grab *grab;
2096 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002097
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002098 wl_list_for_each(seat, &compositor->seat_list, link) {
2099 if (seat->pointer == NULL)
2100 continue;
2101
2102 grab = (struct shell_grab *) seat->pointer->grab;
2103 if (grab->grab.interface == &busy_cursor_grab_interface &&
2104 wl_resource_get_client(grab->shsurf->resource) == client) {
2105 shell_grab_end(grab);
2106 free(grab);
2107 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002108 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002109}
2110
Scott Moreau9521d5e2012-04-19 13:06:17 -06002111static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002112handle_shell_client_destroy(struct wl_listener *listener, void *data);
2113
2114static int
2115xdg_ping_timeout_handler(void *data)
2116{
2117 struct shell_client *sc = data;
2118 struct weston_seat *seat;
2119 struct shell_surface *shsurf;
2120
2121 /* Client is not responding */
2122 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002123 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2124 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2125 continue;
2126 if (seat->pointer->focus->surface->resource == NULL)
2127 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002128
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002129 shsurf = get_shell_surface(seat->pointer->focus->surface);
2130 if (shsurf &&
2131 wl_resource_get_client(shsurf->resource) == sc->client)
2132 set_busy_cursor(shsurf, seat->pointer);
2133 }
2134
2135 return 1;
2136}
2137
2138static void
2139handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2140{
2141 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002142 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002143 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002144 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002145
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002146 if (sc->unresponsive) {
2147 xdg_ping_timeout_handler(sc);
2148 return;
2149 }
2150
2151 sc->ping_serial = serial;
2152 loop = wl_display_get_event_loop(compositor->wl_display);
2153 if (sc->ping_timer == NULL)
2154 sc->ping_timer =
2155 wl_event_loop_add_timer(loop,
2156 xdg_ping_timeout_handler, sc);
2157 if (sc->ping_timer == NULL)
2158 return;
2159
2160 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2161
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002162 if (shell_surface_is_xdg_surface(shsurf) ||
2163 shell_surface_is_xdg_popup(shsurf))
2164 xdg_shell_send_ping(sc->resource, serial);
2165 else if (shell_surface_is_wl_shell_surface(shsurf))
2166 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002167}
2168
Scott Moreauff1db4a2012-04-17 19:06:18 -06002169static void
2170ping_handler(struct weston_surface *surface, uint32_t serial)
2171{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002172 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002173
2174 if (!shsurf)
2175 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002176 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002177 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002178 if (shsurf->surface == shsurf->shell->grab_surface)
2179 return;
2180
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002181 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002182}
2183
2184static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002185handle_pointer_focus(struct wl_listener *listener, void *data)
2186{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002187 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002189 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002190 uint32_t serial;
2191
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002193 return;
2194
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002196 serial = wl_display_next_serial(compositor->wl_display);
2197 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002198}
2199
2200static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002201shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2202{
2203 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002204 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002205}
2206
2207static void
2208shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2209{
2210 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002211 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002212}
2213
2214static void
2215handle_keyboard_focus(struct wl_listener *listener, void *data)
2216{
2217 struct weston_keyboard *keyboard = data;
2218 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2219
2220 if (seat->focused_surface) {
2221 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2222 if (shsurf)
2223 shell_surface_lose_keyboard_focus(shsurf);
2224 }
2225
2226 seat->focused_surface = keyboard->focus;
2227
2228 if (seat->focused_surface) {
2229 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2230 if (shsurf)
2231 shell_surface_gain_keyboard_focus(shsurf);
2232 }
2233}
2234
2235static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002236shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002237{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002238 if (sc->ping_serial != serial)
2239 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002240
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002241 sc->unresponsive = 0;
2242 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002243
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002244 if (sc->ping_timer) {
2245 wl_event_source_remove(sc->ping_timer);
2246 sc->ping_timer = NULL;
2247 }
2248
2249}
2250
2251static void
2252shell_surface_pong(struct wl_client *client,
2253 struct wl_resource *resource, uint32_t serial)
2254{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002255 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2256 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002257
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002258 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002259}
2260
2261static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002262set_title(struct shell_surface *shsurf, const char *title)
2263{
2264 free(shsurf->title);
2265 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002266 shsurf->surface->timeline.force_refresh = 1;
2267}
2268
2269static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002270set_pid(struct shell_surface *shsurf, pid_t pid)
2271{
2272 /* We have no use for it */
2273}
2274
2275static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002276set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2277{
2278 shsurf->type = t;
2279 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002280}
2281
2282static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002283set_window_geometry(struct shell_surface *shsurf,
2284 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002285{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002286 shsurf->next_geometry.x = x;
2287 shsurf->next_geometry.y = y;
2288 shsurf->next_geometry.width = width;
2289 shsurf->next_geometry.height = height;
2290 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002291}
2292
2293static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002294shell_surface_set_title(struct wl_client *client,
2295 struct wl_resource *resource, const char *title)
2296{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002297 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002298
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002299 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002300}
2301
2302static void
2303shell_surface_set_class(struct wl_client *client,
2304 struct wl_resource *resource, const char *class)
2305{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002306 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002307
2308 free(shsurf->class);
2309 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002310 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002311}
2312
Alex Wu4539b082012-03-01 12:57:46 +08002313static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002314restore_output_mode(struct weston_output *output)
2315{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002316 if (output->original_mode)
2317 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002318}
2319
2320static void
2321restore_all_output_modes(struct weston_compositor *compositor)
2322{
2323 struct weston_output *output;
2324
2325 wl_list_for_each(output, &compositor->output_list, link)
2326 restore_output_mode(output);
2327}
2328
Philip Withnall07926d92013-11-25 18:01:40 +00002329/* The surface will be inserted into the list immediately after the link
2330 * returned by this function (i.e. will be stacked immediately above the
2331 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002332static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002333shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2334{
2335 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002336 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002337
2338 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002339 case SHELL_SURFACE_XWAYLAND:
2340 return &shsurf->shell->fullscreen_layer.view_list;
2341
2342 case SHELL_SURFACE_NONE:
2343 return NULL;
2344
Kristian Høgsbergead52422014-01-01 13:51:52 -08002345 case SHELL_SURFACE_POPUP:
2346 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002347 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002348 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002349 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002350 /* Move the surface to its parent layer so
2351 * that surfaces which are transient for
2352 * fullscreen surfaces don't get hidden by the
2353 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002354
2355 /* TODO: Handle a parent with multiple views */
2356 parent = get_default_view(shsurf->parent);
2357 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002358 return container_of(parent->layer_link.link.prev,
2359 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002360 }
Philip Withnall07926d92013-11-25 18:01:40 +00002361
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002362 /* Move the surface to a normal workspace layer so that surfaces
2363 * which were previously fullscreen or transient are no longer
2364 * rendered on top. */
2365 ws = get_current_workspace(shsurf->shell);
2366 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002367 }
2368
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002369 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002370}
2371
Philip Withnall648a4dd2013-11-25 18:01:44 +00002372static void
2373shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2374{
2375 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002376 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002377
2378 /* Move the child layers to the same workspace as shsurf. They will be
2379 * stacked above shsurf. */
2380 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002381 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002382 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002383 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002384 prev = container_of(shsurf->view->layer_link.link.prev,
2385 struct weston_layer_entry, link);
2386 weston_layer_entry_remove(&child->view->layer_link);
2387 weston_layer_entry_insert(prev,
2388 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002389 weston_view_geometry_dirty(child->view);
2390 weston_surface_damage(child->surface);
2391
2392 /* Recurse. We don’t expect this to recurse very far (if
2393 * at all) because that would imply we have transient
2394 * (or popup) children of transient surfaces, which
2395 * would be unusual. */
2396 shell_surface_update_child_surface_layers(child);
2397 }
2398 }
2399}
2400
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002401/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002402 * geometry to ensure the changes are redrawn.
2403 *
2404 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2405 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002406static void
2407shell_surface_update_layer(struct shell_surface *shsurf)
2408{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002409 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002410
2411 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2412
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002413 if (new_layer_link == NULL)
2414 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002415 if (new_layer_link == &shsurf->view->layer_link)
2416 return;
2417
2418 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002419 weston_layer_entry_remove(&shsurf->view->layer_link);
2420 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002421 weston_view_geometry_dirty(shsurf->view);
2422 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002423
2424 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002425}
2426
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002427static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002428shell_surface_set_parent(struct shell_surface *shsurf,
2429 struct weston_surface *parent)
2430{
2431 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002432
2433 wl_list_remove(&shsurf->children_link);
2434 wl_list_init(&shsurf->children_link);
2435
2436 /* Insert into the parent surface’s child list. */
2437 if (parent != NULL) {
2438 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2439 if (parent_shsurf != NULL)
2440 wl_list_insert(&parent_shsurf->children_list,
2441 &shsurf->children_link);
2442 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002443}
2444
2445static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002446shell_surface_set_output(struct shell_surface *shsurf,
2447 struct weston_output *output)
2448{
2449 struct weston_surface *es = shsurf->surface;
2450
2451 /* get the default output, if the client set it as NULL
2452 check whether the ouput is available */
2453 if (output)
2454 shsurf->output = output;
2455 else if (es->output)
2456 shsurf->output = es->output;
2457 else
2458 shsurf->output = get_default_output(es->compositor);
2459}
2460
2461static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002462surface_clear_next_states(struct shell_surface *shsurf)
2463{
2464 shsurf->next_state.maximized = false;
2465 shsurf->next_state.fullscreen = false;
2466
2467 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2468 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2469 shsurf->state_changed = true;
2470}
2471
2472static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002473set_toplevel(struct shell_surface *shsurf)
2474{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002475 shell_surface_set_parent(shsurf, NULL);
2476 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002477 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002478
2479 /* The layer_link is updated in set_surface_type(),
2480 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002481}
2482
2483static void
2484shell_surface_set_toplevel(struct wl_client *client,
2485 struct wl_resource *resource)
2486{
2487 struct shell_surface *surface = wl_resource_get_user_data(resource);
2488
2489 set_toplevel(surface);
2490}
2491
2492static void
2493set_transient(struct shell_surface *shsurf,
2494 struct weston_surface *parent, int x, int y, uint32_t flags)
2495{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002496 assert(parent != NULL);
2497
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002498 shell_surface_set_parent(shsurf, parent);
2499
2500 surface_clear_next_states(shsurf);
2501
Philip Withnallbecb77e2013-11-25 18:01:30 +00002502 shsurf->transient.x = x;
2503 shsurf->transient.y = y;
2504 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002505
Rafael Antognollied207b42013-12-03 15:35:43 -02002506 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002507 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002508 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002509
2510 /* The layer_link is updated in set_surface_type(),
2511 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512}
2513
2514static void
2515shell_surface_set_transient(struct wl_client *client,
2516 struct wl_resource *resource,
2517 struct wl_resource *parent_resource,
2518 int x, int y, uint32_t flags)
2519{
2520 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2521 struct weston_surface *parent =
2522 wl_resource_get_user_data(parent_resource);
2523
2524 set_transient(shsurf, parent, x, y, flags);
2525}
2526
2527static void
2528set_fullscreen(struct shell_surface *shsurf,
2529 uint32_t method,
2530 uint32_t framerate,
2531 struct weston_output *output)
2532{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002533 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002534 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002535
2536 shsurf->fullscreen_output = shsurf->output;
2537 shsurf->fullscreen.type = method;
2538 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002539
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002540 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541}
2542
2543static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002544weston_view_set_initial_position(struct weston_view *view,
2545 struct desktop_shell *shell);
2546
2547static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002548unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002549{
Philip Withnallf85fe842013-11-25 18:01:37 +00002550 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002551 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2552 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002553 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002554 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002555
Alex Wu4539b082012-03-01 12:57:46 +08002556 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2557 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002558
Alex Wu4539b082012-03-01 12:57:46 +08002559 wl_list_remove(&shsurf->fullscreen.transform.link);
2560 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002561
Jason Ekstranda7af7042013-10-12 22:38:11 -05002562 if (shsurf->fullscreen.black_view)
2563 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2564 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002565
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002566 if (shsurf->saved_position_valid)
2567 weston_view_set_position(shsurf->view,
2568 shsurf->saved_x, shsurf->saved_y);
2569 else
2570 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2571
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002572 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002575 shsurf->saved_rotation_valid = false;
2576 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002577
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002578 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002579}
2580
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002581static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582shell_surface_set_fullscreen(struct wl_client *client,
2583 struct wl_resource *resource,
2584 uint32_t method,
2585 uint32_t framerate,
2586 struct wl_resource *output_resource)
2587{
2588 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2589 struct weston_output *output;
2590
2591 if (output_resource)
2592 output = wl_resource_get_user_data(output_resource);
2593 else
2594 output = NULL;
2595
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002596 shell_surface_set_parent(shsurf, NULL);
2597
Rafael Antognolli03b16592013-12-03 15:35:42 -02002598 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002599 shsurf->next_state.fullscreen = true;
2600 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002601 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002602}
2603
2604static void
2605set_popup(struct shell_surface *shsurf,
2606 struct weston_surface *parent,
2607 struct weston_seat *seat,
2608 uint32_t serial,
2609 int32_t x,
2610 int32_t y)
2611{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002612 assert(parent != NULL);
2613
Philip Withnallbecb77e2013-11-25 18:01:30 +00002614 shsurf->popup.shseat = get_shell_seat(seat);
2615 shsurf->popup.serial = serial;
2616 shsurf->popup.x = x;
2617 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002618
Pekka Paalanenb5026542014-11-12 15:09:24 +02002619 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002620}
2621
2622static void
2623shell_surface_set_popup(struct wl_client *client,
2624 struct wl_resource *resource,
2625 struct wl_resource *seat_resource,
2626 uint32_t serial,
2627 struct wl_resource *parent_resource,
2628 int32_t x, int32_t y, uint32_t flags)
2629{
2630 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002631 struct weston_surface *parent =
2632 wl_resource_get_user_data(parent_resource);
2633
2634 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002635
Rafael Antognolli03b16592013-12-03 15:35:42 -02002636 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002637 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002638 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002639 wl_resource_get_user_data(seat_resource),
2640 serial, x, y);
2641}
2642
2643static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002644unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002645{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002646 /* undo all maximized things here */
2647 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002648
2649 if (shsurf->saved_position_valid)
2650 weston_view_set_position(shsurf->view,
2651 shsurf->saved_x, shsurf->saved_y);
2652 else
2653 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002654
2655 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656 wl_list_insert(&shsurf->view->geometry.transformation_list,
2657 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002658 shsurf->saved_rotation_valid = false;
2659 }
2660
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002661 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002662}
2663
Philip Withnallbecb77e2013-11-25 18:01:30 +00002664static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002665set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002666{
2667 struct shell_surface *shsurf;
2668 struct workspace *current_ws;
2669 struct weston_seat *seat;
2670 struct weston_surface *focus;
2671 struct weston_view *view;
2672
2673 view = get_default_view(surface);
2674 if (!view)
2675 return;
2676
2677 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2678
2679 shsurf = get_shell_surface(surface);
2680 current_ws = get_current_workspace(shsurf->shell);
2681
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002682 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002683 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002684
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002685 drop_focus_state(shsurf->shell, current_ws, view->surface);
2686 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2687 if (!seat->keyboard)
2688 continue;
2689 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2690 if (focus == view->surface)
2691 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002692 }
2693
2694 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002695 weston_view_damage_below(view);
2696}
2697
2698static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002699shell_surface_set_maximized(struct wl_client *client,
2700 struct wl_resource *resource,
2701 struct wl_resource *output_resource)
2702{
2703 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2704 struct weston_output *output;
2705
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002706 surface_clear_next_states(shsurf);
2707 shsurf->next_state.maximized = true;
2708 shsurf->state_changed = true;
2709
Pekka Paalanenb5026542014-11-12 15:09:24 +02002710 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002711 shell_surface_set_parent(shsurf, NULL);
2712
Philip Withnallbecb77e2013-11-25 18:01:30 +00002713 if (output_resource)
2714 output = wl_resource_get_user_data(output_resource);
2715 else
2716 output = NULL;
2717
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002718 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002719
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002720 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002721}
2722
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002723/* This is only ever called from set_surface_type(), so there’s no need to
2724 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002725static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002726reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002727{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002728 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002729 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002730 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002731 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002732
Pekka Paalanen98262232011-12-01 10:42:22 +02002733 return 0;
2734}
2735
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002736static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002737set_full_output(struct shell_surface *shsurf)
2738{
2739 shsurf->saved_x = shsurf->view->geometry.x;
2740 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002741 shsurf->saved_width = shsurf->surface->width;
2742 shsurf->saved_height = shsurf->surface->height;
2743 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002744 shsurf->saved_position_valid = true;
2745
2746 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2747 wl_list_remove(&shsurf->rotation.transform.link);
2748 wl_list_init(&shsurf->rotation.transform.link);
2749 weston_view_geometry_dirty(shsurf->view);
2750 shsurf->saved_rotation_valid = true;
2751 }
2752}
2753
2754static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002755set_surface_type(struct shell_surface *shsurf)
2756{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002757 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002759
Philip Withnallbecb77e2013-11-25 18:01:30 +00002760 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002761
Rafael Antognolli03b16592013-12-03 15:35:42 -02002762 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002763 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002764
2765 switch (shsurf->type) {
2766 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002767 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002768 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002769 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770 weston_view_set_position(shsurf->view,
2771 pev->geometry.x + shsurf->transient.x,
2772 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002773 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002774 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002775
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002776 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002777 weston_view_set_position(shsurf->view, shsurf->transient.x,
2778 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002779 break;
2780
Philip Withnall0f640e22013-11-25 18:01:31 +00002781 case SHELL_SURFACE_POPUP:
2782 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002783 default:
2784 break;
2785 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002786
2787 /* Update the surface’s layer. */
2788 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002789}
2790
Tiago Vignattibe143262012-04-16 17:31:41 +03002791static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002792shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002793{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002794 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002795}
2796
Pekka Paalanen8274d902014-08-06 19:36:51 +03002797static int
2798black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2799{
2800 struct weston_surface *fs_surface = surface->configure_private;
2801 int n;
2802 int rem;
2803 int ret;
2804
2805 n = snprintf(buf, len, "black background surface for ");
2806 if (n < 0)
2807 return n;
2808
2809 rem = (int)len - n;
2810 if (rem < 0)
2811 rem = 0;
2812
2813 if (fs_surface->get_label)
2814 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2815 else
2816 ret = snprintf(buf + n, rem, "<unknown>");
2817
2818 if (ret < 0)
2819 return n;
2820
2821 return n + ret;
2822}
2823
Alex Wu21858432012-04-01 20:13:08 +08002824static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002825black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002826
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002828create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002829 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002830 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002831{
2832 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002834
2835 surface = weston_surface_create(ec);
2836 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002837 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002838 return NULL;
2839 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002840 view = weston_view_create(surface);
2841 if (surface == NULL) {
2842 weston_log("no memory\n");
2843 weston_surface_destroy(surface);
2844 return NULL;
2845 }
Alex Wu4539b082012-03-01 12:57:46 +08002846
Alex Wu21858432012-04-01 20:13:08 +08002847 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002848 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002849 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002850 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002851 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002852 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002853 pixman_region32_fini(&surface->input);
2854 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002855
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002856 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002857 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858
2859 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002860}
2861
Philip Withnalled8f1a92013-11-25 18:01:43 +00002862static void
2863shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2864{
2865 struct weston_output *output = shsurf->fullscreen_output;
2866
Rafael Antognolli03b16592013-12-03 15:35:42 -02002867 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002868
2869 if (!shsurf->fullscreen.black_view)
2870 shsurf->fullscreen.black_view =
2871 create_black_surface(shsurf->surface->compositor,
2872 shsurf->surface,
2873 output->x, output->y,
2874 output->width,
2875 output->height);
2876
2877 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002878 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2879 weston_layer_entry_insert(&shsurf->view->layer_link,
2880 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002881 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2882 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002883
2884 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002885}
2886
Alex Wu4539b082012-03-01 12:57:46 +08002887/* Create black surface and append it to the associated fullscreen surface.
2888 * Handle size dismatch and positioning according to the method. */
2889static void
2890shell_configure_fullscreen(struct shell_surface *shsurf)
2891{
2892 struct weston_output *output = shsurf->fullscreen_output;
2893 struct weston_surface *surface = shsurf->surface;
2894 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002895 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002896 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002897
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002898 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2899 restore_output_mode(output);
2900
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002901 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002902 weston_layer_entry_remove(&shsurf->view->layer_link);
2903 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002904
Philip Withnalled8f1a92013-11-25 18:01:43 +00002905 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002906
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002908 &surf_width, &surf_height);
2909
Alex Wu4539b082012-03-01 12:57:46 +08002910 switch (shsurf->fullscreen.type) {
2911 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002912 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002913 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002914 break;
2915 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002916 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002917 if (output->width == surf_width &&
2918 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002919 weston_view_set_position(shsurf->view,
2920 output->x - surf_x,
2921 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002922 break;
2923 }
2924
Alex Wu4539b082012-03-01 12:57:46 +08002925 matrix = &shsurf->fullscreen.transform.matrix;
2926 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002927
Scott Moreau1bad5db2012-08-18 01:04:05 -06002928 output_aspect = (float) output->width /
2929 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002930 /* XXX: Use surf_width and surf_height here? */
2931 surface_aspect = (float) surface->width /
2932 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002933 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002934 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002935 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002936 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002937 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002938 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002939
Alex Wu4539b082012-03-01 12:57:46 +08002940 weston_matrix_scale(matrix, scale, scale, 1);
2941 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002943 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002944 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2945 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002946 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002947
Alex Wu4539b082012-03-01 12:57:46 +08002948 break;
2949 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002950 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002951 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002952 surf_width * surface->buffer_viewport.buffer.scale,
2953 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002954 shsurf->fullscreen.framerate};
2955
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002956 if (weston_output_mode_switch_to_temporary(output, &mode,
2957 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002958 weston_view_set_position(shsurf->view,
2959 output->x - surf_x,
2960 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002961 shsurf->fullscreen.black_view->surface->width = output->width;
2962 shsurf->fullscreen.black_view->surface->height = output->height;
2963 weston_view_set_position(shsurf->fullscreen.black_view,
2964 output->x - surf_x,
2965 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002966 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002967 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002968 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002969 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002970 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002971 }
Alex Wubd3354b2012-04-17 17:20:49 +08002972 }
Alex Wu4539b082012-03-01 12:57:46 +08002973 break;
2974 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002975 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002976 break;
2977 default:
2978 break;
2979 }
2980}
2981
Alex Wu4539b082012-03-01 12:57:46 +08002982static void
2983shell_map_fullscreen(struct shell_surface *shsurf)
2984{
Alex Wubd3354b2012-04-17 17:20:49 +08002985 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002986}
2987
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002988static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002989set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2990{
2991 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002992 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002993 shsurf->transient.x = x;
2994 shsurf->transient.y = y;
2995 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002996
2997 shell_surface_set_parent(shsurf, NULL);
2998
Pekka Paalanenb5026542014-11-12 15:09:24 +02002999 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08003000 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003001}
3002
Kristian Høgsberg47792412014-05-04 13:47:06 -07003003static void
3004shell_interface_set_fullscreen(struct shell_surface *shsurf,
3005 uint32_t method,
3006 uint32_t framerate,
3007 struct weston_output *output)
3008{
3009 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003010 shsurf->next_state.fullscreen = true;
3011 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003012
3013 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003014}
3015
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003016static struct weston_output *
3017get_focused_output(struct weston_compositor *compositor)
3018{
3019 struct weston_seat *seat;
3020 struct weston_output *output = NULL;
3021
3022 wl_list_for_each(seat, &compositor->seat_list, link) {
3023 /* Priority has touch focus, then pointer and
3024 * then keyboard focus. We should probably have
3025 * three for loops and check frist for touch,
3026 * then for pointer, etc. but unless somebody has some
3027 * objections, I think this is sufficient. */
3028 if (seat->touch && seat->touch->focus)
3029 output = seat->touch->focus->output;
3030 else if (seat->pointer && seat->pointer->focus)
3031 output = seat->pointer->focus->output;
3032 else if (seat->keyboard && seat->keyboard->focus)
3033 output = seat->keyboard->focus->output;
3034
3035 if (output)
3036 break;
3037 }
3038
3039 return output;
3040}
3041
3042static void
3043shell_interface_set_maximized(struct shell_surface *shsurf)
3044{
3045 struct weston_output *output;
3046
3047 surface_clear_next_states(shsurf);
3048 shsurf->next_state.maximized = true;
3049 shsurf->state_changed = true;
3050 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3051
3052 if (!weston_surface_is_mapped(shsurf->surface))
3053 output = get_focused_output(shsurf->surface->compositor);
3054 else
3055 output = shsurf->surface->output;
3056
3057 shell_surface_set_output(shsurf, output);
3058 send_configure_for_surface(shsurf);
3059}
3060
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003061static int
3062shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3063{
Derek Foreman794fa0e2015-07-15 13:00:38 -05003064 return surface_move(shsurf, ws->pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003065}
3066
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003067static int
3068shell_interface_resize(struct shell_surface *shsurf,
3069 struct weston_seat *ws,
3070 uint32_t edges)
3071{
3072 return surface_resize(shsurf, ws->pointer, edges);
3073}
3074
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003075static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076
3077static void
3078destroy_shell_seat(struct wl_listener *listener, void *data)
3079{
3080 struct shell_seat *shseat =
3081 container_of(listener,
3082 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003083 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003084
3085 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003086 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003087 shseat->popup_grab.client = NULL;
3088
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003089 wl_list_for_each_safe(shsurf, next,
3090 &shseat->popup_grab.surfaces_list,
3091 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003092 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003093 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003094 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003095 }
3096
3097 wl_list_remove(&shseat->seat_destroy_listener.link);
3098 free(shseat);
3099}
3100
Jason Ekstrand024177c2014-04-21 19:42:58 -05003101static void
3102shell_seat_caps_changed(struct wl_listener *listener, void *data)
3103{
3104 struct shell_seat *seat;
3105
3106 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3107
3108 if (seat->seat->keyboard &&
3109 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3110 wl_signal_add(&seat->seat->keyboard->focus_signal,
3111 &seat->keyboard_focus_listener);
3112 } else if (!seat->seat->keyboard) {
3113 wl_list_init(&seat->keyboard_focus_listener.link);
3114 }
3115
3116 if (seat->seat->pointer &&
3117 wl_list_empty(&seat->pointer_focus_listener.link)) {
3118 wl_signal_add(&seat->seat->pointer->focus_signal,
3119 &seat->pointer_focus_listener);
3120 } else if (!seat->seat->pointer) {
3121 wl_list_init(&seat->pointer_focus_listener.link);
3122 }
3123}
3124
Giulio Camuffo5085a752013-03-25 21:42:45 +01003125static struct shell_seat *
3126create_shell_seat(struct weston_seat *seat)
3127{
3128 struct shell_seat *shseat;
3129
3130 shseat = calloc(1, sizeof *shseat);
3131 if (!shseat) {
3132 weston_log("no memory to allocate shell seat\n");
3133 return NULL;
3134 }
3135
3136 shseat->seat = seat;
3137 wl_list_init(&shseat->popup_grab.surfaces_list);
3138
3139 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3140 wl_signal_add(&seat->destroy_signal,
3141 &shseat->seat_destroy_listener);
3142
Jason Ekstrand024177c2014-04-21 19:42:58 -05003143 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3144 wl_list_init(&shseat->keyboard_focus_listener.link);
3145
3146 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3147 wl_list_init(&shseat->pointer_focus_listener.link);
3148
3149 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3150 wl_signal_add(&seat->updated_caps_signal,
3151 &shseat->caps_changed_listener);
3152 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3153
Giulio Camuffo5085a752013-03-25 21:42:45 +01003154 return shseat;
3155}
3156
3157static struct shell_seat *
3158get_shell_seat(struct weston_seat *seat)
3159{
3160 struct wl_listener *listener;
3161
3162 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003163 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003164
3165 return container_of(listener,
3166 struct shell_seat, seat_destroy_listener);
3167}
3168
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003169static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003170popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003171{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003172 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003173 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003174 struct shell_seat *shseat =
3175 container_of(grab, struct shell_seat, popup_grab.grab);
3176 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003177 wl_fixed_t sx, sy;
3178
Jason Ekstranda7af7042013-10-12 22:38:11 -05003179 view = weston_compositor_pick_view(pointer->seat->compositor,
3180 pointer->x, pointer->y,
3181 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003182
Jason Ekstranda7af7042013-10-12 22:38:11 -05003183 if (view && view->surface->resource &&
3184 wl_resource_get_client(view->surface->resource) == client) {
3185 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003186 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003187 weston_pointer_set_focus(pointer, NULL,
3188 wl_fixed_from_int(0),
3189 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003190 }
3191}
3192
3193static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003194popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3195 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003196{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003197 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003198 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003199 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003200
Jonas Ådahl61917c82014-08-11 22:44:02 +02003201 if (pointer->focus) {
3202 weston_view_from_global_fixed(pointer->focus, x, y,
3203 &pointer->sx, &pointer->sy);
3204 }
3205
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003206 weston_pointer_move(pointer, x, y);
3207
Neil Roberts96d790e2013-09-19 17:32:00 +01003208 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003209 weston_view_from_global_fixed(pointer->focus,
3210 pointer->x, pointer->y,
3211 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003212 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003213 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003214}
3215
3216static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003217popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003218 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003219{
3220 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003221 struct shell_seat *shseat =
3222 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003223 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003224 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003225 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003226 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003227
Neil Roberts96d790e2013-09-19 17:32:00 +01003228 resource_list = &grab->pointer->focus_resource_list;
3229 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003230 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003231 wl_resource_for_each(resource, resource_list) {
3232 wl_pointer_send_button(resource, serial,
3233 time, button, state);
3234 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003235 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003236 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003237 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003238 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003239 }
3240
Daniel Stone4dbadb12012-05-30 16:31:51 +01003241 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003242 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003243}
3244
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003245static void
3246popup_grab_cancel(struct weston_pointer_grab *grab)
3247{
3248 popup_grab_end(grab->pointer);
3249}
3250
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003251static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003252 popup_grab_focus,
3253 popup_grab_motion,
3254 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003255 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003256};
3257
3258static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003259touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3260 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3261{
3262 struct wl_resource *resource;
3263 struct shell_seat *shseat =
3264 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3265 struct wl_display *display = shseat->seat->compositor->wl_display;
3266 uint32_t serial;
3267 struct wl_list *resource_list;
3268
3269 resource_list = &grab->touch->focus_resource_list;
3270 if (!wl_list_empty(resource_list)) {
3271 serial = wl_display_get_serial(display);
3272 wl_resource_for_each(resource, resource_list) {
3273 wl_touch_send_down(resource, serial, time,
3274 grab->touch->focus->surface->resource,
3275 touch_id, sx, sy);
3276 }
3277 }
3278}
3279
3280static void
3281touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3282{
3283 struct wl_resource *resource;
3284 struct shell_seat *shseat =
3285 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3286 struct wl_display *display = shseat->seat->compositor->wl_display;
3287 uint32_t serial;
3288 struct wl_list *resource_list;
3289
3290 resource_list = &grab->touch->focus_resource_list;
3291 if (!wl_list_empty(resource_list)) {
3292 serial = wl_display_get_serial(display);
3293 wl_resource_for_each(resource, resource_list) {
3294 wl_touch_send_up(resource, serial, time, touch_id);
3295 }
3296 }
3297}
3298
3299static void
3300touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3301 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3302{
3303 struct wl_resource *resource;
3304 struct wl_list *resource_list;
3305
3306 resource_list = &grab->touch->focus_resource_list;
3307 if (!wl_list_empty(resource_list)) {
3308 wl_resource_for_each(resource, resource_list) {
3309 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3310 }
3311 }
3312}
3313
3314static void
3315touch_popup_grab_frame(struct weston_touch_grab *grab)
3316{
3317}
3318
3319static void
3320touch_popup_grab_cancel(struct weston_touch_grab *grab)
3321{
3322 touch_popup_grab_end(grab->touch);
3323}
3324
3325static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3326 touch_popup_grab_down,
3327 touch_popup_grab_up,
3328 touch_popup_grab_motion,
3329 touch_popup_grab_frame,
3330 touch_popup_grab_cancel,
3331};
3332
3333static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003334shell_surface_send_popup_done(struct shell_surface *shsurf)
3335{
3336 if (shell_surface_is_wl_shell_surface(shsurf))
3337 wl_shell_surface_send_popup_done(shsurf->resource);
3338 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003339 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003340}
3341
3342static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003343popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003344{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003345 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003346 struct shell_seat *shseat =
3347 container_of(grab, struct shell_seat, popup_grab.grab);
3348 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003349 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003350
3351 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003352 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003353 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003354 shseat->popup_grab.grab.interface = NULL;
3355 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003356 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003357 wl_list_for_each_safe(shsurf, next,
3358 &shseat->popup_grab.surfaces_list,
3359 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003360 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003361 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003362 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003363 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003364 wl_list_init(&shseat->popup_grab.surfaces_list);
3365 }
3366}
3367
3368static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003369touch_popup_grab_end(struct weston_touch *touch)
3370{
3371 struct weston_touch_grab *grab = touch->grab;
3372 struct shell_seat *shseat =
3373 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3374 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003375 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003376
3377 if (touch->grab->interface == &touch_popup_grab_interface) {
3378 weston_touch_end_grab(grab->touch);
3379 shseat->popup_grab.client = NULL;
3380 shseat->popup_grab.touch_grab.interface = NULL;
3381 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3382 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003383 wl_list_for_each_safe(shsurf, next,
3384 &shseat->popup_grab.surfaces_list,
3385 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003386 shell_surface_send_popup_done(shsurf);
3387 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003388 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003389 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003390 wl_list_init(&shseat->popup_grab.surfaces_list);
3391 }
3392}
3393
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003394static struct shell_surface *
3395get_top_popup(struct shell_seat *shseat)
3396{
3397 struct shell_surface *shsurf;
3398
3399 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3400 return NULL;
3401 } else {
3402 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3403 struct shell_surface,
3404 popup.grab_link);
3405 return shsurf;
3406 }
3407}
3408
3409static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003410add_popup_grab(struct shell_surface *shsurf,
3411 struct shell_seat *shseat,
3412 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003413{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003414 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003415 struct shell_surface *parent, *top_surface;
3416
3417 parent = get_shell_surface(shsurf->parent);
3418 top_surface = get_top_popup(shseat);
3419 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003420 (!parent ||
3421 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003422 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003423 wl_resource_post_error(shsurf->owner_resource,
3424 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003425 "xdg_popup was not created on the "
3426 "topmost popup");
3427 return -1;
3428 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003429
3430 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003431 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003432 shseat->popup_grab.client =
3433 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003434
3435 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003436 shseat->popup_grab.grab.interface =
3437 &popup_grab_interface;
3438
3439 /* We must make sure here that this popup was opened
3440 * after a mouse press, and not just by moving around
3441 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003442 if (shseat->seat->pointer->button_count > 0)
3443 shseat->popup_grab.initial_up = 0;
3444 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003445 shseat->popup_grab.touch_grab.interface =
3446 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003447 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003448
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003449 wl_list_insert(&shseat->popup_grab.surfaces_list,
3450 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003451
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003452 if (type == POINTER) {
3453 weston_pointer_start_grab(seat->pointer,
3454 &shseat->popup_grab.grab);
3455 } else if (type == TOUCH) {
3456 weston_touch_start_grab(seat->touch,
3457 &shseat->popup_grab.touch_grab);
3458 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003459 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003460 wl_list_insert(&shseat->popup_grab.surfaces_list,
3461 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003462 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003463
3464 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003465}
3466
3467static void
3468remove_popup_grab(struct shell_surface *shsurf)
3469{
3470 struct shell_seat *shseat = shsurf->popup.shseat;
3471
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003472 if (shell_surface_is_xdg_popup(shsurf) &&
3473 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003474 wl_resource_post_error(shsurf->owner_resource,
3475 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003476 "xdg_popup was destroyed while it was "
3477 "not the topmost popup.");
3478 return;
3479 }
3480
Giulio Camuffo5085a752013-03-25 21:42:45 +01003481 wl_list_remove(&shsurf->popup.grab_link);
3482 wl_list_init(&shsurf->popup.grab_link);
3483 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003484 if (shseat->popup_grab.type == POINTER) {
3485 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3486 shseat->popup_grab.grab.interface = NULL;
3487 } else if (shseat->popup_grab.type == TOUCH) {
3488 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3489 shseat->popup_grab.touch_grab.interface = NULL;
3490 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003491 }
3492}
3493
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003494static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003495shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003496{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003497 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003498 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003499
Jason Ekstranda7af7042013-10-12 22:38:11 -05003500 shsurf->surface->output = parent_view->output;
3501 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003502
Jason Ekstranda7af7042013-10-12 22:38:11 -05003503 weston_view_set_transform_parent(shsurf->view, parent_view);
3504 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3505 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003506
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003507 if (shseat->seat->pointer &&
3508 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003509 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3510 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003511 } else if (shseat->seat->touch &&
3512 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003513 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3514 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003515 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003516 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003517 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003518 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003519
3520 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003521}
3522
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003523static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003524 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003525 shell_surface_move,
3526 shell_surface_resize,
3527 shell_surface_set_toplevel,
3528 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003529 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003530 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003531 shell_surface_set_maximized,
3532 shell_surface_set_title,
3533 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003534};
3535
3536static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003537destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003538{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003539 struct shell_surface *child, *next;
3540
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003541 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3542
Giulio Camuffo5085a752013-03-25 21:42:45 +01003543 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3544 remove_popup_grab(shsurf);
3545 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003546
Alex Wubd3354b2012-04-17 17:20:49 +08003547 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003548 shell_surface_is_top_fullscreen(shsurf))
3549 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003550
Jason Ekstranda7af7042013-10-12 22:38:11 -05003551 if (shsurf->fullscreen.black_view)
3552 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003553
Alex Wubd3354b2012-04-17 17:20:49 +08003554 /* As destroy_resource() use wl_list_for_each_safe(),
3555 * we can always remove the listener.
3556 */
3557 wl_list_remove(&shsurf->surface_destroy_listener.link);
3558 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003559 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003560 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003561
Jason Ekstranda7af7042013-10-12 22:38:11 -05003562 weston_view_destroy(shsurf->view);
3563
Philip Withnall648a4dd2013-11-25 18:01:44 +00003564 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003565 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3566 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003567
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003568 wl_list_remove(&shsurf->link);
3569 free(shsurf);
3570}
3571
3572static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003573shell_destroy_shell_surface(struct wl_resource *resource)
3574{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003575 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003576
Bryan Caina46b9462014-04-04 17:41:24 -05003577 if (!wl_list_empty(&shsurf->popup.grab_link))
3578 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003579 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003580 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003581}
3582
3583static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003584shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003585{
3586 struct shell_surface *shsurf = container_of(listener,
3587 struct shell_surface,
3588 surface_destroy_listener);
3589
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003590 if (shsurf->resource)
3591 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003592
3593 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003594}
3595
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003596static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003597fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003598{
3599 struct shell_surface *shsurf = data;
3600
3601 weston_surface_destroy(shsurf->surface);
3602}
3603
3604static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003605fade_out_done(struct weston_view_animation *animation, void *data)
3606{
3607 struct shell_surface *shsurf = data;
3608 struct wl_event_loop *loop;
3609
3610 loop = wl_display_get_event_loop(
3611 shsurf->surface->compositor->wl_display);
3612
3613 if (!shsurf->destroying) {
3614 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3615 shsurf->destroying = true;
3616 }
3617}
3618
3619static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003620handle_resource_destroy(struct wl_listener *listener, void *data)
3621{
3622 struct shell_surface *shsurf =
3623 container_of(listener, struct shell_surface,
3624 resource_destroy_listener);
3625
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003626 if (!weston_surface_is_mapped(shsurf->surface))
3627 return;
3628
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003629 shsurf->surface->ref_count++;
3630
3631 pixman_region32_fini(&shsurf->surface->pending.input);
3632 pixman_region32_init(&shsurf->surface->pending.input);
3633 pixman_region32_fini(&shsurf->surface->input);
3634 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003635 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3636 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3637 fade_out_done, shsurf);
3638 } else {
3639 weston_surface_destroy(shsurf->surface);
3640 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003641}
3642
3643static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003644shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003645
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003646struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003647get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003648{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003649 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003650 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003651 else
3652 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003653}
3654
Rafael Antognollie2a34552013-12-03 15:35:45 -02003655static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003656create_common_surface(struct shell_client *owner, void *shell,
3657 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003658 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003659{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003660 struct shell_surface *shsurf;
3661
Pekka Paalanen50b67472014-10-01 15:02:41 +03003662 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003663
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003664 shsurf = calloc(1, sizeof *shsurf);
3665 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003666 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003667 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003668 }
3669
Jason Ekstranda7af7042013-10-12 22:38:11 -05003670 shsurf->view = weston_view_create(surface);
3671 if (!shsurf->view) {
3672 weston_log("no memory to allocate shell surface\n");
3673 free(shsurf);
3674 return NULL;
3675 }
3676
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003677 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003678 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003679 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003680
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003681 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3682 wl_resource_add_destroy_listener(surface->resource,
3683 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003684 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003685
Tiago Vignattibc052c92012-04-19 16:18:18 +03003686 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003687 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003688 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003689 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003690 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003691 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003692 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3693 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003694 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003695 wl_list_init(&shsurf->fullscreen.transform.link);
3696
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003697 shsurf->output = get_default_output(shsurf->shell->compositor);
3698
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003699 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003700 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003701 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003702 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003703
3704 /* init link so its safe to always remove it in destroy_shell_surface */
3705 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003706 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003707
Pekka Paalanen460099f2012-01-20 16:48:25 +02003708 /* empty when not in use */
3709 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003710 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003711
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003712 wl_list_init(&shsurf->workspace_transform.link);
3713
Philip Withnall648a4dd2013-11-25 18:01:44 +00003714 wl_list_init(&shsurf->children_link);
3715 wl_list_init(&shsurf->children_list);
3716 shsurf->parent = NULL;
3717
Pekka Paalanenb5026542014-11-12 15:09:24 +02003718 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003719
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003720 shsurf->client = client;
3721
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003722 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003723}
3724
Rafael Antognollie2a34552013-12-03 15:35:45 -02003725static struct shell_surface *
3726create_shell_surface(void *shell, struct weston_surface *surface,
3727 const struct weston_shell_client *client)
3728{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003729 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003730}
3731
Jason Ekstranda7af7042013-10-12 22:38:11 -05003732static struct weston_view *
3733get_primary_view(void *shell, struct shell_surface *shsurf)
3734{
3735 return shsurf->view;
3736}
3737
Tiago Vignattibc052c92012-04-19 16:18:18 +03003738static void
3739shell_get_shell_surface(struct wl_client *client,
3740 struct wl_resource *resource,
3741 uint32_t id,
3742 struct wl_resource *surface_resource)
3743{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003744 struct weston_surface *surface =
3745 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003746 struct shell_client *sc = wl_resource_get_user_data(resource);
3747 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003748 struct shell_surface *shsurf;
3749
Pekka Paalanen50b67472014-10-01 15:02:41 +03003750 if (weston_surface_set_role(surface, "wl_shell_surface",
3751 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003752 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003753
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003754 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003755 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003756 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003757 return;
3758 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003759
Jason Ekstranda85118c2013-06-27 20:17:02 -05003760 shsurf->resource =
3761 wl_resource_create(client,
3762 &wl_shell_surface_interface, 1, id);
3763 wl_resource_set_implementation(shsurf->resource,
3764 &shell_surface_implementation,
3765 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003766 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003767}
3768
Rafael Antognollie2a34552013-12-03 15:35:45 -02003769static bool
3770shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3771{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003772 /* A shell surface without a resource is created from xwayland
3773 * and is considered a wl_shell surface for now. */
3774
3775 return shsurf->resource == NULL ||
3776 wl_resource_instance_of(shsurf->resource,
3777 &wl_shell_surface_interface,
3778 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003779}
3780
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003781static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003782 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003783};
3784
Rafael Antognollie2a34552013-12-03 15:35:45 -02003785/****************************
3786 * xdg-shell implementation */
3787
3788static void
3789xdg_surface_destroy(struct wl_client *client,
3790 struct wl_resource *resource)
3791{
3792 wl_resource_destroy(resource);
3793}
3794
3795static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003796xdg_surface_set_parent(struct wl_client *client,
3797 struct wl_resource *resource,
3798 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003799{
3800 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003801 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003802
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003803 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003804 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003805 shell_surface_set_parent(shsurf, parent->surface);
3806 } else {
3807 shell_surface_set_parent(shsurf, NULL);
3808 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003809}
3810
3811static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003812xdg_surface_set_app_id(struct wl_client *client,
3813 struct wl_resource *resource,
3814 const char *app_id)
3815{
3816 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3817
3818 free(shsurf->class);
3819 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003820 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003821}
3822
3823static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003824xdg_surface_show_window_menu(struct wl_client *client,
3825 struct wl_resource *surface_resource,
3826 struct wl_resource *seat_resource,
3827 uint32_t serial,
3828 int32_t x,
3829 int32_t y)
3830{
3831 /* TODO */
3832}
3833
3834static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003835xdg_surface_set_title(struct wl_client *client,
3836 struct wl_resource *resource, const char *title)
3837{
3838 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3839
3840 set_title(shsurf, title);
3841}
3842
3843static void
3844xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3845 struct wl_resource *seat_resource, uint32_t serial)
3846{
3847 common_surface_move(resource, seat_resource, serial);
3848}
3849
3850static void
3851xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3852 struct wl_resource *seat_resource, uint32_t serial,
3853 uint32_t edges)
3854{
3855 common_surface_resize(resource, seat_resource, serial, edges);
3856}
3857
3858static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003859xdg_surface_ack_configure(struct wl_client *client,
3860 struct wl_resource *resource,
3861 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003862{
3863 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3864
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003865 if (shsurf->state_requested) {
3866 shsurf->next_state = shsurf->requested_state;
3867 shsurf->state_changed = true;
3868 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003869 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003870}
3871
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003872static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003873xdg_surface_set_window_geometry(struct wl_client *client,
3874 struct wl_resource *resource,
3875 int32_t x,
3876 int32_t y,
3877 int32_t width,
3878 int32_t height)
3879{
3880 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3881
3882 set_window_geometry(shsurf, x, y, width, height);
3883}
3884
3885static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003886xdg_surface_set_maximized(struct wl_client *client,
3887 struct wl_resource *resource)
3888{
3889 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003890 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003891
3892 shsurf->state_requested = true;
3893 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003894
3895 if (!weston_surface_is_mapped(shsurf->surface))
3896 output = get_focused_output(shsurf->surface->compositor);
3897 else
3898 output = shsurf->surface->output;
3899
3900 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003901 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003902}
3903
3904static void
3905xdg_surface_unset_maximized(struct wl_client *client,
3906 struct wl_resource *resource)
3907{
3908 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3909
3910 shsurf->state_requested = true;
3911 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003912 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003913}
3914
3915static void
3916xdg_surface_set_fullscreen(struct wl_client *client,
3917 struct wl_resource *resource,
3918 struct wl_resource *output_resource)
3919{
3920 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3921 struct weston_output *output;
3922
3923 shsurf->state_requested = true;
3924 shsurf->requested_state.fullscreen = true;
3925
3926 if (output_resource)
3927 output = wl_resource_get_user_data(output_resource);
3928 else
3929 output = NULL;
3930
Marek Chalupa052917a2014-09-02 11:35:12 +02003931 /* handle clients launching in fullscreen */
3932 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3933 /* Set the output to the one that has focus currently. */
3934 assert(shsurf->surface);
3935 output = get_focused_output(shsurf->surface->compositor);
3936 }
3937
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003938 shell_surface_set_output(shsurf, output);
3939 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003940
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003941 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003942}
3943
3944static void
3945xdg_surface_unset_fullscreen(struct wl_client *client,
3946 struct wl_resource *resource)
3947{
3948 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3949
3950 shsurf->state_requested = true;
3951 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003952 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003953}
3954
3955static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003956xdg_surface_set_minimized(struct wl_client *client,
3957 struct wl_resource *resource)
3958{
3959 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3960
3961 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3962 return;
3963
3964 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003965 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003966}
3967
Rafael Antognollie2a34552013-12-03 15:35:45 -02003968static const struct xdg_surface_interface xdg_surface_implementation = {
3969 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003970 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003971 xdg_surface_set_title,
3972 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003973 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003974 xdg_surface_move,
3975 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003976 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003977 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003978 xdg_surface_set_maximized,
3979 xdg_surface_unset_maximized,
3980 xdg_surface_set_fullscreen,
3981 xdg_surface_unset_fullscreen,
3982 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003983};
3984
3985static void
3986xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003987 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003988{
3989 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003990 uint32_t *s;
3991 struct wl_array states;
3992 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003993
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003994 assert(shsurf);
3995
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003996 if (!shsurf->resource)
3997 return;
3998
3999 wl_array_init(&states);
4000 if (shsurf->requested_state.fullscreen) {
4001 s = wl_array_add(&states, sizeof *s);
4002 *s = XDG_SURFACE_STATE_FULLSCREEN;
4003 } else if (shsurf->requested_state.maximized) {
4004 s = wl_array_add(&states, sizeof *s);
4005 *s = XDG_SURFACE_STATE_MAXIMIZED;
4006 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004007 if (shsurf->resize_edges != 0) {
4008 s = wl_array_add(&states, sizeof *s);
4009 *s = XDG_SURFACE_STATE_RESIZING;
4010 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004011 if (shsurf->focus_count > 0) {
4012 s = wl_array_add(&states, sizeof *s);
4013 *s = XDG_SURFACE_STATE_ACTIVATED;
4014 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004015
4016 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4017 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4018
4019 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004020}
4021
4022static const struct weston_shell_client xdg_client = {
4023 xdg_send_configure
4024};
4025
4026static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004027xdg_shell_destroy(struct wl_client *client,
4028 struct wl_resource *resource)
4029{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004030 struct shell_client *sc = wl_resource_get_user_data(resource);
4031 struct wl_resource *shsurf_resource;
4032 struct shell_surface *shsurf;
4033
4034 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4035 shsurf = wl_resource_get_user_data(shsurf_resource);
4036 if (shsurf->owner_resource == resource) {
4037 wl_resource_post_error(
4038 resource,
4039 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4040 "not all child surface objects destroyed");
4041 return;
4042 }
4043 }
4044
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004045 wl_resource_destroy(resource);
4046}
4047
4048static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004049xdg_use_unstable_version(struct wl_client *client,
4050 struct wl_resource *resource,
4051 int32_t version)
4052{
4053 if (version > 1) {
4054 wl_resource_post_error(resource,
4055 1,
4056 "xdg-shell:: version not implemented yet.");
4057 return;
4058 }
4059}
4060
4061static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004062create_xdg_surface(struct shell_client *owner, void *shell,
4063 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004064 const struct weston_shell_client *client)
4065{
4066 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004067
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004068 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004069 if (!shsurf)
4070 return NULL;
4071
Pekka Paalanenb5026542014-11-12 15:09:24 +02004072 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004073
4074 return shsurf;
4075}
4076
4077static void
4078xdg_get_xdg_surface(struct wl_client *client,
4079 struct wl_resource *resource,
4080 uint32_t id,
4081 struct wl_resource *surface_resource)
4082{
4083 struct weston_surface *surface =
4084 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004085 struct shell_client *sc = wl_resource_get_user_data(resource);
4086 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004087 struct shell_surface *shsurf;
4088
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004089 shsurf = get_shell_surface(surface);
4090 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4091 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4092 "This wl_surface is already an "
4093 "xdg_surface");
4094 return;
4095 }
4096
Pekka Paalanen50b67472014-10-01 15:02:41 +03004097 if (weston_surface_set_role(surface, "xdg_surface",
4098 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004099 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004100
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004101 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004103 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004104 return;
4105 }
4106
4107 shsurf->resource =
4108 wl_resource_create(client,
4109 &xdg_surface_interface, 1, id);
4110 wl_resource_set_implementation(shsurf->resource,
4111 &xdg_surface_implementation,
4112 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004113 shsurf->owner_resource = resource;
4114 wl_list_insert(&sc->surface_list,
4115 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004116}
4117
4118static bool
4119shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4120{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004121 return shsurf->resource &&
4122 wl_resource_instance_of(shsurf->resource,
4123 &xdg_surface_interface,
4124 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004125}
4126
4127/* xdg-popup implementation */
4128
4129static void
4130xdg_popup_destroy(struct wl_client *client,
4131 struct wl_resource *resource)
4132{
4133 wl_resource_destroy(resource);
4134}
4135
Rafael Antognollie2a34552013-12-03 15:35:45 -02004136static const struct xdg_popup_interface xdg_popup_implementation = {
4137 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004138};
4139
4140static void
4141xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004142 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004143{
4144}
4145
4146static const struct weston_shell_client xdg_popup_client = {
4147 xdg_popup_send_configure
4148};
4149
4150static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004151create_xdg_popup(struct shell_client *owner, void *shell,
4152 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004153 const struct weston_shell_client *client,
4154 struct weston_surface *parent,
4155 struct shell_seat *seat,
4156 uint32_t serial,
4157 int32_t x, int32_t y)
4158{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004159 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004160
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004161 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004162 if (!shsurf)
4163 return NULL;
4164
Pekka Paalanenb5026542014-11-12 15:09:24 +02004165 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004166 shsurf->popup.shseat = seat;
4167 shsurf->popup.serial = serial;
4168 shsurf->popup.x = x;
4169 shsurf->popup.y = y;
4170 shell_surface_set_parent(shsurf, parent);
4171
4172 return shsurf;
4173}
4174
4175static void
4176xdg_get_xdg_popup(struct wl_client *client,
4177 struct wl_resource *resource,
4178 uint32_t id,
4179 struct wl_resource *surface_resource,
4180 struct wl_resource *parent_resource,
4181 struct wl_resource *seat_resource,
4182 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004183 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004184{
4185 struct weston_surface *surface =
4186 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004187 struct shell_client *sc = wl_resource_get_user_data(resource);
4188 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004190 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004191 struct weston_surface *parent;
4192 struct shell_seat *seat;
4193
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004194 shsurf = get_shell_surface(surface);
4195 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4196 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4197 "This wl_surface is already an "
4198 "xdg_popup");
4199 return;
4200 }
4201
Pekka Paalanen50b67472014-10-01 15:02:41 +03004202 if (weston_surface_set_role(surface, "xdg_popup",
4203 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004204 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004205
4206 if (!parent_resource) {
4207 wl_resource_post_error(surface_resource,
4208 WL_DISPLAY_ERROR_INVALID_OBJECT,
4209 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004210 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004211 }
4212
4213 parent = wl_resource_get_user_data(parent_resource);
4214 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4215
Jonas Ådahlee45a552015-03-18 16:37:47 +08004216 /* Verify that we are creating the top most popup when mapping,
4217 * as it's not until then we know whether it was mapped as most
4218 * top level or not. */
4219
4220 parent_shsurf = get_shell_surface(parent);
4221 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4222 !shell_surface_is_xdg_surface(parent_shsurf)) {
4223 wl_resource_post_error(resource,
4224 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4225 "xdg_popup parent was invalid");
4226 return;
4227 }
4228
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004229 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004230 parent, seat, serial, x, y);
4231 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004232 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004233 return;
4234 }
4235
4236 shsurf->resource =
4237 wl_resource_create(client,
4238 &xdg_popup_interface, 1, id);
4239 wl_resource_set_implementation(shsurf->resource,
4240 &xdg_popup_implementation,
4241 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004242 shsurf->owner_resource = resource;
4243 wl_list_insert(&sc->surface_list,
4244 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004245}
4246
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004247static void
4248xdg_pong(struct wl_client *client,
4249 struct wl_resource *resource, uint32_t serial)
4250{
4251 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004252
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004253 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004254}
4255
Rafael Antognollie2a34552013-12-03 15:35:45 -02004256static bool
4257shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4258{
4259 return wl_resource_instance_of(shsurf->resource,
4260 &xdg_popup_interface,
4261 &xdg_popup_implementation);
4262}
4263
4264static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004265 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004266 xdg_use_unstable_version,
4267 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004268 xdg_get_xdg_popup,
4269 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004270};
4271
4272static int
4273xdg_shell_unversioned_dispatch(const void *implementation,
4274 void *_target, uint32_t opcode,
4275 const struct wl_message *message,
4276 union wl_argument *args)
4277{
4278 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004279 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004280
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004281 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004282 wl_resource_post_error(resource,
4283 WL_DISPLAY_ERROR_INVALID_OBJECT,
4284 "must call use_unstable_version first");
4285 return 0;
4286 }
4287
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004288#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004289
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004290 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4291 "shell implementation doesn't match protocol version");
4292
Rafael Antognollie2a34552013-12-03 15:35:45 -02004293 if (args[0].i != XDG_SERVER_VERSION) {
4294 wl_resource_post_error(resource,
4295 WL_DISPLAY_ERROR_INVALID_OBJECT,
4296 "incompatible version, server is %d "
4297 "client wants %d",
4298 XDG_SERVER_VERSION, args[0].i);
4299 return 0;
4300 }
4301
4302 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004303 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004304
4305 return 1;
4306}
4307
4308/* end of xdg-shell implementation */
4309/***********************************/
4310
Kristian Høgsberg07937562011-04-12 17:25:42 -04004311static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004312shell_fade(struct desktop_shell *shell, enum fade_type type);
4313
Pekka Paalanen77346a62011-11-30 16:26:35 +02004314static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004315configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004316{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004317 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004318
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004319 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004320 if (v->output == ev->output && v != ev) {
4321 weston_view_unmap(v);
4322 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004323 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004324 }
4325 }
4326
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004327 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004328
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004329 if (wl_list_empty(&ev->layer_link.link)) {
4330 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004331 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004332 }
4333}
4334
Pekka Paalanen8274d902014-08-06 19:36:51 +03004335static int
4336background_get_label(struct weston_surface *surface, char *buf, size_t len)
4337{
4338 return snprintf(buf, len, "background for output %s",
4339 surface->output->name);
4340}
4341
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004342static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004343background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004344{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004345 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004346 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004347
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 view = container_of(es->views.next, struct weston_view, surface_link);
4349
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004350 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004351}
4352
4353static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004354desktop_shell_set_background(struct wl_client *client,
4355 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004356 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004357 struct wl_resource *surface_resource)
4358{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004359 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004360 struct weston_surface *surface =
4361 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004362 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004363
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004364 if (surface->configure) {
4365 wl_resource_post_error(surface_resource,
4366 WL_DISPLAY_ERROR_INVALID_OBJECT,
4367 "surface role already assigned");
4368 return;
4369 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004370
Jason Ekstranda7af7042013-10-12 22:38:11 -05004371 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4372 weston_view_destroy(view);
4373 view = weston_view_create(surface);
4374
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004375 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004376 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004377 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004378 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004379 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004380 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004381 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004382 surface->output->width,
4383 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004384}
4385
Pekka Paalanen8274d902014-08-06 19:36:51 +03004386static int
4387panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4388{
4389 return snprintf(buf, len, "panel for output %s",
4390 surface->output->name);
4391}
4392
Kristian Høgsberg75840622011-09-06 13:48:16 -04004393static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004394panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004395{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004396 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004397 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004398
Jason Ekstranda7af7042013-10-12 22:38:11 -05004399 view = container_of(es->views.next, struct weston_view, surface_link);
4400
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004401 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004402}
4403
4404static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004405desktop_shell_set_panel(struct wl_client *client,
4406 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004407 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004408 struct wl_resource *surface_resource)
4409{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004410 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004411 struct weston_surface *surface =
4412 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004413 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004414
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004415 if (surface->configure) {
4416 wl_resource_post_error(surface_resource,
4417 WL_DISPLAY_ERROR_INVALID_OBJECT,
4418 "surface role already assigned");
4419 return;
4420 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004421
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4423 weston_view_destroy(view);
4424 view = weston_view_create(surface);
4425
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004426 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004427 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004428 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004429 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004430 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004431 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004432 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004433 surface->output->width,
4434 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004435}
4436
Pekka Paalanen8274d902014-08-06 19:36:51 +03004437static int
4438lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4439{
4440 return snprintf(buf, len, "lock window");
4441}
4442
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004443static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004444lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004445{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004446 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004447 struct weston_view *view;
4448
4449 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004450
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004451 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004452 return;
4453
Jason Ekstranda7af7042013-10-12 22:38:11 -05004454 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004455
4456 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004457 weston_layer_entry_insert(&shell->lock_layer.view_list,
4458 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004459 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004460 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004461 }
4462}
4463
4464static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004465handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004466{
Tiago Vignattibe143262012-04-16 17:31:41 +03004467 struct desktop_shell *shell =
4468 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004469
Martin Minarik6d118362012-06-07 18:01:59 +02004470 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004471 shell->lock_surface = NULL;
4472}
4473
4474static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004475desktop_shell_set_lock_surface(struct wl_client *client,
4476 struct wl_resource *resource,
4477 struct wl_resource *surface_resource)
4478{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004479 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004480 struct weston_surface *surface =
4481 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004482
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004483 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004484
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004485 if (!shell->locked)
4486 return;
4487
Pekka Paalanen98262232011-12-01 10:42:22 +02004488 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004489
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004490 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004491 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004492 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004493
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004494 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004495 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004496 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004497 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004498}
4499
4500static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004501resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004502{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004503 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004504
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004505 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004506 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004507 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004508 &shell->input_panel_layer.link);
4509 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004510 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004511 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004512 wl_list_insert(&shell->compositor->cursor_layer.link,
4513 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004514 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004515 wl_list_insert(&shell->fullscreen_layer.link,
4516 &shell->panel_layer.link);
4517 wl_list_insert(&shell->panel_layer.link,
4518 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004519
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004520 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004521
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004522 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004523 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004524 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004525}
4526
4527static void
4528desktop_shell_unlock(struct wl_client *client,
4529 struct wl_resource *resource)
4530{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004531 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004532
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004533 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004534
4535 if (shell->locked)
4536 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004537}
4538
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004539static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004540desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004541 struct wl_resource *resource,
4542 struct wl_resource *surface_resource)
4543{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004544 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004545
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004546 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004547 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004548}
4549
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004550static void
4551desktop_shell_desktop_ready(struct wl_client *client,
4552 struct wl_resource *resource)
4553{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004554 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004555
4556 shell_fade_startup(shell);
4557}
4558
Jonny Lamb765760d2014-08-20 15:53:19 +02004559static void
4560desktop_shell_set_panel_position(struct wl_client *client,
4561 struct wl_resource *resource,
4562 uint32_t position)
4563{
4564 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4565
4566 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4567 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4568 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4569 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4570 wl_resource_post_error(resource,
4571 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4572 "bad position argument");
4573 return;
4574 }
4575
4576 shell->panel_position = position;
4577}
4578
Kristian Høgsberg75840622011-09-06 13:48:16 -04004579static const struct desktop_shell_interface desktop_shell_implementation = {
4580 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004581 desktop_shell_set_panel,
4582 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004583 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004584 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004585 desktop_shell_desktop_ready,
4586 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004587};
4588
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004589static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004590get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004591{
4592 struct shell_surface *shsurf;
4593
4594 shsurf = get_shell_surface(surface);
4595 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004596 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004597 return shsurf->type;
4598}
4599
Kristian Høgsberg75840622011-09-06 13:48:16 -04004600static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004601move_binding(struct weston_pointer *pointer, uint32_t time,
4602 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004603{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004604 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004605 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004606 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004607
Derek Foreman8ae2db52015-07-15 13:00:36 -05004608 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004609 return;
4610
Derek Foreman8ae2db52015-07-15 13:00:36 -05004611 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004612
Pekka Paalanen01388e22013-04-25 13:57:44 +03004613 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004614 if (surface == NULL)
4615 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004616
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004617 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004618 if (shsurf == NULL || shsurf->state.fullscreen ||
4619 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004620 return;
4621
Derek Foreman794fa0e2015-07-15 13:00:38 -05004622 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004623}
4624
4625static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004626maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4627 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004628{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004629 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004630 struct weston_surface *surface;
4631 struct shell_surface *shsurf;
4632
4633 surface = weston_surface_get_main_surface(focus);
4634 if (surface == NULL)
4635 return;
4636
4637 shsurf = get_shell_surface(surface);
4638 if (shsurf == NULL)
4639 return;
4640
4641 if (!shell_surface_is_xdg_surface(shsurf))
4642 return;
4643
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004644 shsurf->state_requested = true;
4645 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004646 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004647}
4648
4649static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004650fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4651 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004652{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004653 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004654 struct weston_surface *surface;
4655 struct shell_surface *shsurf;
4656
4657 surface = weston_surface_get_main_surface(focus);
4658 if (surface == NULL)
4659 return;
4660
4661 shsurf = get_shell_surface(surface);
4662 if (shsurf == NULL)
4663 return;
4664
4665 if (!shell_surface_is_xdg_surface(shsurf))
4666 return;
4667
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004668 shsurf->state_requested = true;
4669 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004670 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004671 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004672}
4673
4674static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004675touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004676{
Derek Foreman362656b2014-09-04 10:23:05 -05004677 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004678 struct weston_surface *surface;
4679 struct shell_surface *shsurf;
4680
Derek Foreman8ae2db52015-07-15 13:00:36 -05004681 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004682 return;
4683
Derek Foreman8ae2db52015-07-15 13:00:36 -05004684 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004685 surface = weston_surface_get_main_surface(focus);
4686 if (surface == NULL)
4687 return;
4688
4689 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004690 if (shsurf == NULL || shsurf->state.fullscreen ||
4691 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004692 return;
4693
Derek Foremanb7674ae2015-07-15 13:00:37 -05004694 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004695}
4696
4697static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004698resize_binding(struct weston_pointer *pointer, uint32_t time,
4699 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004700{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004701 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004702 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004703 uint32_t edges = 0;
4704 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004705 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004706
Derek Foreman8ae2db52015-07-15 13:00:36 -05004707 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004708 return;
4709
Derek Foreman8ae2db52015-07-15 13:00:36 -05004710 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004711
Pekka Paalanen01388e22013-04-25 13:57:44 +03004712 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004713 if (surface == NULL)
4714 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004715
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004716 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004717 if (shsurf == NULL || shsurf->state.fullscreen ||
4718 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004719 return;
4720
Jason Ekstranda7af7042013-10-12 22:38:11 -05004721 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004722 wl_fixed_to_int(pointer->grab_x),
4723 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004724 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004725
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004727 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004728 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004729 edges |= 0;
4730 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004731 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004732
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004733 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004734 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004735 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004736 edges |= 0;
4737 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004738 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004739
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004740 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004741}
4742
4743static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004744surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4745 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004746{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004747 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004748 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004749 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004750 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004751
Pekka Paalanen01388e22013-04-25 13:57:44 +03004752 /* XXX: broken for windows containing sub-surfaces */
4753 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004754 if (surface == NULL)
4755 return;
4756
4757 shsurf = get_shell_surface(surface);
4758 if (!shsurf)
4759 return;
4760
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004762
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 if (shsurf->view->alpha > 1.0)
4764 shsurf->view->alpha = 1.0;
4765 if (shsurf->view->alpha < step)
4766 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004767
Jason Ekstranda7af7042013-10-12 22:38:11 -05004768 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004769 weston_surface_damage(surface);
4770}
4771
4772static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004773do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004774 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004775{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004776 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004777 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004778 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004779
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004780 if (!seat->pointer) {
4781 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4782 return;
4783 }
4784
Scott Moreauccbf29d2012-02-22 14:21:41 -07004785 wl_list_for_each(output, &compositor->output_list, link) {
4786 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004787 wl_fixed_to_double(seat->pointer->x),
4788 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004789 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004790 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004791 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004792 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004793 increment = -output->zoom.increment;
4794 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004795 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004796 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004797 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004798 else
4799 increment = 0;
4800
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004801 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004802
Scott Moreaue6603982012-06-11 13:07:51 -06004803 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004804 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004805 else if (output->zoom.level > output->zoom.max_level)
4806 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004807
4808 if (!output->zoom.active) {
4809 if (output->zoom.level <= 0.0)
4810 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004811 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004812 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004813
Scott Moreaue6603982012-06-11 13:07:51 -06004814 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004815
Jason Ekstranda7af7042013-10-12 22:38:11 -05004816 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004817 }
4818 }
4819}
4820
Scott Moreauccbf29d2012-02-22 14:21:41 -07004821static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004822zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4823 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004825 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826}
4827
4828static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004829zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4830 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004831{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004832 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004833}
4834
4835static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004836terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4837 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004838{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004839 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004840
Daniel Stone325fc2d2012-05-30 16:31:58 +01004841 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004842}
4843
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004844static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004845rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4846 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004847{
4848 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004849 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004850 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004851 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004852 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004853
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004854 weston_pointer_move(pointer, x, y);
4855
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004856 if (!shsurf)
4857 return;
4858
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004859 cx = 0.5f * shsurf->surface->width;
4860 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004861
Daniel Stone37816df2012-05-16 18:45:18 +01004862 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4863 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004864 r = sqrtf(dx * dx + dy * dy);
4865
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004866 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004867 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004868
4869 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004870 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004871 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004872
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004873 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004874 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004875
4876 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004877 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004878 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004879 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004880 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004881
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004882 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004883 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004884 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004885 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004886 wl_list_init(&shsurf->rotation.transform.link);
4887 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004888 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004889 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004890
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004891 /* We need to adjust the position of the surface
4892 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893 cposx = shsurf->view->geometry.x + cx;
4894 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004895 dposx = rotate->center.x - cposx;
4896 dposy = rotate->center.y - cposy;
4897 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004898 weston_view_set_position(shsurf->view,
4899 shsurf->view->geometry.x + dposx,
4900 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004901 }
4902
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004903 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004904 * lazily applies the damage due to rotation update.
4905 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004906 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004907}
4908
4909static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004910rotate_grab_button(struct weston_pointer_grab *grab,
4911 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004912{
4913 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004914 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004915 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004916 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004917 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004918
Daniel Stone4dbadb12012-05-30 16:31:51 +01004919 if (pointer->button_count == 0 &&
4920 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004921 if (shsurf)
4922 weston_matrix_multiply(&shsurf->rotation.rotation,
4923 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004924 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004925 free(rotate);
4926 }
4927}
4928
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004929static void
4930rotate_grab_cancel(struct weston_pointer_grab *grab)
4931{
4932 struct rotate_grab *rotate =
4933 container_of(grab, struct rotate_grab, base.grab);
4934
4935 shell_grab_end(&rotate->base);
4936 free(rotate);
4937}
4938
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004939static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004940 noop_grab_focus,
4941 rotate_grab_motion,
4942 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004943 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004944};
4945
4946static void
Derek Foreman74de4692015-07-15 13:00:39 -05004947surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004948{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004949 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004950 float dx, dy;
4951 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004952
Pekka Paalanen460099f2012-01-20 16:48:25 +02004953 rotate = malloc(sizeof *rotate);
4954 if (!rotate)
4955 return;
4956
Jason Ekstranda7af7042013-10-12 22:38:11 -05004957 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004958 surface->surface->width * 0.5f,
4959 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004960 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004961
Derek Foreman74de4692015-07-15 13:00:39 -05004962 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4963 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004964 r = sqrtf(dx * dx + dy * dy);
4965 if (r > 20.0f) {
4966 struct weston_matrix inverse;
4967
4968 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004969 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004970 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004971
4972 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004973 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004974 } else {
4975 weston_matrix_init(&surface->rotation.rotation);
4976 weston_matrix_init(&rotate->rotation);
4977 }
4978
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004979 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Derek Foreman74de4692015-07-15 13:00:39 -05004980 pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004981}
4982
4983static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004984rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004985 void *data)
4986{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004987 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004988 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004989 struct shell_surface *surface;
4990
Derek Foreman8ae2db52015-07-15 13:00:36 -05004991 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004992 return;
4993
Derek Foreman8ae2db52015-07-15 13:00:36 -05004994 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004995
Pekka Paalanen01388e22013-04-25 13:57:44 +03004996 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004997 if (base_surface == NULL)
4998 return;
4999
5000 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005001 if (surface == NULL || surface->state.fullscreen ||
5002 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005003 return;
5004
Derek Foreman74de4692015-07-15 13:00:39 -05005005 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005006}
5007
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005008/* Move all fullscreen layers down to the current workspace and hide their
5009 * black views. The surfaces' state is set to both fullscreen and lowered,
5010 * and this is reversed when such a surface is re-configured, see
5011 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5012 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005013 * lowering_output = NULL - Lower on all outputs, else only lower on the
5014 * specified output.
5015 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005016 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005017 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005018void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005019lower_fullscreen_layer(struct desktop_shell *shell,
5020 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005021{
5022 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005023 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005024
5025 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005026 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005027 &shell->fullscreen_layer.view_list.link,
5028 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005029 struct shell_surface *shsurf = get_shell_surface(view->surface);
5030
5031 if (!shsurf)
5032 continue;
5033
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005034 /* Only lower surfaces which have lowering_output as their fullscreen
5035 * output, unless a NULL output asks for lowering on all outputs.
5036 */
5037 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5038 continue;
5039
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005040 /* We can have a non-fullscreen popup for a fullscreen surface
5041 * in the fullscreen layer. */
5042 if (shsurf->state.fullscreen) {
5043 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005044 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5045 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005046 weston_view_damage_below(shsurf->fullscreen.black_view);
5047
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005048 }
5049
5050 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005051 weston_layer_entry_remove(&view->layer_link);
5052 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005053 weston_view_damage_below(view);
5054 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005055
5056 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005057 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005058}
5059
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005060void
Tiago Vignattibe143262012-04-16 17:31:41 +03005061activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005062 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005063{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005064 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005065 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005066 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005067 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005068 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005069
Pekka Paalanen01388e22013-04-25 13:57:44 +03005070 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005071 shsurf = get_shell_surface(main_surface);
5072 assert(shsurf);
5073
5074 /* Only demote fullscreen surfaces on the output of activated shsurf.
5075 * Leave fullscreen surfaces on unrelated outputs alone. */
5076 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005077
Daniel Stone37816df2012-05-16 18:45:18 +01005078 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005079
Jonas Ådahl8538b222012-08-29 22:13:03 +02005080 state = ensure_focus_state(shell, seat);
5081 if (state == NULL)
5082 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005083
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005084 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005085 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005086
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005087 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005088 shell_configure_fullscreen(shsurf);
5089 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005090 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005091
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005092 /* Update the surface’s layer. This brings it to the top of the stacking
5093 * order as appropriate. */
5094 shell_surface_update_layer(shsurf);
5095
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005096 if (shell->focus_animation_type != ANIMATION_NONE) {
5097 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005098 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005099 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005100}
5101
Alex Wu21858432012-04-01 20:13:08 +08005102/* no-op func for checking black surface */
5103static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005104black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005105{
5106}
5107
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005108static bool
Alex Wu21858432012-04-01 20:13:08 +08005109is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5110{
5111 if (es->configure == black_surface_configure) {
5112 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005113 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005114 return true;
5115 }
5116 return false;
5117}
5118
Kristian Høgsberg75840622011-09-06 13:48:16 -04005119static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005120activate_binding(struct weston_seat *seat,
5121 struct desktop_shell *shell,
5122 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005123{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005124 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005125
Alex Wu9c35e6b2012-03-05 14:13:13 +08005126 if (!focus)
5127 return;
5128
Pekka Paalanen01388e22013-04-25 13:57:44 +03005129 if (is_black_surface(focus, &main_surface))
5130 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005131
Pekka Paalanen01388e22013-04-25 13:57:44 +03005132 main_surface = weston_surface_get_main_surface(focus);
5133 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005134 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005135
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005136 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005137}
5138
5139static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005140click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5141 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005142{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005143 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005144 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005145 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005146 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005147
Derek Foreman8ae2db52015-07-15 13:00:36 -05005148 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005149}
5150
5151static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005152touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5153 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005154{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005155 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005156 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005157 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005158 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005159
Derek Foreman8ae2db52015-07-15 13:00:36 -05005160 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005161}
5162
5163static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005164unfocus_all_seats(struct desktop_shell *shell)
5165{
5166 struct weston_seat *seat, *next;
5167
5168 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5169 if (seat->keyboard == NULL)
5170 continue;
5171
5172 weston_keyboard_set_focus(seat->keyboard, NULL);
5173 }
5174}
5175
5176static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005177lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005178{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005179 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005180
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005181 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005182 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005183 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005184 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005185
5186 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005187
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005188 /* Hide all surfaces by removing the fullscreen, panel and
5189 * toplevel layers. This way nothing else can show or receive
5190 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005191
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005192 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005193 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005194 if (shell->showing_input_panels)
5195 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005196 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005197 wl_list_insert(&shell->compositor->cursor_layer.link,
5198 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005199
Derek Foreman004b4a12015-07-20 16:28:13 -05005200 weston_compositor_sleep(shell->compositor);
5201
Neil Robertsb4a91702014-04-09 16:33:32 +01005202 /* Remove the keyboard focus on all seats. This will be
5203 * restored to the workspace's saved state via
5204 * restore_focus_state when the compositor is unlocked */
5205 unfocus_all_seats(shell);
5206
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005207 /* TODO: disable bindings that should not work while locked. */
5208
5209 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005210}
5211
5212static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005213unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005214{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005215 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005216 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005217 return;
5218 }
5219
5220 /* If desktop-shell client has gone away, unlock immediately. */
5221 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005222 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005223 return;
5224 }
5225
5226 if (shell->prepare_event_sent)
5227 return;
5228
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005229 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005230 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005231}
5232
5233static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005234shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005235{
5236 struct desktop_shell *shell = data;
5237
5238 shell->fade.animation = NULL;
5239
5240 switch (shell->fade.type) {
5241 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005242 weston_surface_destroy(shell->fade.view->surface);
5243 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005244 break;
5245 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005246 lock(shell);
5247 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005248 default:
5249 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005250 }
5251}
5252
Jason Ekstranda7af7042013-10-12 22:38:11 -05005253static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005254shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005255{
5256 struct weston_compositor *compositor = shell->compositor;
5257 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005258 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005259
5260 surface = weston_surface_create(compositor);
5261 if (!surface)
5262 return NULL;
5263
Jason Ekstranda7af7042013-10-12 22:38:11 -05005264 view = weston_view_create(surface);
5265 if (!view) {
5266 weston_surface_destroy(surface);
5267 return NULL;
5268 }
5269
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005270 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005271 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005272 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005273 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5274 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005275 pixman_region32_init(&surface->input);
5276
Jason Ekstranda7af7042013-10-12 22:38:11 -05005277 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005278}
5279
5280static void
5281shell_fade(struct desktop_shell *shell, enum fade_type type)
5282{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005283 float tint;
5284
5285 switch (type) {
5286 case FADE_IN:
5287 tint = 0.0;
5288 break;
5289 case FADE_OUT:
5290 tint = 1.0;
5291 break;
5292 default:
5293 weston_log("shell: invalid fade type\n");
5294 return;
5295 }
5296
5297 shell->fade.type = type;
5298
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299 if (shell->fade.view == NULL) {
5300 shell->fade.view = shell_fade_create_surface(shell);
5301 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005302 return;
5303
Jason Ekstranda7af7042013-10-12 22:38:11 -05005304 shell->fade.view->alpha = 1.0 - tint;
5305 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005306 }
5307
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005308 if (shell->fade.view->output == NULL) {
5309 /* If the black view gets a NULL output, we lost the
5310 * last output and we'll just cancel the fade. This
5311 * happens when you close the last window under the
5312 * X11 or Wayland backends. */
5313 shell->locked = false;
5314 weston_surface_destroy(shell->fade.view->surface);
5315 shell->fade.view = NULL;
5316 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005317 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005318 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005319 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005320 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005321 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005322 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005323 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005324}
5325
5326static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005327do_shell_fade_startup(void *data)
5328{
5329 struct desktop_shell *shell = data;
5330
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005331 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005332 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005333 } else {
5334 weston_log("desktop shell: "
5335 "unexpected fade-in animation type %d\n",
5336 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005337 weston_surface_destroy(shell->fade.view->surface);
5338 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005339 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005340}
5341
5342static void
5343shell_fade_startup(struct desktop_shell *shell)
5344{
5345 struct wl_event_loop *loop;
5346
5347 if (!shell->fade.startup_timer)
5348 return;
5349
5350 wl_event_source_remove(shell->fade.startup_timer);
5351 shell->fade.startup_timer = NULL;
5352
5353 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5354 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5355}
5356
5357static int
5358fade_startup_timeout(void *data)
5359{
5360 struct desktop_shell *shell = data;
5361
5362 shell_fade_startup(shell);
5363 return 0;
5364}
5365
5366static void
5367shell_fade_init(struct desktop_shell *shell)
5368{
5369 /* Make compositor output all black, and wait for the desktop-shell
5370 * client to signal it is ready, then fade in. The timer triggers a
5371 * fade-in, in case the desktop-shell client takes too long.
5372 */
5373
5374 struct wl_event_loop *loop;
5375
Jason Ekstranda7af7042013-10-12 22:38:11 -05005376 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005377 weston_log("%s: warning: fade surface already exists\n",
5378 __func__);
5379 return;
5380 }
5381
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005382 if (shell->startup_animation_type == ANIMATION_NONE)
5383 return;
5384
Jason Ekstranda7af7042013-10-12 22:38:11 -05005385 shell->fade.view = shell_fade_create_surface(shell);
5386 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005387 return;
5388
Jason Ekstranda7af7042013-10-12 22:38:11 -05005389 weston_view_update_transform(shell->fade.view);
5390 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005391
5392 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5393 shell->fade.startup_timer =
5394 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5395 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5396}
5397
5398static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005399idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005400{
5401 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005402 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005403 struct weston_seat *seat;
5404
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005405 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005406 if (seat->pointer)
5407 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005408 if (seat->touch)
5409 touch_popup_grab_end(seat->touch);
5410 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005411
5412 shell_fade(shell, FADE_OUT);
5413 /* lock() is called from shell_fade_done() */
5414}
5415
5416static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005417wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005418{
5419 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005420 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005421
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005422 unlock(shell);
5423}
5424
5425static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005426center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005427{
Giulio Camuffob8366642013-04-25 13:57:46 +03005428 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005429 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005430
Jason Ekstranda7af7042013-10-12 22:38:11 -05005431 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005432
5433 x = output->x + (output->width - width) / 2 - surf_x / 2;
5434 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005435
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005436 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005437}
5438
5439static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005440weston_view_set_initial_position(struct weston_view *view,
5441 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005442{
5443 struct weston_compositor *compositor = shell->compositor;
5444 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005445 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005446 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005447 struct weston_output *output, *target_output = NULL;
5448 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005449 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005450
5451 /* As a heuristic place the new window on the same output as the
5452 * pointer. Falling back to the output containing 0, 0.
5453 *
5454 * TODO: Do something clever for touch too?
5455 */
5456 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005457 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005458 ix = wl_fixed_to_int(seat->pointer->x);
5459 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005460 break;
5461 }
5462 }
5463
5464 wl_list_for_each(output, &compositor->output_list, link) {
5465 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5466 target_output = output;
5467 break;
5468 }
5469 }
5470
5471 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005472 weston_view_set_position(view, 10 + random() % 400,
5473 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005474 return;
5475 }
5476
5477 /* Valid range within output where the surface will still be onscreen.
5478 * If this is negative it means that the surface is bigger than
5479 * output.
5480 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005481 get_output_work_area(shell, target_output, &area);
5482
Derek Foremanf814c5d2015-04-15 12:23:54 -05005483 x = area.x;
5484 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005485 range_x = area.width - view->surface->width;
5486 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005487
Rob Bradford4cb88c72012-08-13 15:18:44 +01005488 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005489 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005490
5491 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005492 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005493
Jason Ekstranda7af7042013-10-12 22:38:11 -05005494 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005495}
5496
5497static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005498set_maximized_position(struct desktop_shell *shell,
5499 struct shell_surface *shsurf)
5500{
5501 int32_t surf_x, surf_y;
5502 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005503 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005504
Jonny Lambd73c6942014-08-20 15:53:20 +02005505 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005506 if (shsurf->has_set_geometry) {
5507 surf_x = shsurf->geometry.x;
5508 surf_y = shsurf->geometry.y;
5509 } else {
5510 surface_subsurfaces_boundingbox(shsurf->surface,
5511 &surf_x, &surf_y, NULL, NULL);
5512 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005513 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005514
5515 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005516 e->x1 + area.x - surf_x,
5517 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005518}
5519
5520static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005521map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005522 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005523{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005524 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005525 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005526
Pekka Paalanen77346a62011-11-30 16:26:35 +02005527 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005528 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005529 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005530 if (shsurf->state.fullscreen) {
5531 center_on_output(shsurf->view, shsurf->fullscreen_output);
5532 shell_map_fullscreen(shsurf);
5533 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005534 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005535 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005536 weston_view_set_initial_position(shsurf->view, shell);
5537 }
Juan Zhao96879df2012-02-07 08:45:41 +08005538 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005539 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005540 if (shell_map_popup(shsurf) != 0)
5541 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005542 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005543 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005544 weston_view_set_position(shsurf->view,
5545 shsurf->view->geometry.x + sx,
5546 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005547 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005548 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005549 default:
5550 ;
5551 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005552
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005553 /* Surface stacking order, see also activate(). */
5554 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005555
Jason Ekstranda7af7042013-10-12 22:38:11 -05005556 if (shsurf->type != SHELL_SURFACE_NONE) {
5557 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005558 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005559 shsurf->surface->output = shsurf->output;
5560 shsurf->view->output = shsurf->output;
5561 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005562 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005563
Jason Ekstranda7af7042013-10-12 22:38:11 -05005564 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005565 /* XXX: xwayland's using the same fields for transient type */
5566 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005567 if (shsurf->transient.flags ==
5568 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5569 break;
5570 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005571 if (shsurf->state.relative &&
5572 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5573 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005574 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005575 break;
5576 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005577 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005578 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005579 case SHELL_SURFACE_POPUP:
5580 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005581 default:
5582 break;
5583 }
5584
Rafael Antognolli03b16592013-12-03 15:35:42 -02005585 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5586 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005587 {
5588 switch (shell->win_animation_type) {
5589 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005590 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005591 break;
5592 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005593 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005594 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005595 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005596 default:
5597 break;
5598 }
5599 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005600}
5601
5602static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005603configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005604 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005605{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005606 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005607 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005608
Pekka Paalanen77346a62011-11-30 16:26:35 +02005609 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005610
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005611 assert(shsurf);
5612
Rafael Antognolli03b16592013-12-03 15:35:42 -02005613 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005614 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005615 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005616 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005617 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005618 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005619 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005620
Alex Wu4539b082012-03-01 12:57:46 +08005621 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005622 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005623 wl_list_for_each(view, &surface->views, surface_link)
5624 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005625
Rafael Antognolli03b16592013-12-03 15:35:42 -02005626 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005627 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005628 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005629}
5630
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005631static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005632shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005633{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005634 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005635 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005636 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005637
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005638 assert(shsurf);
5639
5640 shell = shsurf->shell;
5641
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005642 if (!weston_surface_is_mapped(es) &&
5643 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005644 remove_popup_grab(shsurf);
5645 }
5646
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005647 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005648 return;
5649
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005650 if (shsurf->has_next_geometry) {
5651 shsurf->geometry = shsurf->next_geometry;
5652 shsurf->has_next_geometry = false;
5653 shsurf->has_set_geometry = true;
5654 } else if (!shsurf->has_set_geometry) {
5655 surface_subsurfaces_boundingbox(shsurf->surface,
5656 &shsurf->geometry.x,
5657 &shsurf->geometry.y,
5658 &shsurf->geometry.width,
5659 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005660 }
5661
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005662 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005663 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005664 type_changed = 1;
5665 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005666
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005667 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005668 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005669 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005670 shsurf->last_width != es->width ||
5671 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005672 float from_x, from_y;
5673 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005674
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005675 if (shsurf->resize_edges) {
5676 sx = 0;
5677 sy = 0;
5678 }
5679
5680 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5681 sx = shsurf->last_width - es->width;
5682 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5683 sy = shsurf->last_height - es->height;
5684
5685 shsurf->last_width = es->width;
5686 shsurf->last_height = es->height;
5687
Jason Ekstranda7af7042013-10-12 22:38:11 -05005688 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5689 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005690 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005691 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005692 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005693 }
5694}
5695
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005696static bool
5697check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5698{
5699 struct timespec now;
5700
5701 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5702 return false;
5703
5704 /*
5705 * If the shell helper client dies before the session has been
5706 * up for roughly 30 seconds, better just make Weston shut down,
5707 * because the user likely has no way to interact with the desktop
5708 * anyway.
5709 */
5710 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5711 weston_log("Error: %s apparently cannot run at all.\n",
5712 shell->client);
5713 weston_log_continue(STAMP_SPACE "Quitting...");
5714 wl_display_terminate(shell->compositor->wl_display);
5715
5716 return true;
5717 }
5718
5719 return false;
5720}
5721
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005722static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005723
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005724static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005725respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005726{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005727 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005728
5729 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5730 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005731 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005732 shell->child.deathstamp = time;
5733 shell->child.deathcount = 0;
5734 }
5735
5736 shell->child.deathcount++;
5737 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005738 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005739 return;
5740 }
5741
Pekka Paalanen826dc142014-08-27 12:11:53 +03005742 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005743 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005744}
5745
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005746static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005747desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5748{
5749 struct desktop_shell *shell;
5750
5751 shell = container_of(listener, struct desktop_shell,
5752 child.client_destroy_listener);
5753
Pekka Paalanen826dc142014-08-27 12:11:53 +03005754 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005755 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005756 /*
5757 * unbind_desktop_shell() will reset shell->child.desktop_shell
5758 * before the respawned process has a chance to create a new
5759 * desktop_shell object, because we are being called from the
5760 * wl_client destructor which destroys all wl_resources before
5761 * returning.
5762 */
5763
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005764 if (!check_desktop_shell_crash_too_early(shell))
5765 respawn_desktop_shell_process(shell);
5766
Pekka Paalanen826dc142014-08-27 12:11:53 +03005767 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005768}
5769
5770static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005771launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005772{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005773 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005774
Pekka Paalanen826dc142014-08-27 12:11:53 +03005775 shell->child.client = weston_client_start(shell->compositor,
5776 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005777
Arnaud Vrac42631192014-08-25 20:56:46 +02005778 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005779 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005780 return;
5781 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005782
5783 shell->child.client_destroy_listener.notify =
5784 desktop_shell_client_destroy;
5785 wl_client_add_destroy_listener(shell->child.client,
5786 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005787}
5788
5789static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005790handle_shell_client_destroy(struct wl_listener *listener, void *data)
5791{
5792 struct shell_client *sc =
5793 container_of(listener, struct shell_client, destroy_listener);
5794
5795 if (sc->ping_timer)
5796 wl_event_source_remove(sc->ping_timer);
5797 free(sc);
5798}
5799
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005800static struct shell_client *
5801shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5802 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005803{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005804 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005805
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005806 sc = zalloc(sizeof *sc);
5807 if (sc == NULL) {
5808 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005809 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005810 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005811
5812 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005813 if (sc->resource == NULL) {
5814 free(sc);
5815 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005816 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005817 }
5818
5819 sc->client = client;
5820 sc->shell = shell;
5821 sc->destroy_listener.notify = handle_shell_client_destroy;
5822 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005823 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005824
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005825 return sc;
5826}
5827
5828static void
5829bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5830{
5831 struct desktop_shell *shell = data;
5832 struct shell_client *sc;
5833
5834 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5835 if (sc)
5836 wl_resource_set_implementation(sc->resource,
5837 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005838 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005839}
5840
5841static void
5842bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5843{
5844 struct desktop_shell *shell = data;
5845 struct shell_client *sc;
5846
5847 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5848 if (sc)
5849 wl_resource_set_dispatcher(sc->resource,
5850 xdg_shell_unversioned_dispatch,
5851 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005852}
5853
5854static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005855unbind_desktop_shell(struct wl_resource *resource)
5856{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005857 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005858
5859 if (shell->locked)
5860 resume_desktop(shell);
5861
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005862 shell->child.desktop_shell = NULL;
5863 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005864}
5865
5866static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005867bind_desktop_shell(struct wl_client *client,
5868 void *data, uint32_t version, uint32_t id)
5869{
Tiago Vignattibe143262012-04-16 17:31:41 +03005870 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005871 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005872
Jason Ekstranda85118c2013-06-27 20:17:02 -05005873 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005874 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005875
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005876 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005877 wl_resource_set_implementation(resource,
5878 &desktop_shell_implementation,
5879 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005880 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005881
5882 if (version < 2)
5883 shell_fade_startup(shell);
5884
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005885 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005886 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005887
5888 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5889 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005890}
5891
Kristian Høgsberg07045392012-02-19 18:52:44 -05005892struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005893 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005894 struct weston_surface *current;
5895 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005896 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005897 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005898};
5899
5900static void
5901switcher_next(struct switcher *switcher)
5902{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005903 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005904 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005905 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005906 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005907
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005908 /* temporary re-display minimized surfaces */
5909 struct weston_view *tmp;
5910 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005911 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5912 weston_layer_entry_remove(&view->layer_link);
5913 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005914 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5915 *minimized = view;
5916 }
5917
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005918 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005919 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005920 if (shsurf &&
5921 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5922 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005923 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005924 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005925 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005926 next = view->surface;
5927 prev = view->surface;
5928 view->alpha = 0.25;
5929 weston_view_geometry_dirty(view);
5930 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005931 }
Alex Wu1659daa2012-04-01 20:13:09 +08005932
Jason Ekstranda7af7042013-10-12 22:38:11 -05005933 if (is_black_surface(view->surface, NULL)) {
5934 view->alpha = 0.25;
5935 weston_view_geometry_dirty(view);
5936 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005937 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005938 }
5939
5940 if (next == NULL)
5941 next = first;
5942
Alex Wu07b26062012-03-12 16:06:01 +08005943 if (next == NULL)
5944 return;
5945
Kristian Høgsberg07045392012-02-19 18:52:44 -05005946 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005947 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005948
5949 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005950 wl_list_for_each(view, &next->views, surface_link)
5951 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005952
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005953 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005954 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005955 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005956}
5957
5958static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005959switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005960{
5961 struct switcher *switcher =
5962 container_of(listener, struct switcher, listener);
5963
5964 switcher_next(switcher);
5965}
5966
5967static void
Daniel Stone351eb612012-05-31 15:27:47 -04005968switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005969{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005970 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005971 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005972 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005973
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005974 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005975 if (is_focus_view(view))
5976 continue;
5977
Jason Ekstranda7af7042013-10-12 22:38:11 -05005978 view->alpha = 1.0;
5979 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005980 }
5981
Alex Wu07b26062012-03-12 16:06:01 +08005982 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005983 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005984 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005985 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005986 weston_keyboard_end_grab(keyboard);
5987 if (keyboard->input_method_resource)
5988 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005989
5990 /* re-hide surfaces that were temporary shown during the switch */
5991 struct weston_view **minimized;
5992 wl_array_for_each(minimized, &switcher->minimized_array) {
5993 /* with the exception of the current selected */
5994 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005995 weston_layer_entry_remove(&(*minimized)->layer_link);
5996 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005997 weston_view_damage_below(*minimized);
5998 }
5999 }
6000 wl_array_release(&switcher->minimized_array);
6001
Kristian Høgsberg07045392012-02-19 18:52:44 -05006002 free(switcher);
6003}
6004
6005static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006006switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006007 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006008{
6009 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006010 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006011
Daniel Stonec9785ea2012-05-30 16:31:52 +01006012 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006013 switcher_next(switcher);
6014}
6015
6016static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006017switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006018 uint32_t mods_depressed, uint32_t mods_latched,
6019 uint32_t mods_locked, uint32_t group)
6020{
6021 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006022 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006023
Daniel Stone351eb612012-05-31 15:27:47 -04006024 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6025 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006026}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006027
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006028static void
6029switcher_cancel(struct weston_keyboard_grab *grab)
6030{
6031 struct switcher *switcher = container_of(grab, struct switcher, grab);
6032
6033 switcher_destroy(switcher);
6034}
6035
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006036static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006037 switcher_key,
6038 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006039 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006040};
6041
6042static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006043switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6044 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006045{
Tiago Vignattibe143262012-04-16 17:31:41 +03006046 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047 struct switcher *switcher;
6048
6049 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006050 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006051 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006052 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006053 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006054 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006055
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006056 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006057 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006058 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006059 weston_keyboard_start_grab(keyboard, &switcher->grab);
6060 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006061 switcher_next(switcher);
6062}
6063
Pekka Paalanen3c647232011-12-22 13:43:43 +02006064static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006065backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6066 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006067{
6068 struct weston_compositor *compositor = data;
6069 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006070 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006071
6072 /* TODO: we're limiting to simple use cases, where we assume just
6073 * control on the primary display. We'd have to extend later if we
6074 * ever get support for setting backlights on random desktop LCD
6075 * panels though */
6076 output = get_default_output(compositor);
6077 if (!output)
6078 return;
6079
6080 if (!output->set_backlight)
6081 return;
6082
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006083 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6084 backlight_new = output->backlight_current - 25;
6085 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6086 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006087
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006088 if (backlight_new < 5)
6089 backlight_new = 5;
6090 if (backlight_new > 255)
6091 backlight_new = 255;
6092
6093 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006094 output->set_backlight(output, output->backlight_current);
6095}
6096
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006097static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006098force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6099 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006100{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006101 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006102 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006103 struct desktop_shell *shell = data;
6104 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006105 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006106
Derek Foreman8ae2db52015-07-15 13:00:36 -05006107 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006108 if (!focus_surface)
6109 return;
6110
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006111 wl_signal_emit(&compositor->kill_signal, focus_surface);
6112
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006113 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006114 wl_client_get_credentials(client, &pid, NULL, NULL);
6115
6116 /* Skip clients that we launched ourselves (the credentials of
6117 * the socketpair is ours) */
6118 if (pid == getpid())
6119 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006120
Daniel Stone325fc2d2012-05-30 16:31:58 +01006121 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006122}
6123
6124static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006125workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006126 uint32_t key, void *data)
6127{
6128 struct desktop_shell *shell = data;
6129 unsigned int new_index = shell->workspaces.current;
6130
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006131 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006132 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006133 if (new_index != 0)
6134 new_index--;
6135
6136 change_workspace(shell, new_index);
6137}
6138
6139static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006140workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006141 uint32_t key, void *data)
6142{
6143 struct desktop_shell *shell = data;
6144 unsigned int new_index = shell->workspaces.current;
6145
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006146 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006147 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006148 if (new_index < shell->workspaces.num - 1)
6149 new_index++;
6150
6151 change_workspace(shell, new_index);
6152}
6153
6154static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006155workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006156 uint32_t key, void *data)
6157{
6158 struct desktop_shell *shell = data;
6159 unsigned int new_index;
6160
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006161 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006162 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006163 new_index = key - KEY_F1;
6164 if (new_index >= shell->workspaces.num)
6165 new_index = shell->workspaces.num - 1;
6166
6167 change_workspace(shell, new_index);
6168}
6169
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006170static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006171workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6172 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006173{
6174 struct desktop_shell *shell = data;
6175 unsigned int new_index = shell->workspaces.current;
6176
6177 if (shell->locked)
6178 return;
6179
6180 if (new_index != 0)
6181 new_index--;
6182
Derek Foreman8ae2db52015-07-15 13:00:36 -05006183 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006184}
6185
6186static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006187workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6188 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006189{
6190 struct desktop_shell *shell = data;
6191 unsigned int new_index = shell->workspaces.current;
6192
6193 if (shell->locked)
6194 return;
6195
6196 if (new_index < shell->workspaces.num - 1)
6197 new_index++;
6198
Derek Foreman8ae2db52015-07-15 13:00:36 -05006199 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006200}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006201
6202static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006203shell_reposition_view_on_output_destroy(struct weston_view *view)
6204{
6205 struct weston_output *output, *first_output;
6206 struct weston_compositor *ec = view->surface->compositor;
6207 struct shell_surface *shsurf;
6208 float x, y;
6209 int visible;
6210
6211 x = view->geometry.x;
6212 y = view->geometry.y;
6213
6214 /* At this point the destroyed output is not in the list anymore.
6215 * If the view is still visible somewhere, we leave where it is,
6216 * otherwise, move it to the first output. */
6217 visible = 0;
6218 wl_list_for_each(output, &ec->output_list, link) {
6219 if (pixman_region32_contains_point(&output->region,
6220 x, y, NULL)) {
6221 visible = 1;
6222 break;
6223 }
6224 }
6225
6226 if (!visible) {
6227 first_output = container_of(ec->output_list.next,
6228 struct weston_output, link);
6229
6230 x = first_output->x + first_output->width / 4;
6231 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006232
6233 weston_view_set_position(view, x, y);
6234 } else {
6235 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006236 }
6237
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006238
6239 shsurf = get_shell_surface(view->surface);
6240
6241 if (shsurf) {
6242 shsurf->saved_position_valid = false;
6243 shsurf->next_state.maximized = false;
6244 shsurf->next_state.fullscreen = false;
6245 shsurf->state_changed = true;
6246 }
6247}
6248
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006249void
6250shell_for_each_layer(struct desktop_shell *shell,
6251 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006252{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006253 struct workspace **ws;
6254
6255 func(shell, &shell->fullscreen_layer, data);
6256 func(shell, &shell->panel_layer, data);
6257 func(shell, &shell->background_layer, data);
6258 func(shell, &shell->lock_layer, data);
6259 func(shell, &shell->input_panel_layer, data);
6260
6261 wl_array_for_each(ws, &shell->workspaces.array)
6262 func(shell, &(*ws)->layer, data);
6263}
6264
6265static void
6266shell_output_destroy_move_layer(struct desktop_shell *shell,
6267 struct weston_layer *layer,
6268 void *data)
6269{
6270 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006271 struct weston_view *view;
6272
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006273 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006274 if (view->output != output)
6275 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006276
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006277 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006278 }
6279}
6280
6281static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006282handle_output_destroy(struct wl_listener *listener, void *data)
6283{
6284 struct shell_output *output_listener =
6285 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006286 struct weston_output *output = output_listener->output;
6287 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006288
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006289 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006290
Xiong Zhang6b481422013-10-23 13:58:32 +08006291 wl_list_remove(&output_listener->destroy_listener.link);
6292 wl_list_remove(&output_listener->link);
6293 free(output_listener);
6294}
6295
6296static void
6297create_shell_output(struct desktop_shell *shell,
6298 struct weston_output *output)
6299{
6300 struct shell_output *shell_output;
6301
6302 shell_output = zalloc(sizeof *shell_output);
6303 if (shell_output == NULL)
6304 return;
6305
6306 shell_output->output = output;
6307 shell_output->shell = shell;
6308 shell_output->destroy_listener.notify = handle_output_destroy;
6309 wl_signal_add(&output->destroy_signal,
6310 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006311 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006312}
6313
6314static void
6315handle_output_create(struct wl_listener *listener, void *data)
6316{
6317 struct desktop_shell *shell =
6318 container_of(listener, struct desktop_shell, output_create_listener);
6319 struct weston_output *output = (struct weston_output *)data;
6320
6321 create_shell_output(shell, output);
6322}
6323
6324static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006325handle_output_move_layer(struct desktop_shell *shell,
6326 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006327{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006328 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006329 struct weston_view *view;
6330 float x, y;
6331
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006332 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006333 if (view->output != output)
6334 continue;
6335
6336 x = view->geometry.x + output->move_x;
6337 y = view->geometry.y + output->move_y;
6338 weston_view_set_position(view, x, y);
6339 }
6340}
6341
6342static void
6343handle_output_move(struct wl_listener *listener, void *data)
6344{
6345 struct desktop_shell *shell;
6346
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006347 shell = container_of(listener, struct desktop_shell,
6348 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006349
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006350 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006351}
6352
6353static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006354setup_output_destroy_handler(struct weston_compositor *ec,
6355 struct desktop_shell *shell)
6356{
6357 struct weston_output *output;
6358
6359 wl_list_init(&shell->output_list);
6360 wl_list_for_each(output, &ec->output_list, link)
6361 create_shell_output(shell, output);
6362
6363 shell->output_create_listener.notify = handle_output_create;
6364 wl_signal_add(&ec->output_created_signal,
6365 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006366
6367 shell->output_move_listener.notify = handle_output_move;
6368 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006369}
6370
6371static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006372shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006373{
Tiago Vignattibe143262012-04-16 17:31:41 +03006374 struct desktop_shell *shell =
6375 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006376 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006377 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006378
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006379 /* Force state to unlocked so we don't try to fade */
6380 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006381
6382 if (shell->child.client) {
6383 /* disable respawn */
6384 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006385 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006386 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006387
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006388 wl_list_remove(&shell->idle_listener.link);
6389 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006390
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006391 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006392 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006393
Xiong Zhang6b481422013-10-23 13:58:32 +08006394 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6395 wl_list_remove(&shell_output->destroy_listener.link);
6396 wl_list_remove(&shell_output->link);
6397 free(shell_output);
6398 }
6399
6400 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006401 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006402
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006403 wl_array_for_each(ws, &shell->workspaces.array)
6404 workspace_destroy(*ws);
6405 wl_array_release(&shell->workspaces.array);
6406
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006407 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006408 free(shell);
6409}
6410
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006411static void
6412shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6413{
6414 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006415 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006416
6417 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006418 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6419 MODIFIER_CTRL | MODIFIER_ALT,
6420 terminate_binding, ec);
6421 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6422 click_to_activate_binding,
6423 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006424 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6425 click_to_activate_binding,
6426 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006427 weston_compositor_add_touch_binding(ec, 0,
6428 touch_to_activate_binding,
6429 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006430 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6431 MODIFIER_SUPER | MODIFIER_ALT,
6432 surface_opacity_binding, NULL);
6433 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6434 MODIFIER_SUPER, zoom_axis_binding,
6435 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006436
6437 /* configurable bindings */
6438 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006439 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6440 zoom_key_binding, NULL);
6441 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6442 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006443 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6444 maximize_binding, NULL);
6445 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6446 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006447 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6448 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006449 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006450 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006451 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006452 weston_compositor_add_button_binding(ec, BTN_LEFT,
6453 mod | MODIFIER_SHIFT,
6454 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006455
6456 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006457 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006458 rotate_binding, NULL);
6459
Daniel Stone325fc2d2012-05-30 16:31:58 +01006460 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6461 shell);
6462 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6463 ec);
6464 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6465 backlight_binding, ec);
6466 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6467 ec);
6468 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6469 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006470 weston_compositor_add_key_binding(ec, KEY_K, mod,
6471 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006472 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6473 workspace_up_binding, shell);
6474 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6475 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006476 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6477 workspace_move_surface_up_binding,
6478 shell);
6479 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6480 workspace_move_surface_down_binding,
6481 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006482
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006483 if (shell->exposay_modifier)
6484 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6485 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006486
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006487 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6488 if (shell->workspaces.num > 1) {
6489 num_workspace_bindings = shell->workspaces.num;
6490 if (num_workspace_bindings > 6)
6491 num_workspace_bindings = 6;
6492 for (i = 0; i < num_workspace_bindings; i++)
6493 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6494 workspace_f_binding,
6495 shell);
6496 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006497
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006498 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006499}
6500
Jason Ekstrand024177c2014-04-21 19:42:58 -05006501static void
6502handle_seat_created(struct wl_listener *listener, void *data)
6503{
6504 struct weston_seat *seat = data;
6505
6506 create_shell_seat(seat);
6507}
6508
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006509WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006510module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006511 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006512{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006513 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006514 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006515 struct workspace **pws;
6516 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006517 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006518
Peter Huttererf3d62272013-08-08 11:57:05 +10006519 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006520 if (shell == NULL)
6521 return -1;
6522
Kristian Høgsberg75840622011-09-06 13:48:16 -04006523 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006524
6525 shell->destroy_listener.notify = shell_destroy;
6526 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006527 shell->idle_listener.notify = idle_handler;
6528 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6529 shell->wake_listener.notify = wake_handler;
6530 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006531
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006532 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006533 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006534 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006535 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006536 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006537 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006538 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006539 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006540 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006541 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006542 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006543 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006544 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006545
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006546 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6547 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006548 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6549 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006550 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006551
6552 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006553 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006554
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006555 if (input_panel_setup(shell) < 0)
6556 return -1;
6557
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006558 shell->text_backend = text_backend_init(ec);
6559 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006560 return -1;
6561
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006562 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006563
Daniel Stonedf8133b2013-11-19 11:37:14 +01006564 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6565 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6566
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006567 for (i = 0; i < shell->workspaces.num; i++) {
6568 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6569 if (pws == NULL)
6570 return -1;
6571
6572 *pws = workspace_create();
6573 if (*pws == NULL)
6574 return -1;
6575 }
6576 activate_workspace(shell, 0);
6577
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006578 weston_layer_init(&shell->minimized_layer, NULL);
6579
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006580 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006581 wl_list_init(&shell->workspaces.animation.link);
6582 shell->workspaces.animation.frame = animate_workspace_change_frame;
6583
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006584 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006585 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006586 return -1;
6587
Rafael Antognollie2a34552013-12-03 15:35:45 -02006588 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6589 shell, bind_xdg_shell) == NULL)
6590 return -1;
6591
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006592 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006593 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006594 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006595 return -1;
6596
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006597 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6598 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006599 return -1;
6600
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006601 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006602
Jonny Lamb765760d2014-08-20 15:53:19 +02006603 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6604
Xiong Zhang6b481422013-10-23 13:58:32 +08006605 setup_output_destroy_handler(ec, shell);
6606
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006607 loop = wl_display_get_event_loop(ec->wl_display);
6608 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006609
Jason Ekstrand024177c2014-04-21 19:42:58 -05006610 wl_list_for_each(seat, &ec->seat_list, link)
6611 handle_seat_created(NULL, seat);
6612 shell->seat_create_listener.notify = handle_seat_created;
6613 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006614
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006615 screenshooter_create(ec);
6616
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006617 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006618
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006619 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006620
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006621 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6622
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006623 return 0;
6624}