blob: f673f0414e9c83ee6f9dafc6e693751f7a785f0b [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,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001946 struct weston_pointer *pointer, 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,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001977 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
Derek Foreman8fbebbd2015-07-15 13:00:40 -05002001 if (surface_resize(shsurf, seat->pointer, 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
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003066static int
3067shell_interface_resize(struct shell_surface *shsurf,
3068 struct weston_seat *ws,
3069 uint32_t edges)
3070{
3071 return surface_resize(shsurf, ws->pointer, edges);
3072}
3073
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003074static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003075
3076static void
3077destroy_shell_seat(struct wl_listener *listener, void *data)
3078{
3079 struct shell_seat *shseat =
3080 container_of(listener,
3081 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003082 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003083
3084 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003085 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003086 shseat->popup_grab.client = NULL;
3087
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003088 wl_list_for_each_safe(shsurf, next,
3089 &shseat->popup_grab.surfaces_list,
3090 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003091 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003092 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003093 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003094 }
3095
3096 wl_list_remove(&shseat->seat_destroy_listener.link);
3097 free(shseat);
3098}
3099
Jason Ekstrand024177c2014-04-21 19:42:58 -05003100static void
3101shell_seat_caps_changed(struct wl_listener *listener, void *data)
3102{
3103 struct shell_seat *seat;
3104
3105 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3106
3107 if (seat->seat->keyboard &&
3108 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3109 wl_signal_add(&seat->seat->keyboard->focus_signal,
3110 &seat->keyboard_focus_listener);
3111 } else if (!seat->seat->keyboard) {
3112 wl_list_init(&seat->keyboard_focus_listener.link);
3113 }
3114
3115 if (seat->seat->pointer &&
3116 wl_list_empty(&seat->pointer_focus_listener.link)) {
3117 wl_signal_add(&seat->seat->pointer->focus_signal,
3118 &seat->pointer_focus_listener);
3119 } else if (!seat->seat->pointer) {
3120 wl_list_init(&seat->pointer_focus_listener.link);
3121 }
3122}
3123
Giulio Camuffo5085a752013-03-25 21:42:45 +01003124static struct shell_seat *
3125create_shell_seat(struct weston_seat *seat)
3126{
3127 struct shell_seat *shseat;
3128
3129 shseat = calloc(1, sizeof *shseat);
3130 if (!shseat) {
3131 weston_log("no memory to allocate shell seat\n");
3132 return NULL;
3133 }
3134
3135 shseat->seat = seat;
3136 wl_list_init(&shseat->popup_grab.surfaces_list);
3137
3138 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3139 wl_signal_add(&seat->destroy_signal,
3140 &shseat->seat_destroy_listener);
3141
Jason Ekstrand024177c2014-04-21 19:42:58 -05003142 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3143 wl_list_init(&shseat->keyboard_focus_listener.link);
3144
3145 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3146 wl_list_init(&shseat->pointer_focus_listener.link);
3147
3148 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3149 wl_signal_add(&seat->updated_caps_signal,
3150 &shseat->caps_changed_listener);
3151 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3152
Giulio Camuffo5085a752013-03-25 21:42:45 +01003153 return shseat;
3154}
3155
3156static struct shell_seat *
3157get_shell_seat(struct weston_seat *seat)
3158{
3159 struct wl_listener *listener;
3160
3161 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003162 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003163
3164 return container_of(listener,
3165 struct shell_seat, seat_destroy_listener);
3166}
3167
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003168static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003169popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003170{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003171 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003173 struct shell_seat *shseat =
3174 container_of(grab, struct shell_seat, popup_grab.grab);
3175 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003176 wl_fixed_t sx, sy;
3177
Jason Ekstranda7af7042013-10-12 22:38:11 -05003178 view = weston_compositor_pick_view(pointer->seat->compositor,
3179 pointer->x, pointer->y,
3180 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003181
Jason Ekstranda7af7042013-10-12 22:38:11 -05003182 if (view && view->surface->resource &&
3183 wl_resource_get_client(view->surface->resource) == client) {
3184 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003185 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003186 weston_pointer_set_focus(pointer, NULL,
3187 wl_fixed_from_int(0),
3188 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003189 }
3190}
3191
3192static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003193popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3194 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003195{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003196 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003197 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003198 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003199
Jonas Ådahl61917c82014-08-11 22:44:02 +02003200 if (pointer->focus) {
3201 weston_view_from_global_fixed(pointer->focus, x, y,
3202 &pointer->sx, &pointer->sy);
3203 }
3204
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003205 weston_pointer_move(pointer, x, y);
3206
Neil Roberts96d790e2013-09-19 17:32:00 +01003207 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003208 weston_view_from_global_fixed(pointer->focus,
3209 pointer->x, pointer->y,
3210 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003211 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003212 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003213}
3214
3215static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003216popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003217 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003218{
3219 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003220 struct shell_seat *shseat =
3221 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003222 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003223 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003224 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003225 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003226
Neil Roberts96d790e2013-09-19 17:32:00 +01003227 resource_list = &grab->pointer->focus_resource_list;
3228 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003229 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003230 wl_resource_for_each(resource, resource_list) {
3231 wl_pointer_send_button(resource, serial,
3232 time, button, state);
3233 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003234 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003235 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003236 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003237 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003238 }
3239
Daniel Stone4dbadb12012-05-30 16:31:51 +01003240 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003241 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003242}
3243
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003244static void
3245popup_grab_cancel(struct weston_pointer_grab *grab)
3246{
3247 popup_grab_end(grab->pointer);
3248}
3249
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003250static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003251 popup_grab_focus,
3252 popup_grab_motion,
3253 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003254 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003255};
3256
3257static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003258touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3259 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3260{
3261 struct wl_resource *resource;
3262 struct shell_seat *shseat =
3263 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3264 struct wl_display *display = shseat->seat->compositor->wl_display;
3265 uint32_t serial;
3266 struct wl_list *resource_list;
3267
3268 resource_list = &grab->touch->focus_resource_list;
3269 if (!wl_list_empty(resource_list)) {
3270 serial = wl_display_get_serial(display);
3271 wl_resource_for_each(resource, resource_list) {
3272 wl_touch_send_down(resource, serial, time,
3273 grab->touch->focus->surface->resource,
3274 touch_id, sx, sy);
3275 }
3276 }
3277}
3278
3279static void
3280touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3281{
3282 struct wl_resource *resource;
3283 struct shell_seat *shseat =
3284 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3285 struct wl_display *display = shseat->seat->compositor->wl_display;
3286 uint32_t serial;
3287 struct wl_list *resource_list;
3288
3289 resource_list = &grab->touch->focus_resource_list;
3290 if (!wl_list_empty(resource_list)) {
3291 serial = wl_display_get_serial(display);
3292 wl_resource_for_each(resource, resource_list) {
3293 wl_touch_send_up(resource, serial, time, touch_id);
3294 }
3295 }
3296}
3297
3298static void
3299touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3300 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3301{
3302 struct wl_resource *resource;
3303 struct wl_list *resource_list;
3304
3305 resource_list = &grab->touch->focus_resource_list;
3306 if (!wl_list_empty(resource_list)) {
3307 wl_resource_for_each(resource, resource_list) {
3308 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3309 }
3310 }
3311}
3312
3313static void
3314touch_popup_grab_frame(struct weston_touch_grab *grab)
3315{
3316}
3317
3318static void
3319touch_popup_grab_cancel(struct weston_touch_grab *grab)
3320{
3321 touch_popup_grab_end(grab->touch);
3322}
3323
3324static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3325 touch_popup_grab_down,
3326 touch_popup_grab_up,
3327 touch_popup_grab_motion,
3328 touch_popup_grab_frame,
3329 touch_popup_grab_cancel,
3330};
3331
3332static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003333shell_surface_send_popup_done(struct shell_surface *shsurf)
3334{
3335 if (shell_surface_is_wl_shell_surface(shsurf))
3336 wl_shell_surface_send_popup_done(shsurf->resource);
3337 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003338 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003339}
3340
3341static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003342popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003343{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003344 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003345 struct shell_seat *shseat =
3346 container_of(grab, struct shell_seat, popup_grab.grab);
3347 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003348 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003349
3350 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003351 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003352 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003353 shseat->popup_grab.grab.interface = NULL;
3354 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003355 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003356 wl_list_for_each_safe(shsurf, next,
3357 &shseat->popup_grab.surfaces_list,
3358 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003359 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003360 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003361 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003362 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003363 wl_list_init(&shseat->popup_grab.surfaces_list);
3364 }
3365}
3366
3367static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003368touch_popup_grab_end(struct weston_touch *touch)
3369{
3370 struct weston_touch_grab *grab = touch->grab;
3371 struct shell_seat *shseat =
3372 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3373 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003374 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003375
3376 if (touch->grab->interface == &touch_popup_grab_interface) {
3377 weston_touch_end_grab(grab->touch);
3378 shseat->popup_grab.client = NULL;
3379 shseat->popup_grab.touch_grab.interface = NULL;
3380 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3381 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003382 wl_list_for_each_safe(shsurf, next,
3383 &shseat->popup_grab.surfaces_list,
3384 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003385 shell_surface_send_popup_done(shsurf);
3386 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003387 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003388 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003389 wl_list_init(&shseat->popup_grab.surfaces_list);
3390 }
3391}
3392
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003393static struct shell_surface *
3394get_top_popup(struct shell_seat *shseat)
3395{
3396 struct shell_surface *shsurf;
3397
3398 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3399 return NULL;
3400 } else {
3401 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3402 struct shell_surface,
3403 popup.grab_link);
3404 return shsurf;
3405 }
3406}
3407
3408static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003409add_popup_grab(struct shell_surface *shsurf,
3410 struct shell_seat *shseat,
3411 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003412{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003413 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003414 struct shell_surface *parent, *top_surface;
3415
3416 parent = get_shell_surface(shsurf->parent);
3417 top_surface = get_top_popup(shseat);
3418 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003419 (!parent ||
3420 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003421 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003422 wl_resource_post_error(shsurf->owner_resource,
3423 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003424 "xdg_popup was not created on the "
3425 "topmost popup");
3426 return -1;
3427 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003428
3429 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003430 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003431 shseat->popup_grab.client =
3432 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003433
3434 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003435 shseat->popup_grab.grab.interface =
3436 &popup_grab_interface;
3437
3438 /* We must make sure here that this popup was opened
3439 * after a mouse press, and not just by moving around
3440 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003441 if (shseat->seat->pointer->button_count > 0)
3442 shseat->popup_grab.initial_up = 0;
3443 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003444 shseat->popup_grab.touch_grab.interface =
3445 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003446 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003447
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003448 wl_list_insert(&shseat->popup_grab.surfaces_list,
3449 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003450
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003451 if (type == POINTER) {
3452 weston_pointer_start_grab(seat->pointer,
3453 &shseat->popup_grab.grab);
3454 } else if (type == TOUCH) {
3455 weston_touch_start_grab(seat->touch,
3456 &shseat->popup_grab.touch_grab);
3457 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003458 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003459 wl_list_insert(&shseat->popup_grab.surfaces_list,
3460 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003461 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003462
3463 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003464}
3465
3466static void
3467remove_popup_grab(struct shell_surface *shsurf)
3468{
3469 struct shell_seat *shseat = shsurf->popup.shseat;
3470
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003471 if (shell_surface_is_xdg_popup(shsurf) &&
3472 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003473 wl_resource_post_error(shsurf->owner_resource,
3474 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003475 "xdg_popup was destroyed while it was "
3476 "not the topmost popup.");
3477 return;
3478 }
3479
Giulio Camuffo5085a752013-03-25 21:42:45 +01003480 wl_list_remove(&shsurf->popup.grab_link);
3481 wl_list_init(&shsurf->popup.grab_link);
3482 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003483 if (shseat->popup_grab.type == POINTER) {
3484 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3485 shseat->popup_grab.grab.interface = NULL;
3486 } else if (shseat->popup_grab.type == TOUCH) {
3487 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3488 shseat->popup_grab.touch_grab.interface = NULL;
3489 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003490 }
3491}
3492
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003493static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003494shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003495{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003496 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003497 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003498
Jason Ekstranda7af7042013-10-12 22:38:11 -05003499 shsurf->surface->output = parent_view->output;
3500 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003501
Jason Ekstranda7af7042013-10-12 22:38:11 -05003502 weston_view_set_transform_parent(shsurf->view, parent_view);
3503 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3504 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003505
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003506 if (shseat->seat->pointer &&
3507 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003508 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3509 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003510 } else if (shseat->seat->touch &&
3511 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003512 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3513 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003514 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003515 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003516 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003517 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003518
3519 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003520}
3521
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003522static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003523 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003524 shell_surface_move,
3525 shell_surface_resize,
3526 shell_surface_set_toplevel,
3527 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003528 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003529 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003530 shell_surface_set_maximized,
3531 shell_surface_set_title,
3532 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003533};
3534
3535static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003536destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003537{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003538 struct shell_surface *child, *next;
3539
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003540 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3541
Giulio Camuffo5085a752013-03-25 21:42:45 +01003542 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3543 remove_popup_grab(shsurf);
3544 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003545
Alex Wubd3354b2012-04-17 17:20:49 +08003546 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003547 shell_surface_is_top_fullscreen(shsurf))
3548 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003549
Jason Ekstranda7af7042013-10-12 22:38:11 -05003550 if (shsurf->fullscreen.black_view)
3551 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003552
Alex Wubd3354b2012-04-17 17:20:49 +08003553 /* As destroy_resource() use wl_list_for_each_safe(),
3554 * we can always remove the listener.
3555 */
3556 wl_list_remove(&shsurf->surface_destroy_listener.link);
3557 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003558 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003559 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003560
Jason Ekstranda7af7042013-10-12 22:38:11 -05003561 weston_view_destroy(shsurf->view);
3562
Philip Withnall648a4dd2013-11-25 18:01:44 +00003563 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003564 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3565 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003566
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003567 wl_list_remove(&shsurf->link);
3568 free(shsurf);
3569}
3570
3571static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003572shell_destroy_shell_surface(struct wl_resource *resource)
3573{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003574 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003575
Bryan Caina46b9462014-04-04 17:41:24 -05003576 if (!wl_list_empty(&shsurf->popup.grab_link))
3577 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003578 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003579 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003580}
3581
3582static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003583shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003584{
3585 struct shell_surface *shsurf = container_of(listener,
3586 struct shell_surface,
3587 surface_destroy_listener);
3588
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003589 if (shsurf->resource)
3590 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003591
3592 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003593}
3594
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003595static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003596fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003597{
3598 struct shell_surface *shsurf = data;
3599
3600 weston_surface_destroy(shsurf->surface);
3601}
3602
3603static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003604fade_out_done(struct weston_view_animation *animation, void *data)
3605{
3606 struct shell_surface *shsurf = data;
3607 struct wl_event_loop *loop;
3608
3609 loop = wl_display_get_event_loop(
3610 shsurf->surface->compositor->wl_display);
3611
3612 if (!shsurf->destroying) {
3613 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3614 shsurf->destroying = true;
3615 }
3616}
3617
3618static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003619handle_resource_destroy(struct wl_listener *listener, void *data)
3620{
3621 struct shell_surface *shsurf =
3622 container_of(listener, struct shell_surface,
3623 resource_destroy_listener);
3624
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003625 if (!weston_surface_is_mapped(shsurf->surface))
3626 return;
3627
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003628 shsurf->surface->ref_count++;
3629
3630 pixman_region32_fini(&shsurf->surface->pending.input);
3631 pixman_region32_init(&shsurf->surface->pending.input);
3632 pixman_region32_fini(&shsurf->surface->input);
3633 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003634 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3635 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3636 fade_out_done, shsurf);
3637 } else {
3638 weston_surface_destroy(shsurf->surface);
3639 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003640}
3641
3642static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003643shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003644
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003645struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003646get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003647{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003648 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003649 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003650 else
3651 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003652}
3653
Rafael Antognollie2a34552013-12-03 15:35:45 -02003654static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003655create_common_surface(struct shell_client *owner, void *shell,
3656 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003657 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003658{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003659 struct shell_surface *shsurf;
3660
Pekka Paalanen50b67472014-10-01 15:02:41 +03003661 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003662
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003663 shsurf = calloc(1, sizeof *shsurf);
3664 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003665 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003666 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003667 }
3668
Jason Ekstranda7af7042013-10-12 22:38:11 -05003669 shsurf->view = weston_view_create(surface);
3670 if (!shsurf->view) {
3671 weston_log("no memory to allocate shell surface\n");
3672 free(shsurf);
3673 return NULL;
3674 }
3675
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003676 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003677 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003678 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003679
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003680 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3681 wl_resource_add_destroy_listener(surface->resource,
3682 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003683 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003684
Tiago Vignattibc052c92012-04-19 16:18:18 +03003685 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003686 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003687 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003688 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003689 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003690 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003691 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3692 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003693 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003694 wl_list_init(&shsurf->fullscreen.transform.link);
3695
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003696 shsurf->output = get_default_output(shsurf->shell->compositor);
3697
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003698 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003699 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003700 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003701 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003702
3703 /* init link so its safe to always remove it in destroy_shell_surface */
3704 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003705 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003706
Pekka Paalanen460099f2012-01-20 16:48:25 +02003707 /* empty when not in use */
3708 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003709 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003710
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003711 wl_list_init(&shsurf->workspace_transform.link);
3712
Philip Withnall648a4dd2013-11-25 18:01:44 +00003713 wl_list_init(&shsurf->children_link);
3714 wl_list_init(&shsurf->children_list);
3715 shsurf->parent = NULL;
3716
Pekka Paalanenb5026542014-11-12 15:09:24 +02003717 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003718
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003719 shsurf->client = client;
3720
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003721 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003722}
3723
Rafael Antognollie2a34552013-12-03 15:35:45 -02003724static struct shell_surface *
3725create_shell_surface(void *shell, struct weston_surface *surface,
3726 const struct weston_shell_client *client)
3727{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003728 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003729}
3730
Jason Ekstranda7af7042013-10-12 22:38:11 -05003731static struct weston_view *
3732get_primary_view(void *shell, struct shell_surface *shsurf)
3733{
3734 return shsurf->view;
3735}
3736
Tiago Vignattibc052c92012-04-19 16:18:18 +03003737static void
3738shell_get_shell_surface(struct wl_client *client,
3739 struct wl_resource *resource,
3740 uint32_t id,
3741 struct wl_resource *surface_resource)
3742{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003743 struct weston_surface *surface =
3744 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003745 struct shell_client *sc = wl_resource_get_user_data(resource);
3746 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003747 struct shell_surface *shsurf;
3748
Pekka Paalanen50b67472014-10-01 15:02:41 +03003749 if (weston_surface_set_role(surface, "wl_shell_surface",
3750 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003751 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003752
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003753 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003754 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003755 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003756 return;
3757 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003758
Jason Ekstranda85118c2013-06-27 20:17:02 -05003759 shsurf->resource =
3760 wl_resource_create(client,
3761 &wl_shell_surface_interface, 1, id);
3762 wl_resource_set_implementation(shsurf->resource,
3763 &shell_surface_implementation,
3764 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003765 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003766}
3767
Rafael Antognollie2a34552013-12-03 15:35:45 -02003768static bool
3769shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3770{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003771 /* A shell surface without a resource is created from xwayland
3772 * and is considered a wl_shell surface for now. */
3773
3774 return shsurf->resource == NULL ||
3775 wl_resource_instance_of(shsurf->resource,
3776 &wl_shell_surface_interface,
3777 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003778}
3779
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003780static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003781 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003782};
3783
Rafael Antognollie2a34552013-12-03 15:35:45 -02003784/****************************
3785 * xdg-shell implementation */
3786
3787static void
3788xdg_surface_destroy(struct wl_client *client,
3789 struct wl_resource *resource)
3790{
3791 wl_resource_destroy(resource);
3792}
3793
3794static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003795xdg_surface_set_parent(struct wl_client *client,
3796 struct wl_resource *resource,
3797 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003798{
3799 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003800 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003801
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003802 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003803 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003804 shell_surface_set_parent(shsurf, parent->surface);
3805 } else {
3806 shell_surface_set_parent(shsurf, NULL);
3807 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003808}
3809
3810static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003811xdg_surface_set_app_id(struct wl_client *client,
3812 struct wl_resource *resource,
3813 const char *app_id)
3814{
3815 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3816
3817 free(shsurf->class);
3818 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003819 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003820}
3821
3822static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003823xdg_surface_show_window_menu(struct wl_client *client,
3824 struct wl_resource *surface_resource,
3825 struct wl_resource *seat_resource,
3826 uint32_t serial,
3827 int32_t x,
3828 int32_t y)
3829{
3830 /* TODO */
3831}
3832
3833static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003834xdg_surface_set_title(struct wl_client *client,
3835 struct wl_resource *resource, const char *title)
3836{
3837 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3838
3839 set_title(shsurf, title);
3840}
3841
3842static void
3843xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3844 struct wl_resource *seat_resource, uint32_t serial)
3845{
3846 common_surface_move(resource, seat_resource, serial);
3847}
3848
3849static void
3850xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3851 struct wl_resource *seat_resource, uint32_t serial,
3852 uint32_t edges)
3853{
3854 common_surface_resize(resource, seat_resource, serial, edges);
3855}
3856
3857static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003858xdg_surface_ack_configure(struct wl_client *client,
3859 struct wl_resource *resource,
3860 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003861{
3862 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3863
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003864 if (shsurf->state_requested) {
3865 shsurf->next_state = shsurf->requested_state;
3866 shsurf->state_changed = true;
3867 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003868 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003869}
3870
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003871static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003872xdg_surface_set_window_geometry(struct wl_client *client,
3873 struct wl_resource *resource,
3874 int32_t x,
3875 int32_t y,
3876 int32_t width,
3877 int32_t height)
3878{
3879 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3880
3881 set_window_geometry(shsurf, x, y, width, height);
3882}
3883
3884static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003885xdg_surface_set_maximized(struct wl_client *client,
3886 struct wl_resource *resource)
3887{
3888 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003889 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003890
3891 shsurf->state_requested = true;
3892 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003893
3894 if (!weston_surface_is_mapped(shsurf->surface))
3895 output = get_focused_output(shsurf->surface->compositor);
3896 else
3897 output = shsurf->surface->output;
3898
3899 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003900 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003901}
3902
3903static void
3904xdg_surface_unset_maximized(struct wl_client *client,
3905 struct wl_resource *resource)
3906{
3907 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3908
3909 shsurf->state_requested = true;
3910 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003911 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003912}
3913
3914static void
3915xdg_surface_set_fullscreen(struct wl_client *client,
3916 struct wl_resource *resource,
3917 struct wl_resource *output_resource)
3918{
3919 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3920 struct weston_output *output;
3921
3922 shsurf->state_requested = true;
3923 shsurf->requested_state.fullscreen = true;
3924
3925 if (output_resource)
3926 output = wl_resource_get_user_data(output_resource);
3927 else
3928 output = NULL;
3929
Marek Chalupa052917a2014-09-02 11:35:12 +02003930 /* handle clients launching in fullscreen */
3931 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3932 /* Set the output to the one that has focus currently. */
3933 assert(shsurf->surface);
3934 output = get_focused_output(shsurf->surface->compositor);
3935 }
3936
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003937 shell_surface_set_output(shsurf, output);
3938 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003939
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003940 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003941}
3942
3943static void
3944xdg_surface_unset_fullscreen(struct wl_client *client,
3945 struct wl_resource *resource)
3946{
3947 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3948
3949 shsurf->state_requested = true;
3950 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003951 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003952}
3953
3954static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003955xdg_surface_set_minimized(struct wl_client *client,
3956 struct wl_resource *resource)
3957{
3958 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3959
3960 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3961 return;
3962
3963 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003964 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003965}
3966
Rafael Antognollie2a34552013-12-03 15:35:45 -02003967static const struct xdg_surface_interface xdg_surface_implementation = {
3968 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003969 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003970 xdg_surface_set_title,
3971 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003972 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003973 xdg_surface_move,
3974 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003975 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003976 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003977 xdg_surface_set_maximized,
3978 xdg_surface_unset_maximized,
3979 xdg_surface_set_fullscreen,
3980 xdg_surface_unset_fullscreen,
3981 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003982};
3983
3984static void
3985xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003986 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003987{
3988 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003989 uint32_t *s;
3990 struct wl_array states;
3991 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003992
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003993 assert(shsurf);
3994
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003995 if (!shsurf->resource)
3996 return;
3997
3998 wl_array_init(&states);
3999 if (shsurf->requested_state.fullscreen) {
4000 s = wl_array_add(&states, sizeof *s);
4001 *s = XDG_SURFACE_STATE_FULLSCREEN;
4002 } else if (shsurf->requested_state.maximized) {
4003 s = wl_array_add(&states, sizeof *s);
4004 *s = XDG_SURFACE_STATE_MAXIMIZED;
4005 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004006 if (shsurf->resize_edges != 0) {
4007 s = wl_array_add(&states, sizeof *s);
4008 *s = XDG_SURFACE_STATE_RESIZING;
4009 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004010 if (shsurf->focus_count > 0) {
4011 s = wl_array_add(&states, sizeof *s);
4012 *s = XDG_SURFACE_STATE_ACTIVATED;
4013 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004014
4015 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4016 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4017
4018 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004019}
4020
4021static const struct weston_shell_client xdg_client = {
4022 xdg_send_configure
4023};
4024
4025static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004026xdg_shell_destroy(struct wl_client *client,
4027 struct wl_resource *resource)
4028{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004029 struct shell_client *sc = wl_resource_get_user_data(resource);
4030 struct wl_resource *shsurf_resource;
4031 struct shell_surface *shsurf;
4032
4033 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4034 shsurf = wl_resource_get_user_data(shsurf_resource);
4035 if (shsurf->owner_resource == resource) {
4036 wl_resource_post_error(
4037 resource,
4038 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4039 "not all child surface objects destroyed");
4040 return;
4041 }
4042 }
4043
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004044 wl_resource_destroy(resource);
4045}
4046
4047static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004048xdg_use_unstable_version(struct wl_client *client,
4049 struct wl_resource *resource,
4050 int32_t version)
4051{
4052 if (version > 1) {
4053 wl_resource_post_error(resource,
4054 1,
4055 "xdg-shell:: version not implemented yet.");
4056 return;
4057 }
4058}
4059
4060static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004061create_xdg_surface(struct shell_client *owner, void *shell,
4062 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004063 const struct weston_shell_client *client)
4064{
4065 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004066
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004067 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004068 if (!shsurf)
4069 return NULL;
4070
Pekka Paalanenb5026542014-11-12 15:09:24 +02004071 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004072
4073 return shsurf;
4074}
4075
4076static void
4077xdg_get_xdg_surface(struct wl_client *client,
4078 struct wl_resource *resource,
4079 uint32_t id,
4080 struct wl_resource *surface_resource)
4081{
4082 struct weston_surface *surface =
4083 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004084 struct shell_client *sc = wl_resource_get_user_data(resource);
4085 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004086 struct shell_surface *shsurf;
4087
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004088 shsurf = get_shell_surface(surface);
4089 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4090 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4091 "This wl_surface is already an "
4092 "xdg_surface");
4093 return;
4094 }
4095
Pekka Paalanen50b67472014-10-01 15:02:41 +03004096 if (weston_surface_set_role(surface, "xdg_surface",
4097 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004098 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004099
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004100 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004101 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004102 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004103 return;
4104 }
4105
4106 shsurf->resource =
4107 wl_resource_create(client,
4108 &xdg_surface_interface, 1, id);
4109 wl_resource_set_implementation(shsurf->resource,
4110 &xdg_surface_implementation,
4111 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004112 shsurf->owner_resource = resource;
4113 wl_list_insert(&sc->surface_list,
4114 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004115}
4116
4117static bool
4118shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4119{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004120 return shsurf->resource &&
4121 wl_resource_instance_of(shsurf->resource,
4122 &xdg_surface_interface,
4123 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004124}
4125
4126/* xdg-popup implementation */
4127
4128static void
4129xdg_popup_destroy(struct wl_client *client,
4130 struct wl_resource *resource)
4131{
4132 wl_resource_destroy(resource);
4133}
4134
Rafael Antognollie2a34552013-12-03 15:35:45 -02004135static const struct xdg_popup_interface xdg_popup_implementation = {
4136 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004137};
4138
4139static void
4140xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004141 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004142{
4143}
4144
4145static const struct weston_shell_client xdg_popup_client = {
4146 xdg_popup_send_configure
4147};
4148
4149static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004150create_xdg_popup(struct shell_client *owner, void *shell,
4151 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004152 const struct weston_shell_client *client,
4153 struct weston_surface *parent,
4154 struct shell_seat *seat,
4155 uint32_t serial,
4156 int32_t x, int32_t y)
4157{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004158 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004159
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004160 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004161 if (!shsurf)
4162 return NULL;
4163
Pekka Paalanenb5026542014-11-12 15:09:24 +02004164 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004165 shsurf->popup.shseat = seat;
4166 shsurf->popup.serial = serial;
4167 shsurf->popup.x = x;
4168 shsurf->popup.y = y;
4169 shell_surface_set_parent(shsurf, parent);
4170
4171 return shsurf;
4172}
4173
4174static void
4175xdg_get_xdg_popup(struct wl_client *client,
4176 struct wl_resource *resource,
4177 uint32_t id,
4178 struct wl_resource *surface_resource,
4179 struct wl_resource *parent_resource,
4180 struct wl_resource *seat_resource,
4181 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004182 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004183{
4184 struct weston_surface *surface =
4185 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004186 struct shell_client *sc = wl_resource_get_user_data(resource);
4187 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004188 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004189 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004190 struct weston_surface *parent;
4191 struct shell_seat *seat;
4192
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004193 shsurf = get_shell_surface(surface);
4194 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4195 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4196 "This wl_surface is already an "
4197 "xdg_popup");
4198 return;
4199 }
4200
Pekka Paalanen50b67472014-10-01 15:02:41 +03004201 if (weston_surface_set_role(surface, "xdg_popup",
4202 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004203 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004204
4205 if (!parent_resource) {
4206 wl_resource_post_error(surface_resource,
4207 WL_DISPLAY_ERROR_INVALID_OBJECT,
4208 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004209 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004210 }
4211
4212 parent = wl_resource_get_user_data(parent_resource);
4213 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4214
Jonas Ådahlee45a552015-03-18 16:37:47 +08004215 /* Verify that we are creating the top most popup when mapping,
4216 * as it's not until then we know whether it was mapped as most
4217 * top level or not. */
4218
4219 parent_shsurf = get_shell_surface(parent);
4220 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4221 !shell_surface_is_xdg_surface(parent_shsurf)) {
4222 wl_resource_post_error(resource,
4223 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4224 "xdg_popup parent was invalid");
4225 return;
4226 }
4227
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004228 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004229 parent, seat, serial, x, y);
4230 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004231 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004232 return;
4233 }
4234
4235 shsurf->resource =
4236 wl_resource_create(client,
4237 &xdg_popup_interface, 1, id);
4238 wl_resource_set_implementation(shsurf->resource,
4239 &xdg_popup_implementation,
4240 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004241 shsurf->owner_resource = resource;
4242 wl_list_insert(&sc->surface_list,
4243 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004244}
4245
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004246static void
4247xdg_pong(struct wl_client *client,
4248 struct wl_resource *resource, uint32_t serial)
4249{
4250 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004251
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004252 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004253}
4254
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255static bool
4256shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4257{
4258 return wl_resource_instance_of(shsurf->resource,
4259 &xdg_popup_interface,
4260 &xdg_popup_implementation);
4261}
4262
4263static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004264 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004265 xdg_use_unstable_version,
4266 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004267 xdg_get_xdg_popup,
4268 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004269};
4270
4271static int
4272xdg_shell_unversioned_dispatch(const void *implementation,
4273 void *_target, uint32_t opcode,
4274 const struct wl_message *message,
4275 union wl_argument *args)
4276{
4277 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004278 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004279
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004280 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004281 wl_resource_post_error(resource,
4282 WL_DISPLAY_ERROR_INVALID_OBJECT,
4283 "must call use_unstable_version first");
4284 return 0;
4285 }
4286
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004287#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004288
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004289 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4290 "shell implementation doesn't match protocol version");
4291
Rafael Antognollie2a34552013-12-03 15:35:45 -02004292 if (args[0].i != XDG_SERVER_VERSION) {
4293 wl_resource_post_error(resource,
4294 WL_DISPLAY_ERROR_INVALID_OBJECT,
4295 "incompatible version, server is %d "
4296 "client wants %d",
4297 XDG_SERVER_VERSION, args[0].i);
4298 return 0;
4299 }
4300
4301 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004302 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004303
4304 return 1;
4305}
4306
4307/* end of xdg-shell implementation */
4308/***********************************/
4309
Kristian Høgsberg07937562011-04-12 17:25:42 -04004310static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004311shell_fade(struct desktop_shell *shell, enum fade_type type);
4312
Pekka Paalanen77346a62011-11-30 16:26:35 +02004313static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004314configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004315{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004316 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004317
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004318 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004319 if (v->output == ev->output && v != ev) {
4320 weston_view_unmap(v);
4321 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004322 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004323 }
4324 }
4325
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004326 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004327
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004328 if (wl_list_empty(&ev->layer_link.link)) {
4329 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004330 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004331 }
4332}
4333
Pekka Paalanen8274d902014-08-06 19:36:51 +03004334static int
4335background_get_label(struct weston_surface *surface, char *buf, size_t len)
4336{
4337 return snprintf(buf, len, "background for output %s",
4338 surface->output->name);
4339}
4340
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004341static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004342background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004343{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004344 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004345 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004346
Jason Ekstranda7af7042013-10-12 22:38:11 -05004347 view = container_of(es->views.next, struct weston_view, surface_link);
4348
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004349 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004350}
4351
4352static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004353desktop_shell_set_background(struct wl_client *client,
4354 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004355 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004356 struct wl_resource *surface_resource)
4357{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004358 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004359 struct weston_surface *surface =
4360 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004362
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004363 if (surface->configure) {
4364 wl_resource_post_error(surface_resource,
4365 WL_DISPLAY_ERROR_INVALID_OBJECT,
4366 "surface role already assigned");
4367 return;
4368 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004369
Jason Ekstranda7af7042013-10-12 22:38:11 -05004370 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4371 weston_view_destroy(view);
4372 view = weston_view_create(surface);
4373
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004374 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004375 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004376 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004377 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004379 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004380 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004381 surface->output->width,
4382 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004383}
4384
Pekka Paalanen8274d902014-08-06 19:36:51 +03004385static int
4386panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4387{
4388 return snprintf(buf, len, "panel for output %s",
4389 surface->output->name);
4390}
4391
Kristian Høgsberg75840622011-09-06 13:48:16 -04004392static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004393panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004394{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004395 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004397
Jason Ekstranda7af7042013-10-12 22:38:11 -05004398 view = container_of(es->views.next, struct weston_view, surface_link);
4399
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004400 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004401}
4402
4403static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004404desktop_shell_set_panel(struct wl_client *client,
4405 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004406 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004407 struct wl_resource *surface_resource)
4408{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004409 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004410 struct weston_surface *surface =
4411 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004412 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004413
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004414 if (surface->configure) {
4415 wl_resource_post_error(surface_resource,
4416 WL_DISPLAY_ERROR_INVALID_OBJECT,
4417 "surface role already assigned");
4418 return;
4419 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004420
Jason Ekstranda7af7042013-10-12 22:38:11 -05004421 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4422 weston_view_destroy(view);
4423 view = weston_view_create(surface);
4424
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004425 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004426 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004427 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004428 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004429 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004430 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004431 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004432 surface->output->width,
4433 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004434}
4435
Pekka Paalanen8274d902014-08-06 19:36:51 +03004436static int
4437lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4438{
4439 return snprintf(buf, len, "lock window");
4440}
4441
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004442static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004443lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004444{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004445 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004446 struct weston_view *view;
4447
4448 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004449
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004450 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004451 return;
4452
Jason Ekstranda7af7042013-10-12 22:38:11 -05004453 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004454
4455 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004456 weston_layer_entry_insert(&shell->lock_layer.view_list,
4457 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004458 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004459 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004460 }
4461}
4462
4463static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004464handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004465{
Tiago Vignattibe143262012-04-16 17:31:41 +03004466 struct desktop_shell *shell =
4467 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004468
Martin Minarik6d118362012-06-07 18:01:59 +02004469 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004470 shell->lock_surface = NULL;
4471}
4472
4473static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004474desktop_shell_set_lock_surface(struct wl_client *client,
4475 struct wl_resource *resource,
4476 struct wl_resource *surface_resource)
4477{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004478 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004479 struct weston_surface *surface =
4480 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004481
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004482 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004483
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004484 if (!shell->locked)
4485 return;
4486
Pekka Paalanen98262232011-12-01 10:42:22 +02004487 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004488
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004489 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004490 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004491 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004492
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004493 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004494 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004495 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004496 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004497}
4498
4499static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004500resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004501{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004502 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004503
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004504 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004505 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004506 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004507 &shell->input_panel_layer.link);
4508 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004509 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004510 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004511 wl_list_insert(&shell->compositor->cursor_layer.link,
4512 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004513 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004514 wl_list_insert(&shell->fullscreen_layer.link,
4515 &shell->panel_layer.link);
4516 wl_list_insert(&shell->panel_layer.link,
4517 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004518
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004519 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004520
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004521 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004522 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004523 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004524}
4525
4526static void
4527desktop_shell_unlock(struct wl_client *client,
4528 struct wl_resource *resource)
4529{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004530 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004531
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004532 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004533
4534 if (shell->locked)
4535 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004536}
4537
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004538static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004539desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004540 struct wl_resource *resource,
4541 struct wl_resource *surface_resource)
4542{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004543 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004544
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004545 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004546 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004547}
4548
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004549static void
4550desktop_shell_desktop_ready(struct wl_client *client,
4551 struct wl_resource *resource)
4552{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004553 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004554
4555 shell_fade_startup(shell);
4556}
4557
Jonny Lamb765760d2014-08-20 15:53:19 +02004558static void
4559desktop_shell_set_panel_position(struct wl_client *client,
4560 struct wl_resource *resource,
4561 uint32_t position)
4562{
4563 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4564
4565 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4566 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4567 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4568 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4569 wl_resource_post_error(resource,
4570 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4571 "bad position argument");
4572 return;
4573 }
4574
4575 shell->panel_position = position;
4576}
4577
Kristian Høgsberg75840622011-09-06 13:48:16 -04004578static const struct desktop_shell_interface desktop_shell_implementation = {
4579 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004580 desktop_shell_set_panel,
4581 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004582 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004583 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004584 desktop_shell_desktop_ready,
4585 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004586};
4587
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004588static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004589get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004590{
4591 struct shell_surface *shsurf;
4592
4593 shsurf = get_shell_surface(surface);
4594 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004595 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004596 return shsurf->type;
4597}
4598
Kristian Høgsberg75840622011-09-06 13:48:16 -04004599static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004600move_binding(struct weston_pointer *pointer, uint32_t time,
4601 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004602{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004603 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004604 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004605 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004606
Derek Foreman8ae2db52015-07-15 13:00:36 -05004607 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004608 return;
4609
Derek Foreman8ae2db52015-07-15 13:00:36 -05004610 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004611
Pekka Paalanen01388e22013-04-25 13:57:44 +03004612 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004613 if (surface == NULL)
4614 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004615
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004616 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004617 if (shsurf == NULL || shsurf->state.fullscreen ||
4618 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004619 return;
4620
Derek Foreman794fa0e2015-07-15 13:00:38 -05004621 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004622}
4623
4624static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004625maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4626 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004627{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004628 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004629 struct weston_surface *surface;
4630 struct shell_surface *shsurf;
4631
4632 surface = weston_surface_get_main_surface(focus);
4633 if (surface == NULL)
4634 return;
4635
4636 shsurf = get_shell_surface(surface);
4637 if (shsurf == NULL)
4638 return;
4639
4640 if (!shell_surface_is_xdg_surface(shsurf))
4641 return;
4642
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004643 shsurf->state_requested = true;
4644 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004645 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004646}
4647
4648static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004649fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4650 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004651{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004652 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004653 struct weston_surface *surface;
4654 struct shell_surface *shsurf;
4655
4656 surface = weston_surface_get_main_surface(focus);
4657 if (surface == NULL)
4658 return;
4659
4660 shsurf = get_shell_surface(surface);
4661 if (shsurf == NULL)
4662 return;
4663
4664 if (!shell_surface_is_xdg_surface(shsurf))
4665 return;
4666
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004667 shsurf->state_requested = true;
4668 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004669 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004670 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004671}
4672
4673static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004674touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004675{
Derek Foreman362656b2014-09-04 10:23:05 -05004676 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004677 struct weston_surface *surface;
4678 struct shell_surface *shsurf;
4679
Derek Foreman8ae2db52015-07-15 13:00:36 -05004680 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004681 return;
4682
Derek Foreman8ae2db52015-07-15 13:00:36 -05004683 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004684 surface = weston_surface_get_main_surface(focus);
4685 if (surface == NULL)
4686 return;
4687
4688 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004689 if (shsurf == NULL || shsurf->state.fullscreen ||
4690 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004691 return;
4692
Derek Foremanb7674ae2015-07-15 13:00:37 -05004693 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004694}
4695
4696static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004697resize_binding(struct weston_pointer *pointer, uint32_t time,
4698 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004699{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004700 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004701 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004702 uint32_t edges = 0;
4703 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004704 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004705
Derek Foreman8ae2db52015-07-15 13:00:36 -05004706 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004707 return;
4708
Derek Foreman8ae2db52015-07-15 13:00:36 -05004709 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004710
Pekka Paalanen01388e22013-04-25 13:57:44 +03004711 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004712 if (surface == NULL)
4713 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004714
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004715 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004716 if (shsurf == NULL || shsurf->state.fullscreen ||
4717 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004718 return;
4719
Jason Ekstranda7af7042013-10-12 22:38:11 -05004720 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004721 wl_fixed_to_int(pointer->grab_x),
4722 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004723 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004724
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004725 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004726 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004727 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004728 edges |= 0;
4729 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004730 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004731
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004732 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004733 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004734 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004735 edges |= 0;
4736 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004737 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004738
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004739 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004740}
4741
4742static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004743surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4744 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004745{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004746 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004747 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004748 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004749 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004750
Pekka Paalanen01388e22013-04-25 13:57:44 +03004751 /* XXX: broken for windows containing sub-surfaces */
4752 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004753 if (surface == NULL)
4754 return;
4755
4756 shsurf = get_shell_surface(surface);
4757 if (!shsurf)
4758 return;
4759
Jason Ekstranda7af7042013-10-12 22:38:11 -05004760 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004761
Jason Ekstranda7af7042013-10-12 22:38:11 -05004762 if (shsurf->view->alpha > 1.0)
4763 shsurf->view->alpha = 1.0;
4764 if (shsurf->view->alpha < step)
4765 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004766
Jason Ekstranda7af7042013-10-12 22:38:11 -05004767 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004768 weston_surface_damage(surface);
4769}
4770
4771static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004772do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004773 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004774{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004775 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004776 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004777 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004778
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004779 if (!seat->pointer) {
4780 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4781 return;
4782 }
4783
Scott Moreauccbf29d2012-02-22 14:21:41 -07004784 wl_list_for_each(output, &compositor->output_list, link) {
4785 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004786 wl_fixed_to_double(seat->pointer->x),
4787 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004788 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004789 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004790 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004791 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004792 increment = -output->zoom.increment;
4793 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004794 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004795 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004796 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004797 else
4798 increment = 0;
4799
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004800 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004801
Scott Moreaue6603982012-06-11 13:07:51 -06004802 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004803 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004804 else if (output->zoom.level > output->zoom.max_level)
4805 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004806 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004807 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004808 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004809
Scott Moreaue6603982012-06-11 13:07:51 -06004810 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004811
Jason Ekstranda7af7042013-10-12 22:38:11 -05004812 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004813 }
4814 }
4815}
4816
Scott Moreauccbf29d2012-02-22 14:21:41 -07004817static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004818zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4819 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004820{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004821 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004822}
4823
4824static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004825zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4826 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004827{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004828 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004829}
4830
4831static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004832terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4833 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004834{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004835 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004836
Daniel Stone325fc2d2012-05-30 16:31:58 +01004837 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004838}
4839
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004840static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004841rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4842 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004843{
4844 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004845 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004846 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004847 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004848 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004849
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004850 weston_pointer_move(pointer, x, y);
4851
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004852 if (!shsurf)
4853 return;
4854
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004855 cx = 0.5f * shsurf->surface->width;
4856 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004857
Daniel Stone37816df2012-05-16 18:45:18 +01004858 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4859 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004860 r = sqrtf(dx * dx + dy * dy);
4861
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004862 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004863 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004864
4865 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004866 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004867 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004868
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004869 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004870 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004871
4872 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004873 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004874 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004875 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004876 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004877
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004878 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004880 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004881 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004882 wl_list_init(&shsurf->rotation.transform.link);
4883 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004884 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004885 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004886
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004887 /* We need to adjust the position of the surface
4888 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004889 cposx = shsurf->view->geometry.x + cx;
4890 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004891 dposx = rotate->center.x - cposx;
4892 dposy = rotate->center.y - cposy;
4893 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 weston_view_set_position(shsurf->view,
4895 shsurf->view->geometry.x + dposx,
4896 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004897 }
4898
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004899 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004900 * lazily applies the damage due to rotation update.
4901 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004902 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004903}
4904
4905static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004906rotate_grab_button(struct weston_pointer_grab *grab,
4907 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004908{
4909 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004910 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004911 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004912 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004913 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004914
Daniel Stone4dbadb12012-05-30 16:31:51 +01004915 if (pointer->button_count == 0 &&
4916 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004917 if (shsurf)
4918 weston_matrix_multiply(&shsurf->rotation.rotation,
4919 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004920 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004921 free(rotate);
4922 }
4923}
4924
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004925static void
4926rotate_grab_cancel(struct weston_pointer_grab *grab)
4927{
4928 struct rotate_grab *rotate =
4929 container_of(grab, struct rotate_grab, base.grab);
4930
4931 shell_grab_end(&rotate->base);
4932 free(rotate);
4933}
4934
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004935static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004936 noop_grab_focus,
4937 rotate_grab_motion,
4938 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004939 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004940};
4941
4942static void
Derek Foreman74de4692015-07-15 13:00:39 -05004943surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004944{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004945 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004946 float dx, dy;
4947 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004948
Pekka Paalanen460099f2012-01-20 16:48:25 +02004949 rotate = malloc(sizeof *rotate);
4950 if (!rotate)
4951 return;
4952
Jason Ekstranda7af7042013-10-12 22:38:11 -05004953 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004954 surface->surface->width * 0.5f,
4955 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004956 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004957
Derek Foreman74de4692015-07-15 13:00:39 -05004958 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4959 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004960 r = sqrtf(dx * dx + dy * dy);
4961 if (r > 20.0f) {
4962 struct weston_matrix inverse;
4963
4964 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004965 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004966 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004967
4968 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004969 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004970 } else {
4971 weston_matrix_init(&surface->rotation.rotation);
4972 weston_matrix_init(&rotate->rotation);
4973 }
4974
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004975 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Derek Foreman74de4692015-07-15 13:00:39 -05004976 pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004977}
4978
4979static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004980rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004981 void *data)
4982{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004983 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004984 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004985 struct shell_surface *surface;
4986
Derek Foreman8ae2db52015-07-15 13:00:36 -05004987 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004988 return;
4989
Derek Foreman8ae2db52015-07-15 13:00:36 -05004990 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004991
Pekka Paalanen01388e22013-04-25 13:57:44 +03004992 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004993 if (base_surface == NULL)
4994 return;
4995
4996 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004997 if (surface == NULL || surface->state.fullscreen ||
4998 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004999 return;
5000
Derek Foreman74de4692015-07-15 13:00:39 -05005001 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005002}
5003
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005004/* Move all fullscreen layers down to the current workspace and hide their
5005 * black views. The surfaces' state is set to both fullscreen and lowered,
5006 * and this is reversed when such a surface is re-configured, see
5007 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5008 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005009 * lowering_output = NULL - Lower on all outputs, else only lower on the
5010 * specified output.
5011 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005012 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005013 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005014void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005015lower_fullscreen_layer(struct desktop_shell *shell,
5016 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005017{
5018 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005019 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005020
5021 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005023 &shell->fullscreen_layer.view_list.link,
5024 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005025 struct shell_surface *shsurf = get_shell_surface(view->surface);
5026
5027 if (!shsurf)
5028 continue;
5029
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005030 /* Only lower surfaces which have lowering_output as their fullscreen
5031 * output, unless a NULL output asks for lowering on all outputs.
5032 */
5033 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5034 continue;
5035
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005036 /* We can have a non-fullscreen popup for a fullscreen surface
5037 * in the fullscreen layer. */
5038 if (shsurf->state.fullscreen) {
5039 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005040 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5041 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005042 weston_view_damage_below(shsurf->fullscreen.black_view);
5043
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005044 }
5045
5046 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005047 weston_layer_entry_remove(&view->layer_link);
5048 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005049 weston_view_damage_below(view);
5050 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005051
5052 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005053 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005054}
5055
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005056void
Tiago Vignattibe143262012-04-16 17:31:41 +03005057activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005058 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005059{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005060 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005061 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005062 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005063 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005064 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005065
Pekka Paalanen01388e22013-04-25 13:57:44 +03005066 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005067 shsurf = get_shell_surface(main_surface);
5068 assert(shsurf);
5069
5070 /* Only demote fullscreen surfaces on the output of activated shsurf.
5071 * Leave fullscreen surfaces on unrelated outputs alone. */
5072 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005073
Daniel Stone37816df2012-05-16 18:45:18 +01005074 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005075
Jonas Ådahl8538b222012-08-29 22:13:03 +02005076 state = ensure_focus_state(shell, seat);
5077 if (state == NULL)
5078 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005079
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005080 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005081 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005082
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005083 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005084 shell_configure_fullscreen(shsurf);
5085 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005086 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005087
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005088 /* Update the surface’s layer. This brings it to the top of the stacking
5089 * order as appropriate. */
5090 shell_surface_update_layer(shsurf);
5091
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005092 if (shell->focus_animation_type != ANIMATION_NONE) {
5093 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005094 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005095 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005096}
5097
Alex Wu21858432012-04-01 20:13:08 +08005098/* no-op func for checking black surface */
5099static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005100black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005101{
5102}
5103
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005104static bool
Alex Wu21858432012-04-01 20:13:08 +08005105is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5106{
5107 if (es->configure == black_surface_configure) {
5108 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005109 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005110 return true;
5111 }
5112 return false;
5113}
5114
Kristian Høgsberg75840622011-09-06 13:48:16 -04005115static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005116activate_binding(struct weston_seat *seat,
5117 struct desktop_shell *shell,
5118 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005119{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005120 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005121
Alex Wu9c35e6b2012-03-05 14:13:13 +08005122 if (!focus)
5123 return;
5124
Pekka Paalanen01388e22013-04-25 13:57:44 +03005125 if (is_black_surface(focus, &main_surface))
5126 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005127
Pekka Paalanen01388e22013-04-25 13:57:44 +03005128 main_surface = weston_surface_get_main_surface(focus);
5129 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005130 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005131
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005132 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005133}
5134
5135static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005136click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5137 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005138{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005139 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005140 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005141 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005142 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005143
Derek Foreman8ae2db52015-07-15 13:00:36 -05005144 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005145}
5146
5147static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005148touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5149 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005150{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005151 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005152 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005153 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005154 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005155
Derek Foreman8ae2db52015-07-15 13:00:36 -05005156 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005157}
5158
5159static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005160unfocus_all_seats(struct desktop_shell *shell)
5161{
5162 struct weston_seat *seat, *next;
5163
5164 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5165 if (seat->keyboard == NULL)
5166 continue;
5167
5168 weston_keyboard_set_focus(seat->keyboard, NULL);
5169 }
5170}
5171
5172static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005173lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005174{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005175 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005176
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005177 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005178 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005179 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005180 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005181
5182 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005183
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005184 /* Hide all surfaces by removing the fullscreen, panel and
5185 * toplevel layers. This way nothing else can show or receive
5186 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005187
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005188 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005189 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005190 if (shell->showing_input_panels)
5191 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005192 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005193 wl_list_insert(&shell->compositor->cursor_layer.link,
5194 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005195
Neil Robertsb4a91702014-04-09 16:33:32 +01005196 /* Remove the keyboard focus on all seats. This will be
5197 * restored to the workspace's saved state via
5198 * restore_focus_state when the compositor is unlocked */
5199 unfocus_all_seats(shell);
5200
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005201 /* TODO: disable bindings that should not work while locked. */
5202
5203 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005204}
5205
5206static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005207unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005208{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005209 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005210 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005211 return;
5212 }
5213
5214 /* If desktop-shell client has gone away, unlock immediately. */
5215 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005216 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005217 return;
5218 }
5219
5220 if (shell->prepare_event_sent)
5221 return;
5222
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005223 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005224 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005225}
5226
5227static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005228shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005229{
5230 struct desktop_shell *shell = data;
5231
5232 shell->fade.animation = NULL;
5233
5234 switch (shell->fade.type) {
5235 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005236 weston_surface_destroy(shell->fade.view->surface);
5237 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005238 break;
5239 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005240 lock(shell);
5241 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005242 default:
5243 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005244 }
5245}
5246
Jason Ekstranda7af7042013-10-12 22:38:11 -05005247static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005248shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005249{
5250 struct weston_compositor *compositor = shell->compositor;
5251 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005252 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005253
5254 surface = weston_surface_create(compositor);
5255 if (!surface)
5256 return NULL;
5257
Jason Ekstranda7af7042013-10-12 22:38:11 -05005258 view = weston_view_create(surface);
5259 if (!view) {
5260 weston_surface_destroy(surface);
5261 return NULL;
5262 }
5263
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005264 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005265 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005266 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005267 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5268 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005269 pixman_region32_init(&surface->input);
5270
Jason Ekstranda7af7042013-10-12 22:38:11 -05005271 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005272}
5273
5274static void
5275shell_fade(struct desktop_shell *shell, enum fade_type type)
5276{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005277 float tint;
5278
5279 switch (type) {
5280 case FADE_IN:
5281 tint = 0.0;
5282 break;
5283 case FADE_OUT:
5284 tint = 1.0;
5285 break;
5286 default:
5287 weston_log("shell: invalid fade type\n");
5288 return;
5289 }
5290
5291 shell->fade.type = type;
5292
Jason Ekstranda7af7042013-10-12 22:38:11 -05005293 if (shell->fade.view == NULL) {
5294 shell->fade.view = shell_fade_create_surface(shell);
5295 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005296 return;
5297
Jason Ekstranda7af7042013-10-12 22:38:11 -05005298 shell->fade.view->alpha = 1.0 - tint;
5299 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005300 }
5301
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005302 if (shell->fade.view->output == NULL) {
5303 /* If the black view gets a NULL output, we lost the
5304 * last output and we'll just cancel the fade. This
5305 * happens when you close the last window under the
5306 * X11 or Wayland backends. */
5307 shell->locked = false;
5308 weston_surface_destroy(shell->fade.view->surface);
5309 shell->fade.view = NULL;
5310 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005311 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005312 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005313 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005314 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005315 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005316 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005317 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005318}
5319
5320static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005321do_shell_fade_startup(void *data)
5322{
5323 struct desktop_shell *shell = data;
5324
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005325 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005326 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005327 } else {
5328 weston_log("desktop shell: "
5329 "unexpected fade-in animation type %d\n",
5330 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005331 weston_surface_destroy(shell->fade.view->surface);
5332 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005333 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005334}
5335
5336static void
5337shell_fade_startup(struct desktop_shell *shell)
5338{
5339 struct wl_event_loop *loop;
5340
5341 if (!shell->fade.startup_timer)
5342 return;
5343
5344 wl_event_source_remove(shell->fade.startup_timer);
5345 shell->fade.startup_timer = NULL;
5346
5347 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5348 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5349}
5350
5351static int
5352fade_startup_timeout(void *data)
5353{
5354 struct desktop_shell *shell = data;
5355
5356 shell_fade_startup(shell);
5357 return 0;
5358}
5359
5360static void
5361shell_fade_init(struct desktop_shell *shell)
5362{
5363 /* Make compositor output all black, and wait for the desktop-shell
5364 * client to signal it is ready, then fade in. The timer triggers a
5365 * fade-in, in case the desktop-shell client takes too long.
5366 */
5367
5368 struct wl_event_loop *loop;
5369
Jason Ekstranda7af7042013-10-12 22:38:11 -05005370 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005371 weston_log("%s: warning: fade surface already exists\n",
5372 __func__);
5373 return;
5374 }
5375
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005376 if (shell->startup_animation_type == ANIMATION_NONE)
5377 return;
5378
Jason Ekstranda7af7042013-10-12 22:38:11 -05005379 shell->fade.view = shell_fade_create_surface(shell);
5380 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005381 return;
5382
Jason Ekstranda7af7042013-10-12 22:38:11 -05005383 weston_view_update_transform(shell->fade.view);
5384 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005385
5386 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5387 shell->fade.startup_timer =
5388 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5389 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5390}
5391
5392static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005393idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005394{
5395 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005396 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005397 struct weston_seat *seat;
5398
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005399 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005400 if (seat->pointer)
5401 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005402 if (seat->touch)
5403 touch_popup_grab_end(seat->touch);
5404 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005405
5406 shell_fade(shell, FADE_OUT);
5407 /* lock() is called from shell_fade_done() */
5408}
5409
5410static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005411wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005412{
5413 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005414 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005415
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005416 unlock(shell);
5417}
5418
5419static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005420center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005421{
Giulio Camuffob8366642013-04-25 13:57:46 +03005422 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005423 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005424
Jason Ekstranda7af7042013-10-12 22:38:11 -05005425 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005426
5427 x = output->x + (output->width - width) / 2 - surf_x / 2;
5428 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005429
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005430 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005431}
5432
5433static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005434weston_view_set_initial_position(struct weston_view *view,
5435 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005436{
5437 struct weston_compositor *compositor = shell->compositor;
5438 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005439 int32_t range_x, range_y;
5440 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005441 struct weston_output *output, *target_output = NULL;
5442 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005443 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005444
5445 /* As a heuristic place the new window on the same output as the
5446 * pointer. Falling back to the output containing 0, 0.
5447 *
5448 * TODO: Do something clever for touch too?
5449 */
5450 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005451 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005452 ix = wl_fixed_to_int(seat->pointer->x);
5453 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005454 break;
5455 }
5456 }
5457
5458 wl_list_for_each(output, &compositor->output_list, link) {
5459 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5460 target_output = output;
5461 break;
5462 }
5463 }
5464
5465 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005466 weston_view_set_position(view, 10 + random() % 400,
5467 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005468 return;
5469 }
5470
5471 /* Valid range within output where the surface will still be onscreen.
5472 * If this is negative it means that the surface is bigger than
5473 * output.
5474 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005475 get_output_work_area(shell, target_output, &area);
5476
5477 dx = area.x;
5478 dy = area.y;
5479 range_x = area.width - view->surface->width;
5480 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005481
Rob Bradford4cb88c72012-08-13 15:18:44 +01005482 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005483 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005484
5485 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005486 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005487
5488 x = target_output->x + dx;
5489 y = target_output->y + dy;
5490
Jason Ekstranda7af7042013-10-12 22:38:11 -05005491 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005492}
5493
5494static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005495set_maximized_position(struct desktop_shell *shell,
5496 struct shell_surface *shsurf)
5497{
5498 int32_t surf_x, surf_y;
5499 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005500 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005501
Jonny Lambd73c6942014-08-20 15:53:20 +02005502 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005503 if (shsurf->has_set_geometry) {
5504 surf_x = shsurf->geometry.x;
5505 surf_y = shsurf->geometry.y;
5506 } else {
5507 surface_subsurfaces_boundingbox(shsurf->surface,
5508 &surf_x, &surf_y, NULL, NULL);
5509 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005510 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005511
5512 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005513 e->x1 + area.x - surf_x,
5514 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005515}
5516
5517static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005518map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005519 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005520{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005521 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005522 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005523
Pekka Paalanen77346a62011-11-30 16:26:35 +02005524 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005525 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005526 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005527 if (shsurf->state.fullscreen) {
5528 center_on_output(shsurf->view, shsurf->fullscreen_output);
5529 shell_map_fullscreen(shsurf);
5530 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005531 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005532 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005533 weston_view_set_initial_position(shsurf->view, shell);
5534 }
Juan Zhao96879df2012-02-07 08:45:41 +08005535 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005536 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005537 if (shell_map_popup(shsurf) != 0)
5538 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005539 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005540 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005541 weston_view_set_position(shsurf->view,
5542 shsurf->view->geometry.x + sx,
5543 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005544 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005545 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005546 default:
5547 ;
5548 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005549
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005550 /* Surface stacking order, see also activate(). */
5551 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005552
Jason Ekstranda7af7042013-10-12 22:38:11 -05005553 if (shsurf->type != SHELL_SURFACE_NONE) {
5554 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005555 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005556 shsurf->surface->output = shsurf->output;
5557 shsurf->view->output = shsurf->output;
5558 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005559 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005560
Jason Ekstranda7af7042013-10-12 22:38:11 -05005561 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005562 /* XXX: xwayland's using the same fields for transient type */
5563 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005564 if (shsurf->transient.flags ==
5565 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5566 break;
5567 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005568 if (shsurf->state.relative &&
5569 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5570 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005571 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005572 break;
5573 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005574 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005575 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005576 case SHELL_SURFACE_POPUP:
5577 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005578 default:
5579 break;
5580 }
5581
Rafael Antognolli03b16592013-12-03 15:35:42 -02005582 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5583 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005584 {
5585 switch (shell->win_animation_type) {
5586 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005587 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005588 break;
5589 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005590 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005591 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005592 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005593 default:
5594 break;
5595 }
5596 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005597}
5598
5599static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005600configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005601 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005602{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005603 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005604 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005605
Pekka Paalanen77346a62011-11-30 16:26:35 +02005606 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005607
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005608 assert(shsurf);
5609
Rafael Antognolli03b16592013-12-03 15:35:42 -02005610 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005611 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005612 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005613 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005614 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005615 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005616 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005617
Alex Wu4539b082012-03-01 12:57:46 +08005618 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005619 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005620 wl_list_for_each(view, &surface->views, surface_link)
5621 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005622
Rafael Antognolli03b16592013-12-03 15:35:42 -02005623 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005624 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005625 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005626}
5627
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005628static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005629shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005630{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005631 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005632 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005633 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005634
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005635 assert(shsurf);
5636
5637 shell = shsurf->shell;
5638
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005639 if (!weston_surface_is_mapped(es) &&
5640 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005641 remove_popup_grab(shsurf);
5642 }
5643
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005644 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005645 return;
5646
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005647 if (shsurf->has_next_geometry) {
5648 shsurf->geometry = shsurf->next_geometry;
5649 shsurf->has_next_geometry = false;
5650 shsurf->has_set_geometry = true;
5651 } else if (!shsurf->has_set_geometry) {
5652 surface_subsurfaces_boundingbox(shsurf->surface,
5653 &shsurf->geometry.x,
5654 &shsurf->geometry.y,
5655 &shsurf->geometry.width,
5656 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005657 }
5658
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005659 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005660 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005661 type_changed = 1;
5662 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005663
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005664 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005665 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005666 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005667 shsurf->last_width != es->width ||
5668 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005669 float from_x, from_y;
5670 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005671
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005672 if (shsurf->resize_edges) {
5673 sx = 0;
5674 sy = 0;
5675 }
5676
5677 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5678 sx = shsurf->last_width - es->width;
5679 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5680 sy = shsurf->last_height - es->height;
5681
5682 shsurf->last_width = es->width;
5683 shsurf->last_height = es->height;
5684
Jason Ekstranda7af7042013-10-12 22:38:11 -05005685 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5686 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005687 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005688 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005689 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005690 }
5691}
5692
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005693static bool
5694check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5695{
5696 struct timespec now;
5697
5698 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5699 return false;
5700
5701 /*
5702 * If the shell helper client dies before the session has been
5703 * up for roughly 30 seconds, better just make Weston shut down,
5704 * because the user likely has no way to interact with the desktop
5705 * anyway.
5706 */
5707 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5708 weston_log("Error: %s apparently cannot run at all.\n",
5709 shell->client);
5710 weston_log_continue(STAMP_SPACE "Quitting...");
5711 wl_display_terminate(shell->compositor->wl_display);
5712
5713 return true;
5714 }
5715
5716 return false;
5717}
5718
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005719static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005720
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005721static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005722respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005723{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005724 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005725
5726 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5727 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005728 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005729 shell->child.deathstamp = time;
5730 shell->child.deathcount = 0;
5731 }
5732
5733 shell->child.deathcount++;
5734 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005735 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005736 return;
5737 }
5738
Pekka Paalanen826dc142014-08-27 12:11:53 +03005739 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005740 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005741}
5742
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005743static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005744desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5745{
5746 struct desktop_shell *shell;
5747
5748 shell = container_of(listener, struct desktop_shell,
5749 child.client_destroy_listener);
5750
Pekka Paalanen826dc142014-08-27 12:11:53 +03005751 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005752 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005753 /*
5754 * unbind_desktop_shell() will reset shell->child.desktop_shell
5755 * before the respawned process has a chance to create a new
5756 * desktop_shell object, because we are being called from the
5757 * wl_client destructor which destroys all wl_resources before
5758 * returning.
5759 */
5760
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005761 if (!check_desktop_shell_crash_too_early(shell))
5762 respawn_desktop_shell_process(shell);
5763
Pekka Paalanen826dc142014-08-27 12:11:53 +03005764 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005765}
5766
5767static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005768launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005769{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005770 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005771
Pekka Paalanen826dc142014-08-27 12:11:53 +03005772 shell->child.client = weston_client_start(shell->compositor,
5773 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005774
Arnaud Vrac42631192014-08-25 20:56:46 +02005775 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005776 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005777 return;
5778 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005779
5780 shell->child.client_destroy_listener.notify =
5781 desktop_shell_client_destroy;
5782 wl_client_add_destroy_listener(shell->child.client,
5783 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005784}
5785
5786static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005787handle_shell_client_destroy(struct wl_listener *listener, void *data)
5788{
5789 struct shell_client *sc =
5790 container_of(listener, struct shell_client, destroy_listener);
5791
5792 if (sc->ping_timer)
5793 wl_event_source_remove(sc->ping_timer);
5794 free(sc);
5795}
5796
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005797static struct shell_client *
5798shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5799 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005800{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005801 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005802
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005803 sc = zalloc(sizeof *sc);
5804 if (sc == NULL) {
5805 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005806 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005807 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005808
5809 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005810 if (sc->resource == NULL) {
5811 free(sc);
5812 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005813 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005814 }
5815
5816 sc->client = client;
5817 sc->shell = shell;
5818 sc->destroy_listener.notify = handle_shell_client_destroy;
5819 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005820 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005821
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005822 return sc;
5823}
5824
5825static void
5826bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5827{
5828 struct desktop_shell *shell = data;
5829 struct shell_client *sc;
5830
5831 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5832 if (sc)
5833 wl_resource_set_implementation(sc->resource,
5834 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005835 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005836}
5837
5838static void
5839bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5840{
5841 struct desktop_shell *shell = data;
5842 struct shell_client *sc;
5843
5844 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5845 if (sc)
5846 wl_resource_set_dispatcher(sc->resource,
5847 xdg_shell_unversioned_dispatch,
5848 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005849}
5850
5851static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005852unbind_desktop_shell(struct wl_resource *resource)
5853{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005854 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005855
5856 if (shell->locked)
5857 resume_desktop(shell);
5858
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005859 shell->child.desktop_shell = NULL;
5860 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005861}
5862
5863static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005864bind_desktop_shell(struct wl_client *client,
5865 void *data, uint32_t version, uint32_t id)
5866{
Tiago Vignattibe143262012-04-16 17:31:41 +03005867 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005868 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005869
Jason Ekstranda85118c2013-06-27 20:17:02 -05005870 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005871 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005872
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005873 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005874 wl_resource_set_implementation(resource,
5875 &desktop_shell_implementation,
5876 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005877 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005878
5879 if (version < 2)
5880 shell_fade_startup(shell);
5881
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005882 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005883 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005884
5885 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5886 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005887}
5888
Kristian Høgsberg07045392012-02-19 18:52:44 -05005889struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005890 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005891 struct weston_surface *current;
5892 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005893 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005894 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005895};
5896
5897static void
5898switcher_next(struct switcher *switcher)
5899{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005900 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005901 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005902 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005903 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005904
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005905 /* temporary re-display minimized surfaces */
5906 struct weston_view *tmp;
5907 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005908 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5909 weston_layer_entry_remove(&view->layer_link);
5910 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005911 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5912 *minimized = view;
5913 }
5914
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005915 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005916 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005917 if (shsurf &&
5918 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5919 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005920 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005921 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005922 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005923 next = view->surface;
5924 prev = view->surface;
5925 view->alpha = 0.25;
5926 weston_view_geometry_dirty(view);
5927 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005928 }
Alex Wu1659daa2012-04-01 20:13:09 +08005929
Jason Ekstranda7af7042013-10-12 22:38:11 -05005930 if (is_black_surface(view->surface, NULL)) {
5931 view->alpha = 0.25;
5932 weston_view_geometry_dirty(view);
5933 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005934 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005935 }
5936
5937 if (next == NULL)
5938 next = first;
5939
Alex Wu07b26062012-03-12 16:06:01 +08005940 if (next == NULL)
5941 return;
5942
Kristian Høgsberg07045392012-02-19 18:52:44 -05005943 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005944 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005945
5946 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005947 wl_list_for_each(view, &next->views, surface_link)
5948 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005949
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005950 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005951 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005952 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005953}
5954
5955static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005956switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005957{
5958 struct switcher *switcher =
5959 container_of(listener, struct switcher, listener);
5960
5961 switcher_next(switcher);
5962}
5963
5964static void
Daniel Stone351eb612012-05-31 15:27:47 -04005965switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005966{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005967 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005968 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005969 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005970
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005971 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005972 if (is_focus_view(view))
5973 continue;
5974
Jason Ekstranda7af7042013-10-12 22:38:11 -05005975 view->alpha = 1.0;
5976 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005977 }
5978
Alex Wu07b26062012-03-12 16:06:01 +08005979 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005980 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005981 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005982 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005983 weston_keyboard_end_grab(keyboard);
5984 if (keyboard->input_method_resource)
5985 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005986
5987 /* re-hide surfaces that were temporary shown during the switch */
5988 struct weston_view **minimized;
5989 wl_array_for_each(minimized, &switcher->minimized_array) {
5990 /* with the exception of the current selected */
5991 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005992 weston_layer_entry_remove(&(*minimized)->layer_link);
5993 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005994 weston_view_damage_below(*minimized);
5995 }
5996 }
5997 wl_array_release(&switcher->minimized_array);
5998
Kristian Høgsberg07045392012-02-19 18:52:44 -05005999 free(switcher);
6000}
6001
6002static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006003switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006004 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006005{
6006 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006007 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006008
Daniel Stonec9785ea2012-05-30 16:31:52 +01006009 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006010 switcher_next(switcher);
6011}
6012
6013static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006014switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006015 uint32_t mods_depressed, uint32_t mods_latched,
6016 uint32_t mods_locked, uint32_t group)
6017{
6018 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006019 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006020
Daniel Stone351eb612012-05-31 15:27:47 -04006021 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6022 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006023}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006024
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006025static void
6026switcher_cancel(struct weston_keyboard_grab *grab)
6027{
6028 struct switcher *switcher = container_of(grab, struct switcher, grab);
6029
6030 switcher_destroy(switcher);
6031}
6032
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006033static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006034 switcher_key,
6035 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006036 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006037};
6038
6039static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006040switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6041 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006042{
Tiago Vignattibe143262012-04-16 17:31:41 +03006043 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044 struct switcher *switcher;
6045
6046 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006047 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006048 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006049 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006050 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006051 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006052
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006053 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006054 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006055 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006056 weston_keyboard_start_grab(keyboard, &switcher->grab);
6057 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006058 switcher_next(switcher);
6059}
6060
Pekka Paalanen3c647232011-12-22 13:43:43 +02006061static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006062backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6063 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006064{
6065 struct weston_compositor *compositor = data;
6066 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006067 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006068
6069 /* TODO: we're limiting to simple use cases, where we assume just
6070 * control on the primary display. We'd have to extend later if we
6071 * ever get support for setting backlights on random desktop LCD
6072 * panels though */
6073 output = get_default_output(compositor);
6074 if (!output)
6075 return;
6076
6077 if (!output->set_backlight)
6078 return;
6079
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006080 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6081 backlight_new = output->backlight_current - 25;
6082 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6083 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006084
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006085 if (backlight_new < 5)
6086 backlight_new = 5;
6087 if (backlight_new > 255)
6088 backlight_new = 255;
6089
6090 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006091 output->set_backlight(output, output->backlight_current);
6092}
6093
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006094static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006095force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6096 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006097{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006098 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006099 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006100 struct desktop_shell *shell = data;
6101 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006102 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006103
Derek Foreman8ae2db52015-07-15 13:00:36 -05006104 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006105 if (!focus_surface)
6106 return;
6107
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006108 wl_signal_emit(&compositor->kill_signal, focus_surface);
6109
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006110 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006111 wl_client_get_credentials(client, &pid, NULL, NULL);
6112
6113 /* Skip clients that we launched ourselves (the credentials of
6114 * the socketpair is ours) */
6115 if (pid == getpid())
6116 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006117
Daniel Stone325fc2d2012-05-30 16:31:58 +01006118 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006119}
6120
6121static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006122workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006123 uint32_t key, void *data)
6124{
6125 struct desktop_shell *shell = data;
6126 unsigned int new_index = shell->workspaces.current;
6127
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006128 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006129 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006130 if (new_index != 0)
6131 new_index--;
6132
6133 change_workspace(shell, new_index);
6134}
6135
6136static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006137workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006138 uint32_t key, void *data)
6139{
6140 struct desktop_shell *shell = data;
6141 unsigned int new_index = shell->workspaces.current;
6142
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006143 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006144 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006145 if (new_index < shell->workspaces.num - 1)
6146 new_index++;
6147
6148 change_workspace(shell, new_index);
6149}
6150
6151static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006152workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006153 uint32_t key, void *data)
6154{
6155 struct desktop_shell *shell = data;
6156 unsigned int new_index;
6157
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006158 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006159 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006160 new_index = key - KEY_F1;
6161 if (new_index >= shell->workspaces.num)
6162 new_index = shell->workspaces.num - 1;
6163
6164 change_workspace(shell, new_index);
6165}
6166
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006167static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006168workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6169 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006170{
6171 struct desktop_shell *shell = data;
6172 unsigned int new_index = shell->workspaces.current;
6173
6174 if (shell->locked)
6175 return;
6176
6177 if (new_index != 0)
6178 new_index--;
6179
Derek Foreman8ae2db52015-07-15 13:00:36 -05006180 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006181}
6182
6183static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006184workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6185 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006186{
6187 struct desktop_shell *shell = data;
6188 unsigned int new_index = shell->workspaces.current;
6189
6190 if (shell->locked)
6191 return;
6192
6193 if (new_index < shell->workspaces.num - 1)
6194 new_index++;
6195
Derek Foreman8ae2db52015-07-15 13:00:36 -05006196 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006197}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006198
6199static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006200shell_reposition_view_on_output_destroy(struct weston_view *view)
6201{
6202 struct weston_output *output, *first_output;
6203 struct weston_compositor *ec = view->surface->compositor;
6204 struct shell_surface *shsurf;
6205 float x, y;
6206 int visible;
6207
6208 x = view->geometry.x;
6209 y = view->geometry.y;
6210
6211 /* At this point the destroyed output is not in the list anymore.
6212 * If the view is still visible somewhere, we leave where it is,
6213 * otherwise, move it to the first output. */
6214 visible = 0;
6215 wl_list_for_each(output, &ec->output_list, link) {
6216 if (pixman_region32_contains_point(&output->region,
6217 x, y, NULL)) {
6218 visible = 1;
6219 break;
6220 }
6221 }
6222
6223 if (!visible) {
6224 first_output = container_of(ec->output_list.next,
6225 struct weston_output, link);
6226
6227 x = first_output->x + first_output->width / 4;
6228 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006229
6230 weston_view_set_position(view, x, y);
6231 } else {
6232 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006233 }
6234
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006235
6236 shsurf = get_shell_surface(view->surface);
6237
6238 if (shsurf) {
6239 shsurf->saved_position_valid = false;
6240 shsurf->next_state.maximized = false;
6241 shsurf->next_state.fullscreen = false;
6242 shsurf->state_changed = true;
6243 }
6244}
6245
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006246void
6247shell_for_each_layer(struct desktop_shell *shell,
6248 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006249{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006250 struct workspace **ws;
6251
6252 func(shell, &shell->fullscreen_layer, data);
6253 func(shell, &shell->panel_layer, data);
6254 func(shell, &shell->background_layer, data);
6255 func(shell, &shell->lock_layer, data);
6256 func(shell, &shell->input_panel_layer, data);
6257
6258 wl_array_for_each(ws, &shell->workspaces.array)
6259 func(shell, &(*ws)->layer, data);
6260}
6261
6262static void
6263shell_output_destroy_move_layer(struct desktop_shell *shell,
6264 struct weston_layer *layer,
6265 void *data)
6266{
6267 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006268 struct weston_view *view;
6269
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006270 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006271 if (view->output != output)
6272 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006273
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006274 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006275 }
6276}
6277
6278static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006279handle_output_destroy(struct wl_listener *listener, void *data)
6280{
6281 struct shell_output *output_listener =
6282 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006283 struct weston_output *output = output_listener->output;
6284 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006285
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006286 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006287
Xiong Zhang6b481422013-10-23 13:58:32 +08006288 wl_list_remove(&output_listener->destroy_listener.link);
6289 wl_list_remove(&output_listener->link);
6290 free(output_listener);
6291}
6292
6293static void
6294create_shell_output(struct desktop_shell *shell,
6295 struct weston_output *output)
6296{
6297 struct shell_output *shell_output;
6298
6299 shell_output = zalloc(sizeof *shell_output);
6300 if (shell_output == NULL)
6301 return;
6302
6303 shell_output->output = output;
6304 shell_output->shell = shell;
6305 shell_output->destroy_listener.notify = handle_output_destroy;
6306 wl_signal_add(&output->destroy_signal,
6307 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006308 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006309}
6310
6311static void
6312handle_output_create(struct wl_listener *listener, void *data)
6313{
6314 struct desktop_shell *shell =
6315 container_of(listener, struct desktop_shell, output_create_listener);
6316 struct weston_output *output = (struct weston_output *)data;
6317
6318 create_shell_output(shell, output);
6319}
6320
6321static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006322handle_output_move_layer(struct desktop_shell *shell,
6323 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006324{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006325 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006326 struct weston_view *view;
6327 float x, y;
6328
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006329 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006330 if (view->output != output)
6331 continue;
6332
6333 x = view->geometry.x + output->move_x;
6334 y = view->geometry.y + output->move_y;
6335 weston_view_set_position(view, x, y);
6336 }
6337}
6338
6339static void
6340handle_output_move(struct wl_listener *listener, void *data)
6341{
6342 struct desktop_shell *shell;
6343
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006344 shell = container_of(listener, struct desktop_shell,
6345 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006346
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006347 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006348}
6349
6350static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006351setup_output_destroy_handler(struct weston_compositor *ec,
6352 struct desktop_shell *shell)
6353{
6354 struct weston_output *output;
6355
6356 wl_list_init(&shell->output_list);
6357 wl_list_for_each(output, &ec->output_list, link)
6358 create_shell_output(shell, output);
6359
6360 shell->output_create_listener.notify = handle_output_create;
6361 wl_signal_add(&ec->output_created_signal,
6362 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006363
6364 shell->output_move_listener.notify = handle_output_move;
6365 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006366}
6367
6368static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006369shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006370{
Tiago Vignattibe143262012-04-16 17:31:41 +03006371 struct desktop_shell *shell =
6372 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006373 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006374 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006375
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006376 /* Force state to unlocked so we don't try to fade */
6377 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006378
6379 if (shell->child.client) {
6380 /* disable respawn */
6381 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006382 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006383 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006384
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006385 wl_list_remove(&shell->idle_listener.link);
6386 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006387
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006388 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006389 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006390
Xiong Zhang6b481422013-10-23 13:58:32 +08006391 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6392 wl_list_remove(&shell_output->destroy_listener.link);
6393 wl_list_remove(&shell_output->link);
6394 free(shell_output);
6395 }
6396
6397 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006398 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006399
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006400 wl_array_for_each(ws, &shell->workspaces.array)
6401 workspace_destroy(*ws);
6402 wl_array_release(&shell->workspaces.array);
6403
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006404 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006405 free(shell);
6406}
6407
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006408static void
6409shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6410{
6411 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006412 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006413
6414 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006415 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6416 MODIFIER_CTRL | MODIFIER_ALT,
6417 terminate_binding, ec);
6418 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6419 click_to_activate_binding,
6420 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006421 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6422 click_to_activate_binding,
6423 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006424 weston_compositor_add_touch_binding(ec, 0,
6425 touch_to_activate_binding,
6426 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006427 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6428 MODIFIER_SUPER | MODIFIER_ALT,
6429 surface_opacity_binding, NULL);
6430 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6431 MODIFIER_SUPER, zoom_axis_binding,
6432 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006433
6434 /* configurable bindings */
6435 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006436 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6437 zoom_key_binding, NULL);
6438 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6439 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006440 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6441 maximize_binding, NULL);
6442 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6443 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006444 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6445 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006446 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006447 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006448 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006449 weston_compositor_add_button_binding(ec, BTN_LEFT,
6450 mod | MODIFIER_SHIFT,
6451 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006452
6453 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006454 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006455 rotate_binding, NULL);
6456
Daniel Stone325fc2d2012-05-30 16:31:58 +01006457 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6458 shell);
6459 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6460 ec);
6461 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6462 backlight_binding, ec);
6463 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6464 ec);
6465 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6466 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006467 weston_compositor_add_key_binding(ec, KEY_K, mod,
6468 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006469 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6470 workspace_up_binding, shell);
6471 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6472 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006473 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6474 workspace_move_surface_up_binding,
6475 shell);
6476 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6477 workspace_move_surface_down_binding,
6478 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006479
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006480 if (shell->exposay_modifier)
6481 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6482 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006483
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006484 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6485 if (shell->workspaces.num > 1) {
6486 num_workspace_bindings = shell->workspaces.num;
6487 if (num_workspace_bindings > 6)
6488 num_workspace_bindings = 6;
6489 for (i = 0; i < num_workspace_bindings; i++)
6490 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6491 workspace_f_binding,
6492 shell);
6493 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006494
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006495 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006496}
6497
Jason Ekstrand024177c2014-04-21 19:42:58 -05006498static void
6499handle_seat_created(struct wl_listener *listener, void *data)
6500{
6501 struct weston_seat *seat = data;
6502
6503 create_shell_seat(seat);
6504}
6505
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006506WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006507module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006508 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006509{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006510 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006511 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006512 struct workspace **pws;
6513 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006514 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006515
Peter Huttererf3d62272013-08-08 11:57:05 +10006516 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006517 if (shell == NULL)
6518 return -1;
6519
Kristian Høgsberg75840622011-09-06 13:48:16 -04006520 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006521
6522 shell->destroy_listener.notify = shell_destroy;
6523 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006524 shell->idle_listener.notify = idle_handler;
6525 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6526 shell->wake_listener.notify = wake_handler;
6527 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006528
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006529 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006530 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006531 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006532 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006533 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006534 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006535 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006536 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006537 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006538 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006539 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006540 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006541 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006542
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006543 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6544 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006545 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6546 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006547 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006548
6549 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006550 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006551
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006552 if (input_panel_setup(shell) < 0)
6553 return -1;
6554
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006555 shell->text_backend = text_backend_init(ec);
6556 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006557 return -1;
6558
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006559 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006560
Daniel Stonedf8133b2013-11-19 11:37:14 +01006561 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6562 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6563
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006564 for (i = 0; i < shell->workspaces.num; i++) {
6565 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6566 if (pws == NULL)
6567 return -1;
6568
6569 *pws = workspace_create();
6570 if (*pws == NULL)
6571 return -1;
6572 }
6573 activate_workspace(shell, 0);
6574
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006575 weston_layer_init(&shell->minimized_layer, NULL);
6576
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006577 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006578 wl_list_init(&shell->workspaces.animation.link);
6579 shell->workspaces.animation.frame = animate_workspace_change_frame;
6580
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006581 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006582 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006583 return -1;
6584
Rafael Antognollie2a34552013-12-03 15:35:45 -02006585 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6586 shell, bind_xdg_shell) == NULL)
6587 return -1;
6588
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006589 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006590 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006591 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006592 return -1;
6593
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006594 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6595 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006596 return -1;
6597
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006598 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006599
Jonny Lamb765760d2014-08-20 15:53:19 +02006600 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6601
Xiong Zhang6b481422013-10-23 13:58:32 +08006602 setup_output_destroy_handler(ec, shell);
6603
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006604 loop = wl_display_get_event_loop(ec->wl_display);
6605 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006606
Jason Ekstrand024177c2014-04-21 19:42:58 -05006607 wl_list_for_each(seat, &ec->seat_list, link)
6608 handle_seat_created(NULL, seat);
6609 shell->seat_create_listener.notify = handle_seat_created;
6610 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006611
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006612 screenshooter_create(ec);
6613
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006614 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006615
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006616 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006617
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006618 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6619
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006620 return 0;
6621}