blob: afc47998e5fd16f74d4fb5b2a54f00f05034a18c [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
Kristian Høgsberge3148752013-05-06 23:19:49 -0400254surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
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
1637surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1638{
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) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001653 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001655 seat->touch->grab_y;
1656
1657 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1658 seat->touch);
1659
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
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001751surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
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) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001768 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001770 seat->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,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001774 seat->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 Foremancf7d95a2015-06-03 15:53:22 -05001793 (surface_move(shsurf, seat, 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) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001800 (surface_touch_move(shsurf, seat) < 0))
1801 wl_resource_post_no_memory(resource);
1802 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001803}
1804
Rafael Antognollie2a34552013-12-03 15:35:45 -02001805static void
1806shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1807 struct wl_resource *seat_resource, uint32_t serial)
1808{
1809 common_surface_move(resource, seat_resource, serial);
1810}
1811
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001812struct weston_resize_grab {
1813 struct shell_grab base;
1814 uint32_t edges;
1815 int32_t width, height;
1816};
1817
1818static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001819resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1820 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001821{
1822 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001823 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001824 struct shell_surface *shsurf = resize->base.shsurf;
1825 int32_t width, height;
1826 wl_fixed_t from_x, from_y;
1827 wl_fixed_t to_x, to_y;
1828
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001829 weston_pointer_move(pointer, x, y);
1830
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001831 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001832 return;
1833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 weston_view_from_global_fixed(shsurf->view,
1835 pointer->grab_x, pointer->grab_y,
1836 &from_x, &from_y);
1837 weston_view_from_global_fixed(shsurf->view,
1838 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001839
1840 width = resize->width;
1841 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1842 width += wl_fixed_to_int(from_x - to_x);
1843 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1844 width += wl_fixed_to_int(to_x - from_x);
1845 }
1846
1847 height = resize->height;
1848 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1849 height += wl_fixed_to_int(from_y - to_y);
1850 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1851 height += wl_fixed_to_int(to_y - from_y);
1852 }
1853
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001854 if (width < 1)
1855 width = 1;
1856 if (height < 1)
1857 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001858 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001859}
1860
1861static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001862send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001863{
1864 struct shell_surface *shsurf = get_shell_surface(surface);
1865
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001866 assert(shsurf);
1867
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001868 if (shsurf->resource)
1869 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001870 shsurf->resize_edges,
1871 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001872}
1873
1874static const struct weston_shell_client shell_client = {
1875 send_configure
1876};
1877
1878static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001879resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001880 uint32_t time, uint32_t button, uint32_t state_w)
1881{
1882 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001883 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001884 enum wl_pointer_button_state state = state_w;
1885
1886 if (pointer->button_count == 0 &&
1887 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1888 shell_grab_end(&resize->base);
1889 free(grab);
1890 }
1891}
1892
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001893static void
1894resize_grab_cancel(struct weston_pointer_grab *grab)
1895{
1896 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1897
1898 shell_grab_end(&resize->base);
1899 free(grab);
1900}
1901
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001902static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001903 noop_grab_focus,
1904 resize_grab_motion,
1905 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001906 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001907};
1908
Giulio Camuffob8366642013-04-25 13:57:46 +03001909/*
1910 * Returns the bounding box of a surface and all its sub-surfaces,
1911 * in the surface coordinates system. */
1912static void
1913surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1914 int32_t *y, int32_t *w, int32_t *h) {
1915 pixman_region32_t region;
1916 pixman_box32_t *box;
1917 struct weston_subsurface *subsurface;
1918
1919 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 surface->width,
1921 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001922
1923 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1924 pixman_region32_union_rect(&region, &region,
1925 subsurface->position.x,
1926 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001927 subsurface->surface->width,
1928 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001929 }
1930
1931 box = pixman_region32_extents(&region);
1932 if (x)
1933 *x = box->x1;
1934 if (y)
1935 *y = box->y1;
1936 if (w)
1937 *w = box->x2 - box->x1;
1938 if (h)
1939 *h = box->y2 - box->y1;
1940
1941 pixman_region32_fini(&region);
1942}
1943
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001944static int
1945surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001946 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001947{
1948 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001949 const unsigned resize_topbottom =
1950 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1951 const unsigned resize_leftright =
1952 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1953 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001954
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001955 if (shsurf->grabbed ||
1956 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001957 return 0;
1958
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001959 /* Check for invalid edge combinations. */
1960 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1961 (edges & resize_topbottom) == resize_topbottom ||
1962 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001963 return 0;
1964
1965 resize = malloc(sizeof *resize);
1966 if (!resize)
1967 return -1;
1968
1969 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001970
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001971 resize->width = shsurf->geometry.width;
1972 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001973
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001974 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001975 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001976 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001977 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001978
1979 return 0;
1980}
1981
1982static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001983common_surface_resize(struct wl_resource *resource,
1984 struct wl_resource *seat_resource, uint32_t serial,
1985 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001986{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001987 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001988 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001989 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001990
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001991 if (seat->pointer == NULL ||
1992 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001993 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001994 seat->pointer->focus == NULL)
1995 return;
1996
1997 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1998 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001999 return;
2000
Kristian Høgsberge3148752013-05-06 23:19:49 -04002001 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002002 wl_resource_post_no_memory(resource);
2003}
2004
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002005static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002006shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2007 struct wl_resource *seat_resource, uint32_t serial,
2008 uint32_t edges)
2009{
2010 common_surface_resize(resource, seat_resource, serial, edges);
2011}
2012
2013static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002014busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002015{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002016 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002017 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002019 wl_fixed_t sx, sy;
2020
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 view = weston_compositor_pick_view(pointer->seat->compositor,
2022 pointer->x, pointer->y,
2023 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002024
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002025 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2026 shell_grab_end(grab);
2027 free(grab);
2028 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002029}
2030
2031static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002032busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2033 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002034{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002035 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002037
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002038static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002039busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002040 uint32_t time, uint32_t button, uint32_t state)
2041{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002042 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002043 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04002044 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002045
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002046 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002047 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foremancf7d95a2015-06-03 15:53:22 -05002048 surface_move(shsurf, seat, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002049 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002050 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04002051 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002052 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002053}
2054
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002055static void
2056busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2057{
2058 struct shell_grab *grab = (struct shell_grab *) base;
2059
2060 shell_grab_end(grab);
2061 free(grab);
2062}
2063
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002064static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002065 busy_cursor_grab_focus,
2066 busy_cursor_grab_motion,
2067 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002068 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002069};
2070
2071static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002072set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002073{
2074 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002075
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002076 if (pointer->grab->interface == &busy_cursor_grab_interface)
2077 return;
2078
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002079 grab = malloc(sizeof *grab);
2080 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002081 return;
2082
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002083 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2084 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002085 /* Mark the shsurf as ungrabbed so that button binding is able
2086 * to move it. */
2087 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002088}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002089
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002090static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002091end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002092{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002093 struct shell_grab *grab;
2094 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002095
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002096 wl_list_for_each(seat, &compositor->seat_list, link) {
2097 if (seat->pointer == NULL)
2098 continue;
2099
2100 grab = (struct shell_grab *) seat->pointer->grab;
2101 if (grab->grab.interface == &busy_cursor_grab_interface &&
2102 wl_resource_get_client(grab->shsurf->resource) == client) {
2103 shell_grab_end(grab);
2104 free(grab);
2105 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002106 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002107}
2108
Scott Moreau9521d5e2012-04-19 13:06:17 -06002109static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002110handle_shell_client_destroy(struct wl_listener *listener, void *data);
2111
2112static int
2113xdg_ping_timeout_handler(void *data)
2114{
2115 struct shell_client *sc = data;
2116 struct weston_seat *seat;
2117 struct shell_surface *shsurf;
2118
2119 /* Client is not responding */
2120 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002121 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2122 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2123 continue;
2124 if (seat->pointer->focus->surface->resource == NULL)
2125 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002126
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002127 shsurf = get_shell_surface(seat->pointer->focus->surface);
2128 if (shsurf &&
2129 wl_resource_get_client(shsurf->resource) == sc->client)
2130 set_busy_cursor(shsurf, seat->pointer);
2131 }
2132
2133 return 1;
2134}
2135
2136static void
2137handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2138{
2139 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002140 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002141 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002142 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002143
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002144 if (sc->unresponsive) {
2145 xdg_ping_timeout_handler(sc);
2146 return;
2147 }
2148
2149 sc->ping_serial = serial;
2150 loop = wl_display_get_event_loop(compositor->wl_display);
2151 if (sc->ping_timer == NULL)
2152 sc->ping_timer =
2153 wl_event_loop_add_timer(loop,
2154 xdg_ping_timeout_handler, sc);
2155 if (sc->ping_timer == NULL)
2156 return;
2157
2158 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2159
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002160 if (shell_surface_is_xdg_surface(shsurf) ||
2161 shell_surface_is_xdg_popup(shsurf))
2162 xdg_shell_send_ping(sc->resource, serial);
2163 else if (shell_surface_is_wl_shell_surface(shsurf))
2164 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002165}
2166
Scott Moreauff1db4a2012-04-17 19:06:18 -06002167static void
2168ping_handler(struct weston_surface *surface, uint32_t serial)
2169{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002170 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002171
2172 if (!shsurf)
2173 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002174 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002175 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002176 if (shsurf->surface == shsurf->shell->grab_surface)
2177 return;
2178
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002179 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002180}
2181
2182static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002183handle_pointer_focus(struct wl_listener *listener, void *data)
2184{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002185 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002186 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002187 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002188 uint32_t serial;
2189
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002191 return;
2192
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002194 serial = wl_display_next_serial(compositor->wl_display);
2195 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002196}
2197
2198static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002199shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2200{
2201 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002202 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002203}
2204
2205static void
2206shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2207{
2208 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002209 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002210}
2211
2212static void
2213handle_keyboard_focus(struct wl_listener *listener, void *data)
2214{
2215 struct weston_keyboard *keyboard = data;
2216 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2217
2218 if (seat->focused_surface) {
2219 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2220 if (shsurf)
2221 shell_surface_lose_keyboard_focus(shsurf);
2222 }
2223
2224 seat->focused_surface = keyboard->focus;
2225
2226 if (seat->focused_surface) {
2227 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2228 if (shsurf)
2229 shell_surface_gain_keyboard_focus(shsurf);
2230 }
2231}
2232
2233static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002234shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002235{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002236 if (sc->ping_serial != serial)
2237 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002238
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002239 sc->unresponsive = 0;
2240 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002241
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002242 if (sc->ping_timer) {
2243 wl_event_source_remove(sc->ping_timer);
2244 sc->ping_timer = NULL;
2245 }
2246
2247}
2248
2249static void
2250shell_surface_pong(struct wl_client *client,
2251 struct wl_resource *resource, uint32_t serial)
2252{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002253 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2254 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002255
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002256 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002257}
2258
2259static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002260set_title(struct shell_surface *shsurf, const char *title)
2261{
2262 free(shsurf->title);
2263 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002264 shsurf->surface->timeline.force_refresh = 1;
2265}
2266
2267static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002268set_pid(struct shell_surface *shsurf, pid_t pid)
2269{
2270 /* We have no use for it */
2271}
2272
2273static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002274set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2275{
2276 shsurf->type = t;
2277 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002278}
2279
2280static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002281set_window_geometry(struct shell_surface *shsurf,
2282 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002283{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002284 shsurf->next_geometry.x = x;
2285 shsurf->next_geometry.y = y;
2286 shsurf->next_geometry.width = width;
2287 shsurf->next_geometry.height = height;
2288 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002289}
2290
2291static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002292shell_surface_set_title(struct wl_client *client,
2293 struct wl_resource *resource, const char *title)
2294{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002295 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002296
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002297 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002298}
2299
2300static void
2301shell_surface_set_class(struct wl_client *client,
2302 struct wl_resource *resource, const char *class)
2303{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002304 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002305
2306 free(shsurf->class);
2307 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002308 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002309}
2310
Alex Wu4539b082012-03-01 12:57:46 +08002311static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002312restore_output_mode(struct weston_output *output)
2313{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002314 if (output->original_mode)
2315 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002316}
2317
2318static void
2319restore_all_output_modes(struct weston_compositor *compositor)
2320{
2321 struct weston_output *output;
2322
2323 wl_list_for_each(output, &compositor->output_list, link)
2324 restore_output_mode(output);
2325}
2326
Philip Withnall07926d92013-11-25 18:01:40 +00002327/* The surface will be inserted into the list immediately after the link
2328 * returned by this function (i.e. will be stacked immediately above the
2329 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002330static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002331shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2332{
2333 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002334 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002335
2336 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002337 case SHELL_SURFACE_XWAYLAND:
2338 return &shsurf->shell->fullscreen_layer.view_list;
2339
2340 case SHELL_SURFACE_NONE:
2341 return NULL;
2342
Kristian Høgsbergead52422014-01-01 13:51:52 -08002343 case SHELL_SURFACE_POPUP:
2344 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002345 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002346 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002347 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002348 /* Move the surface to its parent layer so
2349 * that surfaces which are transient for
2350 * fullscreen surfaces don't get hidden by the
2351 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002352
2353 /* TODO: Handle a parent with multiple views */
2354 parent = get_default_view(shsurf->parent);
2355 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002356 return container_of(parent->layer_link.link.prev,
2357 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002358 }
Philip Withnall07926d92013-11-25 18:01:40 +00002359
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002360 /* Move the surface to a normal workspace layer so that surfaces
2361 * which were previously fullscreen or transient are no longer
2362 * rendered on top. */
2363 ws = get_current_workspace(shsurf->shell);
2364 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002365 }
2366
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002367 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002368}
2369
Philip Withnall648a4dd2013-11-25 18:01:44 +00002370static void
2371shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2372{
2373 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002375
2376 /* Move the child layers to the same workspace as shsurf. They will be
2377 * stacked above shsurf. */
2378 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002379 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002380 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002381 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002382 prev = container_of(shsurf->view->layer_link.link.prev,
2383 struct weston_layer_entry, link);
2384 weston_layer_entry_remove(&child->view->layer_link);
2385 weston_layer_entry_insert(prev,
2386 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002387 weston_view_geometry_dirty(child->view);
2388 weston_surface_damage(child->surface);
2389
2390 /* Recurse. We don’t expect this to recurse very far (if
2391 * at all) because that would imply we have transient
2392 * (or popup) children of transient surfaces, which
2393 * would be unusual. */
2394 shell_surface_update_child_surface_layers(child);
2395 }
2396 }
2397}
2398
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002399/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002400 * geometry to ensure the changes are redrawn.
2401 *
2402 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2403 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002404static void
2405shell_surface_update_layer(struct shell_surface *shsurf)
2406{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002407 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002408
2409 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2410
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002411 if (new_layer_link == NULL)
2412 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002413 if (new_layer_link == &shsurf->view->layer_link)
2414 return;
2415
2416 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002417 weston_layer_entry_remove(&shsurf->view->layer_link);
2418 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002419 weston_view_geometry_dirty(shsurf->view);
2420 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002421
2422 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002423}
2424
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002425static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002426shell_surface_set_parent(struct shell_surface *shsurf,
2427 struct weston_surface *parent)
2428{
2429 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002430
2431 wl_list_remove(&shsurf->children_link);
2432 wl_list_init(&shsurf->children_link);
2433
2434 /* Insert into the parent surface’s child list. */
2435 if (parent != NULL) {
2436 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2437 if (parent_shsurf != NULL)
2438 wl_list_insert(&parent_shsurf->children_list,
2439 &shsurf->children_link);
2440 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002441}
2442
2443static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002444shell_surface_set_output(struct shell_surface *shsurf,
2445 struct weston_output *output)
2446{
2447 struct weston_surface *es = shsurf->surface;
2448
2449 /* get the default output, if the client set it as NULL
2450 check whether the ouput is available */
2451 if (output)
2452 shsurf->output = output;
2453 else if (es->output)
2454 shsurf->output = es->output;
2455 else
2456 shsurf->output = get_default_output(es->compositor);
2457}
2458
2459static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002460surface_clear_next_states(struct shell_surface *shsurf)
2461{
2462 shsurf->next_state.maximized = false;
2463 shsurf->next_state.fullscreen = false;
2464
2465 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2466 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2467 shsurf->state_changed = true;
2468}
2469
2470static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002471set_toplevel(struct shell_surface *shsurf)
2472{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002473 shell_surface_set_parent(shsurf, NULL);
2474 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002475 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002476
2477 /* The layer_link is updated in set_surface_type(),
2478 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002479}
2480
2481static void
2482shell_surface_set_toplevel(struct wl_client *client,
2483 struct wl_resource *resource)
2484{
2485 struct shell_surface *surface = wl_resource_get_user_data(resource);
2486
2487 set_toplevel(surface);
2488}
2489
2490static void
2491set_transient(struct shell_surface *shsurf,
2492 struct weston_surface *parent, int x, int y, uint32_t flags)
2493{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002494 assert(parent != NULL);
2495
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002496 shell_surface_set_parent(shsurf, parent);
2497
2498 surface_clear_next_states(shsurf);
2499
Philip Withnallbecb77e2013-11-25 18:01:30 +00002500 shsurf->transient.x = x;
2501 shsurf->transient.y = y;
2502 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002503
Rafael Antognollied207b42013-12-03 15:35:43 -02002504 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002505 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002506 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002507
2508 /* The layer_link is updated in set_surface_type(),
2509 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002510}
2511
2512static void
2513shell_surface_set_transient(struct wl_client *client,
2514 struct wl_resource *resource,
2515 struct wl_resource *parent_resource,
2516 int x, int y, uint32_t flags)
2517{
2518 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2519 struct weston_surface *parent =
2520 wl_resource_get_user_data(parent_resource);
2521
2522 set_transient(shsurf, parent, x, y, flags);
2523}
2524
2525static void
2526set_fullscreen(struct shell_surface *shsurf,
2527 uint32_t method,
2528 uint32_t framerate,
2529 struct weston_output *output)
2530{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002531 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002532 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002533
2534 shsurf->fullscreen_output = shsurf->output;
2535 shsurf->fullscreen.type = method;
2536 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002537
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002538 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002539}
2540
2541static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002542weston_view_set_initial_position(struct weston_view *view,
2543 struct desktop_shell *shell);
2544
2545static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002546unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002547{
Philip Withnallf85fe842013-11-25 18:01:37 +00002548 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002549 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2550 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002551 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002552 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002553
Alex Wu4539b082012-03-01 12:57:46 +08002554 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2555 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002556
Alex Wu4539b082012-03-01 12:57:46 +08002557 wl_list_remove(&shsurf->fullscreen.transform.link);
2558 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002559
Jason Ekstranda7af7042013-10-12 22:38:11 -05002560 if (shsurf->fullscreen.black_view)
2561 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2562 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002563
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002564 if (shsurf->saved_position_valid)
2565 weston_view_set_position(shsurf->view,
2566 shsurf->saved_x, shsurf->saved_y);
2567 else
2568 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2569
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002570 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002572 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002573 shsurf->saved_rotation_valid = false;
2574 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002575
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002576 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002577}
2578
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002579static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002580shell_surface_set_fullscreen(struct wl_client *client,
2581 struct wl_resource *resource,
2582 uint32_t method,
2583 uint32_t framerate,
2584 struct wl_resource *output_resource)
2585{
2586 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2587 struct weston_output *output;
2588
2589 if (output_resource)
2590 output = wl_resource_get_user_data(output_resource);
2591 else
2592 output = NULL;
2593
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002594 shell_surface_set_parent(shsurf, NULL);
2595
Rafael Antognolli03b16592013-12-03 15:35:42 -02002596 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002597 shsurf->next_state.fullscreen = true;
2598 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002599 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002600}
2601
2602static void
2603set_popup(struct shell_surface *shsurf,
2604 struct weston_surface *parent,
2605 struct weston_seat *seat,
2606 uint32_t serial,
2607 int32_t x,
2608 int32_t y)
2609{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002610 assert(parent != NULL);
2611
Philip Withnallbecb77e2013-11-25 18:01:30 +00002612 shsurf->popup.shseat = get_shell_seat(seat);
2613 shsurf->popup.serial = serial;
2614 shsurf->popup.x = x;
2615 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002616
Pekka Paalanenb5026542014-11-12 15:09:24 +02002617 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002618}
2619
2620static void
2621shell_surface_set_popup(struct wl_client *client,
2622 struct wl_resource *resource,
2623 struct wl_resource *seat_resource,
2624 uint32_t serial,
2625 struct wl_resource *parent_resource,
2626 int32_t x, int32_t y, uint32_t flags)
2627{
2628 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002629 struct weston_surface *parent =
2630 wl_resource_get_user_data(parent_resource);
2631
2632 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002633
Rafael Antognolli03b16592013-12-03 15:35:42 -02002634 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002635 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002636 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002637 wl_resource_get_user_data(seat_resource),
2638 serial, x, y);
2639}
2640
2641static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002642unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002643{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002644 /* undo all maximized things here */
2645 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002646
2647 if (shsurf->saved_position_valid)
2648 weston_view_set_position(shsurf->view,
2649 shsurf->saved_x, shsurf->saved_y);
2650 else
2651 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002652
2653 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 wl_list_insert(&shsurf->view->geometry.transformation_list,
2655 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002656 shsurf->saved_rotation_valid = false;
2657 }
2658
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002659 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002660}
2661
Philip Withnallbecb77e2013-11-25 18:01:30 +00002662static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002663set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002664{
2665 struct shell_surface *shsurf;
2666 struct workspace *current_ws;
2667 struct weston_seat *seat;
2668 struct weston_surface *focus;
2669 struct weston_view *view;
2670
2671 view = get_default_view(surface);
2672 if (!view)
2673 return;
2674
2675 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2676
2677 shsurf = get_shell_surface(surface);
2678 current_ws = get_current_workspace(shsurf->shell);
2679
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002680 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002681 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002682
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002683 drop_focus_state(shsurf->shell, current_ws, view->surface);
2684 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2685 if (!seat->keyboard)
2686 continue;
2687 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2688 if (focus == view->surface)
2689 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002690 }
2691
2692 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002693 weston_view_damage_below(view);
2694}
2695
2696static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002697shell_surface_set_maximized(struct wl_client *client,
2698 struct wl_resource *resource,
2699 struct wl_resource *output_resource)
2700{
2701 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2702 struct weston_output *output;
2703
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002704 surface_clear_next_states(shsurf);
2705 shsurf->next_state.maximized = true;
2706 shsurf->state_changed = true;
2707
Pekka Paalanenb5026542014-11-12 15:09:24 +02002708 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002709 shell_surface_set_parent(shsurf, NULL);
2710
Philip Withnallbecb77e2013-11-25 18:01:30 +00002711 if (output_resource)
2712 output = wl_resource_get_user_data(output_resource);
2713 else
2714 output = NULL;
2715
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002716 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002717
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002718 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002719}
2720
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002721/* This is only ever called from set_surface_type(), so there’s no need to
2722 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002723static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002724reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002725{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002726 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002727 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002728 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002729 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002730
Pekka Paalanen98262232011-12-01 10:42:22 +02002731 return 0;
2732}
2733
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002734static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002735set_full_output(struct shell_surface *shsurf)
2736{
2737 shsurf->saved_x = shsurf->view->geometry.x;
2738 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002739 shsurf->saved_width = shsurf->surface->width;
2740 shsurf->saved_height = shsurf->surface->height;
2741 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002742 shsurf->saved_position_valid = true;
2743
2744 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2745 wl_list_remove(&shsurf->rotation.transform.link);
2746 wl_list_init(&shsurf->rotation.transform.link);
2747 weston_view_geometry_dirty(shsurf->view);
2748 shsurf->saved_rotation_valid = true;
2749 }
2750}
2751
2752static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002753set_surface_type(struct shell_surface *shsurf)
2754{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002755 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002757
Philip Withnallbecb77e2013-11-25 18:01:30 +00002758 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002759
Rafael Antognolli03b16592013-12-03 15:35:42 -02002760 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002761 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002762
2763 switch (shsurf->type) {
2764 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002765 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002766 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002767 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002768 weston_view_set_position(shsurf->view,
2769 pev->geometry.x + shsurf->transient.x,
2770 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002771 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002772 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002773
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002774 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 weston_view_set_position(shsurf->view, shsurf->transient.x,
2776 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002777 break;
2778
Philip Withnall0f640e22013-11-25 18:01:31 +00002779 case SHELL_SURFACE_POPUP:
2780 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002781 default:
2782 break;
2783 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002784
2785 /* Update the surface’s layer. */
2786 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002787}
2788
Tiago Vignattibe143262012-04-16 17:31:41 +03002789static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002790shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002791{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002792 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002793}
2794
Pekka Paalanen8274d902014-08-06 19:36:51 +03002795static int
2796black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2797{
2798 struct weston_surface *fs_surface = surface->configure_private;
2799 int n;
2800 int rem;
2801 int ret;
2802
2803 n = snprintf(buf, len, "black background surface for ");
2804 if (n < 0)
2805 return n;
2806
2807 rem = (int)len - n;
2808 if (rem < 0)
2809 rem = 0;
2810
2811 if (fs_surface->get_label)
2812 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2813 else
2814 ret = snprintf(buf + n, rem, "<unknown>");
2815
2816 if (ret < 0)
2817 return n;
2818
2819 return n + ret;
2820}
2821
Alex Wu21858432012-04-01 20:13:08 +08002822static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002823black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002826create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002827 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002828 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002829{
2830 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002831 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002832
2833 surface = weston_surface_create(ec);
2834 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002835 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002836 return NULL;
2837 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002838 view = weston_view_create(surface);
2839 if (surface == NULL) {
2840 weston_log("no memory\n");
2841 weston_surface_destroy(surface);
2842 return NULL;
2843 }
Alex Wu4539b082012-03-01 12:57:46 +08002844
Alex Wu21858432012-04-01 20:13:08 +08002845 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002846 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002847 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002848 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002849 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002850 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002851 pixman_region32_fini(&surface->input);
2852 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002853
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002854 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002855 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856
2857 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002858}
2859
Philip Withnalled8f1a92013-11-25 18:01:43 +00002860static void
2861shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2862{
2863 struct weston_output *output = shsurf->fullscreen_output;
2864
Rafael Antognolli03b16592013-12-03 15:35:42 -02002865 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002866
2867 if (!shsurf->fullscreen.black_view)
2868 shsurf->fullscreen.black_view =
2869 create_black_surface(shsurf->surface->compositor,
2870 shsurf->surface,
2871 output->x, output->y,
2872 output->width,
2873 output->height);
2874
2875 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002876 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2877 weston_layer_entry_insert(&shsurf->view->layer_link,
2878 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002879 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2880 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002881
2882 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002883}
2884
Alex Wu4539b082012-03-01 12:57:46 +08002885/* Create black surface and append it to the associated fullscreen surface.
2886 * Handle size dismatch and positioning according to the method. */
2887static void
2888shell_configure_fullscreen(struct shell_surface *shsurf)
2889{
2890 struct weston_output *output = shsurf->fullscreen_output;
2891 struct weston_surface *surface = shsurf->surface;
2892 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002893 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002894 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002895
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002896 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2897 restore_output_mode(output);
2898
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002899 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002900 weston_layer_entry_remove(&shsurf->view->layer_link);
2901 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002902
Philip Withnalled8f1a92013-11-25 18:01:43 +00002903 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002904
Jason Ekstranda7af7042013-10-12 22:38:11 -05002905 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002906 &surf_width, &surf_height);
2907
Alex Wu4539b082012-03-01 12:57:46 +08002908 switch (shsurf->fullscreen.type) {
2909 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002910 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002911 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002912 break;
2913 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002914 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002915 if (output->width == surf_width &&
2916 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002917 weston_view_set_position(shsurf->view,
2918 output->x - surf_x,
2919 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002920 break;
2921 }
2922
Alex Wu4539b082012-03-01 12:57:46 +08002923 matrix = &shsurf->fullscreen.transform.matrix;
2924 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002925
Scott Moreau1bad5db2012-08-18 01:04:05 -06002926 output_aspect = (float) output->width /
2927 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 /* XXX: Use surf_width and surf_height here? */
2929 surface_aspect = (float) surface->width /
2930 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002931 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002932 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002933 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002934 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002935 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002936 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002937
Alex Wu4539b082012-03-01 12:57:46 +08002938 weston_matrix_scale(matrix, scale, scale, 1);
2939 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002941 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002942 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2943 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002944 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002945
Alex Wu4539b082012-03-01 12:57:46 +08002946 break;
2947 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002948 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002949 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002950 surf_width * surface->buffer_viewport.buffer.scale,
2951 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002952 shsurf->fullscreen.framerate};
2953
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002954 if (weston_output_mode_switch_to_temporary(output, &mode,
2955 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 weston_view_set_position(shsurf->view,
2957 output->x - surf_x,
2958 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002959 shsurf->fullscreen.black_view->surface->width = output->width;
2960 shsurf->fullscreen.black_view->surface->height = output->height;
2961 weston_view_set_position(shsurf->fullscreen.black_view,
2962 output->x - surf_x,
2963 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002964 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002965 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002966 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002967 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002969 }
Alex Wubd3354b2012-04-17 17:20:49 +08002970 }
Alex Wu4539b082012-03-01 12:57:46 +08002971 break;
2972 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002973 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002974 break;
2975 default:
2976 break;
2977 }
2978}
2979
Alex Wu4539b082012-03-01 12:57:46 +08002980static void
2981shell_map_fullscreen(struct shell_surface *shsurf)
2982{
Alex Wubd3354b2012-04-17 17:20:49 +08002983 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002984}
2985
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002986static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002987set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2988{
2989 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002990 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002991 shsurf->transient.x = x;
2992 shsurf->transient.y = y;
2993 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002994
2995 shell_surface_set_parent(shsurf, NULL);
2996
Pekka Paalanenb5026542014-11-12 15:09:24 +02002997 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002998 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002999}
3000
Kristian Høgsberg47792412014-05-04 13:47:06 -07003001static void
3002shell_interface_set_fullscreen(struct shell_surface *shsurf,
3003 uint32_t method,
3004 uint32_t framerate,
3005 struct weston_output *output)
3006{
3007 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003008 shsurf->next_state.fullscreen = true;
3009 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003010
3011 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003012}
3013
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003014static struct weston_output *
3015get_focused_output(struct weston_compositor *compositor)
3016{
3017 struct weston_seat *seat;
3018 struct weston_output *output = NULL;
3019
3020 wl_list_for_each(seat, &compositor->seat_list, link) {
3021 /* Priority has touch focus, then pointer and
3022 * then keyboard focus. We should probably have
3023 * three for loops and check frist for touch,
3024 * then for pointer, etc. but unless somebody has some
3025 * objections, I think this is sufficient. */
3026 if (seat->touch && seat->touch->focus)
3027 output = seat->touch->focus->output;
3028 else if (seat->pointer && seat->pointer->focus)
3029 output = seat->pointer->focus->output;
3030 else if (seat->keyboard && seat->keyboard->focus)
3031 output = seat->keyboard->focus->output;
3032
3033 if (output)
3034 break;
3035 }
3036
3037 return output;
3038}
3039
3040static void
3041shell_interface_set_maximized(struct shell_surface *shsurf)
3042{
3043 struct weston_output *output;
3044
3045 surface_clear_next_states(shsurf);
3046 shsurf->next_state.maximized = true;
3047 shsurf->state_changed = true;
3048 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3049
3050 if (!weston_surface_is_mapped(shsurf->surface))
3051 output = get_focused_output(shsurf->surface->compositor);
3052 else
3053 output = shsurf->surface->output;
3054
3055 shell_surface_set_output(shsurf, output);
3056 send_configure_for_surface(shsurf);
3057}
3058
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003059static int
3060shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3061{
Derek Foremancf7d95a2015-06-03 15:53:22 -05003062 return surface_move(shsurf, ws, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003063}
3064
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003065static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003066
3067static void
3068destroy_shell_seat(struct wl_listener *listener, void *data)
3069{
3070 struct shell_seat *shseat =
3071 container_of(listener,
3072 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003073 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074
3075 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003076 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003077 shseat->popup_grab.client = NULL;
3078
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003079 wl_list_for_each_safe(shsurf, next,
3080 &shseat->popup_grab.surfaces_list,
3081 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003082 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003083 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003084 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003085 }
3086
3087 wl_list_remove(&shseat->seat_destroy_listener.link);
3088 free(shseat);
3089}
3090
Jason Ekstrand024177c2014-04-21 19:42:58 -05003091static void
3092shell_seat_caps_changed(struct wl_listener *listener, void *data)
3093{
3094 struct shell_seat *seat;
3095
3096 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3097
3098 if (seat->seat->keyboard &&
3099 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3100 wl_signal_add(&seat->seat->keyboard->focus_signal,
3101 &seat->keyboard_focus_listener);
3102 } else if (!seat->seat->keyboard) {
3103 wl_list_init(&seat->keyboard_focus_listener.link);
3104 }
3105
3106 if (seat->seat->pointer &&
3107 wl_list_empty(&seat->pointer_focus_listener.link)) {
3108 wl_signal_add(&seat->seat->pointer->focus_signal,
3109 &seat->pointer_focus_listener);
3110 } else if (!seat->seat->pointer) {
3111 wl_list_init(&seat->pointer_focus_listener.link);
3112 }
3113}
3114
Giulio Camuffo5085a752013-03-25 21:42:45 +01003115static struct shell_seat *
3116create_shell_seat(struct weston_seat *seat)
3117{
3118 struct shell_seat *shseat;
3119
3120 shseat = calloc(1, sizeof *shseat);
3121 if (!shseat) {
3122 weston_log("no memory to allocate shell seat\n");
3123 return NULL;
3124 }
3125
3126 shseat->seat = seat;
3127 wl_list_init(&shseat->popup_grab.surfaces_list);
3128
3129 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3130 wl_signal_add(&seat->destroy_signal,
3131 &shseat->seat_destroy_listener);
3132
Jason Ekstrand024177c2014-04-21 19:42:58 -05003133 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3134 wl_list_init(&shseat->keyboard_focus_listener.link);
3135
3136 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3137 wl_list_init(&shseat->pointer_focus_listener.link);
3138
3139 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3140 wl_signal_add(&seat->updated_caps_signal,
3141 &shseat->caps_changed_listener);
3142 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3143
Giulio Camuffo5085a752013-03-25 21:42:45 +01003144 return shseat;
3145}
3146
3147static struct shell_seat *
3148get_shell_seat(struct weston_seat *seat)
3149{
3150 struct wl_listener *listener;
3151
3152 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003153 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003154
3155 return container_of(listener,
3156 struct shell_seat, seat_destroy_listener);
3157}
3158
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003159static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003160popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003161{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003162 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003163 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003164 struct shell_seat *shseat =
3165 container_of(grab, struct shell_seat, popup_grab.grab);
3166 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003167 wl_fixed_t sx, sy;
3168
Jason Ekstranda7af7042013-10-12 22:38:11 -05003169 view = weston_compositor_pick_view(pointer->seat->compositor,
3170 pointer->x, pointer->y,
3171 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003172
Jason Ekstranda7af7042013-10-12 22:38:11 -05003173 if (view && view->surface->resource &&
3174 wl_resource_get_client(view->surface->resource) == client) {
3175 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003176 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003177 weston_pointer_set_focus(pointer, NULL,
3178 wl_fixed_from_int(0),
3179 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003180 }
3181}
3182
3183static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003184popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3185 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003186{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003187 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003188 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003189 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003190
Jonas Ådahl61917c82014-08-11 22:44:02 +02003191 if (pointer->focus) {
3192 weston_view_from_global_fixed(pointer->focus, x, y,
3193 &pointer->sx, &pointer->sy);
3194 }
3195
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003196 weston_pointer_move(pointer, x, y);
3197
Neil Roberts96d790e2013-09-19 17:32:00 +01003198 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003199 weston_view_from_global_fixed(pointer->focus,
3200 pointer->x, pointer->y,
3201 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003202 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003203 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204}
3205
3206static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003207popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003208 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003209{
3210 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003211 struct shell_seat *shseat =
3212 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003213 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003214 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003215 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003216 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003217
Neil Roberts96d790e2013-09-19 17:32:00 +01003218 resource_list = &grab->pointer->focus_resource_list;
3219 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003220 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003221 wl_resource_for_each(resource, resource_list) {
3222 wl_pointer_send_button(resource, serial,
3223 time, button, state);
3224 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003225 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003226 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003227 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003228 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003229 }
3230
Daniel Stone4dbadb12012-05-30 16:31:51 +01003231 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003232 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003233}
3234
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003235static void
3236popup_grab_cancel(struct weston_pointer_grab *grab)
3237{
3238 popup_grab_end(grab->pointer);
3239}
3240
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003241static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003242 popup_grab_focus,
3243 popup_grab_motion,
3244 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003245 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003246};
3247
3248static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003249touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3250 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3251{
3252 struct wl_resource *resource;
3253 struct shell_seat *shseat =
3254 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3255 struct wl_display *display = shseat->seat->compositor->wl_display;
3256 uint32_t serial;
3257 struct wl_list *resource_list;
3258
3259 resource_list = &grab->touch->focus_resource_list;
3260 if (!wl_list_empty(resource_list)) {
3261 serial = wl_display_get_serial(display);
3262 wl_resource_for_each(resource, resource_list) {
3263 wl_touch_send_down(resource, serial, time,
3264 grab->touch->focus->surface->resource,
3265 touch_id, sx, sy);
3266 }
3267 }
3268}
3269
3270static void
3271touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3272{
3273 struct wl_resource *resource;
3274 struct shell_seat *shseat =
3275 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3276 struct wl_display *display = shseat->seat->compositor->wl_display;
3277 uint32_t serial;
3278 struct wl_list *resource_list;
3279
3280 resource_list = &grab->touch->focus_resource_list;
3281 if (!wl_list_empty(resource_list)) {
3282 serial = wl_display_get_serial(display);
3283 wl_resource_for_each(resource, resource_list) {
3284 wl_touch_send_up(resource, serial, time, touch_id);
3285 }
3286 }
3287}
3288
3289static void
3290touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3291 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3292{
3293 struct wl_resource *resource;
3294 struct wl_list *resource_list;
3295
3296 resource_list = &grab->touch->focus_resource_list;
3297 if (!wl_list_empty(resource_list)) {
3298 wl_resource_for_each(resource, resource_list) {
3299 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3300 }
3301 }
3302}
3303
3304static void
3305touch_popup_grab_frame(struct weston_touch_grab *grab)
3306{
3307}
3308
3309static void
3310touch_popup_grab_cancel(struct weston_touch_grab *grab)
3311{
3312 touch_popup_grab_end(grab->touch);
3313}
3314
3315static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3316 touch_popup_grab_down,
3317 touch_popup_grab_up,
3318 touch_popup_grab_motion,
3319 touch_popup_grab_frame,
3320 touch_popup_grab_cancel,
3321};
3322
3323static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003324shell_surface_send_popup_done(struct shell_surface *shsurf)
3325{
3326 if (shell_surface_is_wl_shell_surface(shsurf))
3327 wl_shell_surface_send_popup_done(shsurf->resource);
3328 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003329 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003330}
3331
3332static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003333popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003334{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003335 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003336 struct shell_seat *shseat =
3337 container_of(grab, struct shell_seat, popup_grab.grab);
3338 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003339 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003340
3341 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003342 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003343 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003344 shseat->popup_grab.grab.interface = NULL;
3345 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003346 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003347 wl_list_for_each_safe(shsurf, next,
3348 &shseat->popup_grab.surfaces_list,
3349 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003350 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003351 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003352 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003353 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003354 wl_list_init(&shseat->popup_grab.surfaces_list);
3355 }
3356}
3357
3358static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003359touch_popup_grab_end(struct weston_touch *touch)
3360{
3361 struct weston_touch_grab *grab = touch->grab;
3362 struct shell_seat *shseat =
3363 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3364 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003365 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003366
3367 if (touch->grab->interface == &touch_popup_grab_interface) {
3368 weston_touch_end_grab(grab->touch);
3369 shseat->popup_grab.client = NULL;
3370 shseat->popup_grab.touch_grab.interface = NULL;
3371 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3372 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003373 wl_list_for_each_safe(shsurf, next,
3374 &shseat->popup_grab.surfaces_list,
3375 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003376 shell_surface_send_popup_done(shsurf);
3377 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003378 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003379 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003380 wl_list_init(&shseat->popup_grab.surfaces_list);
3381 }
3382}
3383
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003384static struct shell_surface *
3385get_top_popup(struct shell_seat *shseat)
3386{
3387 struct shell_surface *shsurf;
3388
3389 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3390 return NULL;
3391 } else {
3392 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3393 struct shell_surface,
3394 popup.grab_link);
3395 return shsurf;
3396 }
3397}
3398
3399static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003400add_popup_grab(struct shell_surface *shsurf,
3401 struct shell_seat *shseat,
3402 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003403{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003404 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003405 struct shell_surface *parent, *top_surface;
3406
3407 parent = get_shell_surface(shsurf->parent);
3408 top_surface = get_top_popup(shseat);
3409 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003410 (!parent ||
3411 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003412 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003413 wl_resource_post_error(shsurf->owner_resource,
3414 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003415 "xdg_popup was not created on the "
3416 "topmost popup");
3417 return -1;
3418 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003419
3420 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003421 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003422 shseat->popup_grab.client =
3423 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003424
3425 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003426 shseat->popup_grab.grab.interface =
3427 &popup_grab_interface;
3428
3429 /* We must make sure here that this popup was opened
3430 * after a mouse press, and not just by moving around
3431 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003432 if (shseat->seat->pointer->button_count > 0)
3433 shseat->popup_grab.initial_up = 0;
3434 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003435 shseat->popup_grab.touch_grab.interface =
3436 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003437 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003438
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003439 wl_list_insert(&shseat->popup_grab.surfaces_list,
3440 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003441
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003442 if (type == POINTER) {
3443 weston_pointer_start_grab(seat->pointer,
3444 &shseat->popup_grab.grab);
3445 } else if (type == TOUCH) {
3446 weston_touch_start_grab(seat->touch,
3447 &shseat->popup_grab.touch_grab);
3448 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003449 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003450 wl_list_insert(&shseat->popup_grab.surfaces_list,
3451 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003452 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003453
3454 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003455}
3456
3457static void
3458remove_popup_grab(struct shell_surface *shsurf)
3459{
3460 struct shell_seat *shseat = shsurf->popup.shseat;
3461
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003462 if (shell_surface_is_xdg_popup(shsurf) &&
3463 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003464 wl_resource_post_error(shsurf->owner_resource,
3465 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003466 "xdg_popup was destroyed while it was "
3467 "not the topmost popup.");
3468 return;
3469 }
3470
Giulio Camuffo5085a752013-03-25 21:42:45 +01003471 wl_list_remove(&shsurf->popup.grab_link);
3472 wl_list_init(&shsurf->popup.grab_link);
3473 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003474 if (shseat->popup_grab.type == POINTER) {
3475 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3476 shseat->popup_grab.grab.interface = NULL;
3477 } else if (shseat->popup_grab.type == TOUCH) {
3478 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3479 shseat->popup_grab.touch_grab.interface = NULL;
3480 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003481 }
3482}
3483
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003484static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003485shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003486{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003487 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003488 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003489
Jason Ekstranda7af7042013-10-12 22:38:11 -05003490 shsurf->surface->output = parent_view->output;
3491 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003492
Jason Ekstranda7af7042013-10-12 22:38:11 -05003493 weston_view_set_transform_parent(shsurf->view, parent_view);
3494 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3495 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003496
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003497 if (shseat->seat->pointer &&
3498 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003499 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3500 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003501 } else if (shseat->seat->touch &&
3502 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003503 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3504 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003505 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003506 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003507 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003508 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003509
3510 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003511}
3512
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003513static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003514 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003515 shell_surface_move,
3516 shell_surface_resize,
3517 shell_surface_set_toplevel,
3518 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003519 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003520 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003521 shell_surface_set_maximized,
3522 shell_surface_set_title,
3523 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003524};
3525
3526static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003527destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003528{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003529 struct shell_surface *child, *next;
3530
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003531 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3532
Giulio Camuffo5085a752013-03-25 21:42:45 +01003533 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3534 remove_popup_grab(shsurf);
3535 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003536
Alex Wubd3354b2012-04-17 17:20:49 +08003537 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003538 shell_surface_is_top_fullscreen(shsurf))
3539 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003540
Jason Ekstranda7af7042013-10-12 22:38:11 -05003541 if (shsurf->fullscreen.black_view)
3542 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003543
Alex Wubd3354b2012-04-17 17:20:49 +08003544 /* As destroy_resource() use wl_list_for_each_safe(),
3545 * we can always remove the listener.
3546 */
3547 wl_list_remove(&shsurf->surface_destroy_listener.link);
3548 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003549 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003550 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003551
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 weston_view_destroy(shsurf->view);
3553
Philip Withnall648a4dd2013-11-25 18:01:44 +00003554 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003555 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3556 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003557
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003558 wl_list_remove(&shsurf->link);
3559 free(shsurf);
3560}
3561
3562static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003563shell_destroy_shell_surface(struct wl_resource *resource)
3564{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003565 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003566
Bryan Caina46b9462014-04-04 17:41:24 -05003567 if (!wl_list_empty(&shsurf->popup.grab_link))
3568 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003569 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003570 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003571}
3572
3573static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003574shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003575{
3576 struct shell_surface *shsurf = container_of(listener,
3577 struct shell_surface,
3578 surface_destroy_listener);
3579
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003580 if (shsurf->resource)
3581 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003582
3583 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003584}
3585
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003586static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003587fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003588{
3589 struct shell_surface *shsurf = data;
3590
3591 weston_surface_destroy(shsurf->surface);
3592}
3593
3594static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003595fade_out_done(struct weston_view_animation *animation, void *data)
3596{
3597 struct shell_surface *shsurf = data;
3598 struct wl_event_loop *loop;
3599
3600 loop = wl_display_get_event_loop(
3601 shsurf->surface->compositor->wl_display);
3602
3603 if (!shsurf->destroying) {
3604 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3605 shsurf->destroying = true;
3606 }
3607}
3608
3609static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003610handle_resource_destroy(struct wl_listener *listener, void *data)
3611{
3612 struct shell_surface *shsurf =
3613 container_of(listener, struct shell_surface,
3614 resource_destroy_listener);
3615
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003616 if (!weston_surface_is_mapped(shsurf->surface))
3617 return;
3618
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003619 shsurf->surface->ref_count++;
3620
3621 pixman_region32_fini(&shsurf->surface->pending.input);
3622 pixman_region32_init(&shsurf->surface->pending.input);
3623 pixman_region32_fini(&shsurf->surface->input);
3624 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003625 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3626 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3627 fade_out_done, shsurf);
3628 } else {
3629 weston_surface_destroy(shsurf->surface);
3630 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003631}
3632
3633static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003634shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003635
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003636struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003637get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003638{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003639 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003640 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003641 else
3642 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003643}
3644
Rafael Antognollie2a34552013-12-03 15:35:45 -02003645static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003646create_common_surface(struct shell_client *owner, void *shell,
3647 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003648 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003649{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003650 struct shell_surface *shsurf;
3651
Pekka Paalanen50b67472014-10-01 15:02:41 +03003652 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003653
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003654 shsurf = calloc(1, sizeof *shsurf);
3655 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003656 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003657 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003658 }
3659
Jason Ekstranda7af7042013-10-12 22:38:11 -05003660 shsurf->view = weston_view_create(surface);
3661 if (!shsurf->view) {
3662 weston_log("no memory to allocate shell surface\n");
3663 free(shsurf);
3664 return NULL;
3665 }
3666
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003667 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003668 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003669 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003670
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003671 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3672 wl_resource_add_destroy_listener(surface->resource,
3673 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003674 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003675
Tiago Vignattibc052c92012-04-19 16:18:18 +03003676 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003677 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003678 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003679 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003680 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003681 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003682 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3683 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003684 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003685 wl_list_init(&shsurf->fullscreen.transform.link);
3686
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003687 shsurf->output = get_default_output(shsurf->shell->compositor);
3688
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003689 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003690 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003691 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003692 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003693
3694 /* init link so its safe to always remove it in destroy_shell_surface */
3695 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003696 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003697
Pekka Paalanen460099f2012-01-20 16:48:25 +02003698 /* empty when not in use */
3699 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003700 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003701
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003702 wl_list_init(&shsurf->workspace_transform.link);
3703
Philip Withnall648a4dd2013-11-25 18:01:44 +00003704 wl_list_init(&shsurf->children_link);
3705 wl_list_init(&shsurf->children_list);
3706 shsurf->parent = NULL;
3707
Pekka Paalanenb5026542014-11-12 15:09:24 +02003708 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003709
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003710 shsurf->client = client;
3711
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003712 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003713}
3714
Rafael Antognollie2a34552013-12-03 15:35:45 -02003715static struct shell_surface *
3716create_shell_surface(void *shell, struct weston_surface *surface,
3717 const struct weston_shell_client *client)
3718{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003719 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003720}
3721
Jason Ekstranda7af7042013-10-12 22:38:11 -05003722static struct weston_view *
3723get_primary_view(void *shell, struct shell_surface *shsurf)
3724{
3725 return shsurf->view;
3726}
3727
Tiago Vignattibc052c92012-04-19 16:18:18 +03003728static void
3729shell_get_shell_surface(struct wl_client *client,
3730 struct wl_resource *resource,
3731 uint32_t id,
3732 struct wl_resource *surface_resource)
3733{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003734 struct weston_surface *surface =
3735 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003736 struct shell_client *sc = wl_resource_get_user_data(resource);
3737 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003738 struct shell_surface *shsurf;
3739
Pekka Paalanen50b67472014-10-01 15:02:41 +03003740 if (weston_surface_set_role(surface, "wl_shell_surface",
3741 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003742 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003743
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003744 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003745 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003746 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003747 return;
3748 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003749
Jason Ekstranda85118c2013-06-27 20:17:02 -05003750 shsurf->resource =
3751 wl_resource_create(client,
3752 &wl_shell_surface_interface, 1, id);
3753 wl_resource_set_implementation(shsurf->resource,
3754 &shell_surface_implementation,
3755 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003756 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003757}
3758
Rafael Antognollie2a34552013-12-03 15:35:45 -02003759static bool
3760shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3761{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003762 /* A shell surface without a resource is created from xwayland
3763 * and is considered a wl_shell surface for now. */
3764
3765 return shsurf->resource == NULL ||
3766 wl_resource_instance_of(shsurf->resource,
3767 &wl_shell_surface_interface,
3768 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003769}
3770
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003771static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003772 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003773};
3774
Rafael Antognollie2a34552013-12-03 15:35:45 -02003775/****************************
3776 * xdg-shell implementation */
3777
3778static void
3779xdg_surface_destroy(struct wl_client *client,
3780 struct wl_resource *resource)
3781{
3782 wl_resource_destroy(resource);
3783}
3784
3785static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003786xdg_surface_set_parent(struct wl_client *client,
3787 struct wl_resource *resource,
3788 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003789{
3790 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003791 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003792
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003793 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003794 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003795 shell_surface_set_parent(shsurf, parent->surface);
3796 } else {
3797 shell_surface_set_parent(shsurf, NULL);
3798 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003799}
3800
3801static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003802xdg_surface_set_app_id(struct wl_client *client,
3803 struct wl_resource *resource,
3804 const char *app_id)
3805{
3806 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3807
3808 free(shsurf->class);
3809 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003810 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003811}
3812
3813static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003814xdg_surface_show_window_menu(struct wl_client *client,
3815 struct wl_resource *surface_resource,
3816 struct wl_resource *seat_resource,
3817 uint32_t serial,
3818 int32_t x,
3819 int32_t y)
3820{
3821 /* TODO */
3822}
3823
3824static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003825xdg_surface_set_title(struct wl_client *client,
3826 struct wl_resource *resource, const char *title)
3827{
3828 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3829
3830 set_title(shsurf, title);
3831}
3832
3833static void
3834xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3835 struct wl_resource *seat_resource, uint32_t serial)
3836{
3837 common_surface_move(resource, seat_resource, serial);
3838}
3839
3840static void
3841xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3842 struct wl_resource *seat_resource, uint32_t serial,
3843 uint32_t edges)
3844{
3845 common_surface_resize(resource, seat_resource, serial, edges);
3846}
3847
3848static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003849xdg_surface_ack_configure(struct wl_client *client,
3850 struct wl_resource *resource,
3851 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003852{
3853 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3854
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003855 if (shsurf->state_requested) {
3856 shsurf->next_state = shsurf->requested_state;
3857 shsurf->state_changed = true;
3858 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003859 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003860}
3861
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003862static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003863xdg_surface_set_window_geometry(struct wl_client *client,
3864 struct wl_resource *resource,
3865 int32_t x,
3866 int32_t y,
3867 int32_t width,
3868 int32_t height)
3869{
3870 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3871
3872 set_window_geometry(shsurf, x, y, width, height);
3873}
3874
3875static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003876xdg_surface_set_maximized(struct wl_client *client,
3877 struct wl_resource *resource)
3878{
3879 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003880 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003881
3882 shsurf->state_requested = true;
3883 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003884
3885 if (!weston_surface_is_mapped(shsurf->surface))
3886 output = get_focused_output(shsurf->surface->compositor);
3887 else
3888 output = shsurf->surface->output;
3889
3890 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003891 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003892}
3893
3894static void
3895xdg_surface_unset_maximized(struct wl_client *client,
3896 struct wl_resource *resource)
3897{
3898 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3899
3900 shsurf->state_requested = true;
3901 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003902 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003903}
3904
3905static void
3906xdg_surface_set_fullscreen(struct wl_client *client,
3907 struct wl_resource *resource,
3908 struct wl_resource *output_resource)
3909{
3910 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3911 struct weston_output *output;
3912
3913 shsurf->state_requested = true;
3914 shsurf->requested_state.fullscreen = true;
3915
3916 if (output_resource)
3917 output = wl_resource_get_user_data(output_resource);
3918 else
3919 output = NULL;
3920
Marek Chalupa052917a2014-09-02 11:35:12 +02003921 /* handle clients launching in fullscreen */
3922 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3923 /* Set the output to the one that has focus currently. */
3924 assert(shsurf->surface);
3925 output = get_focused_output(shsurf->surface->compositor);
3926 }
3927
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003928 shell_surface_set_output(shsurf, output);
3929 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003930
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003931 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003932}
3933
3934static void
3935xdg_surface_unset_fullscreen(struct wl_client *client,
3936 struct wl_resource *resource)
3937{
3938 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3939
3940 shsurf->state_requested = true;
3941 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003942 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003943}
3944
3945static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003946xdg_surface_set_minimized(struct wl_client *client,
3947 struct wl_resource *resource)
3948{
3949 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3950
3951 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3952 return;
3953
3954 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003955 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003956}
3957
Rafael Antognollie2a34552013-12-03 15:35:45 -02003958static const struct xdg_surface_interface xdg_surface_implementation = {
3959 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003960 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003961 xdg_surface_set_title,
3962 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003963 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003964 xdg_surface_move,
3965 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003966 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003967 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003968 xdg_surface_set_maximized,
3969 xdg_surface_unset_maximized,
3970 xdg_surface_set_fullscreen,
3971 xdg_surface_unset_fullscreen,
3972 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003973};
3974
3975static void
3976xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003977 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003978{
3979 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003980 uint32_t *s;
3981 struct wl_array states;
3982 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003983
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003984 assert(shsurf);
3985
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003986 if (!shsurf->resource)
3987 return;
3988
3989 wl_array_init(&states);
3990 if (shsurf->requested_state.fullscreen) {
3991 s = wl_array_add(&states, sizeof *s);
3992 *s = XDG_SURFACE_STATE_FULLSCREEN;
3993 } else if (shsurf->requested_state.maximized) {
3994 s = wl_array_add(&states, sizeof *s);
3995 *s = XDG_SURFACE_STATE_MAXIMIZED;
3996 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003997 if (shsurf->resize_edges != 0) {
3998 s = wl_array_add(&states, sizeof *s);
3999 *s = XDG_SURFACE_STATE_RESIZING;
4000 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004001 if (shsurf->focus_count > 0) {
4002 s = wl_array_add(&states, sizeof *s);
4003 *s = XDG_SURFACE_STATE_ACTIVATED;
4004 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004005
4006 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4007 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4008
4009 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004010}
4011
4012static const struct weston_shell_client xdg_client = {
4013 xdg_send_configure
4014};
4015
4016static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004017xdg_shell_destroy(struct wl_client *client,
4018 struct wl_resource *resource)
4019{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004020 struct shell_client *sc = wl_resource_get_user_data(resource);
4021 struct wl_resource *shsurf_resource;
4022 struct shell_surface *shsurf;
4023
4024 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4025 shsurf = wl_resource_get_user_data(shsurf_resource);
4026 if (shsurf->owner_resource == resource) {
4027 wl_resource_post_error(
4028 resource,
4029 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4030 "not all child surface objects destroyed");
4031 return;
4032 }
4033 }
4034
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004035 wl_resource_destroy(resource);
4036}
4037
4038static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004039xdg_use_unstable_version(struct wl_client *client,
4040 struct wl_resource *resource,
4041 int32_t version)
4042{
4043 if (version > 1) {
4044 wl_resource_post_error(resource,
4045 1,
4046 "xdg-shell:: version not implemented yet.");
4047 return;
4048 }
4049}
4050
4051static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004052create_xdg_surface(struct shell_client *owner, void *shell,
4053 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004054 const struct weston_shell_client *client)
4055{
4056 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004057
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004058 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004059 if (!shsurf)
4060 return NULL;
4061
Pekka Paalanenb5026542014-11-12 15:09:24 +02004062 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004063
4064 return shsurf;
4065}
4066
4067static void
4068xdg_get_xdg_surface(struct wl_client *client,
4069 struct wl_resource *resource,
4070 uint32_t id,
4071 struct wl_resource *surface_resource)
4072{
4073 struct weston_surface *surface =
4074 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004075 struct shell_client *sc = wl_resource_get_user_data(resource);
4076 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004077 struct shell_surface *shsurf;
4078
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004079 shsurf = get_shell_surface(surface);
4080 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4081 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4082 "This wl_surface is already an "
4083 "xdg_surface");
4084 return;
4085 }
4086
Pekka Paalanen50b67472014-10-01 15:02:41 +03004087 if (weston_surface_set_role(surface, "xdg_surface",
4088 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004089 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004090
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004091 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004092 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004093 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004094 return;
4095 }
4096
4097 shsurf->resource =
4098 wl_resource_create(client,
4099 &xdg_surface_interface, 1, id);
4100 wl_resource_set_implementation(shsurf->resource,
4101 &xdg_surface_implementation,
4102 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004103 shsurf->owner_resource = resource;
4104 wl_list_insert(&sc->surface_list,
4105 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004106}
4107
4108static bool
4109shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4110{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004111 return shsurf->resource &&
4112 wl_resource_instance_of(shsurf->resource,
4113 &xdg_surface_interface,
4114 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004115}
4116
4117/* xdg-popup implementation */
4118
4119static void
4120xdg_popup_destroy(struct wl_client *client,
4121 struct wl_resource *resource)
4122{
4123 wl_resource_destroy(resource);
4124}
4125
Rafael Antognollie2a34552013-12-03 15:35:45 -02004126static const struct xdg_popup_interface xdg_popup_implementation = {
4127 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004128};
4129
4130static void
4131xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004132 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004133{
4134}
4135
4136static const struct weston_shell_client xdg_popup_client = {
4137 xdg_popup_send_configure
4138};
4139
4140static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004141create_xdg_popup(struct shell_client *owner, void *shell,
4142 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004143 const struct weston_shell_client *client,
4144 struct weston_surface *parent,
4145 struct shell_seat *seat,
4146 uint32_t serial,
4147 int32_t x, int32_t y)
4148{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004149 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004150
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004151 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004152 if (!shsurf)
4153 return NULL;
4154
Pekka Paalanenb5026542014-11-12 15:09:24 +02004155 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004156 shsurf->popup.shseat = seat;
4157 shsurf->popup.serial = serial;
4158 shsurf->popup.x = x;
4159 shsurf->popup.y = y;
4160 shell_surface_set_parent(shsurf, parent);
4161
4162 return shsurf;
4163}
4164
4165static void
4166xdg_get_xdg_popup(struct wl_client *client,
4167 struct wl_resource *resource,
4168 uint32_t id,
4169 struct wl_resource *surface_resource,
4170 struct wl_resource *parent_resource,
4171 struct wl_resource *seat_resource,
4172 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004173 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004174{
4175 struct weston_surface *surface =
4176 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004177 struct shell_client *sc = wl_resource_get_user_data(resource);
4178 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004179 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004180 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004181 struct weston_surface *parent;
4182 struct shell_seat *seat;
4183
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004184 shsurf = get_shell_surface(surface);
4185 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4186 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4187 "This wl_surface is already an "
4188 "xdg_popup");
4189 return;
4190 }
4191
Pekka Paalanen50b67472014-10-01 15:02:41 +03004192 if (weston_surface_set_role(surface, "xdg_popup",
4193 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004194 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004195
4196 if (!parent_resource) {
4197 wl_resource_post_error(surface_resource,
4198 WL_DISPLAY_ERROR_INVALID_OBJECT,
4199 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004200 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004201 }
4202
4203 parent = wl_resource_get_user_data(parent_resource);
4204 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4205
Jonas Ådahlee45a552015-03-18 16:37:47 +08004206 /* Verify that we are creating the top most popup when mapping,
4207 * as it's not until then we know whether it was mapped as most
4208 * top level or not. */
4209
4210 parent_shsurf = get_shell_surface(parent);
4211 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4212 !shell_surface_is_xdg_surface(parent_shsurf)) {
4213 wl_resource_post_error(resource,
4214 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4215 "xdg_popup parent was invalid");
4216 return;
4217 }
4218
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004219 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004220 parent, seat, serial, x, y);
4221 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004222 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004223 return;
4224 }
4225
4226 shsurf->resource =
4227 wl_resource_create(client,
4228 &xdg_popup_interface, 1, id);
4229 wl_resource_set_implementation(shsurf->resource,
4230 &xdg_popup_implementation,
4231 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004232 shsurf->owner_resource = resource;
4233 wl_list_insert(&sc->surface_list,
4234 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004235}
4236
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004237static void
4238xdg_pong(struct wl_client *client,
4239 struct wl_resource *resource, uint32_t serial)
4240{
4241 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004242
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004243 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004244}
4245
Rafael Antognollie2a34552013-12-03 15:35:45 -02004246static bool
4247shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4248{
4249 return wl_resource_instance_of(shsurf->resource,
4250 &xdg_popup_interface,
4251 &xdg_popup_implementation);
4252}
4253
4254static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004255 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004256 xdg_use_unstable_version,
4257 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004258 xdg_get_xdg_popup,
4259 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004260};
4261
4262static int
4263xdg_shell_unversioned_dispatch(const void *implementation,
4264 void *_target, uint32_t opcode,
4265 const struct wl_message *message,
4266 union wl_argument *args)
4267{
4268 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004269 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004270
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004271 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004272 wl_resource_post_error(resource,
4273 WL_DISPLAY_ERROR_INVALID_OBJECT,
4274 "must call use_unstable_version first");
4275 return 0;
4276 }
4277
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004278#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004279
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004280 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4281 "shell implementation doesn't match protocol version");
4282
Rafael Antognollie2a34552013-12-03 15:35:45 -02004283 if (args[0].i != XDG_SERVER_VERSION) {
4284 wl_resource_post_error(resource,
4285 WL_DISPLAY_ERROR_INVALID_OBJECT,
4286 "incompatible version, server is %d "
4287 "client wants %d",
4288 XDG_SERVER_VERSION, args[0].i);
4289 return 0;
4290 }
4291
4292 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004293 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004294
4295 return 1;
4296}
4297
4298/* end of xdg-shell implementation */
4299/***********************************/
4300
Kristian Høgsberg07937562011-04-12 17:25:42 -04004301static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004302shell_fade(struct desktop_shell *shell, enum fade_type type);
4303
Pekka Paalanen77346a62011-11-30 16:26:35 +02004304static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004305configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004306{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004308
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004309 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004310 if (v->output == ev->output && v != ev) {
4311 weston_view_unmap(v);
4312 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004313 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004314 }
4315 }
4316
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004317 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004318
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004319 if (wl_list_empty(&ev->layer_link.link)) {
4320 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004321 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004322 }
4323}
4324
Pekka Paalanen8274d902014-08-06 19:36:51 +03004325static int
4326background_get_label(struct weston_surface *surface, char *buf, size_t len)
4327{
4328 return snprintf(buf, len, "background for output %s",
4329 surface->output->name);
4330}
4331
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004332static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004333background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004334{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004335 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004336 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004337
Jason Ekstranda7af7042013-10-12 22:38:11 -05004338 view = container_of(es->views.next, struct weston_view, surface_link);
4339
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004340 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004341}
4342
4343static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004344desktop_shell_set_background(struct wl_client *client,
4345 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004346 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004347 struct wl_resource *surface_resource)
4348{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004349 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004350 struct weston_surface *surface =
4351 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004352 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004353
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004354 if (surface->configure) {
4355 wl_resource_post_error(surface_resource,
4356 WL_DISPLAY_ERROR_INVALID_OBJECT,
4357 "surface role already assigned");
4358 return;
4359 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004360
Jason Ekstranda7af7042013-10-12 22:38:11 -05004361 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4362 weston_view_destroy(view);
4363 view = weston_view_create(surface);
4364
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004365 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004366 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004367 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004368 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004369 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004370 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004371 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004372 surface->output->width,
4373 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004374}
4375
Pekka Paalanen8274d902014-08-06 19:36:51 +03004376static int
4377panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4378{
4379 return snprintf(buf, len, "panel for output %s",
4380 surface->output->name);
4381}
4382
Kristian Høgsberg75840622011-09-06 13:48:16 -04004383static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004384panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004385{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004386 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004387 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004388
Jason Ekstranda7af7042013-10-12 22:38:11 -05004389 view = container_of(es->views.next, struct weston_view, surface_link);
4390
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004391 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004392}
4393
4394static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004395desktop_shell_set_panel(struct wl_client *client,
4396 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004397 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004398 struct wl_resource *surface_resource)
4399{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004400 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004401 struct weston_surface *surface =
4402 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004404
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004405 if (surface->configure) {
4406 wl_resource_post_error(surface_resource,
4407 WL_DISPLAY_ERROR_INVALID_OBJECT,
4408 "surface role already assigned");
4409 return;
4410 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004411
Jason Ekstranda7af7042013-10-12 22:38:11 -05004412 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4413 weston_view_destroy(view);
4414 view = weston_view_create(surface);
4415
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004416 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004417 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004418 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004419 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004421 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004422 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004423 surface->output->width,
4424 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004425}
4426
Pekka Paalanen8274d902014-08-06 19:36:51 +03004427static int
4428lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4429{
4430 return snprintf(buf, len, "lock window");
4431}
4432
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004433static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004434lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004435{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004436 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004437 struct weston_view *view;
4438
4439 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004440
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004441 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004442 return;
4443
Jason Ekstranda7af7042013-10-12 22:38:11 -05004444 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004445
4446 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004447 weston_layer_entry_insert(&shell->lock_layer.view_list,
4448 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004450 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004451 }
4452}
4453
4454static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004455handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004456{
Tiago Vignattibe143262012-04-16 17:31:41 +03004457 struct desktop_shell *shell =
4458 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004459
Martin Minarik6d118362012-06-07 18:01:59 +02004460 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004461 shell->lock_surface = NULL;
4462}
4463
4464static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004465desktop_shell_set_lock_surface(struct wl_client *client,
4466 struct wl_resource *resource,
4467 struct wl_resource *surface_resource)
4468{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004469 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004470 struct weston_surface *surface =
4471 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004472
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004473 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004474
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004475 if (!shell->locked)
4476 return;
4477
Pekka Paalanen98262232011-12-01 10:42:22 +02004478 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004479
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004480 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004481 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004482 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004483
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004484 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004485 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004486 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004487 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004488}
4489
4490static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004491resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004492{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004493 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004494
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004495 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004496 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004497 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004498 &shell->input_panel_layer.link);
4499 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004500 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004501 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004502 wl_list_insert(&shell->compositor->cursor_layer.link,
4503 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004504 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004505 wl_list_insert(&shell->fullscreen_layer.link,
4506 &shell->panel_layer.link);
4507 wl_list_insert(&shell->panel_layer.link,
4508 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004509
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004510 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004511
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004512 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004513 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004514 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004515}
4516
4517static void
4518desktop_shell_unlock(struct wl_client *client,
4519 struct wl_resource *resource)
4520{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004521 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004522
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004523 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004524
4525 if (shell->locked)
4526 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004527}
4528
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004529static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004530desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004531 struct wl_resource *resource,
4532 struct wl_resource *surface_resource)
4533{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004534 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004535
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004536 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004537 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004538}
4539
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004540static void
4541desktop_shell_desktop_ready(struct wl_client *client,
4542 struct wl_resource *resource)
4543{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004544 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004545
4546 shell_fade_startup(shell);
4547}
4548
Jonny Lamb765760d2014-08-20 15:53:19 +02004549static void
4550desktop_shell_set_panel_position(struct wl_client *client,
4551 struct wl_resource *resource,
4552 uint32_t position)
4553{
4554 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4555
4556 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4557 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4558 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4559 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4560 wl_resource_post_error(resource,
4561 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4562 "bad position argument");
4563 return;
4564 }
4565
4566 shell->panel_position = position;
4567}
4568
Kristian Høgsberg75840622011-09-06 13:48:16 -04004569static const struct desktop_shell_interface desktop_shell_implementation = {
4570 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004571 desktop_shell_set_panel,
4572 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004573 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004574 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004575 desktop_shell_desktop_ready,
4576 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004577};
4578
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004579static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004580get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004581{
4582 struct shell_surface *shsurf;
4583
4584 shsurf = get_shell_surface(surface);
4585 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004586 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004587 return shsurf->type;
4588}
4589
Kristian Høgsberg75840622011-09-06 13:48:16 -04004590static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004591move_binding(struct weston_pointer *pointer, uint32_t time,
4592 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004593{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004594 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004595 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004596 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004597
Derek Foreman8ae2db52015-07-15 13:00:36 -05004598 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004599 return;
4600
Derek Foreman8ae2db52015-07-15 13:00:36 -05004601 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004602
Pekka Paalanen01388e22013-04-25 13:57:44 +03004603 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004604 if (surface == NULL)
4605 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004606
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004607 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004608 if (shsurf == NULL || shsurf->state.fullscreen ||
4609 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004610 return;
4611
Derek Foreman8ae2db52015-07-15 13:00:36 -05004612 surface_move(shsurf, pointer->seat, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004613}
4614
4615static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004616maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4617 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004618{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004619 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004620 struct weston_surface *surface;
4621 struct shell_surface *shsurf;
4622
4623 surface = weston_surface_get_main_surface(focus);
4624 if (surface == NULL)
4625 return;
4626
4627 shsurf = get_shell_surface(surface);
4628 if (shsurf == NULL)
4629 return;
4630
4631 if (!shell_surface_is_xdg_surface(shsurf))
4632 return;
4633
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004634 shsurf->state_requested = true;
4635 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004636 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004637}
4638
4639static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004640fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4641 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004642{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004643 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004644 struct weston_surface *surface;
4645 struct shell_surface *shsurf;
4646
4647 surface = weston_surface_get_main_surface(focus);
4648 if (surface == NULL)
4649 return;
4650
4651 shsurf = get_shell_surface(surface);
4652 if (shsurf == NULL)
4653 return;
4654
4655 if (!shell_surface_is_xdg_surface(shsurf))
4656 return;
4657
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004658 shsurf->state_requested = true;
4659 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004660 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004661 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004662}
4663
4664static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004665touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004666{
Derek Foreman362656b2014-09-04 10:23:05 -05004667 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004668 struct weston_surface *surface;
4669 struct shell_surface *shsurf;
4670
Derek Foreman8ae2db52015-07-15 13:00:36 -05004671 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004672 return;
4673
Derek Foreman8ae2db52015-07-15 13:00:36 -05004674 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004675 surface = weston_surface_get_main_surface(focus);
4676 if (surface == NULL)
4677 return;
4678
4679 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004680 if (shsurf == NULL || shsurf->state.fullscreen ||
4681 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004682 return;
4683
Derek Foreman8ae2db52015-07-15 13:00:36 -05004684 surface_touch_move(shsurf, touch->seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004685}
4686
4687static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004688resize_binding(struct weston_pointer *pointer, uint32_t time,
4689 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004690{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004691 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004692 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004693 uint32_t edges = 0;
4694 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004695 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004696
Derek Foreman8ae2db52015-07-15 13:00:36 -05004697 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004698 return;
4699
Derek Foreman8ae2db52015-07-15 13:00:36 -05004700 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004701
Pekka Paalanen01388e22013-04-25 13:57:44 +03004702 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004703 if (surface == NULL)
4704 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004705
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004706 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004707 if (shsurf == NULL || shsurf->state.fullscreen ||
4708 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004709 return;
4710
Jason Ekstranda7af7042013-10-12 22:38:11 -05004711 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004712 wl_fixed_to_int(pointer->grab_x),
4713 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004714 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004715
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004716 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004717 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004718 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004719 edges |= 0;
4720 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004721 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004722
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004723 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004724 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004725 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004726 edges |= 0;
4727 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004728 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004729
Derek Foreman8ae2db52015-07-15 13:00:36 -05004730 surface_resize(shsurf, pointer->seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004731}
4732
4733static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004734surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4735 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004736{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004737 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004738 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004739 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004740 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004741
Pekka Paalanen01388e22013-04-25 13:57:44 +03004742 /* XXX: broken for windows containing sub-surfaces */
4743 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004744 if (surface == NULL)
4745 return;
4746
4747 shsurf = get_shell_surface(surface);
4748 if (!shsurf)
4749 return;
4750
Jason Ekstranda7af7042013-10-12 22:38:11 -05004751 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004752
Jason Ekstranda7af7042013-10-12 22:38:11 -05004753 if (shsurf->view->alpha > 1.0)
4754 shsurf->view->alpha = 1.0;
4755 if (shsurf->view->alpha < step)
4756 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004757
Jason Ekstranda7af7042013-10-12 22:38:11 -05004758 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004759 weston_surface_damage(surface);
4760}
4761
4762static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004763do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004764 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004765{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004766 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004767 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004768 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004769
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004770 if (!seat->pointer) {
4771 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4772 return;
4773 }
4774
Scott Moreauccbf29d2012-02-22 14:21:41 -07004775 wl_list_for_each(output, &compositor->output_list, link) {
4776 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004777 wl_fixed_to_double(seat->pointer->x),
4778 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004779 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004780 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004781 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004782 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004783 increment = -output->zoom.increment;
4784 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004785 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004786 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004787 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004788 else
4789 increment = 0;
4790
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004791 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004792
Scott Moreaue6603982012-06-11 13:07:51 -06004793 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004794 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004795 else if (output->zoom.level > output->zoom.max_level)
4796 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004797 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004798 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004799 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004800
Scott Moreaue6603982012-06-11 13:07:51 -06004801 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004802
Jason Ekstranda7af7042013-10-12 22:38:11 -05004803 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004804 }
4805 }
4806}
4807
Scott Moreauccbf29d2012-02-22 14:21:41 -07004808static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004809zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4810 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004811{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004812 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004813}
4814
4815static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004816zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4817 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004818{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004819 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004820}
4821
4822static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004823terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4824 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004825{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004826 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004827
Daniel Stone325fc2d2012-05-30 16:31:58 +01004828 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004829}
4830
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004831static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004832rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4833 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004834{
4835 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004836 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004837 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004838 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004839 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004840
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004841 weston_pointer_move(pointer, x, y);
4842
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004843 if (!shsurf)
4844 return;
4845
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004846 cx = 0.5f * shsurf->surface->width;
4847 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004848
Daniel Stone37816df2012-05-16 18:45:18 +01004849 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4850 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004851 r = sqrtf(dx * dx + dy * dy);
4852
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004853 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004855
4856 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004857 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004858 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004859
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004860 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004861 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004862
4863 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004864 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004865 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004866 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004867 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004868
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004869 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004870 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004871 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004872 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004873 wl_list_init(&shsurf->rotation.transform.link);
4874 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004875 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004876 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004877
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004878 /* We need to adjust the position of the surface
4879 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004880 cposx = shsurf->view->geometry.x + cx;
4881 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004882 dposx = rotate->center.x - cposx;
4883 dposy = rotate->center.y - cposy;
4884 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 weston_view_set_position(shsurf->view,
4886 shsurf->view->geometry.x + dposx,
4887 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004888 }
4889
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004890 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004891 * lazily applies the damage due to rotation update.
4892 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004893 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004894}
4895
4896static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004897rotate_grab_button(struct weston_pointer_grab *grab,
4898 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004899{
4900 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004901 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004902 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004903 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004904 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004905
Daniel Stone4dbadb12012-05-30 16:31:51 +01004906 if (pointer->button_count == 0 &&
4907 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004908 if (shsurf)
4909 weston_matrix_multiply(&shsurf->rotation.rotation,
4910 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004911 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004912 free(rotate);
4913 }
4914}
4915
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004916static void
4917rotate_grab_cancel(struct weston_pointer_grab *grab)
4918{
4919 struct rotate_grab *rotate =
4920 container_of(grab, struct rotate_grab, base.grab);
4921
4922 shell_grab_end(&rotate->base);
4923 free(rotate);
4924}
4925
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004926static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004927 noop_grab_focus,
4928 rotate_grab_motion,
4929 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004930 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004931};
4932
4933static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004934surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004936 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004937 float dx, dy;
4938 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004939
Pekka Paalanen460099f2012-01-20 16:48:25 +02004940 rotate = malloc(sizeof *rotate);
4941 if (!rotate)
4942 return;
4943
Jason Ekstranda7af7042013-10-12 22:38:11 -05004944 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004945 surface->surface->width * 0.5f,
4946 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004947 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004948
Daniel Stone37816df2012-05-16 18:45:18 +01004949 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4950 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004951 r = sqrtf(dx * dx + dy * dy);
4952 if (r > 20.0f) {
4953 struct weston_matrix inverse;
4954
4955 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004956 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004957 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004958
4959 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004960 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004961 } else {
4962 weston_matrix_init(&surface->rotation.rotation);
4963 weston_matrix_init(&rotate->rotation);
4964 }
4965
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004966 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4967 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004968}
4969
4970static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004971rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004972 void *data)
4973{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004974 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004975 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004976 struct shell_surface *surface;
4977
Derek Foreman8ae2db52015-07-15 13:00:36 -05004978 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004979 return;
4980
Derek Foreman8ae2db52015-07-15 13:00:36 -05004981 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004982
Pekka Paalanen01388e22013-04-25 13:57:44 +03004983 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004984 if (base_surface == NULL)
4985 return;
4986
4987 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004988 if (surface == NULL || surface->state.fullscreen ||
4989 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004990 return;
4991
Derek Foreman8ae2db52015-07-15 13:00:36 -05004992 surface_rotate(surface, pointer->seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004993}
4994
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004995/* Move all fullscreen layers down to the current workspace and hide their
4996 * black views. The surfaces' state is set to both fullscreen and lowered,
4997 * and this is reversed when such a surface is re-configured, see
4998 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4999 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005000 * lowering_output = NULL - Lower on all outputs, else only lower on the
5001 * specified output.
5002 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005003 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005004 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005005void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005006lower_fullscreen_layer(struct desktop_shell *shell,
5007 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005008{
5009 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005010 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005011
5012 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005013 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005014 &shell->fullscreen_layer.view_list.link,
5015 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005016 struct shell_surface *shsurf = get_shell_surface(view->surface);
5017
5018 if (!shsurf)
5019 continue;
5020
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005021 /* Only lower surfaces which have lowering_output as their fullscreen
5022 * output, unless a NULL output asks for lowering on all outputs.
5023 */
5024 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5025 continue;
5026
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005027 /* We can have a non-fullscreen popup for a fullscreen surface
5028 * in the fullscreen layer. */
5029 if (shsurf->state.fullscreen) {
5030 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005031 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5032 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005033 weston_view_damage_below(shsurf->fullscreen.black_view);
5034
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005035 }
5036
5037 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005038 weston_layer_entry_remove(&view->layer_link);
5039 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005040 weston_view_damage_below(view);
5041 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005042
5043 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005044 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005045}
5046
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005047void
Tiago Vignattibe143262012-04-16 17:31:41 +03005048activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005049 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005050{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005051 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005052 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005053 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005054 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005055 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005056
Pekka Paalanen01388e22013-04-25 13:57:44 +03005057 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005058 shsurf = get_shell_surface(main_surface);
5059 assert(shsurf);
5060
5061 /* Only demote fullscreen surfaces on the output of activated shsurf.
5062 * Leave fullscreen surfaces on unrelated outputs alone. */
5063 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005064
Daniel Stone37816df2012-05-16 18:45:18 +01005065 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005066
Jonas Ådahl8538b222012-08-29 22:13:03 +02005067 state = ensure_focus_state(shell, seat);
5068 if (state == NULL)
5069 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005070
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005071 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005072 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005073
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005074 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005075 shell_configure_fullscreen(shsurf);
5076 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005077 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005078
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005079 /* Update the surface’s layer. This brings it to the top of the stacking
5080 * order as appropriate. */
5081 shell_surface_update_layer(shsurf);
5082
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005083 if (shell->focus_animation_type != ANIMATION_NONE) {
5084 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005085 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005086 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005087}
5088
Alex Wu21858432012-04-01 20:13:08 +08005089/* no-op func for checking black surface */
5090static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005091black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005092{
5093}
5094
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005095static bool
Alex Wu21858432012-04-01 20:13:08 +08005096is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5097{
5098 if (es->configure == black_surface_configure) {
5099 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005100 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005101 return true;
5102 }
5103 return false;
5104}
5105
Kristian Høgsberg75840622011-09-06 13:48:16 -04005106static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005107activate_binding(struct weston_seat *seat,
5108 struct desktop_shell *shell,
5109 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005110{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005111 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005112
Alex Wu9c35e6b2012-03-05 14:13:13 +08005113 if (!focus)
5114 return;
5115
Pekka Paalanen01388e22013-04-25 13:57:44 +03005116 if (is_black_surface(focus, &main_surface))
5117 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005118
Pekka Paalanen01388e22013-04-25 13:57:44 +03005119 main_surface = weston_surface_get_main_surface(focus);
5120 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005121 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005122
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005123 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005124}
5125
5126static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005127click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5128 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005129{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005130 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005131 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005132 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005133 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005134
Derek Foreman8ae2db52015-07-15 13:00:36 -05005135 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005136}
5137
5138static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005139touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5140 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005141{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005142 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005143 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005144 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005145 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005146
Derek Foreman8ae2db52015-07-15 13:00:36 -05005147 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005148}
5149
5150static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005151unfocus_all_seats(struct desktop_shell *shell)
5152{
5153 struct weston_seat *seat, *next;
5154
5155 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5156 if (seat->keyboard == NULL)
5157 continue;
5158
5159 weston_keyboard_set_focus(seat->keyboard, NULL);
5160 }
5161}
5162
5163static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005164lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005165{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005166 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005167
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005168 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005169 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005170 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005171 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005172
5173 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005174
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005175 /* Hide all surfaces by removing the fullscreen, panel and
5176 * toplevel layers. This way nothing else can show or receive
5177 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005178
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005179 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005180 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005181 if (shell->showing_input_panels)
5182 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005183 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005184 wl_list_insert(&shell->compositor->cursor_layer.link,
5185 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005186
Neil Robertsb4a91702014-04-09 16:33:32 +01005187 /* Remove the keyboard focus on all seats. This will be
5188 * restored to the workspace's saved state via
5189 * restore_focus_state when the compositor is unlocked */
5190 unfocus_all_seats(shell);
5191
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005192 /* TODO: disable bindings that should not work while locked. */
5193
5194 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005195}
5196
5197static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005198unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005199{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005200 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005201 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005202 return;
5203 }
5204
5205 /* If desktop-shell client has gone away, unlock immediately. */
5206 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005207 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005208 return;
5209 }
5210
5211 if (shell->prepare_event_sent)
5212 return;
5213
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005214 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005215 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005216}
5217
5218static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005219shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005220{
5221 struct desktop_shell *shell = data;
5222
5223 shell->fade.animation = NULL;
5224
5225 switch (shell->fade.type) {
5226 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005227 weston_surface_destroy(shell->fade.view->surface);
5228 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005229 break;
5230 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005231 lock(shell);
5232 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005233 default:
5234 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005235 }
5236}
5237
Jason Ekstranda7af7042013-10-12 22:38:11 -05005238static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005239shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005240{
5241 struct weston_compositor *compositor = shell->compositor;
5242 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005243 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005244
5245 surface = weston_surface_create(compositor);
5246 if (!surface)
5247 return NULL;
5248
Jason Ekstranda7af7042013-10-12 22:38:11 -05005249 view = weston_view_create(surface);
5250 if (!view) {
5251 weston_surface_destroy(surface);
5252 return NULL;
5253 }
5254
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005255 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005256 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005257 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005258 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5259 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005260 pixman_region32_init(&surface->input);
5261
Jason Ekstranda7af7042013-10-12 22:38:11 -05005262 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005263}
5264
5265static void
5266shell_fade(struct desktop_shell *shell, enum fade_type type)
5267{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005268 float tint;
5269
5270 switch (type) {
5271 case FADE_IN:
5272 tint = 0.0;
5273 break;
5274 case FADE_OUT:
5275 tint = 1.0;
5276 break;
5277 default:
5278 weston_log("shell: invalid fade type\n");
5279 return;
5280 }
5281
5282 shell->fade.type = type;
5283
Jason Ekstranda7af7042013-10-12 22:38:11 -05005284 if (shell->fade.view == NULL) {
5285 shell->fade.view = shell_fade_create_surface(shell);
5286 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005287 return;
5288
Jason Ekstranda7af7042013-10-12 22:38:11 -05005289 shell->fade.view->alpha = 1.0 - tint;
5290 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005291 }
5292
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005293 if (shell->fade.view->output == NULL) {
5294 /* If the black view gets a NULL output, we lost the
5295 * last output and we'll just cancel the fade. This
5296 * happens when you close the last window under the
5297 * X11 or Wayland backends. */
5298 shell->locked = false;
5299 weston_surface_destroy(shell->fade.view->surface);
5300 shell->fade.view = NULL;
5301 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005302 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005303 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005304 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005305 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005306 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005307 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005308 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005309}
5310
5311static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005312do_shell_fade_startup(void *data)
5313{
5314 struct desktop_shell *shell = data;
5315
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005316 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005317 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005318 } else {
5319 weston_log("desktop shell: "
5320 "unexpected fade-in animation type %d\n",
5321 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005322 weston_surface_destroy(shell->fade.view->surface);
5323 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005324 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005325}
5326
5327static void
5328shell_fade_startup(struct desktop_shell *shell)
5329{
5330 struct wl_event_loop *loop;
5331
5332 if (!shell->fade.startup_timer)
5333 return;
5334
5335 wl_event_source_remove(shell->fade.startup_timer);
5336 shell->fade.startup_timer = NULL;
5337
5338 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5339 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5340}
5341
5342static int
5343fade_startup_timeout(void *data)
5344{
5345 struct desktop_shell *shell = data;
5346
5347 shell_fade_startup(shell);
5348 return 0;
5349}
5350
5351static void
5352shell_fade_init(struct desktop_shell *shell)
5353{
5354 /* Make compositor output all black, and wait for the desktop-shell
5355 * client to signal it is ready, then fade in. The timer triggers a
5356 * fade-in, in case the desktop-shell client takes too long.
5357 */
5358
5359 struct wl_event_loop *loop;
5360
Jason Ekstranda7af7042013-10-12 22:38:11 -05005361 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005362 weston_log("%s: warning: fade surface already exists\n",
5363 __func__);
5364 return;
5365 }
5366
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005367 if (shell->startup_animation_type == ANIMATION_NONE)
5368 return;
5369
Jason Ekstranda7af7042013-10-12 22:38:11 -05005370 shell->fade.view = shell_fade_create_surface(shell);
5371 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005372 return;
5373
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 weston_view_update_transform(shell->fade.view);
5375 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005376
5377 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5378 shell->fade.startup_timer =
5379 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5380 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5381}
5382
5383static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005384idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005385{
5386 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005387 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005388 struct weston_seat *seat;
5389
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005390 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005391 if (seat->pointer)
5392 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005393 if (seat->touch)
5394 touch_popup_grab_end(seat->touch);
5395 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005396
5397 shell_fade(shell, FADE_OUT);
5398 /* lock() is called from shell_fade_done() */
5399}
5400
5401static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005402wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005403{
5404 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005405 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005406
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005407 unlock(shell);
5408}
5409
5410static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005411center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005412{
Giulio Camuffob8366642013-04-25 13:57:46 +03005413 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005414 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005415
Jason Ekstranda7af7042013-10-12 22:38:11 -05005416 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005417
5418 x = output->x + (output->width - width) / 2 - surf_x / 2;
5419 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005420
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005421 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005422}
5423
5424static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005425weston_view_set_initial_position(struct weston_view *view,
5426 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005427{
5428 struct weston_compositor *compositor = shell->compositor;
5429 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005430 int32_t range_x, range_y;
5431 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005432 struct weston_output *output, *target_output = NULL;
5433 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005434 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005435
5436 /* As a heuristic place the new window on the same output as the
5437 * pointer. Falling back to the output containing 0, 0.
5438 *
5439 * TODO: Do something clever for touch too?
5440 */
5441 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005442 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005443 ix = wl_fixed_to_int(seat->pointer->x);
5444 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005445 break;
5446 }
5447 }
5448
5449 wl_list_for_each(output, &compositor->output_list, link) {
5450 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5451 target_output = output;
5452 break;
5453 }
5454 }
5455
5456 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005457 weston_view_set_position(view, 10 + random() % 400,
5458 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005459 return;
5460 }
5461
5462 /* Valid range within output where the surface will still be onscreen.
5463 * If this is negative it means that the surface is bigger than
5464 * output.
5465 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005466 get_output_work_area(shell, target_output, &area);
5467
5468 dx = area.x;
5469 dy = area.y;
5470 range_x = area.width - view->surface->width;
5471 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005472
Rob Bradford4cb88c72012-08-13 15:18:44 +01005473 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005474 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005475
5476 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005477 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005478
5479 x = target_output->x + dx;
5480 y = target_output->y + dy;
5481
Jason Ekstranda7af7042013-10-12 22:38:11 -05005482 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005483}
5484
5485static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005486set_maximized_position(struct desktop_shell *shell,
5487 struct shell_surface *shsurf)
5488{
5489 int32_t surf_x, surf_y;
5490 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005491 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005492
Jonny Lambd73c6942014-08-20 15:53:20 +02005493 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005494 if (shsurf->has_set_geometry) {
5495 surf_x = shsurf->geometry.x;
5496 surf_y = shsurf->geometry.y;
5497 } else {
5498 surface_subsurfaces_boundingbox(shsurf->surface,
5499 &surf_x, &surf_y, NULL, NULL);
5500 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005501 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005502
5503 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005504 e->x1 + area.x - surf_x,
5505 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005506}
5507
5508static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005509map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005510 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005511{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005512 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005513 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005514
Pekka Paalanen77346a62011-11-30 16:26:35 +02005515 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005516 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005517 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005518 if (shsurf->state.fullscreen) {
5519 center_on_output(shsurf->view, shsurf->fullscreen_output);
5520 shell_map_fullscreen(shsurf);
5521 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005522 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005523 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005524 weston_view_set_initial_position(shsurf->view, shell);
5525 }
Juan Zhao96879df2012-02-07 08:45:41 +08005526 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005527 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005528 if (shell_map_popup(shsurf) != 0)
5529 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005530 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005531 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005532 weston_view_set_position(shsurf->view,
5533 shsurf->view->geometry.x + sx,
5534 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005535 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005536 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005537 default:
5538 ;
5539 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005540
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005541 /* Surface stacking order, see also activate(). */
5542 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005543
Jason Ekstranda7af7042013-10-12 22:38:11 -05005544 if (shsurf->type != SHELL_SURFACE_NONE) {
5545 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005546 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005547 shsurf->surface->output = shsurf->output;
5548 shsurf->view->output = shsurf->output;
5549 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005550 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005551
Jason Ekstranda7af7042013-10-12 22:38:11 -05005552 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005553 /* XXX: xwayland's using the same fields for transient type */
5554 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005555 if (shsurf->transient.flags ==
5556 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5557 break;
5558 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005559 if (shsurf->state.relative &&
5560 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5561 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005562 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005563 break;
5564 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005565 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005566 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005567 case SHELL_SURFACE_POPUP:
5568 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005569 default:
5570 break;
5571 }
5572
Rafael Antognolli03b16592013-12-03 15:35:42 -02005573 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5574 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005575 {
5576 switch (shell->win_animation_type) {
5577 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005578 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005579 break;
5580 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005581 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005582 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005583 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005584 default:
5585 break;
5586 }
5587 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005588}
5589
5590static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005591configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005592 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005593{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005594 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005595 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005596
Pekka Paalanen77346a62011-11-30 16:26:35 +02005597 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005598
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005599 assert(shsurf);
5600
Rafael Antognolli03b16592013-12-03 15:35:42 -02005601 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005602 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005603 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005604 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005605 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005606 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005607 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005608
Alex Wu4539b082012-03-01 12:57:46 +08005609 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005610 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005611 wl_list_for_each(view, &surface->views, surface_link)
5612 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005613
Rafael Antognolli03b16592013-12-03 15:35:42 -02005614 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005615 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005616 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005617}
5618
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005619static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005620shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005621{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005622 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005623 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005624 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005625
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005626 assert(shsurf);
5627
5628 shell = shsurf->shell;
5629
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005630 if (!weston_surface_is_mapped(es) &&
5631 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005632 remove_popup_grab(shsurf);
5633 }
5634
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005635 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005636 return;
5637
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005638 if (shsurf->has_next_geometry) {
5639 shsurf->geometry = shsurf->next_geometry;
5640 shsurf->has_next_geometry = false;
5641 shsurf->has_set_geometry = true;
5642 } else if (!shsurf->has_set_geometry) {
5643 surface_subsurfaces_boundingbox(shsurf->surface,
5644 &shsurf->geometry.x,
5645 &shsurf->geometry.y,
5646 &shsurf->geometry.width,
5647 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005648 }
5649
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005650 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005651 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005652 type_changed = 1;
5653 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005654
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005655 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005656 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005657 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005658 shsurf->last_width != es->width ||
5659 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005660 float from_x, from_y;
5661 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005662
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005663 if (shsurf->resize_edges) {
5664 sx = 0;
5665 sy = 0;
5666 }
5667
5668 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5669 sx = shsurf->last_width - es->width;
5670 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5671 sy = shsurf->last_height - es->height;
5672
5673 shsurf->last_width = es->width;
5674 shsurf->last_height = es->height;
5675
Jason Ekstranda7af7042013-10-12 22:38:11 -05005676 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5677 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005678 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005679 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005680 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005681 }
5682}
5683
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005684static bool
5685check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5686{
5687 struct timespec now;
5688
5689 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5690 return false;
5691
5692 /*
5693 * If the shell helper client dies before the session has been
5694 * up for roughly 30 seconds, better just make Weston shut down,
5695 * because the user likely has no way to interact with the desktop
5696 * anyway.
5697 */
5698 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5699 weston_log("Error: %s apparently cannot run at all.\n",
5700 shell->client);
5701 weston_log_continue(STAMP_SPACE "Quitting...");
5702 wl_display_terminate(shell->compositor->wl_display);
5703
5704 return true;
5705 }
5706
5707 return false;
5708}
5709
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005710static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005711
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005712static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005713respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005714{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005715 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005716
5717 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5718 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005719 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005720 shell->child.deathstamp = time;
5721 shell->child.deathcount = 0;
5722 }
5723
5724 shell->child.deathcount++;
5725 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005726 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005727 return;
5728 }
5729
Pekka Paalanen826dc142014-08-27 12:11:53 +03005730 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005731 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005732}
5733
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005734static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005735desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5736{
5737 struct desktop_shell *shell;
5738
5739 shell = container_of(listener, struct desktop_shell,
5740 child.client_destroy_listener);
5741
Pekka Paalanen826dc142014-08-27 12:11:53 +03005742 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005743 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005744 /*
5745 * unbind_desktop_shell() will reset shell->child.desktop_shell
5746 * before the respawned process has a chance to create a new
5747 * desktop_shell object, because we are being called from the
5748 * wl_client destructor which destroys all wl_resources before
5749 * returning.
5750 */
5751
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005752 if (!check_desktop_shell_crash_too_early(shell))
5753 respawn_desktop_shell_process(shell);
5754
Pekka Paalanen826dc142014-08-27 12:11:53 +03005755 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005756}
5757
5758static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005759launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005760{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005761 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005762
Pekka Paalanen826dc142014-08-27 12:11:53 +03005763 shell->child.client = weston_client_start(shell->compositor,
5764 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005765
Arnaud Vrac42631192014-08-25 20:56:46 +02005766 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005767 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005768 return;
5769 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005770
5771 shell->child.client_destroy_listener.notify =
5772 desktop_shell_client_destroy;
5773 wl_client_add_destroy_listener(shell->child.client,
5774 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005775}
5776
5777static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005778handle_shell_client_destroy(struct wl_listener *listener, void *data)
5779{
5780 struct shell_client *sc =
5781 container_of(listener, struct shell_client, destroy_listener);
5782
5783 if (sc->ping_timer)
5784 wl_event_source_remove(sc->ping_timer);
5785 free(sc);
5786}
5787
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005788static struct shell_client *
5789shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5790 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005791{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005792 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005793
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005794 sc = zalloc(sizeof *sc);
5795 if (sc == NULL) {
5796 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005797 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005798 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005799
5800 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005801 if (sc->resource == NULL) {
5802 free(sc);
5803 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005804 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005805 }
5806
5807 sc->client = client;
5808 sc->shell = shell;
5809 sc->destroy_listener.notify = handle_shell_client_destroy;
5810 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005811 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005812
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005813 return sc;
5814}
5815
5816static void
5817bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5818{
5819 struct desktop_shell *shell = data;
5820 struct shell_client *sc;
5821
5822 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5823 if (sc)
5824 wl_resource_set_implementation(sc->resource,
5825 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005826 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005827}
5828
5829static void
5830bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5831{
5832 struct desktop_shell *shell = data;
5833 struct shell_client *sc;
5834
5835 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5836 if (sc)
5837 wl_resource_set_dispatcher(sc->resource,
5838 xdg_shell_unversioned_dispatch,
5839 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005840}
5841
5842static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005843unbind_desktop_shell(struct wl_resource *resource)
5844{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005845 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005846
5847 if (shell->locked)
5848 resume_desktop(shell);
5849
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005850 shell->child.desktop_shell = NULL;
5851 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005852}
5853
5854static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005855bind_desktop_shell(struct wl_client *client,
5856 void *data, uint32_t version, uint32_t id)
5857{
Tiago Vignattibe143262012-04-16 17:31:41 +03005858 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005859 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005860
Jason Ekstranda85118c2013-06-27 20:17:02 -05005861 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005862 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005863
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005864 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005865 wl_resource_set_implementation(resource,
5866 &desktop_shell_implementation,
5867 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005868 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005869
5870 if (version < 2)
5871 shell_fade_startup(shell);
5872
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005873 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005874 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005875
5876 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5877 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005878}
5879
Kristian Høgsberg07045392012-02-19 18:52:44 -05005880struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005881 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005882 struct weston_surface *current;
5883 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005884 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005885 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005886};
5887
5888static void
5889switcher_next(struct switcher *switcher)
5890{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005891 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005892 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005893 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005894 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005895
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005896 /* temporary re-display minimized surfaces */
5897 struct weston_view *tmp;
5898 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005899 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5900 weston_layer_entry_remove(&view->layer_link);
5901 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005902 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5903 *minimized = view;
5904 }
5905
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005906 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005907 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005908 if (shsurf &&
5909 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5910 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005911 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005912 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005913 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005914 next = view->surface;
5915 prev = view->surface;
5916 view->alpha = 0.25;
5917 weston_view_geometry_dirty(view);
5918 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005919 }
Alex Wu1659daa2012-04-01 20:13:09 +08005920
Jason Ekstranda7af7042013-10-12 22:38:11 -05005921 if (is_black_surface(view->surface, NULL)) {
5922 view->alpha = 0.25;
5923 weston_view_geometry_dirty(view);
5924 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005925 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005926 }
5927
5928 if (next == NULL)
5929 next = first;
5930
Alex Wu07b26062012-03-12 16:06:01 +08005931 if (next == NULL)
5932 return;
5933
Kristian Høgsberg07045392012-02-19 18:52:44 -05005934 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005935 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005936
5937 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005938 wl_list_for_each(view, &next->views, surface_link)
5939 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005940
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005941 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005942 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005943 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005944}
5945
5946static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005947switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005948{
5949 struct switcher *switcher =
5950 container_of(listener, struct switcher, listener);
5951
5952 switcher_next(switcher);
5953}
5954
5955static void
Daniel Stone351eb612012-05-31 15:27:47 -04005956switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005957{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005958 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005959 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005960 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005961
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005962 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005963 if (is_focus_view(view))
5964 continue;
5965
Jason Ekstranda7af7042013-10-12 22:38:11 -05005966 view->alpha = 1.0;
5967 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005968 }
5969
Alex Wu07b26062012-03-12 16:06:01 +08005970 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005971 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005972 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005973 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005974 weston_keyboard_end_grab(keyboard);
5975 if (keyboard->input_method_resource)
5976 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005977
5978 /* re-hide surfaces that were temporary shown during the switch */
5979 struct weston_view **minimized;
5980 wl_array_for_each(minimized, &switcher->minimized_array) {
5981 /* with the exception of the current selected */
5982 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005983 weston_layer_entry_remove(&(*minimized)->layer_link);
5984 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005985 weston_view_damage_below(*minimized);
5986 }
5987 }
5988 wl_array_release(&switcher->minimized_array);
5989
Kristian Høgsberg07045392012-02-19 18:52:44 -05005990 free(switcher);
5991}
5992
5993static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005994switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005995 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005996{
5997 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005998 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005999
Daniel Stonec9785ea2012-05-30 16:31:52 +01006000 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006001 switcher_next(switcher);
6002}
6003
6004static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006005switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006006 uint32_t mods_depressed, uint32_t mods_latched,
6007 uint32_t mods_locked, uint32_t group)
6008{
6009 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006010 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006011
Daniel Stone351eb612012-05-31 15:27:47 -04006012 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6013 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006014}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006015
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006016static void
6017switcher_cancel(struct weston_keyboard_grab *grab)
6018{
6019 struct switcher *switcher = container_of(grab, struct switcher, grab);
6020
6021 switcher_destroy(switcher);
6022}
6023
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006024static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006025 switcher_key,
6026 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006027 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006028};
6029
6030static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006031switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6032 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006033{
Tiago Vignattibe143262012-04-16 17:31:41 +03006034 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006035 struct switcher *switcher;
6036
6037 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006038 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006039 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006040 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006041 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006042 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006043
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006044 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006045 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006046 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006047 weston_keyboard_start_grab(keyboard, &switcher->grab);
6048 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006049 switcher_next(switcher);
6050}
6051
Pekka Paalanen3c647232011-12-22 13:43:43 +02006052static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006053backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6054 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006055{
6056 struct weston_compositor *compositor = data;
6057 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006058 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006059
6060 /* TODO: we're limiting to simple use cases, where we assume just
6061 * control on the primary display. We'd have to extend later if we
6062 * ever get support for setting backlights on random desktop LCD
6063 * panels though */
6064 output = get_default_output(compositor);
6065 if (!output)
6066 return;
6067
6068 if (!output->set_backlight)
6069 return;
6070
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006071 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6072 backlight_new = output->backlight_current - 25;
6073 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6074 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006075
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006076 if (backlight_new < 5)
6077 backlight_new = 5;
6078 if (backlight_new > 255)
6079 backlight_new = 255;
6080
6081 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006082 output->set_backlight(output, output->backlight_current);
6083}
6084
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006085static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006086force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6087 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006088{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006089 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006090 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006091 struct desktop_shell *shell = data;
6092 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006093 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006094
Derek Foreman8ae2db52015-07-15 13:00:36 -05006095 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006096 if (!focus_surface)
6097 return;
6098
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006099 wl_signal_emit(&compositor->kill_signal, focus_surface);
6100
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006101 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006102 wl_client_get_credentials(client, &pid, NULL, NULL);
6103
6104 /* Skip clients that we launched ourselves (the credentials of
6105 * the socketpair is ours) */
6106 if (pid == getpid())
6107 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006108
Daniel Stone325fc2d2012-05-30 16:31:58 +01006109 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006110}
6111
6112static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006113workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006114 uint32_t key, void *data)
6115{
6116 struct desktop_shell *shell = data;
6117 unsigned int new_index = shell->workspaces.current;
6118
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006119 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006120 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006121 if (new_index != 0)
6122 new_index--;
6123
6124 change_workspace(shell, new_index);
6125}
6126
6127static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006128workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006129 uint32_t key, void *data)
6130{
6131 struct desktop_shell *shell = data;
6132 unsigned int new_index = shell->workspaces.current;
6133
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006134 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006135 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006136 if (new_index < shell->workspaces.num - 1)
6137 new_index++;
6138
6139 change_workspace(shell, new_index);
6140}
6141
6142static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006143workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006144 uint32_t key, void *data)
6145{
6146 struct desktop_shell *shell = data;
6147 unsigned int new_index;
6148
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006149 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006150 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006151 new_index = key - KEY_F1;
6152 if (new_index >= shell->workspaces.num)
6153 new_index = shell->workspaces.num - 1;
6154
6155 change_workspace(shell, new_index);
6156}
6157
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006158static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006159workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6160 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006161{
6162 struct desktop_shell *shell = data;
6163 unsigned int new_index = shell->workspaces.current;
6164
6165 if (shell->locked)
6166 return;
6167
6168 if (new_index != 0)
6169 new_index--;
6170
Derek Foreman8ae2db52015-07-15 13:00:36 -05006171 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006172}
6173
6174static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006175workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6176 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006177{
6178 struct desktop_shell *shell = data;
6179 unsigned int new_index = shell->workspaces.current;
6180
6181 if (shell->locked)
6182 return;
6183
6184 if (new_index < shell->workspaces.num - 1)
6185 new_index++;
6186
Derek Foreman8ae2db52015-07-15 13:00:36 -05006187 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006188}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006189
6190static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006191shell_reposition_view_on_output_destroy(struct weston_view *view)
6192{
6193 struct weston_output *output, *first_output;
6194 struct weston_compositor *ec = view->surface->compositor;
6195 struct shell_surface *shsurf;
6196 float x, y;
6197 int visible;
6198
6199 x = view->geometry.x;
6200 y = view->geometry.y;
6201
6202 /* At this point the destroyed output is not in the list anymore.
6203 * If the view is still visible somewhere, we leave where it is,
6204 * otherwise, move it to the first output. */
6205 visible = 0;
6206 wl_list_for_each(output, &ec->output_list, link) {
6207 if (pixman_region32_contains_point(&output->region,
6208 x, y, NULL)) {
6209 visible = 1;
6210 break;
6211 }
6212 }
6213
6214 if (!visible) {
6215 first_output = container_of(ec->output_list.next,
6216 struct weston_output, link);
6217
6218 x = first_output->x + first_output->width / 4;
6219 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006220
6221 weston_view_set_position(view, x, y);
6222 } else {
6223 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006224 }
6225
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006226
6227 shsurf = get_shell_surface(view->surface);
6228
6229 if (shsurf) {
6230 shsurf->saved_position_valid = false;
6231 shsurf->next_state.maximized = false;
6232 shsurf->next_state.fullscreen = false;
6233 shsurf->state_changed = true;
6234 }
6235}
6236
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006237void
6238shell_for_each_layer(struct desktop_shell *shell,
6239 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006240{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006241 struct workspace **ws;
6242
6243 func(shell, &shell->fullscreen_layer, data);
6244 func(shell, &shell->panel_layer, data);
6245 func(shell, &shell->background_layer, data);
6246 func(shell, &shell->lock_layer, data);
6247 func(shell, &shell->input_panel_layer, data);
6248
6249 wl_array_for_each(ws, &shell->workspaces.array)
6250 func(shell, &(*ws)->layer, data);
6251}
6252
6253static void
6254shell_output_destroy_move_layer(struct desktop_shell *shell,
6255 struct weston_layer *layer,
6256 void *data)
6257{
6258 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006259 struct weston_view *view;
6260
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006261 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006262 if (view->output != output)
6263 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006264
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006265 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006266 }
6267}
6268
6269static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006270handle_output_destroy(struct wl_listener *listener, void *data)
6271{
6272 struct shell_output *output_listener =
6273 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006274 struct weston_output *output = output_listener->output;
6275 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006276
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006277 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006278
Xiong Zhang6b481422013-10-23 13:58:32 +08006279 wl_list_remove(&output_listener->destroy_listener.link);
6280 wl_list_remove(&output_listener->link);
6281 free(output_listener);
6282}
6283
6284static void
6285create_shell_output(struct desktop_shell *shell,
6286 struct weston_output *output)
6287{
6288 struct shell_output *shell_output;
6289
6290 shell_output = zalloc(sizeof *shell_output);
6291 if (shell_output == NULL)
6292 return;
6293
6294 shell_output->output = output;
6295 shell_output->shell = shell;
6296 shell_output->destroy_listener.notify = handle_output_destroy;
6297 wl_signal_add(&output->destroy_signal,
6298 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006299 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006300}
6301
6302static void
6303handle_output_create(struct wl_listener *listener, void *data)
6304{
6305 struct desktop_shell *shell =
6306 container_of(listener, struct desktop_shell, output_create_listener);
6307 struct weston_output *output = (struct weston_output *)data;
6308
6309 create_shell_output(shell, output);
6310}
6311
6312static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006313handle_output_move_layer(struct desktop_shell *shell,
6314 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006315{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006316 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006317 struct weston_view *view;
6318 float x, y;
6319
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006320 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006321 if (view->output != output)
6322 continue;
6323
6324 x = view->geometry.x + output->move_x;
6325 y = view->geometry.y + output->move_y;
6326 weston_view_set_position(view, x, y);
6327 }
6328}
6329
6330static void
6331handle_output_move(struct wl_listener *listener, void *data)
6332{
6333 struct desktop_shell *shell;
6334
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006335 shell = container_of(listener, struct desktop_shell,
6336 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006337
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006338 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006339}
6340
6341static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006342setup_output_destroy_handler(struct weston_compositor *ec,
6343 struct desktop_shell *shell)
6344{
6345 struct weston_output *output;
6346
6347 wl_list_init(&shell->output_list);
6348 wl_list_for_each(output, &ec->output_list, link)
6349 create_shell_output(shell, output);
6350
6351 shell->output_create_listener.notify = handle_output_create;
6352 wl_signal_add(&ec->output_created_signal,
6353 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006354
6355 shell->output_move_listener.notify = handle_output_move;
6356 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006357}
6358
6359static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006360shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006361{
Tiago Vignattibe143262012-04-16 17:31:41 +03006362 struct desktop_shell *shell =
6363 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006364 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006365 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006366
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006367 /* Force state to unlocked so we don't try to fade */
6368 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006369
6370 if (shell->child.client) {
6371 /* disable respawn */
6372 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006373 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006374 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006375
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006376 wl_list_remove(&shell->idle_listener.link);
6377 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006378
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006379 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006380 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006381
Xiong Zhang6b481422013-10-23 13:58:32 +08006382 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6383 wl_list_remove(&shell_output->destroy_listener.link);
6384 wl_list_remove(&shell_output->link);
6385 free(shell_output);
6386 }
6387
6388 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006389 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006390
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006391 wl_array_for_each(ws, &shell->workspaces.array)
6392 workspace_destroy(*ws);
6393 wl_array_release(&shell->workspaces.array);
6394
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006395 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006396 free(shell);
6397}
6398
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006399static void
6400shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6401{
6402 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006403 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006404
6405 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006406 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6407 MODIFIER_CTRL | MODIFIER_ALT,
6408 terminate_binding, ec);
6409 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6410 click_to_activate_binding,
6411 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006412 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6413 click_to_activate_binding,
6414 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006415 weston_compositor_add_touch_binding(ec, 0,
6416 touch_to_activate_binding,
6417 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006418 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6419 MODIFIER_SUPER | MODIFIER_ALT,
6420 surface_opacity_binding, NULL);
6421 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6422 MODIFIER_SUPER, zoom_axis_binding,
6423 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006424
6425 /* configurable bindings */
6426 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006427 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6428 zoom_key_binding, NULL);
6429 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6430 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006431 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6432 maximize_binding, NULL);
6433 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6434 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006435 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6436 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006437 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006438 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6439 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006440 weston_compositor_add_button_binding(ec, BTN_LEFT,
6441 mod | MODIFIER_SHIFT,
6442 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006443
6444 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6445 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6446 rotate_binding, NULL);
6447
Daniel Stone325fc2d2012-05-30 16:31:58 +01006448 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6449 shell);
6450 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6451 ec);
6452 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6453 backlight_binding, ec);
6454 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6455 ec);
6456 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6457 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006458 weston_compositor_add_key_binding(ec, KEY_K, mod,
6459 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006460 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6461 workspace_up_binding, shell);
6462 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6463 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006464 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6465 workspace_move_surface_up_binding,
6466 shell);
6467 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6468 workspace_move_surface_down_binding,
6469 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006470
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006471 if (shell->exposay_modifier)
6472 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6473 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006474
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006475 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6476 if (shell->workspaces.num > 1) {
6477 num_workspace_bindings = shell->workspaces.num;
6478 if (num_workspace_bindings > 6)
6479 num_workspace_bindings = 6;
6480 for (i = 0; i < num_workspace_bindings; i++)
6481 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6482 workspace_f_binding,
6483 shell);
6484 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006485
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006486 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006487}
6488
Jason Ekstrand024177c2014-04-21 19:42:58 -05006489static void
6490handle_seat_created(struct wl_listener *listener, void *data)
6491{
6492 struct weston_seat *seat = data;
6493
6494 create_shell_seat(seat);
6495}
6496
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006497WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006498module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006499 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006500{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006501 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006502 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006503 struct workspace **pws;
6504 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006505 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006506
Peter Huttererf3d62272013-08-08 11:57:05 +10006507 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006508 if (shell == NULL)
6509 return -1;
6510
Kristian Høgsberg75840622011-09-06 13:48:16 -04006511 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006512
6513 shell->destroy_listener.notify = shell_destroy;
6514 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006515 shell->idle_listener.notify = idle_handler;
6516 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6517 shell->wake_listener.notify = wake_handler;
6518 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006519
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006520 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006521 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006522 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006523 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006524 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006525 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006526 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006527 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006528 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006529 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006530 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006531 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006532 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006533
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006534 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6535 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006536 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6537 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006538 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006539
6540 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006541 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006542
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006543 if (input_panel_setup(shell) < 0)
6544 return -1;
6545
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006546 shell->text_backend = text_backend_init(ec);
6547 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006548 return -1;
6549
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006550 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006551
Daniel Stonedf8133b2013-11-19 11:37:14 +01006552 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6553 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6554
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006555 for (i = 0; i < shell->workspaces.num; i++) {
6556 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6557 if (pws == NULL)
6558 return -1;
6559
6560 *pws = workspace_create();
6561 if (*pws == NULL)
6562 return -1;
6563 }
6564 activate_workspace(shell, 0);
6565
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006566 weston_layer_init(&shell->minimized_layer, NULL);
6567
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006568 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006569 wl_list_init(&shell->workspaces.animation.link);
6570 shell->workspaces.animation.frame = animate_workspace_change_frame;
6571
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006572 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006573 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006574 return -1;
6575
Rafael Antognollie2a34552013-12-03 15:35:45 -02006576 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6577 shell, bind_xdg_shell) == NULL)
6578 return -1;
6579
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006580 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006581 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006582 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006583 return -1;
6584
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006585 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6586 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006587 return -1;
6588
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006589 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006590
Jonny Lamb765760d2014-08-20 15:53:19 +02006591 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6592
Xiong Zhang6b481422013-10-23 13:58:32 +08006593 setup_output_destroy_handler(ec, shell);
6594
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006595 loop = wl_display_get_event_loop(ec->wl_display);
6596 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006597
Jason Ekstrand024177c2014-04-21 19:42:58 -05006598 wl_list_for_each(seat, &ec->seat_list, link)
6599 handle_seat_created(NULL, seat);
6600 shell->seat_create_listener.notify = handle_seat_created;
6601 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006602
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006603 screenshooter_create(ec);
6604
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006605 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006606
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006607 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006608
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006609 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6610
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006611 return 0;
6612}