blob: a215b9d8be59b3ef58594b0bab57218fe2898142 [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:
427
428 weston_view_to_global_float(view,
429 view->surface->width, 0,
430 &x, &y);
431
432 *width = (int) x;
433 *height = view->surface->height + (int) y;
434 return;
435
436 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
437 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
438 weston_view_to_global_float(view,
439 0, view->surface->height,
440 &x, &y);
441
442 *width = view->surface->width + (int) x;
443 *height = (int) y;
444 return;
445
446 default:
447 /* we've already set width and height to
448 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700449 break;
450 }
451 }
452
Jonny Lambd73c6942014-08-20 15:53:20 +0200453 /* the correct view wasn't found */
454}
455
456static void
457get_output_work_area(struct desktop_shell *shell,
458 struct weston_output *output,
459 pixman_rectangle32_t *area)
460{
461 int32_t panel_width = 0, panel_height = 0;
462
463 area->x = 0;
464 area->y = 0;
465
466 get_output_panel_size(shell, output, &panel_width, &panel_height);
467
468 switch (shell->panel_position) {
469 case DESKTOP_SHELL_PANEL_POSITION_TOP:
470 default:
471 area->y = panel_height;
472 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
473 area->width = output->width;
474 area->height = output->height - panel_height;
475 break;
476 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
477 area->x = panel_width;
478 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
479 area->width = output->width - panel_width;
480 area->height = output->height;
481 break;
482 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700483}
484
485static void
486send_configure_for_surface(struct shell_surface *shsurf)
487{
488 int32_t width, height;
489 struct surface_state *state;
490
491 if (shsurf->state_requested)
492 state = &shsurf->requested_state;
493 else if (shsurf->state_changed)
494 state = &shsurf->next_state;
495 else
496 state = &shsurf->state;
497
498 if (state->fullscreen) {
499 width = shsurf->output->width;
500 height = shsurf->output->height;
501 } else if (state->maximized) {
502 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200503 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700504
505 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200506 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700507
Jonny Lambd73c6942014-08-20 15:53:20 +0200508 width = area.width;
509 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900510 } else if (shsurf->resize_edges) {
511 width = shsurf->geometry.width;
512 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700513 } else {
514 width = 0;
515 height = 0;
516 }
517
518 shsurf->client->send_configure(shsurf->surface, width, height);
519}
520
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300521static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400522shell_surface_state_changed(struct shell_surface *shsurf)
523{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700524 if (shell_surface_is_xdg_surface(shsurf))
525 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400526}
527
528static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300529shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300530{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700531 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400532 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700533 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400534
535 if (grab->shsurf->resize_edges) {
536 grab->shsurf->resize_edges = 0;
537 shell_surface_state_changed(grab->shsurf);
538 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700539 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300540
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700541 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300542}
543
544static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700545shell_touch_grab_start(struct shell_touch_grab *grab,
546 const struct weston_touch_grab_interface *interface,
547 struct shell_surface *shsurf,
548 struct weston_touch *touch)
549{
550 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800551
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200552 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700553 if (touch->seat->pointer)
554 popup_grab_end(touch->seat->pointer);
555
Rusty Lynch1084da52013-08-15 09:10:08 -0700556 grab->grab.interface = interface;
557 grab->shsurf = shsurf;
558 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
559 wl_signal_add(&shsurf->destroy_signal,
560 &grab->shsurf_destroy_listener);
561
562 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700563 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700564
565 weston_touch_start_grab(touch, &grab->grab);
566 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500567 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500568 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700569}
570
571static void
572shell_touch_grab_end(struct shell_touch_grab *grab)
573{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700574 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700575 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700576 grab->shsurf->grabbed = 0;
577 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700578
579 weston_touch_end_grab(grab->touch);
580}
581
582static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800584 struct weston_output *output);
585
Daniel Stone496ca172012-05-30 16:31:42 +0100586static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300587get_modifier(char *modifier)
588{
589 if (!modifier)
590 return MODIFIER_SUPER;
591
592 if (!strcmp("ctrl", modifier))
593 return MODIFIER_CTRL;
594 else if (!strcmp("alt", modifier))
595 return MODIFIER_ALT;
596 else if (!strcmp("super", modifier))
597 return MODIFIER_SUPER;
598 else
599 return MODIFIER_SUPER;
600}
601
Juan Zhaoe10d2792012-04-25 19:09:52 +0800602static enum animation_type
603get_animation_type(char *animation)
604{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800605 if (!animation)
606 return ANIMATION_NONE;
607
Juan Zhaoe10d2792012-04-25 19:09:52 +0800608 if (!strcmp("zoom", animation))
609 return ANIMATION_ZOOM;
610 else if (!strcmp("fade", animation))
611 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100612 else if (!strcmp("dim-layer", animation))
613 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800614 else
615 return ANIMATION_NONE;
616}
617
Alex Wu4539b082012-03-01 12:57:46 +0800618static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400619shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200620{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400621 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500622 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300623 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200624
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400625 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400626 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300627 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
628 WESTON_SHELL_CLIENT);
629 if (ret < 0)
630 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400631 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500632 "client", &s, client);
633 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100634 shell->client = s;
635 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400636 "binding-modifier", &s, "super");
637 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200638 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800639
640 weston_config_section_get_string(section,
641 "exposay-modifier", &s, "none");
642 if (strcmp(s, "none") == 0)
643 shell->exposay_modifier = 0;
644 else
645 shell->exposay_modifier = get_modifier(s);
646 free(s);
647
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400648 weston_config_section_get_string(section, "animation", &s, "none");
649 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200650 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200651 weston_config_section_get_string(section, "close-animation", &s, "fade");
652 shell->win_close_animation_type = get_animation_type(s);
653 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700654 weston_config_section_get_string(section,
655 "startup-animation", &s, "fade");
656 shell->startup_animation_type = get_animation_type(s);
657 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700658 if (shell->startup_animation_type == ANIMATION_ZOOM)
659 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100660 weston_config_section_get_string(section, "focus-animation", &s, "none");
661 shell->focus_animation_type = get_animation_type(s);
662 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400663 weston_config_section_get_uint(section, "num-workspaces",
664 &shell->workspaces.num,
665 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200666}
667
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800668struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100669get_default_output(struct weston_compositor *compositor)
670{
671 return container_of(compositor->output_list.next,
672 struct weston_output, link);
673}
674
Pekka Paalanen8274d902014-08-06 19:36:51 +0300675static int
676focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
677{
678 return snprintf(buf, len, "focus highlight effect for output %s",
679 surface->output->name);
680}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681
682/* no-op func for checking focus surface */
683static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600684focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100685{
686}
687
688static struct focus_surface *
689get_focus_surface(struct weston_surface *surface)
690{
691 if (surface->configure == focus_surface_configure)
692 return surface->configure_private;
693 else
694 return NULL;
695}
696
697static bool
698is_focus_surface (struct weston_surface *es)
699{
700 return (es->configure == focus_surface_configure);
701}
702
703static bool
704is_focus_view (struct weston_view *view)
705{
706 return is_focus_surface (view->surface);
707}
708
709static struct focus_surface *
710create_focus_surface(struct weston_compositor *ec,
711 struct weston_output *output)
712{
713 struct focus_surface *fsurf = NULL;
714 struct weston_surface *surface = NULL;
715
716 fsurf = malloc(sizeof *fsurf);
717 if (!fsurf)
718 return NULL;
719
720 fsurf->surface = weston_surface_create(ec);
721 surface = fsurf->surface;
722 if (surface == NULL) {
723 free(fsurf);
724 return NULL;
725 }
726
727 surface->configure = focus_surface_configure;
728 surface->output = output;
729 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300730 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100731
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800732 fsurf->view = weston_view_create(surface);
733 if (fsurf->view == NULL) {
734 weston_surface_destroy(surface);
735 free(fsurf);
736 return NULL;
737 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100738 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100739
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600740 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600741 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100742 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
743 pixman_region32_fini(&surface->opaque);
744 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
745 output->width, output->height);
746 pixman_region32_fini(&surface->input);
747 pixman_region32_init(&surface->input);
748
749 wl_list_init(&fsurf->workspace_transform.link);
750
751 return fsurf;
752}
753
754static void
755focus_surface_destroy(struct focus_surface *fsurf)
756{
757 weston_surface_destroy(fsurf->surface);
758 free(fsurf);
759}
760
761static void
762focus_animation_done(struct weston_view_animation *animation, void *data)
763{
764 struct workspace *ws = data;
765
766 ws->focus_animation = NULL;
767}
768
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200769static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200770focus_state_destroy(struct focus_state *state)
771{
772 wl_list_remove(&state->seat_destroy_listener.link);
773 wl_list_remove(&state->surface_destroy_listener.link);
774 free(state);
775}
776
777static void
778focus_state_seat_destroy(struct wl_listener *listener, void *data)
779{
780 struct focus_state *state = container_of(listener,
781 struct focus_state,
782 seat_destroy_listener);
783
784 wl_list_remove(&state->link);
785 focus_state_destroy(state);
786}
787
788static void
789focus_state_surface_destroy(struct wl_listener *listener, void *data)
790{
791 struct focus_state *state = container_of(listener,
792 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400793 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400794 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 struct weston_surface *main_surface, *next;
796 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200797
Pekka Paalanen01388e22013-04-25 13:57:44 +0300798 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
799
Kristian Høgsberge3778222012-07-31 17:29:30 -0400800 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300801 wl_list_for_each(view,
802 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400804 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100805 if (is_focus_view(view))
806 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400807
Jason Ekstranda7af7042013-10-12 22:38:11 -0500808 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400809 break;
810 }
811
Pekka Paalanen01388e22013-04-25 13:57:44 +0300812 /* if the focus was a sub-surface, activate its main surface */
813 if (main_surface != state->keyboard_focus)
814 next = main_surface;
815
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100816 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400817 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100818 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100819 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400820 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100821 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
822 if (state->ws->focus_animation)
823 weston_view_animation_destroy(state->ws->focus_animation);
824
825 state->ws->focus_animation = weston_fade_run(
826 state->ws->fsurf_front->view,
827 state->ws->fsurf_front->view->alpha, 0.0, 300,
828 focus_animation_done, state->ws);
829 }
830
Kristian Høgsberge3778222012-07-31 17:29:30 -0400831 wl_list_remove(&state->link);
832 focus_state_destroy(state);
833 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200834}
835
836static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400837focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200838{
Jonas Ådahl04769742012-06-13 00:01:24 +0200839 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200840
841 state = malloc(sizeof *state);
842 if (state == NULL)
843 return NULL;
844
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100845 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400846 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200847 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400848 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200849
850 state->seat_destroy_listener.notify = focus_state_seat_destroy;
851 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400852 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200853 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400854 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200855
856 return state;
857}
858
Jonas Ådahl8538b222012-08-29 22:13:03 +0200859static struct focus_state *
860ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
861{
862 struct workspace *ws = get_current_workspace(shell);
863 struct focus_state *state;
864
865 wl_list_for_each(state, &ws->focus_list, link)
866 if (state->seat == seat)
867 break;
868
869 if (&state->link == &ws->focus_list)
870 state = focus_state_create(seat, ws);
871
872 return state;
873}
874
Jonas Ådahl04769742012-06-13 00:01:24 +0200875static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800876focus_state_set_focus(struct focus_state *state,
877 struct weston_surface *surface)
878{
879 if (state->keyboard_focus) {
880 wl_list_remove(&state->surface_destroy_listener.link);
881 wl_list_init(&state->surface_destroy_listener.link);
882 }
883
884 state->keyboard_focus = surface;
885 if (surface)
886 wl_signal_add(&surface->destroy_signal,
887 &state->surface_destroy_listener);
888}
889
890static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400891restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200892{
893 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400894 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100895 struct wl_list pending_seat_list;
896 struct weston_seat *seat, *next_seat;
897
898 /* Temporarily steal the list of seats so that we can keep
899 * track of the seats we've already processed */
900 wl_list_init(&pending_seat_list);
901 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
902 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200903
904 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100905 wl_list_remove(&state->seat->link);
906 wl_list_insert(&shell->compositor->seat_list,
907 &state->seat->link);
908
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800909 if (state->seat->keyboard == NULL)
910 continue;
911
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400912 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200913
Kristian Høgsberge3148752013-05-06 23:19:49 -0400914 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200915 }
Neil Roberts4237f502014-04-09 16:33:31 +0100916
917 /* For any remaining seats that we don't have a focus state
918 * for we'll reset the keyboard focus to NULL */
919 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
920 wl_list_insert(&shell->compositor->seat_list, &seat->link);
921
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300922 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100923 continue;
924
925 weston_keyboard_set_focus(seat->keyboard, NULL);
926 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200927}
928
929static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
931 struct weston_seat *seat)
932{
933 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200934
935 wl_list_for_each(state, &ws->focus_list, link) {
936 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800937 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 return;
939 }
940 }
941}
942
943static void
944drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
945 struct weston_surface *surface)
946{
947 struct focus_state *state;
948
949 wl_list_for_each(state, &ws->focus_list, link)
950 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800951 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200952}
953
954static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100955animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
956 struct weston_view *from, struct weston_view *to)
957{
958 struct weston_output *output;
959 bool focus_surface_created = false;
960
961 /* FIXME: Only support dim animation using two layers */
962 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
963 return;
964
965 output = get_default_output(shell->compositor);
966 if (ws->fsurf_front == NULL && (from || to)) {
967 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800968 if (ws->fsurf_front == NULL)
969 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100970 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800971
972 ws->fsurf_back = create_focus_surface(shell->compositor, output);
973 if (ws->fsurf_back == NULL) {
974 focus_surface_destroy(ws->fsurf_front);
975 return;
976 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800978
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100979 focus_surface_created = true;
980 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300981 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
982 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983 }
984
985 if (ws->focus_animation) {
986 weston_view_animation_destroy(ws->focus_animation);
987 ws->focus_animation = NULL;
988 }
989
990 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300991 weston_layer_entry_insert(&to->layer_link,
992 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300994 weston_layer_entry_insert(&ws->layer.view_list,
995 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996
997 if (focus_surface_created) {
998 ws->focus_animation = weston_fade_run(
999 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001000 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001001 focus_animation_done, ws);
1002 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001003 weston_layer_entry_insert(&from->layer_link,
1004 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001005 ws->focus_animation = weston_stable_fade_run(
1006 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001007 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001008 focus_animation_done, ws);
1009 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001010 weston_layer_entry_insert(&ws->layer.view_list,
1011 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 ws->focus_animation = weston_stable_fade_run(
1013 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001014 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 focus_animation_done, ws);
1016 }
1017}
1018
1019static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001020workspace_destroy(struct workspace *ws)
1021{
Jonas Ådahl04769742012-06-13 00:01:24 +02001022 struct focus_state *state, *next;
1023
1024 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1025 focus_state_destroy(state);
1026
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001027 if (ws->fsurf_front)
1028 focus_surface_destroy(ws->fsurf_front);
1029 if (ws->fsurf_back)
1030 focus_surface_destroy(ws->fsurf_back);
1031
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001032 free(ws);
1033}
1034
Jonas Ådahl04769742012-06-13 00:01:24 +02001035static void
1036seat_destroyed(struct wl_listener *listener, void *data)
1037{
1038 struct weston_seat *seat = data;
1039 struct focus_state *state, *next;
1040 struct workspace *ws = container_of(listener,
1041 struct workspace,
1042 seat_destroyed_listener);
1043
1044 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1045 if (state->seat == seat)
1046 wl_list_remove(&state->link);
1047}
1048
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001049static struct workspace *
1050workspace_create(void)
1051{
1052 struct workspace *ws = malloc(sizeof *ws);
1053 if (ws == NULL)
1054 return NULL;
1055
1056 weston_layer_init(&ws->layer, NULL);
1057
Jonas Ådahl04769742012-06-13 00:01:24 +02001058 wl_list_init(&ws->focus_list);
1059 wl_list_init(&ws->seat_destroyed_listener.link);
1060 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001061 ws->fsurf_front = NULL;
1062 ws->fsurf_back = NULL;
1063 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001064
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001065 return ws;
1066}
1067
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001068static int
1069workspace_is_empty(struct workspace *ws)
1070{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001071 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001072}
1073
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001074static struct workspace *
1075get_workspace(struct desktop_shell *shell, unsigned int index)
1076{
1077 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001078 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001079 pws += index;
1080 return *pws;
1081}
1082
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001083struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001084get_current_workspace(struct desktop_shell *shell)
1085{
1086 return get_workspace(shell, shell->workspaces.current);
1087}
1088
1089static void
1090activate_workspace(struct desktop_shell *shell, unsigned int index)
1091{
1092 struct workspace *ws;
1093
1094 ws = get_workspace(shell, index);
1095 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1096
1097 shell->workspaces.current = index;
1098}
1099
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100static unsigned int
1101get_output_height(struct weston_output *output)
1102{
1103 return abs(output->region.extents.y1 - output->region.extents.y2);
1104}
1105
1106static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001107view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109 struct weston_transform *transform;
1110
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001111 if (is_focus_view(view)) {
1112 struct focus_surface *fsurf = get_focus_surface(view->surface);
1113 transform = &fsurf->workspace_transform;
1114 } else {
1115 struct shell_surface *shsurf = get_shell_surface(view->surface);
1116 transform = &shsurf->workspace_transform;
1117 }
1118
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001121 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001123 weston_matrix_init(&transform->matrix);
1124 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127}
1128
1129static void
1130workspace_translate_out(struct workspace *ws, double fraction)
1131{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 unsigned int height;
1134 double d;
1135
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001136 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001138 d = height * fraction;
1139
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001140 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001141 }
1142}
1143
1144static void
1145workspace_translate_in(struct workspace *ws, double fraction)
1146{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001148 unsigned int height;
1149 double d;
1150
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001151 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001153
1154 if (fraction > 0)
1155 d = -(height - height * fraction);
1156 else
1157 d = height + height * fraction;
1158
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001159 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 }
1161}
1162
1163static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001164broadcast_current_workspace_state(struct desktop_shell *shell)
1165{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001166 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001167
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001168 wl_resource_for_each(resource, &shell->workspaces.client_list)
1169 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001170 shell->workspaces.current,
1171 shell->workspaces.num);
1172}
1173
1174static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001175reverse_workspace_change_animation(struct desktop_shell *shell,
1176 unsigned int index,
1177 struct workspace *from,
1178 struct workspace *to)
1179{
1180 shell->workspaces.current = index;
1181
1182 shell->workspaces.anim_to = to;
1183 shell->workspaces.anim_from = from;
1184 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1185 shell->workspaces.anim_timestamp = 0;
1186
Scott Moreau4272e632012-08-13 09:58:41 -06001187 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001188}
1189
1190static void
1191workspace_deactivate_transforms(struct workspace *ws)
1192{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001194 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001196 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001197 if (is_focus_view(view)) {
1198 struct focus_surface *fsurf = get_focus_surface(view->surface);
1199 transform = &fsurf->workspace_transform;
1200 } else {
1201 struct shell_surface *shsurf = get_shell_surface(view->surface);
1202 transform = &shsurf->workspace_transform;
1203 }
1204
1205 if (!wl_list_empty(&transform->link)) {
1206 wl_list_remove(&transform->link);
1207 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001210 }
1211}
1212
1213static void
1214finish_workspace_change_animation(struct desktop_shell *shell,
1215 struct workspace *from,
1216 struct workspace *to)
1217{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001218 struct weston_view *view;
1219
Scott Moreau4272e632012-08-13 09:58:41 -06001220 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001221
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001222 /* Views that extend past the bottom of the output are still
1223 * visible after the workspace animation ends but before its layer
1224 * is hidden. In that case, we need to damage below those views so
1225 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001226 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001227 weston_view_damage_below(view);
1228
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001229 wl_list_remove(&shell->workspaces.animation.link);
1230 workspace_deactivate_transforms(from);
1231 workspace_deactivate_transforms(to);
1232 shell->workspaces.anim_to = NULL;
1233
1234 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1235}
1236
1237static void
1238animate_workspace_change_frame(struct weston_animation *animation,
1239 struct weston_output *output, uint32_t msecs)
1240{
1241 struct desktop_shell *shell =
1242 container_of(animation, struct desktop_shell,
1243 workspaces.animation);
1244 struct workspace *from = shell->workspaces.anim_from;
1245 struct workspace *to = shell->workspaces.anim_to;
1246 uint32_t t;
1247 double x, y;
1248
1249 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1250 finish_workspace_change_animation(shell, from, to);
1251 return;
1252 }
1253
1254 if (shell->workspaces.anim_timestamp == 0) {
1255 if (shell->workspaces.anim_current == 0.0)
1256 shell->workspaces.anim_timestamp = msecs;
1257 else
1258 shell->workspaces.anim_timestamp =
1259 msecs -
1260 /* Invers of movement function 'y' below. */
1261 (asin(1.0 - shell->workspaces.anim_current) *
1262 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1263 M_2_PI);
1264 }
1265
1266 t = msecs - shell->workspaces.anim_timestamp;
1267
1268 /*
1269 * x = [0, π/2]
1270 * y(x) = sin(x)
1271 */
1272 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1273 y = sin(x);
1274
1275 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001276 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001277
1278 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1279 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1280 shell->workspaces.anim_current = y;
1281
Scott Moreau4272e632012-08-13 09:58:41 -06001282 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001283 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001284 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001285 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001286}
1287
1288static void
1289animate_workspace_change(struct desktop_shell *shell,
1290 unsigned int index,
1291 struct workspace *from,
1292 struct workspace *to)
1293{
1294 struct weston_output *output;
1295
1296 int dir;
1297
1298 if (index > shell->workspaces.current)
1299 dir = -1;
1300 else
1301 dir = 1;
1302
1303 shell->workspaces.current = index;
1304
1305 shell->workspaces.anim_dir = dir;
1306 shell->workspaces.anim_from = from;
1307 shell->workspaces.anim_to = to;
1308 shell->workspaces.anim_current = 0.0;
1309 shell->workspaces.anim_timestamp = 0;
1310
1311 output = container_of(shell->compositor->output_list.next,
1312 struct weston_output, link);
1313 wl_list_insert(&output->animation_list,
1314 &shell->workspaces.animation.link);
1315
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001316 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001317
1318 workspace_translate_in(to, 0);
1319
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001320 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001321
Scott Moreau4272e632012-08-13 09:58:41 -06001322 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001323}
1324
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001325static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001326update_workspace(struct desktop_shell *shell, unsigned int index,
1327 struct workspace *from, struct workspace *to)
1328{
1329 shell->workspaces.current = index;
1330 wl_list_insert(&from->layer.link, &to->layer.link);
1331 wl_list_remove(&from->layer.link);
1332}
1333
1334static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001335change_workspace(struct desktop_shell *shell, unsigned int index)
1336{
1337 struct workspace *from;
1338 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001339 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001340
1341 if (index == shell->workspaces.current)
1342 return;
1343
1344 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001345 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001346 return;
1347
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001348 from = get_current_workspace(shell);
1349 to = get_workspace(shell, index);
1350
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001351 if (shell->workspaces.anim_from == to &&
1352 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001353 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001354 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001355 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001356 return;
1357 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001358
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001359 if (shell->workspaces.anim_to != NULL)
1360 finish_workspace_change_animation(shell,
1361 shell->workspaces.anim_from,
1362 shell->workspaces.anim_to);
1363
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001364 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001365
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001366 if (shell->focus_animation_type != ANIMATION_NONE) {
1367 wl_list_for_each(state, &from->focus_list, link)
1368 if (state->keyboard_focus)
1369 animate_focus_change(shell, from,
1370 get_default_view(state->keyboard_focus), NULL);
1371
1372 wl_list_for_each(state, &to->focus_list, link)
1373 if (state->keyboard_focus)
1374 animate_focus_change(shell, to,
1375 NULL, get_default_view(state->keyboard_focus));
1376 }
1377
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378 if (workspace_is_empty(to) && workspace_is_empty(from))
1379 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001380 else
1381 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001382
1383 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001384}
1385
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001386static bool
1387workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1388{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001389 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001390 struct wl_list *e;
1391
1392 if (wl_list_empty(list))
1393 return false;
1394
1395 e = list->next;
1396
1397 if (e->next != list)
1398 return false;
1399
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001400 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001401}
1402
1403static void
Philip Withnall659163d2013-11-25 18:01:36 +00001404move_surface_to_workspace(struct desktop_shell *shell,
1405 struct shell_surface *shsurf,
1406 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001407{
1408 struct workspace *from;
1409 struct workspace *to;
1410 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001411 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001412 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001413
1414 if (workspace == shell->workspaces.current)
1415 return;
1416
Philip Withnall659163d2013-11-25 18:01:36 +00001417 view = get_default_view(shsurf->surface);
1418 if (!view)
1419 return;
1420
1421 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1422
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001423 if (workspace >= shell->workspaces.num)
1424 workspace = shell->workspaces.num - 1;
1425
Jonas Ådahle9d22502012-08-29 22:13:01 +02001426 from = get_current_workspace(shell);
1427 to = get_workspace(shell, workspace);
1428
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001429 weston_layer_entry_remove(&view->layer_link);
1430 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001431
Philip Withnall648a4dd2013-11-25 18:01:44 +00001432 shell_surface_update_child_surface_layers(shsurf);
1433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001435 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1436 if (!seat->keyboard)
1437 continue;
1438
1439 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001441 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001442 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001443
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001445}
1446
1447static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001448take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001449 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001450 unsigned int index)
1451{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001452 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001454 struct shell_surface *shsurf;
1455 struct workspace *from;
1456 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001457 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001458
Pekka Paalanen01388e22013-04-25 13:57:44 +03001459 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 view = get_default_view(surface);
1461 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001462 index == shell->workspaces.current ||
1463 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001464 return;
1465
1466 from = get_current_workspace(shell);
1467 to = get_workspace(shell, index);
1468
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001469 weston_layer_entry_remove(&view->layer_link);
1470 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001471
Philip Withnall659163d2013-11-25 18:01:36 +00001472 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001473 if (shsurf != NULL)
1474 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001475
Jonas Ådahle9d22502012-08-29 22:13:01 +02001476 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001477 drop_focus_state(shell, from, surface);
1478
1479 if (shell->workspaces.anim_from == to &&
1480 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001481 wl_list_remove(&to->layer.link);
1482 wl_list_insert(from->layer.link.prev, &to->layer.link);
1483
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001484 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001485 broadcast_current_workspace_state(shell);
1486
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001487 return;
1488 }
1489
1490 if (shell->workspaces.anim_to != NULL)
1491 finish_workspace_change_animation(shell,
1492 shell->workspaces.anim_from,
1493 shell->workspaces.anim_to);
1494
1495 if (workspace_is_empty(from) &&
1496 workspace_has_only(to, surface))
1497 update_workspace(shell, index, from, to);
1498 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001499 if (shsurf != NULL &&
1500 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001501 wl_list_insert(&shell->workspaces.anim_sticky_list,
1502 &shsurf->workspace_transform.link);
1503
1504 animate_workspace_change(shell, index, from, to);
1505 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001506
1507 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001508
1509 state = ensure_focus_state(shell, seat);
1510 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001511 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001512}
1513
1514static void
1515workspace_manager_move_surface(struct wl_client *client,
1516 struct wl_resource *resource,
1517 struct wl_resource *surface_resource,
1518 uint32_t workspace)
1519{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001520 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001521 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001522 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001523 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001524 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001525
Pekka Paalanen01388e22013-04-25 13:57:44 +03001526 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001527 shell_surface = get_shell_surface(main_surface);
1528 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001530
1531 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001532}
1533
1534static const struct workspace_manager_interface workspace_manager_implementation = {
1535 workspace_manager_move_surface,
1536};
1537
1538static void
1539unbind_resource(struct wl_resource *resource)
1540{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001541 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001542}
1543
1544static void
1545bind_workspace_manager(struct wl_client *client,
1546 void *data, uint32_t version, uint32_t id)
1547{
1548 struct desktop_shell *shell = data;
1549 struct wl_resource *resource;
1550
Jason Ekstranda85118c2013-06-27 20:17:02 -05001551 resource = wl_resource_create(client,
1552 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001553
1554 if (resource == NULL) {
1555 weston_log("couldn't add workspace manager object");
1556 return;
1557 }
1558
Jason Ekstranda85118c2013-06-27 20:17:02 -05001559 wl_resource_set_implementation(resource,
1560 &workspace_manager_implementation,
1561 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001562 wl_list_insert(&shell->workspaces.client_list,
1563 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001564
1565 workspace_manager_send_state(resource,
1566 shell->workspaces.current,
1567 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001568}
1569
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001570static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001571touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1572 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1573{
1574}
1575
1576static void
1577touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1578{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001579 struct weston_touch_move_grab *move =
1580 (struct weston_touch_move_grab *) container_of(
1581 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001582
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001583 if (touch_id == 0)
1584 move->active = 0;
1585
Jonas Ådahl9484b692013-12-02 22:05:03 +01001586 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001587 shell_touch_grab_end(&move->base);
1588 free(move);
1589 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001590}
1591
1592static void
1593touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1594 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1595{
1596 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1597 struct shell_surface *shsurf = move->base.shsurf;
1598 struct weston_surface *es;
1599 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1600 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1601
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001602 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001603 return;
1604
1605 es = shsurf->surface;
1606
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001607 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001608
1609 weston_compositor_schedule_repaint(es->compositor);
1610}
1611
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001612static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001613touch_move_grab_frame(struct weston_touch_grab *grab)
1614{
1615}
1616
1617static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001618touch_move_grab_cancel(struct weston_touch_grab *grab)
1619{
1620 struct weston_touch_move_grab *move =
1621 (struct weston_touch_move_grab *) container_of(
1622 grab, struct shell_touch_grab, grab);
1623
1624 shell_touch_grab_end(&move->base);
1625 free(move);
1626}
1627
Rusty Lynch1084da52013-08-15 09:10:08 -07001628static const struct weston_touch_grab_interface touch_move_grab_interface = {
1629 touch_move_grab_down,
1630 touch_move_grab_up,
1631 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001632 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001633 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001634};
1635
1636static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001637surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001638{
1639 struct weston_touch_move_grab *move;
1640
1641 if (!shsurf)
1642 return -1;
1643
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001644 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001645 return 0;
1646
1647 move = malloc(sizeof *move);
1648 if (!move)
1649 return -1;
1650
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001651 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001653 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001655 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001656
1657 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001658 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001659
1660 return 0;
1661}
1662
1663static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001664noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001665{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001666}
1667
1668static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001669constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1670{
1671 struct shell_surface *shsurf = move->base.shsurf;
1672 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001673 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001674 const int safety = 50;
1675
1676 x = wl_fixed_to_int(pointer->x + move->dx);
1677 y = wl_fixed_to_int(pointer->y + move->dy);
1678
Jonny Lambd73c6942014-08-20 15:53:20 +02001679 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1680 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1681 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001682
Jonny Lambd73c6942014-08-20 15:53:20 +02001683 bottom = y + shsurf->geometry.height;
1684 if (bottom - panel_height < safety)
1685 y = panel_height + safety -
1686 shsurf->geometry.height;
1687
1688 if (move->client_initiated &&
1689 y + shsurf->geometry.y < panel_height)
1690 y = panel_height - shsurf->geometry.y;
1691 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001692
1693 *cx = x;
1694 *cy = y;
1695}
1696
1697static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001698move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1699 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001700{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001701 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001702 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001703 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001704 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001705
1706 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001707 if (!shsurf)
1708 return;
1709
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001710 constrain_position(move, &cx, &cy);
1711
1712 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001713
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001715}
1716
1717static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001718move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001719 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001720{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001721 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1722 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001723 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001724 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001725
Daniel Stone4dbadb12012-05-30 16:31:51 +01001726 if (pointer->button_count == 0 &&
1727 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001728 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001729 free(grab);
1730 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001731}
1732
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001733static void
1734move_grab_cancel(struct weston_pointer_grab *grab)
1735{
1736 struct shell_grab *shell_grab =
1737 container_of(grab, struct shell_grab, grab);
1738
1739 shell_grab_end(shell_grab);
1740 free(grab);
1741}
1742
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001743static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001744 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001745 move_grab_motion,
1746 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001747 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001748};
1749
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001751surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001752 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753{
1754 struct weston_move_grab *move;
1755
1756 if (!shsurf)
1757 return -1;
1758
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001759 if (shsurf->grabbed ||
1760 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001761 return 0;
1762
1763 move = malloc(sizeof *move);
1764 if (!move)
1765 return -1;
1766
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001768 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001770 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001771 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001772
1773 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Derek Foreman794fa0e2015-07-15 13:00:38 -05001774 pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001775
1776 return 0;
1777}
1778
1779static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001780common_surface_move(struct wl_resource *resource,
1781 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001782{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001783 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001784 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001785 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001786
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001787 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001788 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001789 seat->pointer->button_count > 0 &&
1790 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001792 if ((surface == shsurf->surface) &&
Derek Foreman794fa0e2015-07-15 13:00:38 -05001793 (surface_move(shsurf, seat->pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001794 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001795 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001796 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001797 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001799 if ((surface == shsurf->surface) &&
Derek Foremanb7674ae2015-07-15 13:00:37 -05001800 (surface_touch_move(shsurf, seat->touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001801 wl_resource_post_no_memory(resource);
1802 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001803}
1804
Rafael Antognollie2a34552013-12-03 15:35:45 -02001805static void
1806shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1807 struct wl_resource *seat_resource, uint32_t serial)
1808{
1809 common_surface_move(resource, seat_resource, serial);
1810}
1811
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001812struct weston_resize_grab {
1813 struct shell_grab base;
1814 uint32_t edges;
1815 int32_t width, height;
1816};
1817
1818static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001819resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1820 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821{
1822 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001823 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001824 struct shell_surface *shsurf = resize->base.shsurf;
1825 int32_t width, height;
1826 wl_fixed_t from_x, from_y;
1827 wl_fixed_t to_x, to_y;
1828
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001829 weston_pointer_move(pointer, x, y);
1830
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001831 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001832 return;
1833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 weston_view_from_global_fixed(shsurf->view,
1835 pointer->grab_x, pointer->grab_y,
1836 &from_x, &from_y);
1837 weston_view_from_global_fixed(shsurf->view,
1838 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001839
1840 width = resize->width;
1841 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1842 width += wl_fixed_to_int(from_x - to_x);
1843 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1844 width += wl_fixed_to_int(to_x - from_x);
1845 }
1846
1847 height = resize->height;
1848 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1849 height += wl_fixed_to_int(from_y - to_y);
1850 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1851 height += wl_fixed_to_int(to_y - from_y);
1852 }
1853
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001854 if (width < 1)
1855 width = 1;
1856 if (height < 1)
1857 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001858 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001859}
1860
1861static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001862send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001863{
1864 struct shell_surface *shsurf = get_shell_surface(surface);
1865
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001866 assert(shsurf);
1867
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001868 if (shsurf->resource)
1869 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001870 shsurf->resize_edges,
1871 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001872}
1873
1874static const struct weston_shell_client shell_client = {
1875 send_configure
1876};
1877
1878static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001879resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001880 uint32_t time, uint32_t button, uint32_t state_w)
1881{
1882 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001883 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001884 enum wl_pointer_button_state state = state_w;
1885
1886 if (pointer->button_count == 0 &&
1887 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1888 shell_grab_end(&resize->base);
1889 free(grab);
1890 }
1891}
1892
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001893static void
1894resize_grab_cancel(struct weston_pointer_grab *grab)
1895{
1896 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1897
1898 shell_grab_end(&resize->base);
1899 free(grab);
1900}
1901
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001902static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001903 noop_grab_focus,
1904 resize_grab_motion,
1905 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001906 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001907};
1908
Giulio Camuffob8366642013-04-25 13:57:46 +03001909/*
1910 * Returns the bounding box of a surface and all its sub-surfaces,
1911 * in the surface coordinates system. */
1912static void
1913surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1914 int32_t *y, int32_t *w, int32_t *h) {
1915 pixman_region32_t region;
1916 pixman_box32_t *box;
1917 struct weston_subsurface *subsurface;
1918
1919 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 surface->width,
1921 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001922
1923 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1924 pixman_region32_union_rect(&region, &region,
1925 subsurface->position.x,
1926 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001927 subsurface->surface->width,
1928 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001929 }
1930
1931 box = pixman_region32_extents(&region);
1932 if (x)
1933 *x = box->x1;
1934 if (y)
1935 *y = box->y1;
1936 if (w)
1937 *w = box->x2 - box->x1;
1938 if (h)
1939 *h = box->y2 - box->y1;
1940
1941 pixman_region32_fini(&region);
1942}
1943
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001944static int
1945surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001946 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001947{
1948 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001949 const unsigned resize_topbottom =
1950 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1951 const unsigned resize_leftright =
1952 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1953 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001954
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001955 if (shsurf->grabbed ||
1956 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001957 return 0;
1958
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001959 /* Check for invalid edge combinations. */
1960 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1961 (edges & resize_topbottom) == resize_topbottom ||
1962 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001963 return 0;
1964
1965 resize = malloc(sizeof *resize);
1966 if (!resize)
1967 return -1;
1968
1969 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001970
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001971 resize->width = shsurf->geometry.width;
1972 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001973
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001974 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001975 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001976 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001977 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001978
1979 return 0;
1980}
1981
1982static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001983common_surface_resize(struct wl_resource *resource,
1984 struct wl_resource *seat_resource, uint32_t serial,
1985 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001986{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001987 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001988 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001989 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001990
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001991 if (seat->pointer == NULL ||
1992 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001993 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001994 seat->pointer->focus == NULL)
1995 return;
1996
1997 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1998 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001999 return;
2000
Kristian Høgsberge3148752013-05-06 23:19:49 -04002001 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002002 wl_resource_post_no_memory(resource);
2003}
2004
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002005static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002006shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2007 struct wl_resource *seat_resource, uint32_t serial,
2008 uint32_t edges)
2009{
2010 common_surface_resize(resource, seat_resource, serial, edges);
2011}
2012
2013static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002014busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002015{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002016 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002017 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002019 wl_fixed_t sx, sy;
2020
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 view = weston_compositor_pick_view(pointer->seat->compositor,
2022 pointer->x, pointer->y,
2023 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002024
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002025 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2026 shell_grab_end(grab);
2027 free(grab);
2028 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002029}
2030
2031static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002032busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2033 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002034{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002035 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002037
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002038static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002039busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040 uint32_t time, uint32_t button, uint32_t state)
2041{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002042 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002043 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002044 struct weston_pointer *pointer = grab->grab.pointer;
2045 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002046
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002047 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002048 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002049 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002050 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002051 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002052 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002053 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002054}
2055
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002056static void
2057busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2058{
2059 struct shell_grab *grab = (struct shell_grab *) base;
2060
2061 shell_grab_end(grab);
2062 free(grab);
2063}
2064
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002065static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002066 busy_cursor_grab_focus,
2067 busy_cursor_grab_motion,
2068 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002069 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002070};
2071
2072static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002073set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002074{
2075 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002076
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002077 if (pointer->grab->interface == &busy_cursor_grab_interface)
2078 return;
2079
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002080 grab = malloc(sizeof *grab);
2081 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002082 return;
2083
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002084 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2085 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002086 /* Mark the shsurf as ungrabbed so that button binding is able
2087 * to move it. */
2088 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002089}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002090
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002091static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002092end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002093{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002094 struct shell_grab *grab;
2095 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002096
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002097 wl_list_for_each(seat, &compositor->seat_list, link) {
2098 if (seat->pointer == NULL)
2099 continue;
2100
2101 grab = (struct shell_grab *) seat->pointer->grab;
2102 if (grab->grab.interface == &busy_cursor_grab_interface &&
2103 wl_resource_get_client(grab->shsurf->resource) == client) {
2104 shell_grab_end(grab);
2105 free(grab);
2106 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002107 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002108}
2109
Scott Moreau9521d5e2012-04-19 13:06:17 -06002110static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002111handle_shell_client_destroy(struct wl_listener *listener, void *data);
2112
2113static int
2114xdg_ping_timeout_handler(void *data)
2115{
2116 struct shell_client *sc = data;
2117 struct weston_seat *seat;
2118 struct shell_surface *shsurf;
2119
2120 /* Client is not responding */
2121 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002122 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2123 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2124 continue;
2125 if (seat->pointer->focus->surface->resource == NULL)
2126 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002127
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002128 shsurf = get_shell_surface(seat->pointer->focus->surface);
2129 if (shsurf &&
2130 wl_resource_get_client(shsurf->resource) == sc->client)
2131 set_busy_cursor(shsurf, seat->pointer);
2132 }
2133
2134 return 1;
2135}
2136
2137static void
2138handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2139{
2140 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002141 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002142 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002143 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002144
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002145 if (sc->unresponsive) {
2146 xdg_ping_timeout_handler(sc);
2147 return;
2148 }
2149
2150 sc->ping_serial = serial;
2151 loop = wl_display_get_event_loop(compositor->wl_display);
2152 if (sc->ping_timer == NULL)
2153 sc->ping_timer =
2154 wl_event_loop_add_timer(loop,
2155 xdg_ping_timeout_handler, sc);
2156 if (sc->ping_timer == NULL)
2157 return;
2158
2159 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2160
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002161 if (shell_surface_is_xdg_surface(shsurf) ||
2162 shell_surface_is_xdg_popup(shsurf))
2163 xdg_shell_send_ping(sc->resource, serial);
2164 else if (shell_surface_is_wl_shell_surface(shsurf))
2165 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002166}
2167
Scott Moreauff1db4a2012-04-17 19:06:18 -06002168static void
2169ping_handler(struct weston_surface *surface, uint32_t serial)
2170{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002171 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002172
2173 if (!shsurf)
2174 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002175 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002176 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002177 if (shsurf->surface == shsurf->shell->grab_surface)
2178 return;
2179
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002180 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002181}
2182
2183static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002184handle_pointer_focus(struct wl_listener *listener, void *data)
2185{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002186 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002188 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002189 uint32_t serial;
2190
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002192 return;
2193
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002195 serial = wl_display_next_serial(compositor->wl_display);
2196 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002197}
2198
2199static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002200shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2201{
2202 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002203 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002204}
2205
2206static void
2207shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2208{
2209 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002210 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002211}
2212
2213static void
2214handle_keyboard_focus(struct wl_listener *listener, void *data)
2215{
2216 struct weston_keyboard *keyboard = data;
2217 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2218
2219 if (seat->focused_surface) {
2220 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2221 if (shsurf)
2222 shell_surface_lose_keyboard_focus(shsurf);
2223 }
2224
2225 seat->focused_surface = keyboard->focus;
2226
2227 if (seat->focused_surface) {
2228 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2229 if (shsurf)
2230 shell_surface_gain_keyboard_focus(shsurf);
2231 }
2232}
2233
2234static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002235shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002236{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002237 if (sc->ping_serial != serial)
2238 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002239
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002240 sc->unresponsive = 0;
2241 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002242
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002243 if (sc->ping_timer) {
2244 wl_event_source_remove(sc->ping_timer);
2245 sc->ping_timer = NULL;
2246 }
2247
2248}
2249
2250static void
2251shell_surface_pong(struct wl_client *client,
2252 struct wl_resource *resource, uint32_t serial)
2253{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002254 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2255 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002256
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002257 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002258}
2259
2260static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002261set_title(struct shell_surface *shsurf, const char *title)
2262{
2263 free(shsurf->title);
2264 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002265 shsurf->surface->timeline.force_refresh = 1;
2266}
2267
2268static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002269set_pid(struct shell_surface *shsurf, pid_t pid)
2270{
2271 /* We have no use for it */
2272}
2273
2274static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002275set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2276{
2277 shsurf->type = t;
2278 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002279}
2280
2281static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002282set_window_geometry(struct shell_surface *shsurf,
2283 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002284{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002285 shsurf->next_geometry.x = x;
2286 shsurf->next_geometry.y = y;
2287 shsurf->next_geometry.width = width;
2288 shsurf->next_geometry.height = height;
2289 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002290}
2291
2292static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002293shell_surface_set_title(struct wl_client *client,
2294 struct wl_resource *resource, const char *title)
2295{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002296 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002297
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002298 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002299}
2300
2301static void
2302shell_surface_set_class(struct wl_client *client,
2303 struct wl_resource *resource, const char *class)
2304{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002305 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002306
2307 free(shsurf->class);
2308 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002309 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002310}
2311
Alex Wu4539b082012-03-01 12:57:46 +08002312static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002313restore_output_mode(struct weston_output *output)
2314{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002315 if (output->original_mode)
2316 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002317}
2318
2319static void
2320restore_all_output_modes(struct weston_compositor *compositor)
2321{
2322 struct weston_output *output;
2323
2324 wl_list_for_each(output, &compositor->output_list, link)
2325 restore_output_mode(output);
2326}
2327
Philip Withnall07926d92013-11-25 18:01:40 +00002328/* The surface will be inserted into the list immediately after the link
2329 * returned by this function (i.e. will be stacked immediately above the
2330 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002331static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002332shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2333{
2334 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002335 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002336
2337 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002338 case SHELL_SURFACE_XWAYLAND:
2339 return &shsurf->shell->fullscreen_layer.view_list;
2340
2341 case SHELL_SURFACE_NONE:
2342 return NULL;
2343
Kristian Høgsbergead52422014-01-01 13:51:52 -08002344 case SHELL_SURFACE_POPUP:
2345 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002346 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002347 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002348 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002349 /* Move the surface to its parent layer so
2350 * that surfaces which are transient for
2351 * fullscreen surfaces don't get hidden by the
2352 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002353
2354 /* TODO: Handle a parent with multiple views */
2355 parent = get_default_view(shsurf->parent);
2356 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002357 return container_of(parent->layer_link.link.prev,
2358 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002359 }
Philip Withnall07926d92013-11-25 18:01:40 +00002360
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002361 /* Move the surface to a normal workspace layer so that surfaces
2362 * which were previously fullscreen or transient are no longer
2363 * rendered on top. */
2364 ws = get_current_workspace(shsurf->shell);
2365 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002366 }
2367
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002368 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002369}
2370
Philip Withnall648a4dd2013-11-25 18:01:44 +00002371static void
2372shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2373{
2374 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002375 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002376
2377 /* Move the child layers to the same workspace as shsurf. They will be
2378 * stacked above shsurf. */
2379 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002380 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002381 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002382 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002383 prev = container_of(shsurf->view->layer_link.link.prev,
2384 struct weston_layer_entry, link);
2385 weston_layer_entry_remove(&child->view->layer_link);
2386 weston_layer_entry_insert(prev,
2387 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002388 weston_view_geometry_dirty(child->view);
2389 weston_surface_damage(child->surface);
2390
2391 /* Recurse. We don’t expect this to recurse very far (if
2392 * at all) because that would imply we have transient
2393 * (or popup) children of transient surfaces, which
2394 * would be unusual. */
2395 shell_surface_update_child_surface_layers(child);
2396 }
2397 }
2398}
2399
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002400/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002401 * geometry to ensure the changes are redrawn.
2402 *
2403 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2404 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002405static void
2406shell_surface_update_layer(struct shell_surface *shsurf)
2407{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002408 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002409
2410 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2411
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002412 if (new_layer_link == NULL)
2413 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002414 if (new_layer_link == &shsurf->view->layer_link)
2415 return;
2416
2417 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002418 weston_layer_entry_remove(&shsurf->view->layer_link);
2419 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002420 weston_view_geometry_dirty(shsurf->view);
2421 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002422
2423 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002424}
2425
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002426static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002427shell_surface_set_parent(struct shell_surface *shsurf,
2428 struct weston_surface *parent)
2429{
2430 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002431
2432 wl_list_remove(&shsurf->children_link);
2433 wl_list_init(&shsurf->children_link);
2434
2435 /* Insert into the parent surface’s child list. */
2436 if (parent != NULL) {
2437 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2438 if (parent_shsurf != NULL)
2439 wl_list_insert(&parent_shsurf->children_list,
2440 &shsurf->children_link);
2441 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002442}
2443
2444static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002445shell_surface_set_output(struct shell_surface *shsurf,
2446 struct weston_output *output)
2447{
2448 struct weston_surface *es = shsurf->surface;
2449
2450 /* get the default output, if the client set it as NULL
2451 check whether the ouput is available */
2452 if (output)
2453 shsurf->output = output;
2454 else if (es->output)
2455 shsurf->output = es->output;
2456 else
2457 shsurf->output = get_default_output(es->compositor);
2458}
2459
2460static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002461surface_clear_next_states(struct shell_surface *shsurf)
2462{
2463 shsurf->next_state.maximized = false;
2464 shsurf->next_state.fullscreen = false;
2465
2466 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2467 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2468 shsurf->state_changed = true;
2469}
2470
2471static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002472set_toplevel(struct shell_surface *shsurf)
2473{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002474 shell_surface_set_parent(shsurf, NULL);
2475 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002476 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002477
2478 /* The layer_link is updated in set_surface_type(),
2479 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002480}
2481
2482static void
2483shell_surface_set_toplevel(struct wl_client *client,
2484 struct wl_resource *resource)
2485{
2486 struct shell_surface *surface = wl_resource_get_user_data(resource);
2487
2488 set_toplevel(surface);
2489}
2490
2491static void
2492set_transient(struct shell_surface *shsurf,
2493 struct weston_surface *parent, int x, int y, uint32_t flags)
2494{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002495 assert(parent != NULL);
2496
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002497 shell_surface_set_parent(shsurf, parent);
2498
2499 surface_clear_next_states(shsurf);
2500
Philip Withnallbecb77e2013-11-25 18:01:30 +00002501 shsurf->transient.x = x;
2502 shsurf->transient.y = y;
2503 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002504
Rafael Antognollied207b42013-12-03 15:35:43 -02002505 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002506 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002507 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002508
2509 /* The layer_link is updated in set_surface_type(),
2510 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002511}
2512
2513static void
2514shell_surface_set_transient(struct wl_client *client,
2515 struct wl_resource *resource,
2516 struct wl_resource *parent_resource,
2517 int x, int y, uint32_t flags)
2518{
2519 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2520 struct weston_surface *parent =
2521 wl_resource_get_user_data(parent_resource);
2522
2523 set_transient(shsurf, parent, x, y, flags);
2524}
2525
2526static void
2527set_fullscreen(struct shell_surface *shsurf,
2528 uint32_t method,
2529 uint32_t framerate,
2530 struct weston_output *output)
2531{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002532 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002533 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002534
2535 shsurf->fullscreen_output = shsurf->output;
2536 shsurf->fullscreen.type = method;
2537 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002538
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002539 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002540}
2541
2542static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002543weston_view_set_initial_position(struct weston_view *view,
2544 struct desktop_shell *shell);
2545
2546static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002547unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002548{
Philip Withnallf85fe842013-11-25 18:01:37 +00002549 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002550 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2551 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002552 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002553 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002554
Alex Wu4539b082012-03-01 12:57:46 +08002555 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2556 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002557
Alex Wu4539b082012-03-01 12:57:46 +08002558 wl_list_remove(&shsurf->fullscreen.transform.link);
2559 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002560
Jason Ekstranda7af7042013-10-12 22:38:11 -05002561 if (shsurf->fullscreen.black_view)
2562 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2563 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002564
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002565 if (shsurf->saved_position_valid)
2566 weston_view_set_position(shsurf->view,
2567 shsurf->saved_x, shsurf->saved_y);
2568 else
2569 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2570
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002571 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002572 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002573 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002574 shsurf->saved_rotation_valid = false;
2575 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002576
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002577 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002578}
2579
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002580static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002581shell_surface_set_fullscreen(struct wl_client *client,
2582 struct wl_resource *resource,
2583 uint32_t method,
2584 uint32_t framerate,
2585 struct wl_resource *output_resource)
2586{
2587 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2588 struct weston_output *output;
2589
2590 if (output_resource)
2591 output = wl_resource_get_user_data(output_resource);
2592 else
2593 output = NULL;
2594
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002595 shell_surface_set_parent(shsurf, NULL);
2596
Rafael Antognolli03b16592013-12-03 15:35:42 -02002597 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002598 shsurf->next_state.fullscreen = true;
2599 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002600 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002601}
2602
2603static void
2604set_popup(struct shell_surface *shsurf,
2605 struct weston_surface *parent,
2606 struct weston_seat *seat,
2607 uint32_t serial,
2608 int32_t x,
2609 int32_t y)
2610{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002611 assert(parent != NULL);
2612
Philip Withnallbecb77e2013-11-25 18:01:30 +00002613 shsurf->popup.shseat = get_shell_seat(seat);
2614 shsurf->popup.serial = serial;
2615 shsurf->popup.x = x;
2616 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002617
Pekka Paalanenb5026542014-11-12 15:09:24 +02002618 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002619}
2620
2621static void
2622shell_surface_set_popup(struct wl_client *client,
2623 struct wl_resource *resource,
2624 struct wl_resource *seat_resource,
2625 uint32_t serial,
2626 struct wl_resource *parent_resource,
2627 int32_t x, int32_t y, uint32_t flags)
2628{
2629 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002630 struct weston_surface *parent =
2631 wl_resource_get_user_data(parent_resource);
2632
2633 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002634
Rafael Antognolli03b16592013-12-03 15:35:42 -02002635 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002636 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002637 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002638 wl_resource_get_user_data(seat_resource),
2639 serial, x, y);
2640}
2641
2642static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002643unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002644{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002645 /* undo all maximized things here */
2646 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002647
2648 if (shsurf->saved_position_valid)
2649 weston_view_set_position(shsurf->view,
2650 shsurf->saved_x, shsurf->saved_y);
2651 else
2652 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002653
2654 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655 wl_list_insert(&shsurf->view->geometry.transformation_list,
2656 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002657 shsurf->saved_rotation_valid = false;
2658 }
2659
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002660 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002661}
2662
Philip Withnallbecb77e2013-11-25 18:01:30 +00002663static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002664set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002665{
2666 struct shell_surface *shsurf;
2667 struct workspace *current_ws;
2668 struct weston_seat *seat;
2669 struct weston_surface *focus;
2670 struct weston_view *view;
2671
2672 view = get_default_view(surface);
2673 if (!view)
2674 return;
2675
2676 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2677
2678 shsurf = get_shell_surface(surface);
2679 current_ws = get_current_workspace(shsurf->shell);
2680
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002681 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002682 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002683
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002684 drop_focus_state(shsurf->shell, current_ws, view->surface);
2685 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2686 if (!seat->keyboard)
2687 continue;
2688 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2689 if (focus == view->surface)
2690 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002691 }
2692
2693 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002694 weston_view_damage_below(view);
2695}
2696
2697static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002698shell_surface_set_maximized(struct wl_client *client,
2699 struct wl_resource *resource,
2700 struct wl_resource *output_resource)
2701{
2702 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2703 struct weston_output *output;
2704
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002705 surface_clear_next_states(shsurf);
2706 shsurf->next_state.maximized = true;
2707 shsurf->state_changed = true;
2708
Pekka Paalanenb5026542014-11-12 15:09:24 +02002709 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002710 shell_surface_set_parent(shsurf, NULL);
2711
Philip Withnallbecb77e2013-11-25 18:01:30 +00002712 if (output_resource)
2713 output = wl_resource_get_user_data(output_resource);
2714 else
2715 output = NULL;
2716
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002717 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002718
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002719 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002720}
2721
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002722/* This is only ever called from set_surface_type(), so there’s no need to
2723 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002724static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002725reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002726{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002727 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002728 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002729 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002730 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002731
Pekka Paalanen98262232011-12-01 10:42:22 +02002732 return 0;
2733}
2734
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002735static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002736set_full_output(struct shell_surface *shsurf)
2737{
2738 shsurf->saved_x = shsurf->view->geometry.x;
2739 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002740 shsurf->saved_width = shsurf->surface->width;
2741 shsurf->saved_height = shsurf->surface->height;
2742 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002743 shsurf->saved_position_valid = true;
2744
2745 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2746 wl_list_remove(&shsurf->rotation.transform.link);
2747 wl_list_init(&shsurf->rotation.transform.link);
2748 weston_view_geometry_dirty(shsurf->view);
2749 shsurf->saved_rotation_valid = true;
2750 }
2751}
2752
2753static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002754set_surface_type(struct shell_surface *shsurf)
2755{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002756 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002757 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002758
Philip Withnallbecb77e2013-11-25 18:01:30 +00002759 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002760
Rafael Antognolli03b16592013-12-03 15:35:42 -02002761 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002762 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002763
2764 switch (shsurf->type) {
2765 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002766 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002767 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002768 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769 weston_view_set_position(shsurf->view,
2770 pev->geometry.x + shsurf->transient.x,
2771 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002772 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002773 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002774
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002775 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002776 weston_view_set_position(shsurf->view, shsurf->transient.x,
2777 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002778 break;
2779
Philip Withnall0f640e22013-11-25 18:01:31 +00002780 case SHELL_SURFACE_POPUP:
2781 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002782 default:
2783 break;
2784 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002785
2786 /* Update the surface’s layer. */
2787 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002788}
2789
Tiago Vignattibe143262012-04-16 17:31:41 +03002790static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002791shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002792{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002793 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002794}
2795
Pekka Paalanen8274d902014-08-06 19:36:51 +03002796static int
2797black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2798{
2799 struct weston_surface *fs_surface = surface->configure_private;
2800 int n;
2801 int rem;
2802 int ret;
2803
2804 n = snprintf(buf, len, "black background surface for ");
2805 if (n < 0)
2806 return n;
2807
2808 rem = (int)len - n;
2809 if (rem < 0)
2810 rem = 0;
2811
2812 if (fs_surface->get_label)
2813 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2814 else
2815 ret = snprintf(buf + n, rem, "<unknown>");
2816
2817 if (ret < 0)
2818 return n;
2819
2820 return n + ret;
2821}
2822
Alex Wu21858432012-04-01 20:13:08 +08002823static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002824black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002825
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002827create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002828 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002829 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002830{
2831 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002832 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002833
2834 surface = weston_surface_create(ec);
2835 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002836 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002837 return NULL;
2838 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002839 view = weston_view_create(surface);
2840 if (surface == NULL) {
2841 weston_log("no memory\n");
2842 weston_surface_destroy(surface);
2843 return NULL;
2844 }
Alex Wu4539b082012-03-01 12:57:46 +08002845
Alex Wu21858432012-04-01 20:13:08 +08002846 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002847 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002848 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002849 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002850 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002851 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002852 pixman_region32_fini(&surface->input);
2853 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002854
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002855 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002856 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002857
2858 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002859}
2860
Philip Withnalled8f1a92013-11-25 18:01:43 +00002861static void
2862shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2863{
2864 struct weston_output *output = shsurf->fullscreen_output;
2865
Rafael Antognolli03b16592013-12-03 15:35:42 -02002866 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002867
2868 if (!shsurf->fullscreen.black_view)
2869 shsurf->fullscreen.black_view =
2870 create_black_surface(shsurf->surface->compositor,
2871 shsurf->surface,
2872 output->x, output->y,
2873 output->width,
2874 output->height);
2875
2876 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002877 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2878 weston_layer_entry_insert(&shsurf->view->layer_link,
2879 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002880 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2881 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002882
2883 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002884}
2885
Alex Wu4539b082012-03-01 12:57:46 +08002886/* Create black surface and append it to the associated fullscreen surface.
2887 * Handle size dismatch and positioning according to the method. */
2888static void
2889shell_configure_fullscreen(struct shell_surface *shsurf)
2890{
2891 struct weston_output *output = shsurf->fullscreen_output;
2892 struct weston_surface *surface = shsurf->surface;
2893 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002894 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002895 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002896
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002897 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2898 restore_output_mode(output);
2899
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002900 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002901 weston_layer_entry_remove(&shsurf->view->layer_link);
2902 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002903
Philip Withnalled8f1a92013-11-25 18:01:43 +00002904 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002905
Jason Ekstranda7af7042013-10-12 22:38:11 -05002906 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002907 &surf_width, &surf_height);
2908
Alex Wu4539b082012-03-01 12:57:46 +08002909 switch (shsurf->fullscreen.type) {
2910 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002911 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002913 break;
2914 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002915 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002916 if (output->width == surf_width &&
2917 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002918 weston_view_set_position(shsurf->view,
2919 output->x - surf_x,
2920 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002921 break;
2922 }
2923
Alex Wu4539b082012-03-01 12:57:46 +08002924 matrix = &shsurf->fullscreen.transform.matrix;
2925 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002926
Scott Moreau1bad5db2012-08-18 01:04:05 -06002927 output_aspect = (float) output->width /
2928 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002929 /* XXX: Use surf_width and surf_height here? */
2930 surface_aspect = (float) surface->width /
2931 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002932 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002933 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002934 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002935 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002936 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002937 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002938
Alex Wu4539b082012-03-01 12:57:46 +08002939 weston_matrix_scale(matrix, scale, scale, 1);
2940 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002941 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002942 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002943 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2944 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002945 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002946
Alex Wu4539b082012-03-01 12:57:46 +08002947 break;
2948 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002949 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002950 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002951 surf_width * surface->buffer_viewport.buffer.scale,
2952 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002953 shsurf->fullscreen.framerate};
2954
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002955 if (weston_output_mode_switch_to_temporary(output, &mode,
2956 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002957 weston_view_set_position(shsurf->view,
2958 output->x - surf_x,
2959 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002960 shsurf->fullscreen.black_view->surface->width = output->width;
2961 shsurf->fullscreen.black_view->surface->height = output->height;
2962 weston_view_set_position(shsurf->fullscreen.black_view,
2963 output->x - surf_x,
2964 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002965 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002966 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002967 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002968 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002969 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002970 }
Alex Wubd3354b2012-04-17 17:20:49 +08002971 }
Alex Wu4539b082012-03-01 12:57:46 +08002972 break;
2973 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002974 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002975 break;
2976 default:
2977 break;
2978 }
2979}
2980
Alex Wu4539b082012-03-01 12:57:46 +08002981static void
2982shell_map_fullscreen(struct shell_surface *shsurf)
2983{
Alex Wubd3354b2012-04-17 17:20:49 +08002984 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002985}
2986
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002987static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002988set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2989{
2990 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002991 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002992 shsurf->transient.x = x;
2993 shsurf->transient.y = y;
2994 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002995
2996 shell_surface_set_parent(shsurf, NULL);
2997
Pekka Paalanenb5026542014-11-12 15:09:24 +02002998 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002999 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003000}
3001
Kristian Høgsberg47792412014-05-04 13:47:06 -07003002static void
3003shell_interface_set_fullscreen(struct shell_surface *shsurf,
3004 uint32_t method,
3005 uint32_t framerate,
3006 struct weston_output *output)
3007{
3008 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003009 shsurf->next_state.fullscreen = true;
3010 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003011
3012 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003013}
3014
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003015static struct weston_output *
3016get_focused_output(struct weston_compositor *compositor)
3017{
3018 struct weston_seat *seat;
3019 struct weston_output *output = NULL;
3020
3021 wl_list_for_each(seat, &compositor->seat_list, link) {
3022 /* Priority has touch focus, then pointer and
3023 * then keyboard focus. We should probably have
3024 * three for loops and check frist for touch,
3025 * then for pointer, etc. but unless somebody has some
3026 * objections, I think this is sufficient. */
3027 if (seat->touch && seat->touch->focus)
3028 output = seat->touch->focus->output;
3029 else if (seat->pointer && seat->pointer->focus)
3030 output = seat->pointer->focus->output;
3031 else if (seat->keyboard && seat->keyboard->focus)
3032 output = seat->keyboard->focus->output;
3033
3034 if (output)
3035 break;
3036 }
3037
3038 return output;
3039}
3040
3041static void
3042shell_interface_set_maximized(struct shell_surface *shsurf)
3043{
3044 struct weston_output *output;
3045
3046 surface_clear_next_states(shsurf);
3047 shsurf->next_state.maximized = true;
3048 shsurf->state_changed = true;
3049 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3050
3051 if (!weston_surface_is_mapped(shsurf->surface))
3052 output = get_focused_output(shsurf->surface->compositor);
3053 else
3054 output = shsurf->surface->output;
3055
3056 shell_surface_set_output(shsurf, output);
3057 send_configure_for_surface(shsurf);
3058}
3059
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003060static int
3061shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3062{
Derek Foreman794fa0e2015-07-15 13:00:38 -05003063 return surface_move(shsurf, ws->pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003064}
3065
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003066static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003067
3068static void
3069destroy_shell_seat(struct wl_listener *listener, void *data)
3070{
3071 struct shell_seat *shseat =
3072 container_of(listener,
3073 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003074 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003075
3076 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003077 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003078 shseat->popup_grab.client = NULL;
3079
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003080 wl_list_for_each_safe(shsurf, next,
3081 &shseat->popup_grab.surfaces_list,
3082 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003083 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003084 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003085 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003086 }
3087
3088 wl_list_remove(&shseat->seat_destroy_listener.link);
3089 free(shseat);
3090}
3091
Jason Ekstrand024177c2014-04-21 19:42:58 -05003092static void
3093shell_seat_caps_changed(struct wl_listener *listener, void *data)
3094{
3095 struct shell_seat *seat;
3096
3097 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3098
3099 if (seat->seat->keyboard &&
3100 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3101 wl_signal_add(&seat->seat->keyboard->focus_signal,
3102 &seat->keyboard_focus_listener);
3103 } else if (!seat->seat->keyboard) {
3104 wl_list_init(&seat->keyboard_focus_listener.link);
3105 }
3106
3107 if (seat->seat->pointer &&
3108 wl_list_empty(&seat->pointer_focus_listener.link)) {
3109 wl_signal_add(&seat->seat->pointer->focus_signal,
3110 &seat->pointer_focus_listener);
3111 } else if (!seat->seat->pointer) {
3112 wl_list_init(&seat->pointer_focus_listener.link);
3113 }
3114}
3115
Giulio Camuffo5085a752013-03-25 21:42:45 +01003116static struct shell_seat *
3117create_shell_seat(struct weston_seat *seat)
3118{
3119 struct shell_seat *shseat;
3120
3121 shseat = calloc(1, sizeof *shseat);
3122 if (!shseat) {
3123 weston_log("no memory to allocate shell seat\n");
3124 return NULL;
3125 }
3126
3127 shseat->seat = seat;
3128 wl_list_init(&shseat->popup_grab.surfaces_list);
3129
3130 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3131 wl_signal_add(&seat->destroy_signal,
3132 &shseat->seat_destroy_listener);
3133
Jason Ekstrand024177c2014-04-21 19:42:58 -05003134 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3135 wl_list_init(&shseat->keyboard_focus_listener.link);
3136
3137 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3138 wl_list_init(&shseat->pointer_focus_listener.link);
3139
3140 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3141 wl_signal_add(&seat->updated_caps_signal,
3142 &shseat->caps_changed_listener);
3143 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3144
Giulio Camuffo5085a752013-03-25 21:42:45 +01003145 return shseat;
3146}
3147
3148static struct shell_seat *
3149get_shell_seat(struct weston_seat *seat)
3150{
3151 struct wl_listener *listener;
3152
3153 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003154 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003155
3156 return container_of(listener,
3157 struct shell_seat, seat_destroy_listener);
3158}
3159
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003160static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003161popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003162{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003163 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003164 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003165 struct shell_seat *shseat =
3166 container_of(grab, struct shell_seat, popup_grab.grab);
3167 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003168 wl_fixed_t sx, sy;
3169
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 view = weston_compositor_pick_view(pointer->seat->compositor,
3171 pointer->x, pointer->y,
3172 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003173
Jason Ekstranda7af7042013-10-12 22:38:11 -05003174 if (view && view->surface->resource &&
3175 wl_resource_get_client(view->surface->resource) == client) {
3176 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003177 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003178 weston_pointer_set_focus(pointer, NULL,
3179 wl_fixed_from_int(0),
3180 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181 }
3182}
3183
3184static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003185popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3186 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003187{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003188 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003189 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003190 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003191
Jonas Ådahl61917c82014-08-11 22:44:02 +02003192 if (pointer->focus) {
3193 weston_view_from_global_fixed(pointer->focus, x, y,
3194 &pointer->sx, &pointer->sy);
3195 }
3196
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003197 weston_pointer_move(pointer, x, y);
3198
Neil Roberts96d790e2013-09-19 17:32:00 +01003199 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003200 weston_view_from_global_fixed(pointer->focus,
3201 pointer->x, pointer->y,
3202 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003203 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003204 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003205}
3206
3207static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003208popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003209 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003210{
3211 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003212 struct shell_seat *shseat =
3213 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003214 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003215 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003216 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003217 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003218
Neil Roberts96d790e2013-09-19 17:32:00 +01003219 resource_list = &grab->pointer->focus_resource_list;
3220 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003221 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003222 wl_resource_for_each(resource, resource_list) {
3223 wl_pointer_send_button(resource, serial,
3224 time, button, state);
3225 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003226 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003227 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003228 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003229 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003230 }
3231
Daniel Stone4dbadb12012-05-30 16:31:51 +01003232 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003233 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003234}
3235
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003236static void
3237popup_grab_cancel(struct weston_pointer_grab *grab)
3238{
3239 popup_grab_end(grab->pointer);
3240}
3241
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003242static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003243 popup_grab_focus,
3244 popup_grab_motion,
3245 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003246 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003247};
3248
3249static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003250touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3251 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3252{
3253 struct wl_resource *resource;
3254 struct shell_seat *shseat =
3255 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3256 struct wl_display *display = shseat->seat->compositor->wl_display;
3257 uint32_t serial;
3258 struct wl_list *resource_list;
3259
3260 resource_list = &grab->touch->focus_resource_list;
3261 if (!wl_list_empty(resource_list)) {
3262 serial = wl_display_get_serial(display);
3263 wl_resource_for_each(resource, resource_list) {
3264 wl_touch_send_down(resource, serial, time,
3265 grab->touch->focus->surface->resource,
3266 touch_id, sx, sy);
3267 }
3268 }
3269}
3270
3271static void
3272touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3273{
3274 struct wl_resource *resource;
3275 struct shell_seat *shseat =
3276 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3277 struct wl_display *display = shseat->seat->compositor->wl_display;
3278 uint32_t serial;
3279 struct wl_list *resource_list;
3280
3281 resource_list = &grab->touch->focus_resource_list;
3282 if (!wl_list_empty(resource_list)) {
3283 serial = wl_display_get_serial(display);
3284 wl_resource_for_each(resource, resource_list) {
3285 wl_touch_send_up(resource, serial, time, touch_id);
3286 }
3287 }
3288}
3289
3290static void
3291touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3292 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3293{
3294 struct wl_resource *resource;
3295 struct wl_list *resource_list;
3296
3297 resource_list = &grab->touch->focus_resource_list;
3298 if (!wl_list_empty(resource_list)) {
3299 wl_resource_for_each(resource, resource_list) {
3300 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3301 }
3302 }
3303}
3304
3305static void
3306touch_popup_grab_frame(struct weston_touch_grab *grab)
3307{
3308}
3309
3310static void
3311touch_popup_grab_cancel(struct weston_touch_grab *grab)
3312{
3313 touch_popup_grab_end(grab->touch);
3314}
3315
3316static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3317 touch_popup_grab_down,
3318 touch_popup_grab_up,
3319 touch_popup_grab_motion,
3320 touch_popup_grab_frame,
3321 touch_popup_grab_cancel,
3322};
3323
3324static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003325shell_surface_send_popup_done(struct shell_surface *shsurf)
3326{
3327 if (shell_surface_is_wl_shell_surface(shsurf))
3328 wl_shell_surface_send_popup_done(shsurf->resource);
3329 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003330 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003331}
3332
3333static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003334popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003335{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003336 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003337 struct shell_seat *shseat =
3338 container_of(grab, struct shell_seat, popup_grab.grab);
3339 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003340 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003341
3342 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003343 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003344 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003345 shseat->popup_grab.grab.interface = NULL;
3346 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003347 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003348 wl_list_for_each_safe(shsurf, next,
3349 &shseat->popup_grab.surfaces_list,
3350 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003351 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003352 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003353 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003354 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003355 wl_list_init(&shseat->popup_grab.surfaces_list);
3356 }
3357}
3358
3359static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003360touch_popup_grab_end(struct weston_touch *touch)
3361{
3362 struct weston_touch_grab *grab = touch->grab;
3363 struct shell_seat *shseat =
3364 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3365 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003366 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003367
3368 if (touch->grab->interface == &touch_popup_grab_interface) {
3369 weston_touch_end_grab(grab->touch);
3370 shseat->popup_grab.client = NULL;
3371 shseat->popup_grab.touch_grab.interface = NULL;
3372 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3373 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003374 wl_list_for_each_safe(shsurf, next,
3375 &shseat->popup_grab.surfaces_list,
3376 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003377 shell_surface_send_popup_done(shsurf);
3378 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003379 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003380 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003381 wl_list_init(&shseat->popup_grab.surfaces_list);
3382 }
3383}
3384
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003385static struct shell_surface *
3386get_top_popup(struct shell_seat *shseat)
3387{
3388 struct shell_surface *shsurf;
3389
3390 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3391 return NULL;
3392 } else {
3393 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3394 struct shell_surface,
3395 popup.grab_link);
3396 return shsurf;
3397 }
3398}
3399
3400static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003401add_popup_grab(struct shell_surface *shsurf,
3402 struct shell_seat *shseat,
3403 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003404{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003405 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003406 struct shell_surface *parent, *top_surface;
3407
3408 parent = get_shell_surface(shsurf->parent);
3409 top_surface = get_top_popup(shseat);
3410 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003411 (!parent ||
3412 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003413 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003414 wl_resource_post_error(shsurf->owner_resource,
3415 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003416 "xdg_popup was not created on the "
3417 "topmost popup");
3418 return -1;
3419 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003420
3421 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003422 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003423 shseat->popup_grab.client =
3424 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003425
3426 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003427 shseat->popup_grab.grab.interface =
3428 &popup_grab_interface;
3429
3430 /* We must make sure here that this popup was opened
3431 * after a mouse press, and not just by moving around
3432 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003433 if (shseat->seat->pointer->button_count > 0)
3434 shseat->popup_grab.initial_up = 0;
3435 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003436 shseat->popup_grab.touch_grab.interface =
3437 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003438 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003439
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003440 wl_list_insert(&shseat->popup_grab.surfaces_list,
3441 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003442
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003443 if (type == POINTER) {
3444 weston_pointer_start_grab(seat->pointer,
3445 &shseat->popup_grab.grab);
3446 } else if (type == TOUCH) {
3447 weston_touch_start_grab(seat->touch,
3448 &shseat->popup_grab.touch_grab);
3449 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003450 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003451 wl_list_insert(&shseat->popup_grab.surfaces_list,
3452 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003453 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003454
3455 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003456}
3457
3458static void
3459remove_popup_grab(struct shell_surface *shsurf)
3460{
3461 struct shell_seat *shseat = shsurf->popup.shseat;
3462
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003463 if (shell_surface_is_xdg_popup(shsurf) &&
3464 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003465 wl_resource_post_error(shsurf->owner_resource,
3466 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003467 "xdg_popup was destroyed while it was "
3468 "not the topmost popup.");
3469 return;
3470 }
3471
Giulio Camuffo5085a752013-03-25 21:42:45 +01003472 wl_list_remove(&shsurf->popup.grab_link);
3473 wl_list_init(&shsurf->popup.grab_link);
3474 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003475 if (shseat->popup_grab.type == POINTER) {
3476 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3477 shseat->popup_grab.grab.interface = NULL;
3478 } else if (shseat->popup_grab.type == TOUCH) {
3479 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3480 shseat->popup_grab.touch_grab.interface = NULL;
3481 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003482 }
3483}
3484
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003485static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003486shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003487{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003488 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003489 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003490
Jason Ekstranda7af7042013-10-12 22:38:11 -05003491 shsurf->surface->output = parent_view->output;
3492 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003493
Jason Ekstranda7af7042013-10-12 22:38:11 -05003494 weston_view_set_transform_parent(shsurf->view, parent_view);
3495 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3496 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003497
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003498 if (shseat->seat->pointer &&
3499 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003500 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3501 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003502 } else if (shseat->seat->touch &&
3503 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003504 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3505 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003506 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003507 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003508 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003509 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003510
3511 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003512}
3513
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003514static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003515 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003516 shell_surface_move,
3517 shell_surface_resize,
3518 shell_surface_set_toplevel,
3519 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003520 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003521 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003522 shell_surface_set_maximized,
3523 shell_surface_set_title,
3524 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003525};
3526
3527static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003528destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003529{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003530 struct shell_surface *child, *next;
3531
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003532 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3533
Giulio Camuffo5085a752013-03-25 21:42:45 +01003534 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3535 remove_popup_grab(shsurf);
3536 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003537
Alex Wubd3354b2012-04-17 17:20:49 +08003538 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003539 shell_surface_is_top_fullscreen(shsurf))
3540 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003541
Jason Ekstranda7af7042013-10-12 22:38:11 -05003542 if (shsurf->fullscreen.black_view)
3543 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003544
Alex Wubd3354b2012-04-17 17:20:49 +08003545 /* As destroy_resource() use wl_list_for_each_safe(),
3546 * we can always remove the listener.
3547 */
3548 wl_list_remove(&shsurf->surface_destroy_listener.link);
3549 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003550 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003551 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003552
Jason Ekstranda7af7042013-10-12 22:38:11 -05003553 weston_view_destroy(shsurf->view);
3554
Philip Withnall648a4dd2013-11-25 18:01:44 +00003555 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003556 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3557 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003558
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003559 wl_list_remove(&shsurf->link);
3560 free(shsurf);
3561}
3562
3563static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003564shell_destroy_shell_surface(struct wl_resource *resource)
3565{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003566 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003567
Bryan Caina46b9462014-04-04 17:41:24 -05003568 if (!wl_list_empty(&shsurf->popup.grab_link))
3569 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003570 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003571 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003572}
3573
3574static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003575shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003576{
3577 struct shell_surface *shsurf = container_of(listener,
3578 struct shell_surface,
3579 surface_destroy_listener);
3580
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003581 if (shsurf->resource)
3582 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003583
3584 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003585}
3586
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003587static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003588fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003589{
3590 struct shell_surface *shsurf = data;
3591
3592 weston_surface_destroy(shsurf->surface);
3593}
3594
3595static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003596fade_out_done(struct weston_view_animation *animation, void *data)
3597{
3598 struct shell_surface *shsurf = data;
3599 struct wl_event_loop *loop;
3600
3601 loop = wl_display_get_event_loop(
3602 shsurf->surface->compositor->wl_display);
3603
3604 if (!shsurf->destroying) {
3605 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3606 shsurf->destroying = true;
3607 }
3608}
3609
3610static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003611handle_resource_destroy(struct wl_listener *listener, void *data)
3612{
3613 struct shell_surface *shsurf =
3614 container_of(listener, struct shell_surface,
3615 resource_destroy_listener);
3616
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003617 if (!weston_surface_is_mapped(shsurf->surface))
3618 return;
3619
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003620 shsurf->surface->ref_count++;
3621
3622 pixman_region32_fini(&shsurf->surface->pending.input);
3623 pixman_region32_init(&shsurf->surface->pending.input);
3624 pixman_region32_fini(&shsurf->surface->input);
3625 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003626 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3627 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3628 fade_out_done, shsurf);
3629 } else {
3630 weston_surface_destroy(shsurf->surface);
3631 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003632}
3633
3634static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003635shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003636
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003637struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003638get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003639{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003640 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003641 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003642 else
3643 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003644}
3645
Rafael Antognollie2a34552013-12-03 15:35:45 -02003646static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003647create_common_surface(struct shell_client *owner, void *shell,
3648 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003649 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003650{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003651 struct shell_surface *shsurf;
3652
Pekka Paalanen50b67472014-10-01 15:02:41 +03003653 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003654
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003655 shsurf = calloc(1, sizeof *shsurf);
3656 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003657 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003658 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003659 }
3660
Jason Ekstranda7af7042013-10-12 22:38:11 -05003661 shsurf->view = weston_view_create(surface);
3662 if (!shsurf->view) {
3663 weston_log("no memory to allocate shell surface\n");
3664 free(shsurf);
3665 return NULL;
3666 }
3667
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003668 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003669 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003670 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003671
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003672 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3673 wl_resource_add_destroy_listener(surface->resource,
3674 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003675 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003676
Tiago Vignattibc052c92012-04-19 16:18:18 +03003677 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003678 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003679 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003680 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003681 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003682 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003683 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3684 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003685 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003686 wl_list_init(&shsurf->fullscreen.transform.link);
3687
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003688 shsurf->output = get_default_output(shsurf->shell->compositor);
3689
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003690 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003691 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003692 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003693 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003694
3695 /* init link so its safe to always remove it in destroy_shell_surface */
3696 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003697 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003698
Pekka Paalanen460099f2012-01-20 16:48:25 +02003699 /* empty when not in use */
3700 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003701 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003702
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003703 wl_list_init(&shsurf->workspace_transform.link);
3704
Philip Withnall648a4dd2013-11-25 18:01:44 +00003705 wl_list_init(&shsurf->children_link);
3706 wl_list_init(&shsurf->children_list);
3707 shsurf->parent = NULL;
3708
Pekka Paalanenb5026542014-11-12 15:09:24 +02003709 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003710
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003711 shsurf->client = client;
3712
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003713 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003714}
3715
Rafael Antognollie2a34552013-12-03 15:35:45 -02003716static struct shell_surface *
3717create_shell_surface(void *shell, struct weston_surface *surface,
3718 const struct weston_shell_client *client)
3719{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003720 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003721}
3722
Jason Ekstranda7af7042013-10-12 22:38:11 -05003723static struct weston_view *
3724get_primary_view(void *shell, struct shell_surface *shsurf)
3725{
3726 return shsurf->view;
3727}
3728
Tiago Vignattibc052c92012-04-19 16:18:18 +03003729static void
3730shell_get_shell_surface(struct wl_client *client,
3731 struct wl_resource *resource,
3732 uint32_t id,
3733 struct wl_resource *surface_resource)
3734{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003735 struct weston_surface *surface =
3736 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003737 struct shell_client *sc = wl_resource_get_user_data(resource);
3738 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003739 struct shell_surface *shsurf;
3740
Pekka Paalanen50b67472014-10-01 15:02:41 +03003741 if (weston_surface_set_role(surface, "wl_shell_surface",
3742 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003743 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003744
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003745 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003746 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003747 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003748 return;
3749 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003750
Jason Ekstranda85118c2013-06-27 20:17:02 -05003751 shsurf->resource =
3752 wl_resource_create(client,
3753 &wl_shell_surface_interface, 1, id);
3754 wl_resource_set_implementation(shsurf->resource,
3755 &shell_surface_implementation,
3756 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003757 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003758}
3759
Rafael Antognollie2a34552013-12-03 15:35:45 -02003760static bool
3761shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3762{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003763 /* A shell surface without a resource is created from xwayland
3764 * and is considered a wl_shell surface for now. */
3765
3766 return shsurf->resource == NULL ||
3767 wl_resource_instance_of(shsurf->resource,
3768 &wl_shell_surface_interface,
3769 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003770}
3771
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003772static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003773 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003774};
3775
Rafael Antognollie2a34552013-12-03 15:35:45 -02003776/****************************
3777 * xdg-shell implementation */
3778
3779static void
3780xdg_surface_destroy(struct wl_client *client,
3781 struct wl_resource *resource)
3782{
3783 wl_resource_destroy(resource);
3784}
3785
3786static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003787xdg_surface_set_parent(struct wl_client *client,
3788 struct wl_resource *resource,
3789 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003790{
3791 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003792 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003793
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003794 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003795 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003796 shell_surface_set_parent(shsurf, parent->surface);
3797 } else {
3798 shell_surface_set_parent(shsurf, NULL);
3799 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003800}
3801
3802static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003803xdg_surface_set_app_id(struct wl_client *client,
3804 struct wl_resource *resource,
3805 const char *app_id)
3806{
3807 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3808
3809 free(shsurf->class);
3810 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003811 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003812}
3813
3814static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003815xdg_surface_show_window_menu(struct wl_client *client,
3816 struct wl_resource *surface_resource,
3817 struct wl_resource *seat_resource,
3818 uint32_t serial,
3819 int32_t x,
3820 int32_t y)
3821{
3822 /* TODO */
3823}
3824
3825static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003826xdg_surface_set_title(struct wl_client *client,
3827 struct wl_resource *resource, const char *title)
3828{
3829 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3830
3831 set_title(shsurf, title);
3832}
3833
3834static void
3835xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3836 struct wl_resource *seat_resource, uint32_t serial)
3837{
3838 common_surface_move(resource, seat_resource, serial);
3839}
3840
3841static void
3842xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3843 struct wl_resource *seat_resource, uint32_t serial,
3844 uint32_t edges)
3845{
3846 common_surface_resize(resource, seat_resource, serial, edges);
3847}
3848
3849static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003850xdg_surface_ack_configure(struct wl_client *client,
3851 struct wl_resource *resource,
3852 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003853{
3854 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3855
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003856 if (shsurf->state_requested) {
3857 shsurf->next_state = shsurf->requested_state;
3858 shsurf->state_changed = true;
3859 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003860 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003861}
3862
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003863static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003864xdg_surface_set_window_geometry(struct wl_client *client,
3865 struct wl_resource *resource,
3866 int32_t x,
3867 int32_t y,
3868 int32_t width,
3869 int32_t height)
3870{
3871 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3872
3873 set_window_geometry(shsurf, x, y, width, height);
3874}
3875
3876static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003877xdg_surface_set_maximized(struct wl_client *client,
3878 struct wl_resource *resource)
3879{
3880 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003881 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003882
3883 shsurf->state_requested = true;
3884 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003885
3886 if (!weston_surface_is_mapped(shsurf->surface))
3887 output = get_focused_output(shsurf->surface->compositor);
3888 else
3889 output = shsurf->surface->output;
3890
3891 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003892 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003893}
3894
3895static void
3896xdg_surface_unset_maximized(struct wl_client *client,
3897 struct wl_resource *resource)
3898{
3899 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3900
3901 shsurf->state_requested = true;
3902 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003903 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003904}
3905
3906static void
3907xdg_surface_set_fullscreen(struct wl_client *client,
3908 struct wl_resource *resource,
3909 struct wl_resource *output_resource)
3910{
3911 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3912 struct weston_output *output;
3913
3914 shsurf->state_requested = true;
3915 shsurf->requested_state.fullscreen = true;
3916
3917 if (output_resource)
3918 output = wl_resource_get_user_data(output_resource);
3919 else
3920 output = NULL;
3921
Marek Chalupa052917a2014-09-02 11:35:12 +02003922 /* handle clients launching in fullscreen */
3923 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3924 /* Set the output to the one that has focus currently. */
3925 assert(shsurf->surface);
3926 output = get_focused_output(shsurf->surface->compositor);
3927 }
3928
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003929 shell_surface_set_output(shsurf, output);
3930 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003931
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003932 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003933}
3934
3935static void
3936xdg_surface_unset_fullscreen(struct wl_client *client,
3937 struct wl_resource *resource)
3938{
3939 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3940
3941 shsurf->state_requested = true;
3942 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003943 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003944}
3945
3946static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003947xdg_surface_set_minimized(struct wl_client *client,
3948 struct wl_resource *resource)
3949{
3950 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3951
3952 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3953 return;
3954
3955 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003956 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003957}
3958
Rafael Antognollie2a34552013-12-03 15:35:45 -02003959static const struct xdg_surface_interface xdg_surface_implementation = {
3960 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003961 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003962 xdg_surface_set_title,
3963 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003964 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003965 xdg_surface_move,
3966 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003967 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003968 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003969 xdg_surface_set_maximized,
3970 xdg_surface_unset_maximized,
3971 xdg_surface_set_fullscreen,
3972 xdg_surface_unset_fullscreen,
3973 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003974};
3975
3976static void
3977xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003978 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003979{
3980 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003981 uint32_t *s;
3982 struct wl_array states;
3983 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003984
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003985 assert(shsurf);
3986
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003987 if (!shsurf->resource)
3988 return;
3989
3990 wl_array_init(&states);
3991 if (shsurf->requested_state.fullscreen) {
3992 s = wl_array_add(&states, sizeof *s);
3993 *s = XDG_SURFACE_STATE_FULLSCREEN;
3994 } else if (shsurf->requested_state.maximized) {
3995 s = wl_array_add(&states, sizeof *s);
3996 *s = XDG_SURFACE_STATE_MAXIMIZED;
3997 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003998 if (shsurf->resize_edges != 0) {
3999 s = wl_array_add(&states, sizeof *s);
4000 *s = XDG_SURFACE_STATE_RESIZING;
4001 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004002 if (shsurf->focus_count > 0) {
4003 s = wl_array_add(&states, sizeof *s);
4004 *s = XDG_SURFACE_STATE_ACTIVATED;
4005 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004006
4007 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4008 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4009
4010 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004011}
4012
4013static const struct weston_shell_client xdg_client = {
4014 xdg_send_configure
4015};
4016
4017static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004018xdg_shell_destroy(struct wl_client *client,
4019 struct wl_resource *resource)
4020{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004021 struct shell_client *sc = wl_resource_get_user_data(resource);
4022 struct wl_resource *shsurf_resource;
4023 struct shell_surface *shsurf;
4024
4025 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4026 shsurf = wl_resource_get_user_data(shsurf_resource);
4027 if (shsurf->owner_resource == resource) {
4028 wl_resource_post_error(
4029 resource,
4030 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4031 "not all child surface objects destroyed");
4032 return;
4033 }
4034 }
4035
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004036 wl_resource_destroy(resource);
4037}
4038
4039static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004040xdg_use_unstable_version(struct wl_client *client,
4041 struct wl_resource *resource,
4042 int32_t version)
4043{
4044 if (version > 1) {
4045 wl_resource_post_error(resource,
4046 1,
4047 "xdg-shell:: version not implemented yet.");
4048 return;
4049 }
4050}
4051
4052static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004053create_xdg_surface(struct shell_client *owner, void *shell,
4054 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004055 const struct weston_shell_client *client)
4056{
4057 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004058
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004059 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004060 if (!shsurf)
4061 return NULL;
4062
Pekka Paalanenb5026542014-11-12 15:09:24 +02004063 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004064
4065 return shsurf;
4066}
4067
4068static void
4069xdg_get_xdg_surface(struct wl_client *client,
4070 struct wl_resource *resource,
4071 uint32_t id,
4072 struct wl_resource *surface_resource)
4073{
4074 struct weston_surface *surface =
4075 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004076 struct shell_client *sc = wl_resource_get_user_data(resource);
4077 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004078 struct shell_surface *shsurf;
4079
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004080 shsurf = get_shell_surface(surface);
4081 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4082 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4083 "This wl_surface is already an "
4084 "xdg_surface");
4085 return;
4086 }
4087
Pekka Paalanen50b67472014-10-01 15:02:41 +03004088 if (weston_surface_set_role(surface, "xdg_surface",
4089 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004090 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004091
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004092 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004093 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004094 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004095 return;
4096 }
4097
4098 shsurf->resource =
4099 wl_resource_create(client,
4100 &xdg_surface_interface, 1, id);
4101 wl_resource_set_implementation(shsurf->resource,
4102 &xdg_surface_implementation,
4103 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004104 shsurf->owner_resource = resource;
4105 wl_list_insert(&sc->surface_list,
4106 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004107}
4108
4109static bool
4110shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4111{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004112 return shsurf->resource &&
4113 wl_resource_instance_of(shsurf->resource,
4114 &xdg_surface_interface,
4115 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004116}
4117
4118/* xdg-popup implementation */
4119
4120static void
4121xdg_popup_destroy(struct wl_client *client,
4122 struct wl_resource *resource)
4123{
4124 wl_resource_destroy(resource);
4125}
4126
Rafael Antognollie2a34552013-12-03 15:35:45 -02004127static const struct xdg_popup_interface xdg_popup_implementation = {
4128 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004129};
4130
4131static void
4132xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004133 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004134{
4135}
4136
4137static const struct weston_shell_client xdg_popup_client = {
4138 xdg_popup_send_configure
4139};
4140
4141static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004142create_xdg_popup(struct shell_client *owner, void *shell,
4143 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004144 const struct weston_shell_client *client,
4145 struct weston_surface *parent,
4146 struct shell_seat *seat,
4147 uint32_t serial,
4148 int32_t x, int32_t y)
4149{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004150 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004151
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004152 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004153 if (!shsurf)
4154 return NULL;
4155
Pekka Paalanenb5026542014-11-12 15:09:24 +02004156 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004157 shsurf->popup.shseat = seat;
4158 shsurf->popup.serial = serial;
4159 shsurf->popup.x = x;
4160 shsurf->popup.y = y;
4161 shell_surface_set_parent(shsurf, parent);
4162
4163 return shsurf;
4164}
4165
4166static void
4167xdg_get_xdg_popup(struct wl_client *client,
4168 struct wl_resource *resource,
4169 uint32_t id,
4170 struct wl_resource *surface_resource,
4171 struct wl_resource *parent_resource,
4172 struct wl_resource *seat_resource,
4173 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004174 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004175{
4176 struct weston_surface *surface =
4177 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004178 struct shell_client *sc = wl_resource_get_user_data(resource);
4179 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004180 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004181 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004182 struct weston_surface *parent;
4183 struct shell_seat *seat;
4184
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004185 shsurf = get_shell_surface(surface);
4186 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4187 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4188 "This wl_surface is already an "
4189 "xdg_popup");
4190 return;
4191 }
4192
Pekka Paalanen50b67472014-10-01 15:02:41 +03004193 if (weston_surface_set_role(surface, "xdg_popup",
4194 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004195 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004196
4197 if (!parent_resource) {
4198 wl_resource_post_error(surface_resource,
4199 WL_DISPLAY_ERROR_INVALID_OBJECT,
4200 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004201 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004202 }
4203
4204 parent = wl_resource_get_user_data(parent_resource);
4205 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4206
Jonas Ådahlee45a552015-03-18 16:37:47 +08004207 /* Verify that we are creating the top most popup when mapping,
4208 * as it's not until then we know whether it was mapped as most
4209 * top level or not. */
4210
4211 parent_shsurf = get_shell_surface(parent);
4212 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4213 !shell_surface_is_xdg_surface(parent_shsurf)) {
4214 wl_resource_post_error(resource,
4215 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4216 "xdg_popup parent was invalid");
4217 return;
4218 }
4219
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004220 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004221 parent, seat, serial, x, y);
4222 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004223 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004224 return;
4225 }
4226
4227 shsurf->resource =
4228 wl_resource_create(client,
4229 &xdg_popup_interface, 1, id);
4230 wl_resource_set_implementation(shsurf->resource,
4231 &xdg_popup_implementation,
4232 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004233 shsurf->owner_resource = resource;
4234 wl_list_insert(&sc->surface_list,
4235 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004236}
4237
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004238static void
4239xdg_pong(struct wl_client *client,
4240 struct wl_resource *resource, uint32_t serial)
4241{
4242 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004243
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004244 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004245}
4246
Rafael Antognollie2a34552013-12-03 15:35:45 -02004247static bool
4248shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4249{
4250 return wl_resource_instance_of(shsurf->resource,
4251 &xdg_popup_interface,
4252 &xdg_popup_implementation);
4253}
4254
4255static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004256 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004257 xdg_use_unstable_version,
4258 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004259 xdg_get_xdg_popup,
4260 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004261};
4262
4263static int
4264xdg_shell_unversioned_dispatch(const void *implementation,
4265 void *_target, uint32_t opcode,
4266 const struct wl_message *message,
4267 union wl_argument *args)
4268{
4269 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004270 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004271
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004272 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004273 wl_resource_post_error(resource,
4274 WL_DISPLAY_ERROR_INVALID_OBJECT,
4275 "must call use_unstable_version first");
4276 return 0;
4277 }
4278
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004279#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004280
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004281 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4282 "shell implementation doesn't match protocol version");
4283
Rafael Antognollie2a34552013-12-03 15:35:45 -02004284 if (args[0].i != XDG_SERVER_VERSION) {
4285 wl_resource_post_error(resource,
4286 WL_DISPLAY_ERROR_INVALID_OBJECT,
4287 "incompatible version, server is %d "
4288 "client wants %d",
4289 XDG_SERVER_VERSION, args[0].i);
4290 return 0;
4291 }
4292
4293 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004294 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004295
4296 return 1;
4297}
4298
4299/* end of xdg-shell implementation */
4300/***********************************/
4301
Kristian Høgsberg07937562011-04-12 17:25:42 -04004302static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004303shell_fade(struct desktop_shell *shell, enum fade_type type);
4304
Pekka Paalanen77346a62011-11-30 16:26:35 +02004305static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004306configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004307{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004308 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004309
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004310 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004311 if (v->output == ev->output && v != ev) {
4312 weston_view_unmap(v);
4313 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004314 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004315 }
4316 }
4317
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004318 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004319
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004320 if (wl_list_empty(&ev->layer_link.link)) {
4321 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004322 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004323 }
4324}
4325
Pekka Paalanen8274d902014-08-06 19:36:51 +03004326static int
4327background_get_label(struct weston_surface *surface, char *buf, size_t len)
4328{
4329 return snprintf(buf, len, "background for output %s",
4330 surface->output->name);
4331}
4332
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004333static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004334background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004335{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004336 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004337 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004338
Jason Ekstranda7af7042013-10-12 22:38:11 -05004339 view = container_of(es->views.next, struct weston_view, surface_link);
4340
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004341 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004342}
4343
4344static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004345desktop_shell_set_background(struct wl_client *client,
4346 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004347 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004348 struct wl_resource *surface_resource)
4349{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004350 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004351 struct weston_surface *surface =
4352 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004354
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004355 if (surface->configure) {
4356 wl_resource_post_error(surface_resource,
4357 WL_DISPLAY_ERROR_INVALID_OBJECT,
4358 "surface role already assigned");
4359 return;
4360 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004361
Jason Ekstranda7af7042013-10-12 22:38:11 -05004362 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4363 weston_view_destroy(view);
4364 view = weston_view_create(surface);
4365
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004366 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004367 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004368 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004369 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004371 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004372 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004373 surface->output->width,
4374 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004375}
4376
Pekka Paalanen8274d902014-08-06 19:36:51 +03004377static int
4378panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4379{
4380 return snprintf(buf, len, "panel for output %s",
4381 surface->output->name);
4382}
4383
Kristian Høgsberg75840622011-09-06 13:48:16 -04004384static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004385panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004386{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004387 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004389
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 view = container_of(es->views.next, struct weston_view, surface_link);
4391
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004392 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004393}
4394
4395static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004396desktop_shell_set_panel(struct wl_client *client,
4397 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004398 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004399 struct wl_resource *surface_resource)
4400{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004401 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004402 struct weston_surface *surface =
4403 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004405
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004406 if (surface->configure) {
4407 wl_resource_post_error(surface_resource,
4408 WL_DISPLAY_ERROR_INVALID_OBJECT,
4409 "surface role already assigned");
4410 return;
4411 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004412
Jason Ekstranda7af7042013-10-12 22:38:11 -05004413 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4414 weston_view_destroy(view);
4415 view = weston_view_create(surface);
4416
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004417 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004418 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004419 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004420 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004421 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004422 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004423 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004424 surface->output->width,
4425 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004426}
4427
Pekka Paalanen8274d902014-08-06 19:36:51 +03004428static int
4429lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4430{
4431 return snprintf(buf, len, "lock window");
4432}
4433
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004434static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004435lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004436{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004437 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004438 struct weston_view *view;
4439
4440 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004441
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004442 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004443 return;
4444
Jason Ekstranda7af7042013-10-12 22:38:11 -05004445 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004446
4447 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004448 weston_layer_entry_insert(&shell->lock_layer.view_list,
4449 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004450 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004451 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004452 }
4453}
4454
4455static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004456handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004457{
Tiago Vignattibe143262012-04-16 17:31:41 +03004458 struct desktop_shell *shell =
4459 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004460
Martin Minarik6d118362012-06-07 18:01:59 +02004461 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004462 shell->lock_surface = NULL;
4463}
4464
4465static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004466desktop_shell_set_lock_surface(struct wl_client *client,
4467 struct wl_resource *resource,
4468 struct wl_resource *surface_resource)
4469{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004470 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004471 struct weston_surface *surface =
4472 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004473
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004474 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004475
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004476 if (!shell->locked)
4477 return;
4478
Pekka Paalanen98262232011-12-01 10:42:22 +02004479 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004480
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004481 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004482 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004483 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004484
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004485 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004486 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004487 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004488 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004489}
4490
4491static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004492resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004493{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004494 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004495
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004496 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004497 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004498 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004499 &shell->input_panel_layer.link);
4500 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004501 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004502 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004503 wl_list_insert(&shell->compositor->cursor_layer.link,
4504 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004505 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004506 wl_list_insert(&shell->fullscreen_layer.link,
4507 &shell->panel_layer.link);
4508 wl_list_insert(&shell->panel_layer.link,
4509 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004510
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004511 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004512
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004513 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004514 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004515 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004516}
4517
4518static void
4519desktop_shell_unlock(struct wl_client *client,
4520 struct wl_resource *resource)
4521{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004522 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004523
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004524 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004525
4526 if (shell->locked)
4527 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004528}
4529
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004530static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004531desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004532 struct wl_resource *resource,
4533 struct wl_resource *surface_resource)
4534{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004535 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004536
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004537 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004538 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004539}
4540
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004541static void
4542desktop_shell_desktop_ready(struct wl_client *client,
4543 struct wl_resource *resource)
4544{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004545 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004546
4547 shell_fade_startup(shell);
4548}
4549
Jonny Lamb765760d2014-08-20 15:53:19 +02004550static void
4551desktop_shell_set_panel_position(struct wl_client *client,
4552 struct wl_resource *resource,
4553 uint32_t position)
4554{
4555 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4556
4557 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4558 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4559 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4560 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4561 wl_resource_post_error(resource,
4562 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4563 "bad position argument");
4564 return;
4565 }
4566
4567 shell->panel_position = position;
4568}
4569
Kristian Høgsberg75840622011-09-06 13:48:16 -04004570static const struct desktop_shell_interface desktop_shell_implementation = {
4571 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004572 desktop_shell_set_panel,
4573 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004574 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004575 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004576 desktop_shell_desktop_ready,
4577 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004578};
4579
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004580static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004581get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004582{
4583 struct shell_surface *shsurf;
4584
4585 shsurf = get_shell_surface(surface);
4586 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004587 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004588 return shsurf->type;
4589}
4590
Kristian Høgsberg75840622011-09-06 13:48:16 -04004591static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004592move_binding(struct weston_pointer *pointer, uint32_t time,
4593 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004594{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004595 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004596 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004597 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004598
Derek Foreman8ae2db52015-07-15 13:00:36 -05004599 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004600 return;
4601
Derek Foreman8ae2db52015-07-15 13:00:36 -05004602 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004603
Pekka Paalanen01388e22013-04-25 13:57:44 +03004604 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004605 if (surface == NULL)
4606 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004607
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004608 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004609 if (shsurf == NULL || shsurf->state.fullscreen ||
4610 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004611 return;
4612
Derek Foreman794fa0e2015-07-15 13:00:38 -05004613 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004614}
4615
4616static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004617maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4618 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004619{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004620 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004621 struct weston_surface *surface;
4622 struct shell_surface *shsurf;
4623
4624 surface = weston_surface_get_main_surface(focus);
4625 if (surface == NULL)
4626 return;
4627
4628 shsurf = get_shell_surface(surface);
4629 if (shsurf == NULL)
4630 return;
4631
4632 if (!shell_surface_is_xdg_surface(shsurf))
4633 return;
4634
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004635 shsurf->state_requested = true;
4636 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004637 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004638}
4639
4640static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004641fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4642 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004643{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004644 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004645 struct weston_surface *surface;
4646 struct shell_surface *shsurf;
4647
4648 surface = weston_surface_get_main_surface(focus);
4649 if (surface == NULL)
4650 return;
4651
4652 shsurf = get_shell_surface(surface);
4653 if (shsurf == NULL)
4654 return;
4655
4656 if (!shell_surface_is_xdg_surface(shsurf))
4657 return;
4658
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004659 shsurf->state_requested = true;
4660 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004661 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004662 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004663}
4664
4665static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004666touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004667{
Derek Foreman362656b2014-09-04 10:23:05 -05004668 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004669 struct weston_surface *surface;
4670 struct shell_surface *shsurf;
4671
Derek Foreman8ae2db52015-07-15 13:00:36 -05004672 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004673 return;
4674
Derek Foreman8ae2db52015-07-15 13:00:36 -05004675 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004676 surface = weston_surface_get_main_surface(focus);
4677 if (surface == NULL)
4678 return;
4679
4680 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004681 if (shsurf == NULL || shsurf->state.fullscreen ||
4682 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004683 return;
4684
Derek Foremanb7674ae2015-07-15 13:00:37 -05004685 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004686}
4687
4688static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004689resize_binding(struct weston_pointer *pointer, uint32_t time,
4690 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004691{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004692 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004693 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004694 uint32_t edges = 0;
4695 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004696 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004697
Derek Foreman8ae2db52015-07-15 13:00:36 -05004698 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004699 return;
4700
Derek Foreman8ae2db52015-07-15 13:00:36 -05004701 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004702
Pekka Paalanen01388e22013-04-25 13:57:44 +03004703 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004704 if (surface == NULL)
4705 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004706
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004707 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004708 if (shsurf == NULL || shsurf->state.fullscreen ||
4709 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004710 return;
4711
Jason Ekstranda7af7042013-10-12 22:38:11 -05004712 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004713 wl_fixed_to_int(pointer->grab_x),
4714 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004715 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004716
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004717 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004718 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004719 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004720 edges |= 0;
4721 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004722 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004723
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004724 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004725 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004727 edges |= 0;
4728 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004729 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004730
Derek Foreman8ae2db52015-07-15 13:00:36 -05004731 surface_resize(shsurf, pointer->seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004732}
4733
4734static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004735surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4736 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004737{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004738 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004739 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004740 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004741 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004742
Pekka Paalanen01388e22013-04-25 13:57:44 +03004743 /* XXX: broken for windows containing sub-surfaces */
4744 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004745 if (surface == NULL)
4746 return;
4747
4748 shsurf = get_shell_surface(surface);
4749 if (!shsurf)
4750 return;
4751
Jason Ekstranda7af7042013-10-12 22:38:11 -05004752 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004753
Jason Ekstranda7af7042013-10-12 22:38:11 -05004754 if (shsurf->view->alpha > 1.0)
4755 shsurf->view->alpha = 1.0;
4756 if (shsurf->view->alpha < step)
4757 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004758
Jason Ekstranda7af7042013-10-12 22:38:11 -05004759 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004760 weston_surface_damage(surface);
4761}
4762
4763static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004764do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004765 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004766{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004767 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004768 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004769 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004770
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004771 if (!seat->pointer) {
4772 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4773 return;
4774 }
4775
Scott Moreauccbf29d2012-02-22 14:21:41 -07004776 wl_list_for_each(output, &compositor->output_list, link) {
4777 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004778 wl_fixed_to_double(seat->pointer->x),
4779 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004780 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004781 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004782 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004783 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004784 increment = -output->zoom.increment;
4785 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004786 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004787 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004788 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004789 else
4790 increment = 0;
4791
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004792 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004793
Scott Moreaue6603982012-06-11 13:07:51 -06004794 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004795 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004796 else if (output->zoom.level > output->zoom.max_level)
4797 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004798 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004799 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004800 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004801
Scott Moreaue6603982012-06-11 13:07:51 -06004802 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004803
Jason Ekstranda7af7042013-10-12 22:38:11 -05004804 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004805 }
4806 }
4807}
4808
Scott Moreauccbf29d2012-02-22 14:21:41 -07004809static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004810zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4811 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004812{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004813 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004814}
4815
4816static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004817zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4818 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004819{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004820 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004821}
4822
4823static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004824terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4825 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004826{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004827 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004828
Daniel Stone325fc2d2012-05-30 16:31:58 +01004829 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004830}
4831
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004832static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004833rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4834 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004835{
4836 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004837 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004838 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004839 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004840 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004841
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004842 weston_pointer_move(pointer, x, y);
4843
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004844 if (!shsurf)
4845 return;
4846
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004847 cx = 0.5f * shsurf->surface->width;
4848 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004849
Daniel Stone37816df2012-05-16 18:45:18 +01004850 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4851 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004852 r = sqrtf(dx * dx + dy * dy);
4853
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004854 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004855 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004856
4857 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004858 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004859 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004860
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004861 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004862 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004863
4864 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004865 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004866 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004867 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004868 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004869
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004870 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004871 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004872 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004873 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004874 wl_list_init(&shsurf->rotation.transform.link);
4875 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004876 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004877 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004878
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004879 /* We need to adjust the position of the surface
4880 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004881 cposx = shsurf->view->geometry.x + cx;
4882 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004883 dposx = rotate->center.x - cposx;
4884 dposy = rotate->center.y - cposy;
4885 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004886 weston_view_set_position(shsurf->view,
4887 shsurf->view->geometry.x + dposx,
4888 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004889 }
4890
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004891 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004892 * lazily applies the damage due to rotation update.
4893 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004894 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004895}
4896
4897static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004898rotate_grab_button(struct weston_pointer_grab *grab,
4899 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004900{
4901 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004902 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004903 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004904 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004905 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004906
Daniel Stone4dbadb12012-05-30 16:31:51 +01004907 if (pointer->button_count == 0 &&
4908 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004909 if (shsurf)
4910 weston_matrix_multiply(&shsurf->rotation.rotation,
4911 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004912 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004913 free(rotate);
4914 }
4915}
4916
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004917static void
4918rotate_grab_cancel(struct weston_pointer_grab *grab)
4919{
4920 struct rotate_grab *rotate =
4921 container_of(grab, struct rotate_grab, base.grab);
4922
4923 shell_grab_end(&rotate->base);
4924 free(rotate);
4925}
4926
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004927static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004928 noop_grab_focus,
4929 rotate_grab_motion,
4930 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004931 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004932};
4933
4934static void
Derek Foreman74de4692015-07-15 13:00:39 -05004935surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004936{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004937 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004938 float dx, dy;
4939 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004940
Pekka Paalanen460099f2012-01-20 16:48:25 +02004941 rotate = malloc(sizeof *rotate);
4942 if (!rotate)
4943 return;
4944
Jason Ekstranda7af7042013-10-12 22:38:11 -05004945 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004946 surface->surface->width * 0.5f,
4947 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004948 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004949
Derek Foreman74de4692015-07-15 13:00:39 -05004950 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4951 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004952 r = sqrtf(dx * dx + dy * dy);
4953 if (r > 20.0f) {
4954 struct weston_matrix inverse;
4955
4956 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004957 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004958 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004959
4960 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004961 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004962 } else {
4963 weston_matrix_init(&surface->rotation.rotation);
4964 weston_matrix_init(&rotate->rotation);
4965 }
4966
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004967 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Derek Foreman74de4692015-07-15 13:00:39 -05004968 pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004969}
4970
4971static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004972rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004973 void *data)
4974{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004975 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004976 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004977 struct shell_surface *surface;
4978
Derek Foreman8ae2db52015-07-15 13:00:36 -05004979 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004980 return;
4981
Derek Foreman8ae2db52015-07-15 13:00:36 -05004982 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004983
Pekka Paalanen01388e22013-04-25 13:57:44 +03004984 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004985 if (base_surface == NULL)
4986 return;
4987
4988 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004989 if (surface == NULL || surface->state.fullscreen ||
4990 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004991 return;
4992
Derek Foreman74de4692015-07-15 13:00:39 -05004993 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004994}
4995
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004996/* Move all fullscreen layers down to the current workspace and hide their
4997 * black views. The surfaces' state is set to both fullscreen and lowered,
4998 * and this is reversed when such a surface is re-configured, see
4999 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5000 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005001 * lowering_output = NULL - Lower on all outputs, else only lower on the
5002 * specified output.
5003 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005004 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005005 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005006void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005007lower_fullscreen_layer(struct desktop_shell *shell,
5008 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005009{
5010 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005011 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005012
5013 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005014 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005015 &shell->fullscreen_layer.view_list.link,
5016 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005017 struct shell_surface *shsurf = get_shell_surface(view->surface);
5018
5019 if (!shsurf)
5020 continue;
5021
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005022 /* Only lower surfaces which have lowering_output as their fullscreen
5023 * output, unless a NULL output asks for lowering on all outputs.
5024 */
5025 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5026 continue;
5027
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005028 /* We can have a non-fullscreen popup for a fullscreen surface
5029 * in the fullscreen layer. */
5030 if (shsurf->state.fullscreen) {
5031 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005032 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5033 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005034 weston_view_damage_below(shsurf->fullscreen.black_view);
5035
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005036 }
5037
5038 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005039 weston_layer_entry_remove(&view->layer_link);
5040 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005041 weston_view_damage_below(view);
5042 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005043
5044 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005045 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005046}
5047
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005048void
Tiago Vignattibe143262012-04-16 17:31:41 +03005049activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005050 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005051{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005052 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005053 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005054 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005055 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005056 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005057
Pekka Paalanen01388e22013-04-25 13:57:44 +03005058 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005059 shsurf = get_shell_surface(main_surface);
5060 assert(shsurf);
5061
5062 /* Only demote fullscreen surfaces on the output of activated shsurf.
5063 * Leave fullscreen surfaces on unrelated outputs alone. */
5064 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005065
Daniel Stone37816df2012-05-16 18:45:18 +01005066 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005067
Jonas Ådahl8538b222012-08-29 22:13:03 +02005068 state = ensure_focus_state(shell, seat);
5069 if (state == NULL)
5070 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005071
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005072 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005073 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005074
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005075 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005076 shell_configure_fullscreen(shsurf);
5077 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005078 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005079
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005080 /* Update the surface’s layer. This brings it to the top of the stacking
5081 * order as appropriate. */
5082 shell_surface_update_layer(shsurf);
5083
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005084 if (shell->focus_animation_type != ANIMATION_NONE) {
5085 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005086 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005087 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005088}
5089
Alex Wu21858432012-04-01 20:13:08 +08005090/* no-op func for checking black surface */
5091static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005092black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005093{
5094}
5095
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005096static bool
Alex Wu21858432012-04-01 20:13:08 +08005097is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5098{
5099 if (es->configure == black_surface_configure) {
5100 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005101 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005102 return true;
5103 }
5104 return false;
5105}
5106
Kristian Høgsberg75840622011-09-06 13:48:16 -04005107static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005108activate_binding(struct weston_seat *seat,
5109 struct desktop_shell *shell,
5110 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005111{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005112 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005113
Alex Wu9c35e6b2012-03-05 14:13:13 +08005114 if (!focus)
5115 return;
5116
Pekka Paalanen01388e22013-04-25 13:57:44 +03005117 if (is_black_surface(focus, &main_surface))
5118 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005119
Pekka Paalanen01388e22013-04-25 13:57:44 +03005120 main_surface = weston_surface_get_main_surface(focus);
5121 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005122 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005123
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005124 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005125}
5126
5127static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005128click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5129 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005130{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005131 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005132 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005133 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005134 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005135
Derek Foreman8ae2db52015-07-15 13:00:36 -05005136 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005137}
5138
5139static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005140touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5141 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005142{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005143 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005144 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005145 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005146 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005147
Derek Foreman8ae2db52015-07-15 13:00:36 -05005148 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005149}
5150
5151static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005152unfocus_all_seats(struct desktop_shell *shell)
5153{
5154 struct weston_seat *seat, *next;
5155
5156 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5157 if (seat->keyboard == NULL)
5158 continue;
5159
5160 weston_keyboard_set_focus(seat->keyboard, NULL);
5161 }
5162}
5163
5164static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005165lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005166{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005167 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005168
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005169 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005170 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005171 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005172 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005173
5174 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005175
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005176 /* Hide all surfaces by removing the fullscreen, panel and
5177 * toplevel layers. This way nothing else can show or receive
5178 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005179
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005180 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005181 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005182 if (shell->showing_input_panels)
5183 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005184 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005185 wl_list_insert(&shell->compositor->cursor_layer.link,
5186 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005187
Neil Robertsb4a91702014-04-09 16:33:32 +01005188 /* Remove the keyboard focus on all seats. This will be
5189 * restored to the workspace's saved state via
5190 * restore_focus_state when the compositor is unlocked */
5191 unfocus_all_seats(shell);
5192
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005193 /* TODO: disable bindings that should not work while locked. */
5194
5195 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005196}
5197
5198static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005199unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005200{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005201 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005202 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005203 return;
5204 }
5205
5206 /* If desktop-shell client has gone away, unlock immediately. */
5207 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005208 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005209 return;
5210 }
5211
5212 if (shell->prepare_event_sent)
5213 return;
5214
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005215 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005216 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005217}
5218
5219static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005220shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005221{
5222 struct desktop_shell *shell = data;
5223
5224 shell->fade.animation = NULL;
5225
5226 switch (shell->fade.type) {
5227 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005228 weston_surface_destroy(shell->fade.view->surface);
5229 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005230 break;
5231 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005232 lock(shell);
5233 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005234 default:
5235 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005236 }
5237}
5238
Jason Ekstranda7af7042013-10-12 22:38:11 -05005239static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005240shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005241{
5242 struct weston_compositor *compositor = shell->compositor;
5243 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005244 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005245
5246 surface = weston_surface_create(compositor);
5247 if (!surface)
5248 return NULL;
5249
Jason Ekstranda7af7042013-10-12 22:38:11 -05005250 view = weston_view_create(surface);
5251 if (!view) {
5252 weston_surface_destroy(surface);
5253 return NULL;
5254 }
5255
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005256 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005257 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005258 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005259 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5260 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005261 pixman_region32_init(&surface->input);
5262
Jason Ekstranda7af7042013-10-12 22:38:11 -05005263 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005264}
5265
5266static void
5267shell_fade(struct desktop_shell *shell, enum fade_type type)
5268{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005269 float tint;
5270
5271 switch (type) {
5272 case FADE_IN:
5273 tint = 0.0;
5274 break;
5275 case FADE_OUT:
5276 tint = 1.0;
5277 break;
5278 default:
5279 weston_log("shell: invalid fade type\n");
5280 return;
5281 }
5282
5283 shell->fade.type = type;
5284
Jason Ekstranda7af7042013-10-12 22:38:11 -05005285 if (shell->fade.view == NULL) {
5286 shell->fade.view = shell_fade_create_surface(shell);
5287 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005288 return;
5289
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 shell->fade.view->alpha = 1.0 - tint;
5291 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005292 }
5293
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005294 if (shell->fade.view->output == NULL) {
5295 /* If the black view gets a NULL output, we lost the
5296 * last output and we'll just cancel the fade. This
5297 * happens when you close the last window under the
5298 * X11 or Wayland backends. */
5299 shell->locked = false;
5300 weston_surface_destroy(shell->fade.view->surface);
5301 shell->fade.view = NULL;
5302 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005303 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005304 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005305 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005306 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005307 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005308 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005309 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005310}
5311
5312static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005313do_shell_fade_startup(void *data)
5314{
5315 struct desktop_shell *shell = data;
5316
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005317 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005318 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005319 } else {
5320 weston_log("desktop shell: "
5321 "unexpected fade-in animation type %d\n",
5322 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005323 weston_surface_destroy(shell->fade.view->surface);
5324 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005325 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005326}
5327
5328static void
5329shell_fade_startup(struct desktop_shell *shell)
5330{
5331 struct wl_event_loop *loop;
5332
5333 if (!shell->fade.startup_timer)
5334 return;
5335
5336 wl_event_source_remove(shell->fade.startup_timer);
5337 shell->fade.startup_timer = NULL;
5338
5339 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5340 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5341}
5342
5343static int
5344fade_startup_timeout(void *data)
5345{
5346 struct desktop_shell *shell = data;
5347
5348 shell_fade_startup(shell);
5349 return 0;
5350}
5351
5352static void
5353shell_fade_init(struct desktop_shell *shell)
5354{
5355 /* Make compositor output all black, and wait for the desktop-shell
5356 * client to signal it is ready, then fade in. The timer triggers a
5357 * fade-in, in case the desktop-shell client takes too long.
5358 */
5359
5360 struct wl_event_loop *loop;
5361
Jason Ekstranda7af7042013-10-12 22:38:11 -05005362 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005363 weston_log("%s: warning: fade surface already exists\n",
5364 __func__);
5365 return;
5366 }
5367
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005368 if (shell->startup_animation_type == ANIMATION_NONE)
5369 return;
5370
Jason Ekstranda7af7042013-10-12 22:38:11 -05005371 shell->fade.view = shell_fade_create_surface(shell);
5372 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005373 return;
5374
Jason Ekstranda7af7042013-10-12 22:38:11 -05005375 weston_view_update_transform(shell->fade.view);
5376 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005377
5378 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5379 shell->fade.startup_timer =
5380 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5381 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5382}
5383
5384static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005385idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005386{
5387 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005388 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005389 struct weston_seat *seat;
5390
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005391 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005392 if (seat->pointer)
5393 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005394 if (seat->touch)
5395 touch_popup_grab_end(seat->touch);
5396 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005397
5398 shell_fade(shell, FADE_OUT);
5399 /* lock() is called from shell_fade_done() */
5400}
5401
5402static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005403wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005404{
5405 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005406 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005407
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005408 unlock(shell);
5409}
5410
5411static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005412center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005413{
Giulio Camuffob8366642013-04-25 13:57:46 +03005414 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005415 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005416
Jason Ekstranda7af7042013-10-12 22:38:11 -05005417 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005418
5419 x = output->x + (output->width - width) / 2 - surf_x / 2;
5420 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005421
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005422 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005423}
5424
5425static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005426weston_view_set_initial_position(struct weston_view *view,
5427 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005428{
5429 struct weston_compositor *compositor = shell->compositor;
5430 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005431 int32_t range_x, range_y;
5432 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005433 struct weston_output *output, *target_output = NULL;
5434 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005435 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005436
5437 /* As a heuristic place the new window on the same output as the
5438 * pointer. Falling back to the output containing 0, 0.
5439 *
5440 * TODO: Do something clever for touch too?
5441 */
5442 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005443 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005444 ix = wl_fixed_to_int(seat->pointer->x);
5445 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005446 break;
5447 }
5448 }
5449
5450 wl_list_for_each(output, &compositor->output_list, link) {
5451 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5452 target_output = output;
5453 break;
5454 }
5455 }
5456
5457 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005458 weston_view_set_position(view, 10 + random() % 400,
5459 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005460 return;
5461 }
5462
5463 /* Valid range within output where the surface will still be onscreen.
5464 * If this is negative it means that the surface is bigger than
5465 * output.
5466 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005467 get_output_work_area(shell, target_output, &area);
5468
5469 dx = area.x;
5470 dy = area.y;
5471 range_x = area.width - view->surface->width;
5472 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005473
Rob Bradford4cb88c72012-08-13 15:18:44 +01005474 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005475 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005476
5477 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005478 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005479
5480 x = target_output->x + dx;
5481 y = target_output->y + dy;
5482
Jason Ekstranda7af7042013-10-12 22:38:11 -05005483 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005484}
5485
5486static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005487set_maximized_position(struct desktop_shell *shell,
5488 struct shell_surface *shsurf)
5489{
5490 int32_t surf_x, surf_y;
5491 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005492 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005493
Jonny Lambd73c6942014-08-20 15:53:20 +02005494 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005495 if (shsurf->has_set_geometry) {
5496 surf_x = shsurf->geometry.x;
5497 surf_y = shsurf->geometry.y;
5498 } else {
5499 surface_subsurfaces_boundingbox(shsurf->surface,
5500 &surf_x, &surf_y, NULL, NULL);
5501 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005502 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005503
5504 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005505 e->x1 + area.x - surf_x,
5506 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005507}
5508
5509static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005510map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005511 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005512{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005513 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005514 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005515
Pekka Paalanen77346a62011-11-30 16:26:35 +02005516 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005517 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005518 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005519 if (shsurf->state.fullscreen) {
5520 center_on_output(shsurf->view, shsurf->fullscreen_output);
5521 shell_map_fullscreen(shsurf);
5522 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005523 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005524 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005525 weston_view_set_initial_position(shsurf->view, shell);
5526 }
Juan Zhao96879df2012-02-07 08:45:41 +08005527 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005528 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005529 if (shell_map_popup(shsurf) != 0)
5530 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005531 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005532 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005533 weston_view_set_position(shsurf->view,
5534 shsurf->view->geometry.x + sx,
5535 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005536 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005537 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005538 default:
5539 ;
5540 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005541
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005542 /* Surface stacking order, see also activate(). */
5543 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005544
Jason Ekstranda7af7042013-10-12 22:38:11 -05005545 if (shsurf->type != SHELL_SURFACE_NONE) {
5546 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005547 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005548 shsurf->surface->output = shsurf->output;
5549 shsurf->view->output = shsurf->output;
5550 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005551 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005552
Jason Ekstranda7af7042013-10-12 22:38:11 -05005553 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005554 /* XXX: xwayland's using the same fields for transient type */
5555 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005556 if (shsurf->transient.flags ==
5557 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5558 break;
5559 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005560 if (shsurf->state.relative &&
5561 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5562 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005563 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005564 break;
5565 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005566 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005567 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005568 case SHELL_SURFACE_POPUP:
5569 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005570 default:
5571 break;
5572 }
5573
Rafael Antognolli03b16592013-12-03 15:35:42 -02005574 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5575 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005576 {
5577 switch (shell->win_animation_type) {
5578 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005579 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005580 break;
5581 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005582 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005583 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005584 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005585 default:
5586 break;
5587 }
5588 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005589}
5590
5591static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005592configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005593 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005594{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005595 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005596 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005597
Pekka Paalanen77346a62011-11-30 16:26:35 +02005598 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005599
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005600 assert(shsurf);
5601
Rafael Antognolli03b16592013-12-03 15:35:42 -02005602 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005603 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005604 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005605 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005606 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005607 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005608 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005609
Alex Wu4539b082012-03-01 12:57:46 +08005610 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005611 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005612 wl_list_for_each(view, &surface->views, surface_link)
5613 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005614
Rafael Antognolli03b16592013-12-03 15:35:42 -02005615 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005616 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005617 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005618}
5619
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005620static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005621shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005622{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005623 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005624 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005625 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005626
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005627 assert(shsurf);
5628
5629 shell = shsurf->shell;
5630
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005631 if (!weston_surface_is_mapped(es) &&
5632 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005633 remove_popup_grab(shsurf);
5634 }
5635
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005636 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005637 return;
5638
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005639 if (shsurf->has_next_geometry) {
5640 shsurf->geometry = shsurf->next_geometry;
5641 shsurf->has_next_geometry = false;
5642 shsurf->has_set_geometry = true;
5643 } else if (!shsurf->has_set_geometry) {
5644 surface_subsurfaces_boundingbox(shsurf->surface,
5645 &shsurf->geometry.x,
5646 &shsurf->geometry.y,
5647 &shsurf->geometry.width,
5648 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005649 }
5650
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005651 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005652 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005653 type_changed = 1;
5654 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005655
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005656 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005657 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005658 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005659 shsurf->last_width != es->width ||
5660 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005661 float from_x, from_y;
5662 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005663
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005664 if (shsurf->resize_edges) {
5665 sx = 0;
5666 sy = 0;
5667 }
5668
5669 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5670 sx = shsurf->last_width - es->width;
5671 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5672 sy = shsurf->last_height - es->height;
5673
5674 shsurf->last_width = es->width;
5675 shsurf->last_height = es->height;
5676
Jason Ekstranda7af7042013-10-12 22:38:11 -05005677 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5678 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005679 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005680 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005681 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005682 }
5683}
5684
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005685static bool
5686check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5687{
5688 struct timespec now;
5689
5690 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5691 return false;
5692
5693 /*
5694 * If the shell helper client dies before the session has been
5695 * up for roughly 30 seconds, better just make Weston shut down,
5696 * because the user likely has no way to interact with the desktop
5697 * anyway.
5698 */
5699 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5700 weston_log("Error: %s apparently cannot run at all.\n",
5701 shell->client);
5702 weston_log_continue(STAMP_SPACE "Quitting...");
5703 wl_display_terminate(shell->compositor->wl_display);
5704
5705 return true;
5706 }
5707
5708 return false;
5709}
5710
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005711static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005712
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005713static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005714respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005715{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005716 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005717
5718 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5719 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005720 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005721 shell->child.deathstamp = time;
5722 shell->child.deathcount = 0;
5723 }
5724
5725 shell->child.deathcount++;
5726 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005727 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005728 return;
5729 }
5730
Pekka Paalanen826dc142014-08-27 12:11:53 +03005731 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005732 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005733}
5734
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005735static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005736desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5737{
5738 struct desktop_shell *shell;
5739
5740 shell = container_of(listener, struct desktop_shell,
5741 child.client_destroy_listener);
5742
Pekka Paalanen826dc142014-08-27 12:11:53 +03005743 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005744 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005745 /*
5746 * unbind_desktop_shell() will reset shell->child.desktop_shell
5747 * before the respawned process has a chance to create a new
5748 * desktop_shell object, because we are being called from the
5749 * wl_client destructor which destroys all wl_resources before
5750 * returning.
5751 */
5752
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005753 if (!check_desktop_shell_crash_too_early(shell))
5754 respawn_desktop_shell_process(shell);
5755
Pekka Paalanen826dc142014-08-27 12:11:53 +03005756 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005757}
5758
5759static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005760launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005761{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005762 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005763
Pekka Paalanen826dc142014-08-27 12:11:53 +03005764 shell->child.client = weston_client_start(shell->compositor,
5765 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005766
Arnaud Vrac42631192014-08-25 20:56:46 +02005767 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005768 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005769 return;
5770 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005771
5772 shell->child.client_destroy_listener.notify =
5773 desktop_shell_client_destroy;
5774 wl_client_add_destroy_listener(shell->child.client,
5775 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005776}
5777
5778static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005779handle_shell_client_destroy(struct wl_listener *listener, void *data)
5780{
5781 struct shell_client *sc =
5782 container_of(listener, struct shell_client, destroy_listener);
5783
5784 if (sc->ping_timer)
5785 wl_event_source_remove(sc->ping_timer);
5786 free(sc);
5787}
5788
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005789static struct shell_client *
5790shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5791 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005792{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005793 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005794
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005795 sc = zalloc(sizeof *sc);
5796 if (sc == NULL) {
5797 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005798 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005799 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005800
5801 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005802 if (sc->resource == NULL) {
5803 free(sc);
5804 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005805 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005806 }
5807
5808 sc->client = client;
5809 sc->shell = shell;
5810 sc->destroy_listener.notify = handle_shell_client_destroy;
5811 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005812 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005813
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005814 return sc;
5815}
5816
5817static void
5818bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5819{
5820 struct desktop_shell *shell = data;
5821 struct shell_client *sc;
5822
5823 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5824 if (sc)
5825 wl_resource_set_implementation(sc->resource,
5826 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005827 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005828}
5829
5830static void
5831bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5832{
5833 struct desktop_shell *shell = data;
5834 struct shell_client *sc;
5835
5836 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5837 if (sc)
5838 wl_resource_set_dispatcher(sc->resource,
5839 xdg_shell_unversioned_dispatch,
5840 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005841}
5842
5843static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005844unbind_desktop_shell(struct wl_resource *resource)
5845{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005846 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005847
5848 if (shell->locked)
5849 resume_desktop(shell);
5850
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005851 shell->child.desktop_shell = NULL;
5852 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005853}
5854
5855static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005856bind_desktop_shell(struct wl_client *client,
5857 void *data, uint32_t version, uint32_t id)
5858{
Tiago Vignattibe143262012-04-16 17:31:41 +03005859 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005860 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005861
Jason Ekstranda85118c2013-06-27 20:17:02 -05005862 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005863 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005864
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005865 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005866 wl_resource_set_implementation(resource,
5867 &desktop_shell_implementation,
5868 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005869 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005870
5871 if (version < 2)
5872 shell_fade_startup(shell);
5873
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005874 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005875 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005876
5877 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5878 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005879}
5880
Kristian Høgsberg07045392012-02-19 18:52:44 -05005881struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005882 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005883 struct weston_surface *current;
5884 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005885 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005886 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005887};
5888
5889static void
5890switcher_next(struct switcher *switcher)
5891{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005892 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005893 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005894 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005895 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005896
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005897 /* temporary re-display minimized surfaces */
5898 struct weston_view *tmp;
5899 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005900 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5901 weston_layer_entry_remove(&view->layer_link);
5902 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005903 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5904 *minimized = view;
5905 }
5906
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005907 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005908 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005909 if (shsurf &&
5910 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5911 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005912 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005913 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005914 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005915 next = view->surface;
5916 prev = view->surface;
5917 view->alpha = 0.25;
5918 weston_view_geometry_dirty(view);
5919 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005920 }
Alex Wu1659daa2012-04-01 20:13:09 +08005921
Jason Ekstranda7af7042013-10-12 22:38:11 -05005922 if (is_black_surface(view->surface, NULL)) {
5923 view->alpha = 0.25;
5924 weston_view_geometry_dirty(view);
5925 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005926 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005927 }
5928
5929 if (next == NULL)
5930 next = first;
5931
Alex Wu07b26062012-03-12 16:06:01 +08005932 if (next == NULL)
5933 return;
5934
Kristian Høgsberg07045392012-02-19 18:52:44 -05005935 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005936 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005937
5938 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005939 wl_list_for_each(view, &next->views, surface_link)
5940 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005941
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005942 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005943 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005944 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005945}
5946
5947static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005948switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005949{
5950 struct switcher *switcher =
5951 container_of(listener, struct switcher, listener);
5952
5953 switcher_next(switcher);
5954}
5955
5956static void
Daniel Stone351eb612012-05-31 15:27:47 -04005957switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005958{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005959 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005960 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005961 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005962
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005963 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005964 if (is_focus_view(view))
5965 continue;
5966
Jason Ekstranda7af7042013-10-12 22:38:11 -05005967 view->alpha = 1.0;
5968 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005969 }
5970
Alex Wu07b26062012-03-12 16:06:01 +08005971 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005972 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005973 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005974 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005975 weston_keyboard_end_grab(keyboard);
5976 if (keyboard->input_method_resource)
5977 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005978
5979 /* re-hide surfaces that were temporary shown during the switch */
5980 struct weston_view **minimized;
5981 wl_array_for_each(minimized, &switcher->minimized_array) {
5982 /* with the exception of the current selected */
5983 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005984 weston_layer_entry_remove(&(*minimized)->layer_link);
5985 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005986 weston_view_damage_below(*minimized);
5987 }
5988 }
5989 wl_array_release(&switcher->minimized_array);
5990
Kristian Høgsberg07045392012-02-19 18:52:44 -05005991 free(switcher);
5992}
5993
5994static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005995switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005996 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005997{
5998 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005999 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006000
Daniel Stonec9785ea2012-05-30 16:31:52 +01006001 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006002 switcher_next(switcher);
6003}
6004
6005static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006006switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006007 uint32_t mods_depressed, uint32_t mods_latched,
6008 uint32_t mods_locked, uint32_t group)
6009{
6010 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006011 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006012
Daniel Stone351eb612012-05-31 15:27:47 -04006013 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6014 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006015}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006016
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006017static void
6018switcher_cancel(struct weston_keyboard_grab *grab)
6019{
6020 struct switcher *switcher = container_of(grab, struct switcher, grab);
6021
6022 switcher_destroy(switcher);
6023}
6024
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006025static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006026 switcher_key,
6027 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006028 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006029};
6030
6031static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006032switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6033 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006034{
Tiago Vignattibe143262012-04-16 17:31:41 +03006035 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006036 struct switcher *switcher;
6037
6038 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006039 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006040 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006041 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006042 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006043 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006045 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006046 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006048 weston_keyboard_start_grab(keyboard, &switcher->grab);
6049 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006050 switcher_next(switcher);
6051}
6052
Pekka Paalanen3c647232011-12-22 13:43:43 +02006053static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006054backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6055 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006056{
6057 struct weston_compositor *compositor = data;
6058 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006059 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006060
6061 /* TODO: we're limiting to simple use cases, where we assume just
6062 * control on the primary display. We'd have to extend later if we
6063 * ever get support for setting backlights on random desktop LCD
6064 * panels though */
6065 output = get_default_output(compositor);
6066 if (!output)
6067 return;
6068
6069 if (!output->set_backlight)
6070 return;
6071
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006072 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6073 backlight_new = output->backlight_current - 25;
6074 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6075 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006076
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006077 if (backlight_new < 5)
6078 backlight_new = 5;
6079 if (backlight_new > 255)
6080 backlight_new = 255;
6081
6082 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006083 output->set_backlight(output, output->backlight_current);
6084}
6085
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006086static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006087force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6088 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006089{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006090 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006091 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006092 struct desktop_shell *shell = data;
6093 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006094 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006095
Derek Foreman8ae2db52015-07-15 13:00:36 -05006096 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006097 if (!focus_surface)
6098 return;
6099
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006100 wl_signal_emit(&compositor->kill_signal, focus_surface);
6101
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006102 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006103 wl_client_get_credentials(client, &pid, NULL, NULL);
6104
6105 /* Skip clients that we launched ourselves (the credentials of
6106 * the socketpair is ours) */
6107 if (pid == getpid())
6108 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006109
Daniel Stone325fc2d2012-05-30 16:31:58 +01006110 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006111}
6112
6113static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006114workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006115 uint32_t key, void *data)
6116{
6117 struct desktop_shell *shell = data;
6118 unsigned int new_index = shell->workspaces.current;
6119
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006120 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006121 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006122 if (new_index != 0)
6123 new_index--;
6124
6125 change_workspace(shell, new_index);
6126}
6127
6128static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006129workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006130 uint32_t key, void *data)
6131{
6132 struct desktop_shell *shell = data;
6133 unsigned int new_index = shell->workspaces.current;
6134
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006135 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006136 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006137 if (new_index < shell->workspaces.num - 1)
6138 new_index++;
6139
6140 change_workspace(shell, new_index);
6141}
6142
6143static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006144workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006145 uint32_t key, void *data)
6146{
6147 struct desktop_shell *shell = data;
6148 unsigned int new_index;
6149
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006150 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006151 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006152 new_index = key - KEY_F1;
6153 if (new_index >= shell->workspaces.num)
6154 new_index = shell->workspaces.num - 1;
6155
6156 change_workspace(shell, new_index);
6157}
6158
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006159static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006160workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6161 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006162{
6163 struct desktop_shell *shell = data;
6164 unsigned int new_index = shell->workspaces.current;
6165
6166 if (shell->locked)
6167 return;
6168
6169 if (new_index != 0)
6170 new_index--;
6171
Derek Foreman8ae2db52015-07-15 13:00:36 -05006172 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006173}
6174
6175static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006176workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6177 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006178{
6179 struct desktop_shell *shell = data;
6180 unsigned int new_index = shell->workspaces.current;
6181
6182 if (shell->locked)
6183 return;
6184
6185 if (new_index < shell->workspaces.num - 1)
6186 new_index++;
6187
Derek Foreman8ae2db52015-07-15 13:00:36 -05006188 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006189}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006190
6191static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006192shell_reposition_view_on_output_destroy(struct weston_view *view)
6193{
6194 struct weston_output *output, *first_output;
6195 struct weston_compositor *ec = view->surface->compositor;
6196 struct shell_surface *shsurf;
6197 float x, y;
6198 int visible;
6199
6200 x = view->geometry.x;
6201 y = view->geometry.y;
6202
6203 /* At this point the destroyed output is not in the list anymore.
6204 * If the view is still visible somewhere, we leave where it is,
6205 * otherwise, move it to the first output. */
6206 visible = 0;
6207 wl_list_for_each(output, &ec->output_list, link) {
6208 if (pixman_region32_contains_point(&output->region,
6209 x, y, NULL)) {
6210 visible = 1;
6211 break;
6212 }
6213 }
6214
6215 if (!visible) {
6216 first_output = container_of(ec->output_list.next,
6217 struct weston_output, link);
6218
6219 x = first_output->x + first_output->width / 4;
6220 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006221
6222 weston_view_set_position(view, x, y);
6223 } else {
6224 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006225 }
6226
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006227
6228 shsurf = get_shell_surface(view->surface);
6229
6230 if (shsurf) {
6231 shsurf->saved_position_valid = false;
6232 shsurf->next_state.maximized = false;
6233 shsurf->next_state.fullscreen = false;
6234 shsurf->state_changed = true;
6235 }
6236}
6237
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006238void
6239shell_for_each_layer(struct desktop_shell *shell,
6240 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006241{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006242 struct workspace **ws;
6243
6244 func(shell, &shell->fullscreen_layer, data);
6245 func(shell, &shell->panel_layer, data);
6246 func(shell, &shell->background_layer, data);
6247 func(shell, &shell->lock_layer, data);
6248 func(shell, &shell->input_panel_layer, data);
6249
6250 wl_array_for_each(ws, &shell->workspaces.array)
6251 func(shell, &(*ws)->layer, data);
6252}
6253
6254static void
6255shell_output_destroy_move_layer(struct desktop_shell *shell,
6256 struct weston_layer *layer,
6257 void *data)
6258{
6259 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006260 struct weston_view *view;
6261
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006262 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006263 if (view->output != output)
6264 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006265
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006266 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006267 }
6268}
6269
6270static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006271handle_output_destroy(struct wl_listener *listener, void *data)
6272{
6273 struct shell_output *output_listener =
6274 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006275 struct weston_output *output = output_listener->output;
6276 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006277
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006278 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006279
Xiong Zhang6b481422013-10-23 13:58:32 +08006280 wl_list_remove(&output_listener->destroy_listener.link);
6281 wl_list_remove(&output_listener->link);
6282 free(output_listener);
6283}
6284
6285static void
6286create_shell_output(struct desktop_shell *shell,
6287 struct weston_output *output)
6288{
6289 struct shell_output *shell_output;
6290
6291 shell_output = zalloc(sizeof *shell_output);
6292 if (shell_output == NULL)
6293 return;
6294
6295 shell_output->output = output;
6296 shell_output->shell = shell;
6297 shell_output->destroy_listener.notify = handle_output_destroy;
6298 wl_signal_add(&output->destroy_signal,
6299 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006300 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006301}
6302
6303static void
6304handle_output_create(struct wl_listener *listener, void *data)
6305{
6306 struct desktop_shell *shell =
6307 container_of(listener, struct desktop_shell, output_create_listener);
6308 struct weston_output *output = (struct weston_output *)data;
6309
6310 create_shell_output(shell, output);
6311}
6312
6313static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006314handle_output_move_layer(struct desktop_shell *shell,
6315 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006316{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006317 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006318 struct weston_view *view;
6319 float x, y;
6320
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006321 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006322 if (view->output != output)
6323 continue;
6324
6325 x = view->geometry.x + output->move_x;
6326 y = view->geometry.y + output->move_y;
6327 weston_view_set_position(view, x, y);
6328 }
6329}
6330
6331static void
6332handle_output_move(struct wl_listener *listener, void *data)
6333{
6334 struct desktop_shell *shell;
6335
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006336 shell = container_of(listener, struct desktop_shell,
6337 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006338
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006339 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006340}
6341
6342static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006343setup_output_destroy_handler(struct weston_compositor *ec,
6344 struct desktop_shell *shell)
6345{
6346 struct weston_output *output;
6347
6348 wl_list_init(&shell->output_list);
6349 wl_list_for_each(output, &ec->output_list, link)
6350 create_shell_output(shell, output);
6351
6352 shell->output_create_listener.notify = handle_output_create;
6353 wl_signal_add(&ec->output_created_signal,
6354 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006355
6356 shell->output_move_listener.notify = handle_output_move;
6357 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006358}
6359
6360static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006361shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006362{
Tiago Vignattibe143262012-04-16 17:31:41 +03006363 struct desktop_shell *shell =
6364 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006365 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006366 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006367
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006368 /* Force state to unlocked so we don't try to fade */
6369 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006370
6371 if (shell->child.client) {
6372 /* disable respawn */
6373 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006374 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006375 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006376
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006377 wl_list_remove(&shell->idle_listener.link);
6378 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006379
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006380 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006381 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006382
Xiong Zhang6b481422013-10-23 13:58:32 +08006383 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6384 wl_list_remove(&shell_output->destroy_listener.link);
6385 wl_list_remove(&shell_output->link);
6386 free(shell_output);
6387 }
6388
6389 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006390 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006391
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006392 wl_array_for_each(ws, &shell->workspaces.array)
6393 workspace_destroy(*ws);
6394 wl_array_release(&shell->workspaces.array);
6395
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006396 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006397 free(shell);
6398}
6399
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006400static void
6401shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6402{
6403 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006404 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006405
6406 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006407 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6408 MODIFIER_CTRL | MODIFIER_ALT,
6409 terminate_binding, ec);
6410 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6411 click_to_activate_binding,
6412 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006413 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6414 click_to_activate_binding,
6415 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006416 weston_compositor_add_touch_binding(ec, 0,
6417 touch_to_activate_binding,
6418 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006419 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6420 MODIFIER_SUPER | MODIFIER_ALT,
6421 surface_opacity_binding, NULL);
6422 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6423 MODIFIER_SUPER, zoom_axis_binding,
6424 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006425
6426 /* configurable bindings */
6427 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006428 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6429 zoom_key_binding, NULL);
6430 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6431 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006432 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6433 maximize_binding, NULL);
6434 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6435 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006436 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6437 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006438 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006439 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6440 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006441 weston_compositor_add_button_binding(ec, BTN_LEFT,
6442 mod | MODIFIER_SHIFT,
6443 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006444
6445 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6446 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6447 rotate_binding, NULL);
6448
Daniel Stone325fc2d2012-05-30 16:31:58 +01006449 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6450 shell);
6451 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6452 ec);
6453 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6454 backlight_binding, ec);
6455 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6456 ec);
6457 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6458 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006459 weston_compositor_add_key_binding(ec, KEY_K, mod,
6460 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006461 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6462 workspace_up_binding, shell);
6463 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6464 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006465 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6466 workspace_move_surface_up_binding,
6467 shell);
6468 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6469 workspace_move_surface_down_binding,
6470 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006471
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006472 if (shell->exposay_modifier)
6473 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6474 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006475
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006476 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6477 if (shell->workspaces.num > 1) {
6478 num_workspace_bindings = shell->workspaces.num;
6479 if (num_workspace_bindings > 6)
6480 num_workspace_bindings = 6;
6481 for (i = 0; i < num_workspace_bindings; i++)
6482 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6483 workspace_f_binding,
6484 shell);
6485 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006486
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006487 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006488}
6489
Jason Ekstrand024177c2014-04-21 19:42:58 -05006490static void
6491handle_seat_created(struct wl_listener *listener, void *data)
6492{
6493 struct weston_seat *seat = data;
6494
6495 create_shell_seat(seat);
6496}
6497
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006498WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006499module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006500 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006501{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006502 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006503 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006504 struct workspace **pws;
6505 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006506 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006507
Peter Huttererf3d62272013-08-08 11:57:05 +10006508 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006509 if (shell == NULL)
6510 return -1;
6511
Kristian Høgsberg75840622011-09-06 13:48:16 -04006512 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006513
6514 shell->destroy_listener.notify = shell_destroy;
6515 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006516 shell->idle_listener.notify = idle_handler;
6517 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6518 shell->wake_listener.notify = wake_handler;
6519 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006520
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006521 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006522 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006523 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006524 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006525 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006526 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006527 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006528 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006529 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006530 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006531 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006532 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006533 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006534
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006535 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6536 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006537 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6538 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006539 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006540
6541 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006542 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006543
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006544 if (input_panel_setup(shell) < 0)
6545 return -1;
6546
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006547 shell->text_backend = text_backend_init(ec);
6548 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006549 return -1;
6550
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006551 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006552
Daniel Stonedf8133b2013-11-19 11:37:14 +01006553 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6554 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6555
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006556 for (i = 0; i < shell->workspaces.num; i++) {
6557 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6558 if (pws == NULL)
6559 return -1;
6560
6561 *pws = workspace_create();
6562 if (*pws == NULL)
6563 return -1;
6564 }
6565 activate_workspace(shell, 0);
6566
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006567 weston_layer_init(&shell->minimized_layer, NULL);
6568
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006569 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006570 wl_list_init(&shell->workspaces.animation.link);
6571 shell->workspaces.animation.frame = animate_workspace_change_frame;
6572
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006573 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006574 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006575 return -1;
6576
Rafael Antognollie2a34552013-12-03 15:35:45 -02006577 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6578 shell, bind_xdg_shell) == NULL)
6579 return -1;
6580
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006581 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006582 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006583 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006584 return -1;
6585
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006586 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6587 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006588 return -1;
6589
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006590 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006591
Jonny Lamb765760d2014-08-20 15:53:19 +02006592 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6593
Xiong Zhang6b481422013-10-23 13:58:32 +08006594 setup_output_destroy_handler(ec, shell);
6595
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006596 loop = wl_display_get_event_loop(ec->wl_display);
6597 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006598
Jason Ekstrand024177c2014-04-21 19:42:58 -05006599 wl_list_for_each(seat, &ec->seat_list, link)
6600 handle_seat_created(NULL, seat);
6601 shell->seat_create_listener.notify = handle_seat_created;
6602 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006603
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006604 screenshooter_create(ec);
6605
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006606 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006607
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006608 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006609
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006610 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6611
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006612 return 0;
6613}