blob: 9799acb1b77fd48b472d9bff876361a40bcbf45c [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;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700202 int 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,
1752 int 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) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001793 (surface_move(shsurf, seat, 1) < 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);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002048 surface_move(shsurf, seat, 0);
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 {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002966 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002967 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002968 }
Alex Wubd3354b2012-04-17 17:20:49 +08002969 }
Alex Wu4539b082012-03-01 12:57:46 +08002970 break;
2971 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002972 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002973 break;
2974 default:
2975 break;
2976 }
2977}
2978
Alex Wu4539b082012-03-01 12:57:46 +08002979static void
2980shell_map_fullscreen(struct shell_surface *shsurf)
2981{
Alex Wubd3354b2012-04-17 17:20:49 +08002982 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002983}
2984
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002985static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002986set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2987{
2988 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002989 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002990 shsurf->transient.x = x;
2991 shsurf->transient.y = y;
2992 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002993
2994 shell_surface_set_parent(shsurf, NULL);
2995
Pekka Paalanenb5026542014-11-12 15:09:24 +02002996 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002997 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002998}
2999
Kristian Høgsberg47792412014-05-04 13:47:06 -07003000static void
3001shell_interface_set_fullscreen(struct shell_surface *shsurf,
3002 uint32_t method,
3003 uint32_t framerate,
3004 struct weston_output *output)
3005{
3006 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003007 shsurf->next_state.fullscreen = true;
3008 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003009
3010 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003011}
3012
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003013static struct weston_output *
3014get_focused_output(struct weston_compositor *compositor)
3015{
3016 struct weston_seat *seat;
3017 struct weston_output *output = NULL;
3018
3019 wl_list_for_each(seat, &compositor->seat_list, link) {
3020 /* Priority has touch focus, then pointer and
3021 * then keyboard focus. We should probably have
3022 * three for loops and check frist for touch,
3023 * then for pointer, etc. but unless somebody has some
3024 * objections, I think this is sufficient. */
3025 if (seat->touch && seat->touch->focus)
3026 output = seat->touch->focus->output;
3027 else if (seat->pointer && seat->pointer->focus)
3028 output = seat->pointer->focus->output;
3029 else if (seat->keyboard && seat->keyboard->focus)
3030 output = seat->keyboard->focus->output;
3031
3032 if (output)
3033 break;
3034 }
3035
3036 return output;
3037}
3038
3039static void
3040shell_interface_set_maximized(struct shell_surface *shsurf)
3041{
3042 struct weston_output *output;
3043
3044 surface_clear_next_states(shsurf);
3045 shsurf->next_state.maximized = true;
3046 shsurf->state_changed = true;
3047 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3048
3049 if (!weston_surface_is_mapped(shsurf->surface))
3050 output = get_focused_output(shsurf->surface->compositor);
3051 else
3052 output = shsurf->surface->output;
3053
3054 shell_surface_set_output(shsurf, output);
3055 send_configure_for_surface(shsurf);
3056}
3057
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003058static int
3059shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3060{
3061 return surface_move(shsurf, ws, 1);
3062}
3063
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003064static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065
3066static void
3067destroy_shell_seat(struct wl_listener *listener, void *data)
3068{
3069 struct shell_seat *shseat =
3070 container_of(listener,
3071 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003072 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003073
3074 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003075 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076 shseat->popup_grab.client = NULL;
3077
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003078 wl_list_for_each_safe(shsurf, next,
3079 &shseat->popup_grab.surfaces_list,
3080 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003081 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003082 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003083 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003084 }
3085
3086 wl_list_remove(&shseat->seat_destroy_listener.link);
3087 free(shseat);
3088}
3089
Jason Ekstrand024177c2014-04-21 19:42:58 -05003090static void
3091shell_seat_caps_changed(struct wl_listener *listener, void *data)
3092{
3093 struct shell_seat *seat;
3094
3095 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3096
3097 if (seat->seat->keyboard &&
3098 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3099 wl_signal_add(&seat->seat->keyboard->focus_signal,
3100 &seat->keyboard_focus_listener);
3101 } else if (!seat->seat->keyboard) {
3102 wl_list_init(&seat->keyboard_focus_listener.link);
3103 }
3104
3105 if (seat->seat->pointer &&
3106 wl_list_empty(&seat->pointer_focus_listener.link)) {
3107 wl_signal_add(&seat->seat->pointer->focus_signal,
3108 &seat->pointer_focus_listener);
3109 } else if (!seat->seat->pointer) {
3110 wl_list_init(&seat->pointer_focus_listener.link);
3111 }
3112}
3113
Giulio Camuffo5085a752013-03-25 21:42:45 +01003114static struct shell_seat *
3115create_shell_seat(struct weston_seat *seat)
3116{
3117 struct shell_seat *shseat;
3118
3119 shseat = calloc(1, sizeof *shseat);
3120 if (!shseat) {
3121 weston_log("no memory to allocate shell seat\n");
3122 return NULL;
3123 }
3124
3125 shseat->seat = seat;
3126 wl_list_init(&shseat->popup_grab.surfaces_list);
3127
3128 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3129 wl_signal_add(&seat->destroy_signal,
3130 &shseat->seat_destroy_listener);
3131
Jason Ekstrand024177c2014-04-21 19:42:58 -05003132 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3133 wl_list_init(&shseat->keyboard_focus_listener.link);
3134
3135 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3136 wl_list_init(&shseat->pointer_focus_listener.link);
3137
3138 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3139 wl_signal_add(&seat->updated_caps_signal,
3140 &shseat->caps_changed_listener);
3141 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3142
Giulio Camuffo5085a752013-03-25 21:42:45 +01003143 return shseat;
3144}
3145
3146static struct shell_seat *
3147get_shell_seat(struct weston_seat *seat)
3148{
3149 struct wl_listener *listener;
3150
3151 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003152 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003153
3154 return container_of(listener,
3155 struct shell_seat, seat_destroy_listener);
3156}
3157
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003158static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003159popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003160{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003161 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003162 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003163 struct shell_seat *shseat =
3164 container_of(grab, struct shell_seat, popup_grab.grab);
3165 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003166 wl_fixed_t sx, sy;
3167
Jason Ekstranda7af7042013-10-12 22:38:11 -05003168 view = weston_compositor_pick_view(pointer->seat->compositor,
3169 pointer->x, pointer->y,
3170 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003171
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 if (view && view->surface->resource &&
3173 wl_resource_get_client(view->surface->resource) == client) {
3174 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003175 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003176 weston_pointer_set_focus(pointer, NULL,
3177 wl_fixed_from_int(0),
3178 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003179 }
3180}
3181
3182static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003183popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3184 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003185{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003186 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003187 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003188 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003189
Jonas Ådahl61917c82014-08-11 22:44:02 +02003190 if (pointer->focus) {
3191 weston_view_from_global_fixed(pointer->focus, x, y,
3192 &pointer->sx, &pointer->sy);
3193 }
3194
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003195 weston_pointer_move(pointer, x, y);
3196
Neil Roberts96d790e2013-09-19 17:32:00 +01003197 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003198 weston_view_from_global_fixed(pointer->focus,
3199 pointer->x, pointer->y,
3200 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003201 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003202 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003203}
3204
3205static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003206popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003207 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003208{
3209 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003210 struct shell_seat *shseat =
3211 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003212 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003213 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003214 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003215 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003216
Neil Roberts96d790e2013-09-19 17:32:00 +01003217 resource_list = &grab->pointer->focus_resource_list;
3218 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003219 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003220 wl_resource_for_each(resource, resource_list) {
3221 wl_pointer_send_button(resource, serial,
3222 time, button, state);
3223 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003224 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003225 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003226 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003227 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003228 }
3229
Daniel Stone4dbadb12012-05-30 16:31:51 +01003230 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003231 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003232}
3233
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003234static void
3235popup_grab_cancel(struct weston_pointer_grab *grab)
3236{
3237 popup_grab_end(grab->pointer);
3238}
3239
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003240static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003241 popup_grab_focus,
3242 popup_grab_motion,
3243 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003244 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003245};
3246
3247static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003248touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3249 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3250{
3251 struct wl_resource *resource;
3252 struct shell_seat *shseat =
3253 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3254 struct wl_display *display = shseat->seat->compositor->wl_display;
3255 uint32_t serial;
3256 struct wl_list *resource_list;
3257
3258 resource_list = &grab->touch->focus_resource_list;
3259 if (!wl_list_empty(resource_list)) {
3260 serial = wl_display_get_serial(display);
3261 wl_resource_for_each(resource, resource_list) {
3262 wl_touch_send_down(resource, serial, time,
3263 grab->touch->focus->surface->resource,
3264 touch_id, sx, sy);
3265 }
3266 }
3267}
3268
3269static void
3270touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3271{
3272 struct wl_resource *resource;
3273 struct shell_seat *shseat =
3274 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3275 struct wl_display *display = shseat->seat->compositor->wl_display;
3276 uint32_t serial;
3277 struct wl_list *resource_list;
3278
3279 resource_list = &grab->touch->focus_resource_list;
3280 if (!wl_list_empty(resource_list)) {
3281 serial = wl_display_get_serial(display);
3282 wl_resource_for_each(resource, resource_list) {
3283 wl_touch_send_up(resource, serial, time, touch_id);
3284 }
3285 }
3286}
3287
3288static void
3289touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3290 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3291{
3292 struct wl_resource *resource;
3293 struct wl_list *resource_list;
3294
3295 resource_list = &grab->touch->focus_resource_list;
3296 if (!wl_list_empty(resource_list)) {
3297 wl_resource_for_each(resource, resource_list) {
3298 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3299 }
3300 }
3301}
3302
3303static void
3304touch_popup_grab_frame(struct weston_touch_grab *grab)
3305{
3306}
3307
3308static void
3309touch_popup_grab_cancel(struct weston_touch_grab *grab)
3310{
3311 touch_popup_grab_end(grab->touch);
3312}
3313
3314static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3315 touch_popup_grab_down,
3316 touch_popup_grab_up,
3317 touch_popup_grab_motion,
3318 touch_popup_grab_frame,
3319 touch_popup_grab_cancel,
3320};
3321
3322static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003323shell_surface_send_popup_done(struct shell_surface *shsurf)
3324{
3325 if (shell_surface_is_wl_shell_surface(shsurf))
3326 wl_shell_surface_send_popup_done(shsurf->resource);
3327 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003328 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003329}
3330
3331static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003332popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003333{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003334 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003335 struct shell_seat *shseat =
3336 container_of(grab, struct shell_seat, popup_grab.grab);
3337 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003338 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003339
3340 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003341 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003342 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003343 shseat->popup_grab.grab.interface = NULL;
3344 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003345 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003346 wl_list_for_each_safe(shsurf, next,
3347 &shseat->popup_grab.surfaces_list,
3348 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003349 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003350 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003351 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003352 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003353 wl_list_init(&shseat->popup_grab.surfaces_list);
3354 }
3355}
3356
3357static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003358touch_popup_grab_end(struct weston_touch *touch)
3359{
3360 struct weston_touch_grab *grab = touch->grab;
3361 struct shell_seat *shseat =
3362 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3363 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003364 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003365
3366 if (touch->grab->interface == &touch_popup_grab_interface) {
3367 weston_touch_end_grab(grab->touch);
3368 shseat->popup_grab.client = NULL;
3369 shseat->popup_grab.touch_grab.interface = NULL;
3370 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3371 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003372 wl_list_for_each_safe(shsurf, next,
3373 &shseat->popup_grab.surfaces_list,
3374 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003375 shell_surface_send_popup_done(shsurf);
3376 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003377 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003378 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003379 wl_list_init(&shseat->popup_grab.surfaces_list);
3380 }
3381}
3382
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003383static struct shell_surface *
3384get_top_popup(struct shell_seat *shseat)
3385{
3386 struct shell_surface *shsurf;
3387
3388 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3389 return NULL;
3390 } else {
3391 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3392 struct shell_surface,
3393 popup.grab_link);
3394 return shsurf;
3395 }
3396}
3397
3398static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003399add_popup_grab(struct shell_surface *shsurf,
3400 struct shell_seat *shseat,
3401 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003402{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003403 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003404 struct shell_surface *parent, *top_surface;
3405
3406 parent = get_shell_surface(shsurf->parent);
3407 top_surface = get_top_popup(shseat);
3408 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003409 (!parent ||
3410 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003411 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003412 wl_resource_post_error(shsurf->owner_resource,
3413 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003414 "xdg_popup was not created on the "
3415 "topmost popup");
3416 return -1;
3417 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003418
3419 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003420 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003421 shseat->popup_grab.client =
3422 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003423
3424 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003425 shseat->popup_grab.grab.interface =
3426 &popup_grab_interface;
3427
3428 /* We must make sure here that this popup was opened
3429 * after a mouse press, and not just by moving around
3430 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003431 if (shseat->seat->pointer->button_count > 0)
3432 shseat->popup_grab.initial_up = 0;
3433 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003434 shseat->popup_grab.touch_grab.interface =
3435 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003436 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003437
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003438 wl_list_insert(&shseat->popup_grab.surfaces_list,
3439 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003440
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003441 if (type == POINTER) {
3442 weston_pointer_start_grab(seat->pointer,
3443 &shseat->popup_grab.grab);
3444 } else if (type == TOUCH) {
3445 weston_touch_start_grab(seat->touch,
3446 &shseat->popup_grab.touch_grab);
3447 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003448 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003449 wl_list_insert(&shseat->popup_grab.surfaces_list,
3450 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003451 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003452
3453 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003454}
3455
3456static void
3457remove_popup_grab(struct shell_surface *shsurf)
3458{
3459 struct shell_seat *shseat = shsurf->popup.shseat;
3460
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003461 if (shell_surface_is_xdg_popup(shsurf) &&
3462 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003463 wl_resource_post_error(shsurf->owner_resource,
3464 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003465 "xdg_popup was destroyed while it was "
3466 "not the topmost popup.");
3467 return;
3468 }
3469
Giulio Camuffo5085a752013-03-25 21:42:45 +01003470 wl_list_remove(&shsurf->popup.grab_link);
3471 wl_list_init(&shsurf->popup.grab_link);
3472 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003473 if (shseat->popup_grab.type == POINTER) {
3474 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3475 shseat->popup_grab.grab.interface = NULL;
3476 } else if (shseat->popup_grab.type == TOUCH) {
3477 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3478 shseat->popup_grab.touch_grab.interface = NULL;
3479 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003480 }
3481}
3482
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003483static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003484shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003485{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003486 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003487 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003488
Jason Ekstranda7af7042013-10-12 22:38:11 -05003489 shsurf->surface->output = parent_view->output;
3490 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003491
Jason Ekstranda7af7042013-10-12 22:38:11 -05003492 weston_view_set_transform_parent(shsurf->view, parent_view);
3493 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3494 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003495
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003496 if (shseat->seat->pointer &&
3497 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003498 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3499 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003500 } else if (shseat->seat->touch &&
3501 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003502 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3503 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003504 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003505 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003506 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003507 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003508
3509 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003510}
3511
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003512static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003513 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003514 shell_surface_move,
3515 shell_surface_resize,
3516 shell_surface_set_toplevel,
3517 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003518 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003519 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003520 shell_surface_set_maximized,
3521 shell_surface_set_title,
3522 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003523};
3524
3525static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003526destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003527{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003528 struct shell_surface *child, *next;
3529
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003530 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3531
Giulio Camuffo5085a752013-03-25 21:42:45 +01003532 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3533 remove_popup_grab(shsurf);
3534 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003535
Alex Wubd3354b2012-04-17 17:20:49 +08003536 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003537 shell_surface_is_top_fullscreen(shsurf))
3538 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003539
Jason Ekstranda7af7042013-10-12 22:38:11 -05003540 if (shsurf->fullscreen.black_view)
3541 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003542
Alex Wubd3354b2012-04-17 17:20:49 +08003543 /* As destroy_resource() use wl_list_for_each_safe(),
3544 * we can always remove the listener.
3545 */
3546 wl_list_remove(&shsurf->surface_destroy_listener.link);
3547 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003548 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003549 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003550
Jason Ekstranda7af7042013-10-12 22:38:11 -05003551 weston_view_destroy(shsurf->view);
3552
Philip Withnall648a4dd2013-11-25 18:01:44 +00003553 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003554 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3555 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003556
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003557 wl_list_remove(&shsurf->link);
3558 free(shsurf);
3559}
3560
3561static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003562shell_destroy_shell_surface(struct wl_resource *resource)
3563{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003564 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003565
Bryan Caina46b9462014-04-04 17:41:24 -05003566 if (!wl_list_empty(&shsurf->popup.grab_link))
3567 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003568 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003569 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003570}
3571
3572static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003573shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003574{
3575 struct shell_surface *shsurf = container_of(listener,
3576 struct shell_surface,
3577 surface_destroy_listener);
3578
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003579 if (shsurf->resource)
3580 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003581
3582 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003583}
3584
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003585static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003586fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003587{
3588 struct shell_surface *shsurf = data;
3589
3590 weston_surface_destroy(shsurf->surface);
3591}
3592
3593static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003594fade_out_done(struct weston_view_animation *animation, void *data)
3595{
3596 struct shell_surface *shsurf = data;
3597 struct wl_event_loop *loop;
3598
3599 loop = wl_display_get_event_loop(
3600 shsurf->surface->compositor->wl_display);
3601
3602 if (!shsurf->destroying) {
3603 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3604 shsurf->destroying = true;
3605 }
3606}
3607
3608static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003609handle_resource_destroy(struct wl_listener *listener, void *data)
3610{
3611 struct shell_surface *shsurf =
3612 container_of(listener, struct shell_surface,
3613 resource_destroy_listener);
3614
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003615 if (!weston_surface_is_mapped(shsurf->surface))
3616 return;
3617
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003618 shsurf->surface->ref_count++;
3619
3620 pixman_region32_fini(&shsurf->surface->pending.input);
3621 pixman_region32_init(&shsurf->surface->pending.input);
3622 pixman_region32_fini(&shsurf->surface->input);
3623 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003624 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3625 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3626 fade_out_done, shsurf);
3627 } else {
3628 weston_surface_destroy(shsurf->surface);
3629 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003630}
3631
3632static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003633shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003634
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003635struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003636get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003637{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003638 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003639 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003640 else
3641 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003642}
3643
Rafael Antognollie2a34552013-12-03 15:35:45 -02003644static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003645create_common_surface(struct shell_client *owner, void *shell,
3646 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003647 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003648{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003649 struct shell_surface *shsurf;
3650
Pekka Paalanen50b67472014-10-01 15:02:41 +03003651 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003652
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003653 shsurf = calloc(1, sizeof *shsurf);
3654 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003655 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003656 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003657 }
3658
Jason Ekstranda7af7042013-10-12 22:38:11 -05003659 shsurf->view = weston_view_create(surface);
3660 if (!shsurf->view) {
3661 weston_log("no memory to allocate shell surface\n");
3662 free(shsurf);
3663 return NULL;
3664 }
3665
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003666 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003667 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003668 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003669
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003670 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3671 wl_resource_add_destroy_listener(surface->resource,
3672 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003673 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003674
Tiago Vignattibc052c92012-04-19 16:18:18 +03003675 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003676 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003677 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003678 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003679 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003680 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003681 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3682 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003683 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003684 wl_list_init(&shsurf->fullscreen.transform.link);
3685
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003686 shsurf->output = get_default_output(shsurf->shell->compositor);
3687
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003688 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003689 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003690 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003691 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003692
3693 /* init link so its safe to always remove it in destroy_shell_surface */
3694 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003695 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003696
Pekka Paalanen460099f2012-01-20 16:48:25 +02003697 /* empty when not in use */
3698 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003699 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003700
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003701 wl_list_init(&shsurf->workspace_transform.link);
3702
Philip Withnall648a4dd2013-11-25 18:01:44 +00003703 wl_list_init(&shsurf->children_link);
3704 wl_list_init(&shsurf->children_list);
3705 shsurf->parent = NULL;
3706
Pekka Paalanenb5026542014-11-12 15:09:24 +02003707 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003708
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003709 shsurf->client = client;
3710
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003711 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003712}
3713
Rafael Antognollie2a34552013-12-03 15:35:45 -02003714static struct shell_surface *
3715create_shell_surface(void *shell, struct weston_surface *surface,
3716 const struct weston_shell_client *client)
3717{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003718 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003719}
3720
Jason Ekstranda7af7042013-10-12 22:38:11 -05003721static struct weston_view *
3722get_primary_view(void *shell, struct shell_surface *shsurf)
3723{
3724 return shsurf->view;
3725}
3726
Tiago Vignattibc052c92012-04-19 16:18:18 +03003727static void
3728shell_get_shell_surface(struct wl_client *client,
3729 struct wl_resource *resource,
3730 uint32_t id,
3731 struct wl_resource *surface_resource)
3732{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003733 struct weston_surface *surface =
3734 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003735 struct shell_client *sc = wl_resource_get_user_data(resource);
3736 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003737 struct shell_surface *shsurf;
3738
Pekka Paalanen50b67472014-10-01 15:02:41 +03003739 if (weston_surface_set_role(surface, "wl_shell_surface",
3740 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003741 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003742
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003743 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003744 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003745 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003746 return;
3747 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003748
Jason Ekstranda85118c2013-06-27 20:17:02 -05003749 shsurf->resource =
3750 wl_resource_create(client,
3751 &wl_shell_surface_interface, 1, id);
3752 wl_resource_set_implementation(shsurf->resource,
3753 &shell_surface_implementation,
3754 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003755 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003756}
3757
Rafael Antognollie2a34552013-12-03 15:35:45 -02003758static bool
3759shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3760{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003761 /* A shell surface without a resource is created from xwayland
3762 * and is considered a wl_shell surface for now. */
3763
3764 return shsurf->resource == NULL ||
3765 wl_resource_instance_of(shsurf->resource,
3766 &wl_shell_surface_interface,
3767 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003768}
3769
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003770static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003771 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003772};
3773
Rafael Antognollie2a34552013-12-03 15:35:45 -02003774/****************************
3775 * xdg-shell implementation */
3776
3777static void
3778xdg_surface_destroy(struct wl_client *client,
3779 struct wl_resource *resource)
3780{
3781 wl_resource_destroy(resource);
3782}
3783
3784static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003785xdg_surface_set_parent(struct wl_client *client,
3786 struct wl_resource *resource,
3787 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003788{
3789 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003790 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003791
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003792 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003793 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003794 shell_surface_set_parent(shsurf, parent->surface);
3795 } else {
3796 shell_surface_set_parent(shsurf, NULL);
3797 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003798}
3799
3800static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003801xdg_surface_set_app_id(struct wl_client *client,
3802 struct wl_resource *resource,
3803 const char *app_id)
3804{
3805 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3806
3807 free(shsurf->class);
3808 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003809 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003810}
3811
3812static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003813xdg_surface_show_window_menu(struct wl_client *client,
3814 struct wl_resource *surface_resource,
3815 struct wl_resource *seat_resource,
3816 uint32_t serial,
3817 int32_t x,
3818 int32_t y)
3819{
3820 /* TODO */
3821}
3822
3823static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003824xdg_surface_set_title(struct wl_client *client,
3825 struct wl_resource *resource, const char *title)
3826{
3827 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3828
3829 set_title(shsurf, title);
3830}
3831
3832static void
3833xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3834 struct wl_resource *seat_resource, uint32_t serial)
3835{
3836 common_surface_move(resource, seat_resource, serial);
3837}
3838
3839static void
3840xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3841 struct wl_resource *seat_resource, uint32_t serial,
3842 uint32_t edges)
3843{
3844 common_surface_resize(resource, seat_resource, serial, edges);
3845}
3846
3847static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003848xdg_surface_ack_configure(struct wl_client *client,
3849 struct wl_resource *resource,
3850 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003851{
3852 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3853
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003854 if (shsurf->state_requested) {
3855 shsurf->next_state = shsurf->requested_state;
3856 shsurf->state_changed = true;
3857 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003858 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003859}
3860
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003861static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003862xdg_surface_set_window_geometry(struct wl_client *client,
3863 struct wl_resource *resource,
3864 int32_t x,
3865 int32_t y,
3866 int32_t width,
3867 int32_t height)
3868{
3869 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3870
3871 set_window_geometry(shsurf, x, y, width, height);
3872}
3873
3874static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003875xdg_surface_set_maximized(struct wl_client *client,
3876 struct wl_resource *resource)
3877{
3878 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003879 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003880
3881 shsurf->state_requested = true;
3882 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003883
3884 if (!weston_surface_is_mapped(shsurf->surface))
3885 output = get_focused_output(shsurf->surface->compositor);
3886 else
3887 output = shsurf->surface->output;
3888
3889 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003890 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003891}
3892
3893static void
3894xdg_surface_unset_maximized(struct wl_client *client,
3895 struct wl_resource *resource)
3896{
3897 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3898
3899 shsurf->state_requested = true;
3900 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003901 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003902}
3903
3904static void
3905xdg_surface_set_fullscreen(struct wl_client *client,
3906 struct wl_resource *resource,
3907 struct wl_resource *output_resource)
3908{
3909 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3910 struct weston_output *output;
3911
3912 shsurf->state_requested = true;
3913 shsurf->requested_state.fullscreen = true;
3914
3915 if (output_resource)
3916 output = wl_resource_get_user_data(output_resource);
3917 else
3918 output = NULL;
3919
Marek Chalupa052917a2014-09-02 11:35:12 +02003920 /* handle clients launching in fullscreen */
3921 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3922 /* Set the output to the one that has focus currently. */
3923 assert(shsurf->surface);
3924 output = get_focused_output(shsurf->surface->compositor);
3925 }
3926
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003927 shell_surface_set_output(shsurf, output);
3928 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003929
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003930 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003931}
3932
3933static void
3934xdg_surface_unset_fullscreen(struct wl_client *client,
3935 struct wl_resource *resource)
3936{
3937 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3938
3939 shsurf->state_requested = true;
3940 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003941 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003942}
3943
3944static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003945xdg_surface_set_minimized(struct wl_client *client,
3946 struct wl_resource *resource)
3947{
3948 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3949
3950 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3951 return;
3952
3953 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003954 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003955}
3956
Rafael Antognollie2a34552013-12-03 15:35:45 -02003957static const struct xdg_surface_interface xdg_surface_implementation = {
3958 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003959 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003960 xdg_surface_set_title,
3961 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003962 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003963 xdg_surface_move,
3964 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003965 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003966 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003967 xdg_surface_set_maximized,
3968 xdg_surface_unset_maximized,
3969 xdg_surface_set_fullscreen,
3970 xdg_surface_unset_fullscreen,
3971 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003972};
3973
3974static void
3975xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003976 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003977{
3978 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003979 uint32_t *s;
3980 struct wl_array states;
3981 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003982
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003983 assert(shsurf);
3984
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003985 if (!shsurf->resource)
3986 return;
3987
3988 wl_array_init(&states);
3989 if (shsurf->requested_state.fullscreen) {
3990 s = wl_array_add(&states, sizeof *s);
3991 *s = XDG_SURFACE_STATE_FULLSCREEN;
3992 } else if (shsurf->requested_state.maximized) {
3993 s = wl_array_add(&states, sizeof *s);
3994 *s = XDG_SURFACE_STATE_MAXIMIZED;
3995 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003996 if (shsurf->resize_edges != 0) {
3997 s = wl_array_add(&states, sizeof *s);
3998 *s = XDG_SURFACE_STATE_RESIZING;
3999 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004000 if (shsurf->focus_count > 0) {
4001 s = wl_array_add(&states, sizeof *s);
4002 *s = XDG_SURFACE_STATE_ACTIVATED;
4003 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004004
4005 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4006 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4007
4008 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004009}
4010
4011static const struct weston_shell_client xdg_client = {
4012 xdg_send_configure
4013};
4014
4015static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004016xdg_shell_destroy(struct wl_client *client,
4017 struct wl_resource *resource)
4018{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004019 struct shell_client *sc = wl_resource_get_user_data(resource);
4020 struct wl_resource *shsurf_resource;
4021 struct shell_surface *shsurf;
4022
4023 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4024 shsurf = wl_resource_get_user_data(shsurf_resource);
4025 if (shsurf->owner_resource == resource) {
4026 wl_resource_post_error(
4027 resource,
4028 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4029 "not all child surface objects destroyed");
4030 return;
4031 }
4032 }
4033
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004034 wl_resource_destroy(resource);
4035}
4036
4037static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004038xdg_use_unstable_version(struct wl_client *client,
4039 struct wl_resource *resource,
4040 int32_t version)
4041{
4042 if (version > 1) {
4043 wl_resource_post_error(resource,
4044 1,
4045 "xdg-shell:: version not implemented yet.");
4046 return;
4047 }
4048}
4049
4050static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004051create_xdg_surface(struct shell_client *owner, void *shell,
4052 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004053 const struct weston_shell_client *client)
4054{
4055 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004056
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004057 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004058 if (!shsurf)
4059 return NULL;
4060
Pekka Paalanenb5026542014-11-12 15:09:24 +02004061 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004062
4063 return shsurf;
4064}
4065
4066static void
4067xdg_get_xdg_surface(struct wl_client *client,
4068 struct wl_resource *resource,
4069 uint32_t id,
4070 struct wl_resource *surface_resource)
4071{
4072 struct weston_surface *surface =
4073 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004074 struct shell_client *sc = wl_resource_get_user_data(resource);
4075 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004076 struct shell_surface *shsurf;
4077
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004078 shsurf = get_shell_surface(surface);
4079 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4080 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4081 "This wl_surface is already an "
4082 "xdg_surface");
4083 return;
4084 }
4085
Pekka Paalanen50b67472014-10-01 15:02:41 +03004086 if (weston_surface_set_role(surface, "xdg_surface",
4087 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004088 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004089
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004090 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004091 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004092 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004093 return;
4094 }
4095
4096 shsurf->resource =
4097 wl_resource_create(client,
4098 &xdg_surface_interface, 1, id);
4099 wl_resource_set_implementation(shsurf->resource,
4100 &xdg_surface_implementation,
4101 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004102 shsurf->owner_resource = resource;
4103 wl_list_insert(&sc->surface_list,
4104 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004105}
4106
4107static bool
4108shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4109{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004110 return shsurf->resource &&
4111 wl_resource_instance_of(shsurf->resource,
4112 &xdg_surface_interface,
4113 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004114}
4115
4116/* xdg-popup implementation */
4117
4118static void
4119xdg_popup_destroy(struct wl_client *client,
4120 struct wl_resource *resource)
4121{
4122 wl_resource_destroy(resource);
4123}
4124
Rafael Antognollie2a34552013-12-03 15:35:45 -02004125static const struct xdg_popup_interface xdg_popup_implementation = {
4126 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004127};
4128
4129static void
4130xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004131 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004132{
4133}
4134
4135static const struct weston_shell_client xdg_popup_client = {
4136 xdg_popup_send_configure
4137};
4138
4139static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004140create_xdg_popup(struct shell_client *owner, void *shell,
4141 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004142 const struct weston_shell_client *client,
4143 struct weston_surface *parent,
4144 struct shell_seat *seat,
4145 uint32_t serial,
4146 int32_t x, int32_t y)
4147{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004148 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004149
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004150 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004151 if (!shsurf)
4152 return NULL;
4153
Pekka Paalanenb5026542014-11-12 15:09:24 +02004154 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004155 shsurf->popup.shseat = seat;
4156 shsurf->popup.serial = serial;
4157 shsurf->popup.x = x;
4158 shsurf->popup.y = y;
4159 shell_surface_set_parent(shsurf, parent);
4160
4161 return shsurf;
4162}
4163
4164static void
4165xdg_get_xdg_popup(struct wl_client *client,
4166 struct wl_resource *resource,
4167 uint32_t id,
4168 struct wl_resource *surface_resource,
4169 struct wl_resource *parent_resource,
4170 struct wl_resource *seat_resource,
4171 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004172 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004173{
4174 struct weston_surface *surface =
4175 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004176 struct shell_client *sc = wl_resource_get_user_data(resource);
4177 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004178 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004179 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004180 struct weston_surface *parent;
4181 struct shell_seat *seat;
4182
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004183 shsurf = get_shell_surface(surface);
4184 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4185 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4186 "This wl_surface is already an "
4187 "xdg_popup");
4188 return;
4189 }
4190
Pekka Paalanen50b67472014-10-01 15:02:41 +03004191 if (weston_surface_set_role(surface, "xdg_popup",
4192 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004193 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004194
4195 if (!parent_resource) {
4196 wl_resource_post_error(surface_resource,
4197 WL_DISPLAY_ERROR_INVALID_OBJECT,
4198 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004199 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004200 }
4201
4202 parent = wl_resource_get_user_data(parent_resource);
4203 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4204
Jonas Ådahlee45a552015-03-18 16:37:47 +08004205 /* Verify that we are creating the top most popup when mapping,
4206 * as it's not until then we know whether it was mapped as most
4207 * top level or not. */
4208
4209 parent_shsurf = get_shell_surface(parent);
4210 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4211 !shell_surface_is_xdg_surface(parent_shsurf)) {
4212 wl_resource_post_error(resource,
4213 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4214 "xdg_popup parent was invalid");
4215 return;
4216 }
4217
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004218 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004219 parent, seat, serial, x, y);
4220 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004221 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004222 return;
4223 }
4224
4225 shsurf->resource =
4226 wl_resource_create(client,
4227 &xdg_popup_interface, 1, id);
4228 wl_resource_set_implementation(shsurf->resource,
4229 &xdg_popup_implementation,
4230 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004231 shsurf->owner_resource = resource;
4232 wl_list_insert(&sc->surface_list,
4233 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004234}
4235
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004236static void
4237xdg_pong(struct wl_client *client,
4238 struct wl_resource *resource, uint32_t serial)
4239{
4240 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004241
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004242 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004243}
4244
Rafael Antognollie2a34552013-12-03 15:35:45 -02004245static bool
4246shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4247{
4248 return wl_resource_instance_of(shsurf->resource,
4249 &xdg_popup_interface,
4250 &xdg_popup_implementation);
4251}
4252
4253static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004254 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004255 xdg_use_unstable_version,
4256 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004257 xdg_get_xdg_popup,
4258 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004259};
4260
4261static int
4262xdg_shell_unversioned_dispatch(const void *implementation,
4263 void *_target, uint32_t opcode,
4264 const struct wl_message *message,
4265 union wl_argument *args)
4266{
4267 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004268 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004269
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004270 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004271 wl_resource_post_error(resource,
4272 WL_DISPLAY_ERROR_INVALID_OBJECT,
4273 "must call use_unstable_version first");
4274 return 0;
4275 }
4276
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004277#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004278
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004279 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4280 "shell implementation doesn't match protocol version");
4281
Rafael Antognollie2a34552013-12-03 15:35:45 -02004282 if (args[0].i != XDG_SERVER_VERSION) {
4283 wl_resource_post_error(resource,
4284 WL_DISPLAY_ERROR_INVALID_OBJECT,
4285 "incompatible version, server is %d "
4286 "client wants %d",
4287 XDG_SERVER_VERSION, args[0].i);
4288 return 0;
4289 }
4290
4291 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004292 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004293
4294 return 1;
4295}
4296
4297/* end of xdg-shell implementation */
4298/***********************************/
4299
Kristian Høgsberg07937562011-04-12 17:25:42 -04004300static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004301shell_fade(struct desktop_shell *shell, enum fade_type type);
4302
Pekka Paalanen77346a62011-11-30 16:26:35 +02004303static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004304configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004305{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004306 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004307
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004308 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 if (v->output == ev->output && v != ev) {
4310 weston_view_unmap(v);
4311 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004312 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004313 }
4314 }
4315
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004316 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004317
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004318 if (wl_list_empty(&ev->layer_link.link)) {
4319 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004320 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004321 }
4322}
4323
Pekka Paalanen8274d902014-08-06 19:36:51 +03004324static int
4325background_get_label(struct weston_surface *surface, char *buf, size_t len)
4326{
4327 return snprintf(buf, len, "background for output %s",
4328 surface->output->name);
4329}
4330
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004331static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004332background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004333{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004334 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004335 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004336
Jason Ekstranda7af7042013-10-12 22:38:11 -05004337 view = container_of(es->views.next, struct weston_view, surface_link);
4338
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004339 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004340}
4341
4342static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004343desktop_shell_set_background(struct wl_client *client,
4344 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004345 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004346 struct wl_resource *surface_resource)
4347{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004348 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004349 struct weston_surface *surface =
4350 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004351 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004352
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004353 if (surface->configure) {
4354 wl_resource_post_error(surface_resource,
4355 WL_DISPLAY_ERROR_INVALID_OBJECT,
4356 "surface role already assigned");
4357 return;
4358 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004359
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4361 weston_view_destroy(view);
4362 view = weston_view_create(surface);
4363
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004364 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004365 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004366 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004367 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004368 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004369 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004370 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004371 surface->output->width,
4372 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004373}
4374
Pekka Paalanen8274d902014-08-06 19:36:51 +03004375static int
4376panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4377{
4378 return snprintf(buf, len, "panel for output %s",
4379 surface->output->name);
4380}
4381
Kristian Høgsberg75840622011-09-06 13:48:16 -04004382static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004383panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004384{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004385 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004386 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004387
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 view = container_of(es->views.next, struct weston_view, surface_link);
4389
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004390 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004391}
4392
4393static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004394desktop_shell_set_panel(struct wl_client *client,
4395 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004396 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004397 struct wl_resource *surface_resource)
4398{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004399 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004400 struct weston_surface *surface =
4401 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004403
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004404 if (surface->configure) {
4405 wl_resource_post_error(surface_resource,
4406 WL_DISPLAY_ERROR_INVALID_OBJECT,
4407 "surface role already assigned");
4408 return;
4409 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004410
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4412 weston_view_destroy(view);
4413 view = weston_view_create(surface);
4414
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004415 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004416 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004417 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004418 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004419 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004420 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004421 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004422 surface->output->width,
4423 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004424}
4425
Pekka Paalanen8274d902014-08-06 19:36:51 +03004426static int
4427lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4428{
4429 return snprintf(buf, len, "lock window");
4430}
4431
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004432static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004433lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004434{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004435 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436 struct weston_view *view;
4437
4438 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004439
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004440 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004441 return;
4442
Jason Ekstranda7af7042013-10-12 22:38:11 -05004443 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004444
4445 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004446 weston_layer_entry_insert(&shell->lock_layer.view_list,
4447 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004448 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004449 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004450 }
4451}
4452
4453static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004454handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004455{
Tiago Vignattibe143262012-04-16 17:31:41 +03004456 struct desktop_shell *shell =
4457 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004458
Martin Minarik6d118362012-06-07 18:01:59 +02004459 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004460 shell->lock_surface = NULL;
4461}
4462
4463static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004464desktop_shell_set_lock_surface(struct wl_client *client,
4465 struct wl_resource *resource,
4466 struct wl_resource *surface_resource)
4467{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004468 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004469 struct weston_surface *surface =
4470 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004471
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004472 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004473
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004474 if (!shell->locked)
4475 return;
4476
Pekka Paalanen98262232011-12-01 10:42:22 +02004477 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004478
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004479 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004480 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004481 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004482
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004483 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004484 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004485 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004486 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004487}
4488
4489static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004490resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004491{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004492 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004493
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004494 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004495 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004496 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004497 &shell->input_panel_layer.link);
4498 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004499 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004500 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004501 wl_list_insert(&shell->compositor->cursor_layer.link,
4502 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004503 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004504 wl_list_insert(&shell->fullscreen_layer.link,
4505 &shell->panel_layer.link);
4506 wl_list_insert(&shell->panel_layer.link,
4507 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004508
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004509 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004510
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004511 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004512 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004513 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004514}
4515
4516static void
4517desktop_shell_unlock(struct wl_client *client,
4518 struct wl_resource *resource)
4519{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004520 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004521
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004522 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004523
4524 if (shell->locked)
4525 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004526}
4527
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004528static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004529desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004530 struct wl_resource *resource,
4531 struct wl_resource *surface_resource)
4532{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004533 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004534
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004535 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004536 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004537}
4538
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004539static void
4540desktop_shell_desktop_ready(struct wl_client *client,
4541 struct wl_resource *resource)
4542{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004543 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004544
4545 shell_fade_startup(shell);
4546}
4547
Jonny Lamb765760d2014-08-20 15:53:19 +02004548static void
4549desktop_shell_set_panel_position(struct wl_client *client,
4550 struct wl_resource *resource,
4551 uint32_t position)
4552{
4553 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4554
4555 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4556 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4557 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4558 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4559 wl_resource_post_error(resource,
4560 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4561 "bad position argument");
4562 return;
4563 }
4564
4565 shell->panel_position = position;
4566}
4567
Kristian Høgsberg75840622011-09-06 13:48:16 -04004568static const struct desktop_shell_interface desktop_shell_implementation = {
4569 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004570 desktop_shell_set_panel,
4571 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004572 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004573 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004574 desktop_shell_desktop_ready,
4575 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004576};
4577
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004578static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004579get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004580{
4581 struct shell_surface *shsurf;
4582
4583 shsurf = get_shell_surface(surface);
4584 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004585 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004586 return shsurf->type;
4587}
4588
Kristian Høgsberg75840622011-09-06 13:48:16 -04004589static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004590move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004591{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004592 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004593 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004594 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004595
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004596 if (seat->pointer->focus == NULL)
4597 return;
4598
4599 focus = seat->pointer->focus->surface;
4600
Pekka Paalanen01388e22013-04-25 13:57:44 +03004601 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004602 if (surface == NULL)
4603 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004604
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004605 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004606 if (shsurf == NULL || shsurf->state.fullscreen ||
4607 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004608 return;
4609
Derek Foreman8aeeac82015-01-30 13:24:36 -06004610 surface_move(shsurf, seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004611}
4612
4613static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004614maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4615{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004616 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004617 struct weston_surface *surface;
4618 struct shell_surface *shsurf;
4619
4620 surface = weston_surface_get_main_surface(focus);
4621 if (surface == NULL)
4622 return;
4623
4624 shsurf = get_shell_surface(surface);
4625 if (shsurf == NULL)
4626 return;
4627
4628 if (!shell_surface_is_xdg_surface(shsurf))
4629 return;
4630
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004631 shsurf->state_requested = true;
4632 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004633 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004634}
4635
4636static void
4637fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4638{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004639 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004640 struct weston_surface *surface;
4641 struct shell_surface *shsurf;
4642
4643 surface = weston_surface_get_main_surface(focus);
4644 if (surface == NULL)
4645 return;
4646
4647 shsurf = get_shell_surface(surface);
4648 if (shsurf == NULL)
4649 return;
4650
4651 if (!shell_surface_is_xdg_surface(shsurf))
4652 return;
4653
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004654 shsurf->state_requested = true;
4655 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004656 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004657 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004658}
4659
4660static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004661touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4662{
Derek Foreman362656b2014-09-04 10:23:05 -05004663 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004664 struct weston_surface *surface;
4665 struct shell_surface *shsurf;
4666
Derek Foreman362656b2014-09-04 10:23:05 -05004667 if (seat->touch->focus == NULL)
4668 return;
4669
4670 focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004671 surface = weston_surface_get_main_surface(focus);
4672 if (surface == NULL)
4673 return;
4674
4675 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004676 if (shsurf == NULL || shsurf->state.fullscreen ||
4677 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004678 return;
4679
Derek Foreman8aeeac82015-01-30 13:24:36 -06004680 surface_touch_move(shsurf, seat);
Neil Robertsaba0f252013-10-03 16:43:05 +01004681}
4682
4683static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004684resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004685{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004686 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004687 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004688 uint32_t edges = 0;
4689 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004690 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004691
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004692 if (seat->pointer->focus == NULL)
4693 return;
4694
4695 focus = seat->pointer->focus->surface;
4696
Pekka Paalanen01388e22013-04-25 13:57:44 +03004697 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004698 if (surface == NULL)
4699 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004700
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004701 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004702 if (shsurf == NULL || shsurf->state.fullscreen ||
4703 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004704 return;
4705
Jason Ekstranda7af7042013-10-12 22:38:11 -05004706 weston_view_from_global(shsurf->view,
4707 wl_fixed_to_int(seat->pointer->grab_x),
4708 wl_fixed_to_int(seat->pointer->grab_y),
4709 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004710
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004711 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004712 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004713 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004714 edges |= 0;
4715 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004716 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004717
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004718 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004719 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004720 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004721 edges |= 0;
4722 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004723 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004724
Derek Foreman8aeeac82015-01-30 13:24:36 -06004725 surface_resize(shsurf, seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004726}
4727
4728static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004729surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004730 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004731{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004732 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004733 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004734 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004735 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004736
Pekka Paalanen01388e22013-04-25 13:57:44 +03004737 /* XXX: broken for windows containing sub-surfaces */
4738 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004739 if (surface == NULL)
4740 return;
4741
4742 shsurf = get_shell_surface(surface);
4743 if (!shsurf)
4744 return;
4745
Jason Ekstranda7af7042013-10-12 22:38:11 -05004746 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004747
Jason Ekstranda7af7042013-10-12 22:38:11 -05004748 if (shsurf->view->alpha > 1.0)
4749 shsurf->view->alpha = 1.0;
4750 if (shsurf->view->alpha < step)
4751 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004752
Jason Ekstranda7af7042013-10-12 22:38:11 -05004753 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004754 weston_surface_damage(surface);
4755}
4756
4757static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004758do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004759 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004760{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004761 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004762 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004763 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004764
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004765 if (!seat->pointer) {
4766 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4767 return;
4768 }
4769
Scott Moreauccbf29d2012-02-22 14:21:41 -07004770 wl_list_for_each(output, &compositor->output_list, link) {
4771 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004772 wl_fixed_to_double(seat->pointer->x),
4773 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004774 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004775 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004776 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004777 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004778 increment = -output->zoom.increment;
4779 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004780 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004781 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004782 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004783 else
4784 increment = 0;
4785
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004786 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004787
Scott Moreaue6603982012-06-11 13:07:51 -06004788 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004789 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004790 else if (output->zoom.level > output->zoom.max_level)
4791 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004792 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004793 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004794 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004795
Scott Moreaue6603982012-06-11 13:07:51 -06004796 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004797
Jason Ekstranda7af7042013-10-12 22:38:11 -05004798 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004799 }
4800 }
4801}
4802
Scott Moreauccbf29d2012-02-22 14:21:41 -07004803static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004804zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004805 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004806{
4807 do_zoom(seat, time, 0, axis, value);
4808}
4809
4810static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004811zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004812 void *data)
4813{
4814 do_zoom(seat, time, key, 0, 0);
4815}
4816
4817static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004818terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004819 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004820{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004821 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004822
Daniel Stone325fc2d2012-05-30 16:31:58 +01004823 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004824}
4825
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004826static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004827rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4828 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004829{
4830 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004831 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004832 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004833 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004834 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004835
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004836 weston_pointer_move(pointer, x, y);
4837
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004838 if (!shsurf)
4839 return;
4840
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004841 cx = 0.5f * shsurf->surface->width;
4842 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004843
Daniel Stone37816df2012-05-16 18:45:18 +01004844 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4845 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004846 r = sqrtf(dx * dx + dy * dy);
4847
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004848 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004850
4851 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004852 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004853 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004854
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004855 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004856 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004857
4858 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004859 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004860 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004861 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004862 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004863
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004864 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004865 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004866 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004867 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004868 wl_list_init(&shsurf->rotation.transform.link);
4869 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004870 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004871 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004872
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004873 /* We need to adjust the position of the surface
4874 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004875 cposx = shsurf->view->geometry.x + cx;
4876 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004877 dposx = rotate->center.x - cposx;
4878 dposy = rotate->center.y - cposy;
4879 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004880 weston_view_set_position(shsurf->view,
4881 shsurf->view->geometry.x + dposx,
4882 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004883 }
4884
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004885 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004886 * lazily applies the damage due to rotation update.
4887 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004888 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004889}
4890
4891static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004892rotate_grab_button(struct weston_pointer_grab *grab,
4893 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004894{
4895 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004896 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004897 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004898 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004899 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004900
Daniel Stone4dbadb12012-05-30 16:31:51 +01004901 if (pointer->button_count == 0 &&
4902 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004903 if (shsurf)
4904 weston_matrix_multiply(&shsurf->rotation.rotation,
4905 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004906 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004907 free(rotate);
4908 }
4909}
4910
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004911static void
4912rotate_grab_cancel(struct weston_pointer_grab *grab)
4913{
4914 struct rotate_grab *rotate =
4915 container_of(grab, struct rotate_grab, base.grab);
4916
4917 shell_grab_end(&rotate->base);
4918 free(rotate);
4919}
4920
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004921static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004922 noop_grab_focus,
4923 rotate_grab_motion,
4924 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004925 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004926};
4927
4928static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004929surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004930{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004931 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004932 float dx, dy;
4933 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004934
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935 rotate = malloc(sizeof *rotate);
4936 if (!rotate)
4937 return;
4938
Jason Ekstranda7af7042013-10-12 22:38:11 -05004939 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004940 surface->surface->width * 0.5f,
4941 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004942 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004943
Daniel Stone37816df2012-05-16 18:45:18 +01004944 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4945 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004946 r = sqrtf(dx * dx + dy * dy);
4947 if (r > 20.0f) {
4948 struct weston_matrix inverse;
4949
4950 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004951 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004952 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004953
4954 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004955 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004956 } else {
4957 weston_matrix_init(&surface->rotation.rotation);
4958 weston_matrix_init(&rotate->rotation);
4959 }
4960
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004961 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4962 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963}
4964
4965static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004966rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004967 void *data)
4968{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004969 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004970 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004971 struct shell_surface *surface;
4972
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004973 if (seat->pointer->focus == NULL)
4974 return;
4975
4976 focus = seat->pointer->focus->surface;
4977
Pekka Paalanen01388e22013-04-25 13:57:44 +03004978 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004979 if (base_surface == NULL)
4980 return;
4981
4982 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004983 if (surface == NULL || surface->state.fullscreen ||
4984 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004985 return;
4986
4987 surface_rotate(surface, seat);
4988}
4989
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004990/* Move all fullscreen layers down to the current workspace and hide their
4991 * black views. The surfaces' state is set to both fullscreen and lowered,
4992 * and this is reversed when such a surface is re-configured, see
4993 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4994 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004995 * lowering_output = NULL - Lower on all outputs, else only lower on the
4996 * specified output.
4997 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004998 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004999 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005000void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005001lower_fullscreen_layer(struct desktop_shell *shell,
5002 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005003{
5004 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005005 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005006
5007 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005008 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005009 &shell->fullscreen_layer.view_list.link,
5010 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005011 struct shell_surface *shsurf = get_shell_surface(view->surface);
5012
5013 if (!shsurf)
5014 continue;
5015
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005016 /* Only lower surfaces which have lowering_output as their fullscreen
5017 * output, unless a NULL output asks for lowering on all outputs.
5018 */
5019 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5020 continue;
5021
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005022 /* We can have a non-fullscreen popup for a fullscreen surface
5023 * in the fullscreen layer. */
5024 if (shsurf->state.fullscreen) {
5025 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005026 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5027 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005028 weston_view_damage_below(shsurf->fullscreen.black_view);
5029
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005030 }
5031
5032 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005033 weston_layer_entry_remove(&view->layer_link);
5034 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005035 weston_view_damage_below(view);
5036 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005037
5038 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005039 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005040}
5041
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005042void
Tiago Vignattibe143262012-04-16 17:31:41 +03005043activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005044 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005045{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005046 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005047 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005048 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005049 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005050 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005051
Pekka Paalanen01388e22013-04-25 13:57:44 +03005052 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005053 shsurf = get_shell_surface(main_surface);
5054 assert(shsurf);
5055
5056 /* Only demote fullscreen surfaces on the output of activated shsurf.
5057 * Leave fullscreen surfaces on unrelated outputs alone. */
5058 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005059
Daniel Stone37816df2012-05-16 18:45:18 +01005060 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005061
Jonas Ådahl8538b222012-08-29 22:13:03 +02005062 state = ensure_focus_state(shell, seat);
5063 if (state == NULL)
5064 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005065
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005066 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005067 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005068
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005069 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005070 shell_configure_fullscreen(shsurf);
5071 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005072 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005073
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005074 /* Update the surface’s layer. This brings it to the top of the stacking
5075 * order as appropriate. */
5076 shell_surface_update_layer(shsurf);
5077
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005078 if (shell->focus_animation_type != ANIMATION_NONE) {
5079 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005080 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005081 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005082}
5083
Alex Wu21858432012-04-01 20:13:08 +08005084/* no-op func for checking black surface */
5085static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005086black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005087{
5088}
5089
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005090static bool
Alex Wu21858432012-04-01 20:13:08 +08005091is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5092{
5093 if (es->configure == black_surface_configure) {
5094 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005095 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005096 return true;
5097 }
5098 return false;
5099}
5100
Kristian Høgsberg75840622011-09-06 13:48:16 -04005101static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005102activate_binding(struct weston_seat *seat,
5103 struct desktop_shell *shell,
5104 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005105{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005106 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005107
Alex Wu9c35e6b2012-03-05 14:13:13 +08005108 if (!focus)
5109 return;
5110
Pekka Paalanen01388e22013-04-25 13:57:44 +03005111 if (is_black_surface(focus, &main_surface))
5112 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005113
Pekka Paalanen01388e22013-04-25 13:57:44 +03005114 main_surface = weston_surface_get_main_surface(focus);
5115 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005116 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005117
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005118 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005119}
5120
5121static void
5122click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
5123 void *data)
5124{
5125 if (seat->pointer->grab != &seat->pointer->default_grab)
5126 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005127 if (seat->pointer->focus == NULL)
5128 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005129
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005130 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005131}
5132
5133static void
5134touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
5135{
5136 if (seat->touch->grab != &seat->touch->default_grab)
5137 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005138 if (seat->touch->focus == NULL)
5139 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005140
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07005141 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005142}
5143
5144static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005145unfocus_all_seats(struct desktop_shell *shell)
5146{
5147 struct weston_seat *seat, *next;
5148
5149 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5150 if (seat->keyboard == NULL)
5151 continue;
5152
5153 weston_keyboard_set_focus(seat->keyboard, NULL);
5154 }
5155}
5156
5157static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005158lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005159{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005160 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005161
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005162 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005163 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005164 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005165 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005166
5167 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005168
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005169 /* Hide all surfaces by removing the fullscreen, panel and
5170 * toplevel layers. This way nothing else can show or receive
5171 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005172
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005173 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005174 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005175 if (shell->showing_input_panels)
5176 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005177 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005178 wl_list_insert(&shell->compositor->cursor_layer.link,
5179 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005180
Neil Robertsb4a91702014-04-09 16:33:32 +01005181 /* Remove the keyboard focus on all seats. This will be
5182 * restored to the workspace's saved state via
5183 * restore_focus_state when the compositor is unlocked */
5184 unfocus_all_seats(shell);
5185
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005186 /* TODO: disable bindings that should not work while locked. */
5187
5188 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005189}
5190
5191static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005192unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005193{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005194 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005195 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005196 return;
5197 }
5198
5199 /* If desktop-shell client has gone away, unlock immediately. */
5200 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005201 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005202 return;
5203 }
5204
5205 if (shell->prepare_event_sent)
5206 return;
5207
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005208 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005209 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005210}
5211
5212static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005213shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005214{
5215 struct desktop_shell *shell = data;
5216
5217 shell->fade.animation = NULL;
5218
5219 switch (shell->fade.type) {
5220 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005221 weston_surface_destroy(shell->fade.view->surface);
5222 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005223 break;
5224 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005225 lock(shell);
5226 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005227 default:
5228 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005229 }
5230}
5231
Jason Ekstranda7af7042013-10-12 22:38:11 -05005232static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005233shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005234{
5235 struct weston_compositor *compositor = shell->compositor;
5236 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005237 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005238
5239 surface = weston_surface_create(compositor);
5240 if (!surface)
5241 return NULL;
5242
Jason Ekstranda7af7042013-10-12 22:38:11 -05005243 view = weston_view_create(surface);
5244 if (!view) {
5245 weston_surface_destroy(surface);
5246 return NULL;
5247 }
5248
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005249 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005250 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005251 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005252 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5253 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005254 pixman_region32_init(&surface->input);
5255
Jason Ekstranda7af7042013-10-12 22:38:11 -05005256 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005257}
5258
5259static void
5260shell_fade(struct desktop_shell *shell, enum fade_type type)
5261{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005262 float tint;
5263
5264 switch (type) {
5265 case FADE_IN:
5266 tint = 0.0;
5267 break;
5268 case FADE_OUT:
5269 tint = 1.0;
5270 break;
5271 default:
5272 weston_log("shell: invalid fade type\n");
5273 return;
5274 }
5275
5276 shell->fade.type = type;
5277
Jason Ekstranda7af7042013-10-12 22:38:11 -05005278 if (shell->fade.view == NULL) {
5279 shell->fade.view = shell_fade_create_surface(shell);
5280 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005281 return;
5282
Jason Ekstranda7af7042013-10-12 22:38:11 -05005283 shell->fade.view->alpha = 1.0 - tint;
5284 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005285 }
5286
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005287 if (shell->fade.view->output == NULL) {
5288 /* If the black view gets a NULL output, we lost the
5289 * last output and we'll just cancel the fade. This
5290 * happens when you close the last window under the
5291 * X11 or Wayland backends. */
5292 shell->locked = false;
5293 weston_surface_destroy(shell->fade.view->surface);
5294 shell->fade.view = NULL;
5295 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005296 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005297 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005298 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005300 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005301 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005302 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005303}
5304
5305static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005306do_shell_fade_startup(void *data)
5307{
5308 struct desktop_shell *shell = data;
5309
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005310 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005311 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005312 } else {
5313 weston_log("desktop shell: "
5314 "unexpected fade-in animation type %d\n",
5315 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005316 weston_surface_destroy(shell->fade.view->surface);
5317 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005318 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005319}
5320
5321static void
5322shell_fade_startup(struct desktop_shell *shell)
5323{
5324 struct wl_event_loop *loop;
5325
5326 if (!shell->fade.startup_timer)
5327 return;
5328
5329 wl_event_source_remove(shell->fade.startup_timer);
5330 shell->fade.startup_timer = NULL;
5331
5332 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5333 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5334}
5335
5336static int
5337fade_startup_timeout(void *data)
5338{
5339 struct desktop_shell *shell = data;
5340
5341 shell_fade_startup(shell);
5342 return 0;
5343}
5344
5345static void
5346shell_fade_init(struct desktop_shell *shell)
5347{
5348 /* Make compositor output all black, and wait for the desktop-shell
5349 * client to signal it is ready, then fade in. The timer triggers a
5350 * fade-in, in case the desktop-shell client takes too long.
5351 */
5352
5353 struct wl_event_loop *loop;
5354
Jason Ekstranda7af7042013-10-12 22:38:11 -05005355 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005356 weston_log("%s: warning: fade surface already exists\n",
5357 __func__);
5358 return;
5359 }
5360
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005361 if (shell->startup_animation_type == ANIMATION_NONE)
5362 return;
5363
Jason Ekstranda7af7042013-10-12 22:38:11 -05005364 shell->fade.view = shell_fade_create_surface(shell);
5365 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005366 return;
5367
Jason Ekstranda7af7042013-10-12 22:38:11 -05005368 weston_view_update_transform(shell->fade.view);
5369 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005370
5371 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5372 shell->fade.startup_timer =
5373 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5374 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5375}
5376
5377static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005378idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005379{
5380 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005381 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005382 struct weston_seat *seat;
5383
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005384 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005385 if (seat->pointer)
5386 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005387 if (seat->touch)
5388 touch_popup_grab_end(seat->touch);
5389 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005390
5391 shell_fade(shell, FADE_OUT);
5392 /* lock() is called from shell_fade_done() */
5393}
5394
5395static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005396wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005397{
5398 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005399 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005400
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005401 unlock(shell);
5402}
5403
5404static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005405center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005406{
Giulio Camuffob8366642013-04-25 13:57:46 +03005407 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005408 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005409
Jason Ekstranda7af7042013-10-12 22:38:11 -05005410 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005411
5412 x = output->x + (output->width - width) / 2 - surf_x / 2;
5413 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005414
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005415 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005416}
5417
5418static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005419weston_view_set_initial_position(struct weston_view *view,
5420 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005421{
5422 struct weston_compositor *compositor = shell->compositor;
5423 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005424 int32_t range_x, range_y;
5425 int32_t dx, dy, x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005426 struct weston_output *output, *target_output = NULL;
5427 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005428 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005429
5430 /* As a heuristic place the new window on the same output as the
5431 * pointer. Falling back to the output containing 0, 0.
5432 *
5433 * TODO: Do something clever for touch too?
5434 */
5435 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005436 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005437 ix = wl_fixed_to_int(seat->pointer->x);
5438 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005439 break;
5440 }
5441 }
5442
5443 wl_list_for_each(output, &compositor->output_list, link) {
5444 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5445 target_output = output;
5446 break;
5447 }
5448 }
5449
5450 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005451 weston_view_set_position(view, 10 + random() % 400,
5452 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005453 return;
5454 }
5455
5456 /* Valid range within output where the surface will still be onscreen.
5457 * If this is negative it means that the surface is bigger than
5458 * output.
5459 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005460 get_output_work_area(shell, target_output, &area);
5461
5462 dx = area.x;
5463 dy = area.y;
5464 range_x = area.width - view->surface->width;
5465 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005466
Rob Bradford4cb88c72012-08-13 15:18:44 +01005467 if (range_x > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005468 dx += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005469
5470 if (range_y > 0)
Jonny Lambd73c6942014-08-20 15:53:20 +02005471 dy += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005472
5473 x = target_output->x + dx;
5474 y = target_output->y + dy;
5475
Jason Ekstranda7af7042013-10-12 22:38:11 -05005476 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005477}
5478
5479static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005480set_maximized_position(struct desktop_shell *shell,
5481 struct shell_surface *shsurf)
5482{
5483 int32_t surf_x, surf_y;
5484 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005485 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005486
Jonny Lambd73c6942014-08-20 15:53:20 +02005487 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005488 if (shsurf->has_set_geometry) {
5489 surf_x = shsurf->geometry.x;
5490 surf_y = shsurf->geometry.y;
5491 } else {
5492 surface_subsurfaces_boundingbox(shsurf->surface,
5493 &surf_x, &surf_y, NULL, NULL);
5494 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005495 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005496
5497 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005498 e->x1 + area.x - surf_x,
5499 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005500}
5501
5502static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005503map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005504 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005505{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005506 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005507 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005508
Pekka Paalanen77346a62011-11-30 16:26:35 +02005509 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005510 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005511 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005512 if (shsurf->state.fullscreen) {
5513 center_on_output(shsurf->view, shsurf->fullscreen_output);
5514 shell_map_fullscreen(shsurf);
5515 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005516 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005517 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005518 weston_view_set_initial_position(shsurf->view, shell);
5519 }
Juan Zhao96879df2012-02-07 08:45:41 +08005520 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005521 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005522 if (shell_map_popup(shsurf) != 0)
5523 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005524 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005525 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005526 weston_view_set_position(shsurf->view,
5527 shsurf->view->geometry.x + sx,
5528 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005529 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005530 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005531 default:
5532 ;
5533 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005534
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005535 /* Surface stacking order, see also activate(). */
5536 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005537
Jason Ekstranda7af7042013-10-12 22:38:11 -05005538 if (shsurf->type != SHELL_SURFACE_NONE) {
5539 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005540 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005541 shsurf->surface->output = shsurf->output;
5542 shsurf->view->output = shsurf->output;
5543 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005544 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005545
Jason Ekstranda7af7042013-10-12 22:38:11 -05005546 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005547 /* XXX: xwayland's using the same fields for transient type */
5548 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005549 if (shsurf->transient.flags ==
5550 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5551 break;
5552 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005553 if (shsurf->state.relative &&
5554 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5555 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005556 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005557 break;
5558 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005559 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005560 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005561 case SHELL_SURFACE_POPUP:
5562 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005563 default:
5564 break;
5565 }
5566
Rafael Antognolli03b16592013-12-03 15:35:42 -02005567 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5568 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005569 {
5570 switch (shell->win_animation_type) {
5571 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005572 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005573 break;
5574 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005575 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005576 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005577 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005578 default:
5579 break;
5580 }
5581 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005582}
5583
5584static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005585configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005586 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005587{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005588 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005589 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005590
Pekka Paalanen77346a62011-11-30 16:26:35 +02005591 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005592
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005593 assert(shsurf);
5594
Rafael Antognolli03b16592013-12-03 15:35:42 -02005595 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005596 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005597 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005598 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005599 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005600 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005601 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005602
Alex Wu4539b082012-03-01 12:57:46 +08005603 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005604 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005605 wl_list_for_each(view, &surface->views, surface_link)
5606 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005607
Rafael Antognolli03b16592013-12-03 15:35:42 -02005608 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005609 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005610 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005611}
5612
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005613static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005614shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005615{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005616 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005617 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005618 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005619
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005620 assert(shsurf);
5621
5622 shell = shsurf->shell;
5623
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005624 if (!weston_surface_is_mapped(es) &&
5625 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005626 remove_popup_grab(shsurf);
5627 }
5628
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005629 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005630 return;
5631
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005632 if (shsurf->has_next_geometry) {
5633 shsurf->geometry = shsurf->next_geometry;
5634 shsurf->has_next_geometry = false;
5635 shsurf->has_set_geometry = true;
5636 } else if (!shsurf->has_set_geometry) {
5637 surface_subsurfaces_boundingbox(shsurf->surface,
5638 &shsurf->geometry.x,
5639 &shsurf->geometry.y,
5640 &shsurf->geometry.width,
5641 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005642 }
5643
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005644 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005645 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005646 type_changed = 1;
5647 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005648
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005649 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005650 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005651 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005652 shsurf->last_width != es->width ||
5653 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005654 float from_x, from_y;
5655 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005656
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005657 if (shsurf->resize_edges) {
5658 sx = 0;
5659 sy = 0;
5660 }
5661
5662 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5663 sx = shsurf->last_width - es->width;
5664 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5665 sy = shsurf->last_height - es->height;
5666
5667 shsurf->last_width = es->width;
5668 shsurf->last_height = es->height;
5669
Jason Ekstranda7af7042013-10-12 22:38:11 -05005670 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5671 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005672 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005673 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005674 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005675 }
5676}
5677
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005678static bool
5679check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5680{
5681 struct timespec now;
5682
5683 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5684 return false;
5685
5686 /*
5687 * If the shell helper client dies before the session has been
5688 * up for roughly 30 seconds, better just make Weston shut down,
5689 * because the user likely has no way to interact with the desktop
5690 * anyway.
5691 */
5692 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5693 weston_log("Error: %s apparently cannot run at all.\n",
5694 shell->client);
5695 weston_log_continue(STAMP_SPACE "Quitting...");
5696 wl_display_terminate(shell->compositor->wl_display);
5697
5698 return true;
5699 }
5700
5701 return false;
5702}
5703
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005704static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005705
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005706static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005707respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005708{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005709 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005710
5711 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5712 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005713 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005714 shell->child.deathstamp = time;
5715 shell->child.deathcount = 0;
5716 }
5717
5718 shell->child.deathcount++;
5719 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005720 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005721 return;
5722 }
5723
Pekka Paalanen826dc142014-08-27 12:11:53 +03005724 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005725 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005726}
5727
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005728static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005729desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5730{
5731 struct desktop_shell *shell;
5732
5733 shell = container_of(listener, struct desktop_shell,
5734 child.client_destroy_listener);
5735
Pekka Paalanen826dc142014-08-27 12:11:53 +03005736 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005737 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005738 /*
5739 * unbind_desktop_shell() will reset shell->child.desktop_shell
5740 * before the respawned process has a chance to create a new
5741 * desktop_shell object, because we are being called from the
5742 * wl_client destructor which destroys all wl_resources before
5743 * returning.
5744 */
5745
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005746 if (!check_desktop_shell_crash_too_early(shell))
5747 respawn_desktop_shell_process(shell);
5748
Pekka Paalanen826dc142014-08-27 12:11:53 +03005749 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005750}
5751
5752static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005753launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005754{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005755 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005756
Pekka Paalanen826dc142014-08-27 12:11:53 +03005757 shell->child.client = weston_client_start(shell->compositor,
5758 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005759
Arnaud Vrac42631192014-08-25 20:56:46 +02005760 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005761 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005762 return;
5763 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005764
5765 shell->child.client_destroy_listener.notify =
5766 desktop_shell_client_destroy;
5767 wl_client_add_destroy_listener(shell->child.client,
5768 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005769}
5770
5771static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005772handle_shell_client_destroy(struct wl_listener *listener, void *data)
5773{
5774 struct shell_client *sc =
5775 container_of(listener, struct shell_client, destroy_listener);
5776
5777 if (sc->ping_timer)
5778 wl_event_source_remove(sc->ping_timer);
5779 free(sc);
5780}
5781
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005782static struct shell_client *
5783shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5784 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005785{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005786 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005787
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005788 sc = zalloc(sizeof *sc);
5789 if (sc == NULL) {
5790 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005791 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005792 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005793
5794 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005795 if (sc->resource == NULL) {
5796 free(sc);
5797 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005798 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005799 }
5800
5801 sc->client = client;
5802 sc->shell = shell;
5803 sc->destroy_listener.notify = handle_shell_client_destroy;
5804 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005805 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005806
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005807 return sc;
5808}
5809
5810static void
5811bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5812{
5813 struct desktop_shell *shell = data;
5814 struct shell_client *sc;
5815
5816 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5817 if (sc)
5818 wl_resource_set_implementation(sc->resource,
5819 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005820 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005821}
5822
5823static void
5824bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5825{
5826 struct desktop_shell *shell = data;
5827 struct shell_client *sc;
5828
5829 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5830 if (sc)
5831 wl_resource_set_dispatcher(sc->resource,
5832 xdg_shell_unversioned_dispatch,
5833 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005834}
5835
5836static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005837unbind_desktop_shell(struct wl_resource *resource)
5838{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005839 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005840
5841 if (shell->locked)
5842 resume_desktop(shell);
5843
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005844 shell->child.desktop_shell = NULL;
5845 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005846}
5847
5848static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005849bind_desktop_shell(struct wl_client *client,
5850 void *data, uint32_t version, uint32_t id)
5851{
Tiago Vignattibe143262012-04-16 17:31:41 +03005852 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005853 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005854
Jason Ekstranda85118c2013-06-27 20:17:02 -05005855 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005856 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005857
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005858 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005859 wl_resource_set_implementation(resource,
5860 &desktop_shell_implementation,
5861 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005862 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005863
5864 if (version < 2)
5865 shell_fade_startup(shell);
5866
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005867 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005868 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005869
5870 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5871 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005872}
5873
Kristian Høgsberg07045392012-02-19 18:52:44 -05005874struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005875 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005876 struct weston_surface *current;
5877 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005878 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005879 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005880};
5881
5882static void
5883switcher_next(struct switcher *switcher)
5884{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005885 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005886 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005887 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005888 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005889
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005890 /* temporary re-display minimized surfaces */
5891 struct weston_view *tmp;
5892 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005893 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5894 weston_layer_entry_remove(&view->layer_link);
5895 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005896 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5897 *minimized = view;
5898 }
5899
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005900 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005901 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005902 if (shsurf &&
5903 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5904 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005905 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005906 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005907 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005908 next = view->surface;
5909 prev = view->surface;
5910 view->alpha = 0.25;
5911 weston_view_geometry_dirty(view);
5912 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005913 }
Alex Wu1659daa2012-04-01 20:13:09 +08005914
Jason Ekstranda7af7042013-10-12 22:38:11 -05005915 if (is_black_surface(view->surface, NULL)) {
5916 view->alpha = 0.25;
5917 weston_view_geometry_dirty(view);
5918 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005919 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005920 }
5921
5922 if (next == NULL)
5923 next = first;
5924
Alex Wu07b26062012-03-12 16:06:01 +08005925 if (next == NULL)
5926 return;
5927
Kristian Høgsberg07045392012-02-19 18:52:44 -05005928 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005929 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005930
5931 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005932 wl_list_for_each(view, &next->views, surface_link)
5933 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005934
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005935 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005936 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005937 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005938}
5939
5940static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005941switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005942{
5943 struct switcher *switcher =
5944 container_of(listener, struct switcher, listener);
5945
5946 switcher_next(switcher);
5947}
5948
5949static void
Daniel Stone351eb612012-05-31 15:27:47 -04005950switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005951{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005952 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005953 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005954 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005955
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005956 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005957 if (is_focus_view(view))
5958 continue;
5959
Jason Ekstranda7af7042013-10-12 22:38:11 -05005960 view->alpha = 1.0;
5961 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005962 }
5963
Alex Wu07b26062012-03-12 16:06:01 +08005964 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005965 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005966 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005967 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005968 weston_keyboard_end_grab(keyboard);
5969 if (keyboard->input_method_resource)
5970 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005971
5972 /* re-hide surfaces that were temporary shown during the switch */
5973 struct weston_view **minimized;
5974 wl_array_for_each(minimized, &switcher->minimized_array) {
5975 /* with the exception of the current selected */
5976 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005977 weston_layer_entry_remove(&(*minimized)->layer_link);
5978 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005979 weston_view_damage_below(*minimized);
5980 }
5981 }
5982 wl_array_release(&switcher->minimized_array);
5983
Kristian Høgsberg07045392012-02-19 18:52:44 -05005984 free(switcher);
5985}
5986
5987static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005988switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005989 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005990{
5991 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005992 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005993
Daniel Stonec9785ea2012-05-30 16:31:52 +01005994 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005995 switcher_next(switcher);
5996}
5997
5998static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005999switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006000 uint32_t mods_depressed, uint32_t mods_latched,
6001 uint32_t mods_locked, uint32_t group)
6002{
6003 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006004 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006005
Daniel Stone351eb612012-05-31 15:27:47 -04006006 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6007 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006008}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006009
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006010static void
6011switcher_cancel(struct weston_keyboard_grab *grab)
6012{
6013 struct switcher *switcher = container_of(grab, struct switcher, grab);
6014
6015 switcher_destroy(switcher);
6016}
6017
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006018static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006019 switcher_key,
6020 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006021 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006022};
6023
6024static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006025switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006026 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006027{
Tiago Vignattibe143262012-04-16 17:31:41 +03006028 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006029 struct switcher *switcher;
6030
6031 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006032 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006033 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006034 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006035 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006036 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006037
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006038 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006039 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006040 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006041 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
6042 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006043 switcher_next(switcher);
6044}
6045
Pekka Paalanen3c647232011-12-22 13:43:43 +02006046static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006047backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006048 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006049{
6050 struct weston_compositor *compositor = data;
6051 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006052 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006053
6054 /* TODO: we're limiting to simple use cases, where we assume just
6055 * control on the primary display. We'd have to extend later if we
6056 * ever get support for setting backlights on random desktop LCD
6057 * panels though */
6058 output = get_default_output(compositor);
6059 if (!output)
6060 return;
6061
6062 if (!output->set_backlight)
6063 return;
6064
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006065 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6066 backlight_new = output->backlight_current - 25;
6067 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6068 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006069
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006070 if (backlight_new < 5)
6071 backlight_new = 5;
6072 if (backlight_new > 255)
6073 backlight_new = 255;
6074
6075 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006076 output->set_backlight(output, output->backlight_current);
6077}
6078
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006079static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006080force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006081 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006082{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006083 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006084 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006085 struct desktop_shell *shell = data;
6086 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006087 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006088
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006089 focus_surface = seat->keyboard->focus;
6090 if (!focus_surface)
6091 return;
6092
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006093 wl_signal_emit(&compositor->kill_signal, focus_surface);
6094
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006095 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006096 wl_client_get_credentials(client, &pid, NULL, NULL);
6097
6098 /* Skip clients that we launched ourselves (the credentials of
6099 * the socketpair is ours) */
6100 if (pid == getpid())
6101 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006102
Daniel Stone325fc2d2012-05-30 16:31:58 +01006103 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006104}
6105
6106static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006107workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006108 uint32_t key, void *data)
6109{
6110 struct desktop_shell *shell = data;
6111 unsigned int new_index = shell->workspaces.current;
6112
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006113 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006114 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006115 if (new_index != 0)
6116 new_index--;
6117
6118 change_workspace(shell, new_index);
6119}
6120
6121static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006122workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006123 uint32_t key, void *data)
6124{
6125 struct desktop_shell *shell = data;
6126 unsigned int new_index = shell->workspaces.current;
6127
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006128 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006129 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006130 if (new_index < shell->workspaces.num - 1)
6131 new_index++;
6132
6133 change_workspace(shell, new_index);
6134}
6135
6136static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006137workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006138 uint32_t key, void *data)
6139{
6140 struct desktop_shell *shell = data;
6141 unsigned int new_index;
6142
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006143 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006144 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006145 new_index = key - KEY_F1;
6146 if (new_index >= shell->workspaces.num)
6147 new_index = shell->workspaces.num - 1;
6148
6149 change_workspace(shell, new_index);
6150}
6151
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006152static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006153workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006154 uint32_t key, void *data)
6155{
6156 struct desktop_shell *shell = data;
6157 unsigned int new_index = shell->workspaces.current;
6158
6159 if (shell->locked)
6160 return;
6161
6162 if (new_index != 0)
6163 new_index--;
6164
6165 take_surface_to_workspace_by_seat(shell, seat, new_index);
6166}
6167
6168static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04006169workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006170 uint32_t key, void *data)
6171{
6172 struct desktop_shell *shell = data;
6173 unsigned int new_index = shell->workspaces.current;
6174
6175 if (shell->locked)
6176 return;
6177
6178 if (new_index < shell->workspaces.num - 1)
6179 new_index++;
6180
6181 take_surface_to_workspace_by_seat(shell, seat, new_index);
6182}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006183
6184static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006185shell_reposition_view_on_output_destroy(struct weston_view *view)
6186{
6187 struct weston_output *output, *first_output;
6188 struct weston_compositor *ec = view->surface->compositor;
6189 struct shell_surface *shsurf;
6190 float x, y;
6191 int visible;
6192
6193 x = view->geometry.x;
6194 y = view->geometry.y;
6195
6196 /* At this point the destroyed output is not in the list anymore.
6197 * If the view is still visible somewhere, we leave where it is,
6198 * otherwise, move it to the first output. */
6199 visible = 0;
6200 wl_list_for_each(output, &ec->output_list, link) {
6201 if (pixman_region32_contains_point(&output->region,
6202 x, y, NULL)) {
6203 visible = 1;
6204 break;
6205 }
6206 }
6207
6208 if (!visible) {
6209 first_output = container_of(ec->output_list.next,
6210 struct weston_output, link);
6211
6212 x = first_output->x + first_output->width / 4;
6213 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006214
6215 weston_view_set_position(view, x, y);
6216 } else {
6217 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006218 }
6219
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006220
6221 shsurf = get_shell_surface(view->surface);
6222
6223 if (shsurf) {
6224 shsurf->saved_position_valid = false;
6225 shsurf->next_state.maximized = false;
6226 shsurf->next_state.fullscreen = false;
6227 shsurf->state_changed = true;
6228 }
6229}
6230
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006231void
6232shell_for_each_layer(struct desktop_shell *shell,
6233 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006234{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006235 struct workspace **ws;
6236
6237 func(shell, &shell->fullscreen_layer, data);
6238 func(shell, &shell->panel_layer, data);
6239 func(shell, &shell->background_layer, data);
6240 func(shell, &shell->lock_layer, data);
6241 func(shell, &shell->input_panel_layer, data);
6242
6243 wl_array_for_each(ws, &shell->workspaces.array)
6244 func(shell, &(*ws)->layer, data);
6245}
6246
6247static void
6248shell_output_destroy_move_layer(struct desktop_shell *shell,
6249 struct weston_layer *layer,
6250 void *data)
6251{
6252 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006253 struct weston_view *view;
6254
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006255 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006256 if (view->output != output)
6257 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006258
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006259 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006260 }
6261}
6262
6263static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006264handle_output_destroy(struct wl_listener *listener, void *data)
6265{
6266 struct shell_output *output_listener =
6267 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006268 struct weston_output *output = output_listener->output;
6269 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006270
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006271 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006272
Xiong Zhang6b481422013-10-23 13:58:32 +08006273 wl_list_remove(&output_listener->destroy_listener.link);
6274 wl_list_remove(&output_listener->link);
6275 free(output_listener);
6276}
6277
6278static void
6279create_shell_output(struct desktop_shell *shell,
6280 struct weston_output *output)
6281{
6282 struct shell_output *shell_output;
6283
6284 shell_output = zalloc(sizeof *shell_output);
6285 if (shell_output == NULL)
6286 return;
6287
6288 shell_output->output = output;
6289 shell_output->shell = shell;
6290 shell_output->destroy_listener.notify = handle_output_destroy;
6291 wl_signal_add(&output->destroy_signal,
6292 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006293 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006294}
6295
6296static void
6297handle_output_create(struct wl_listener *listener, void *data)
6298{
6299 struct desktop_shell *shell =
6300 container_of(listener, struct desktop_shell, output_create_listener);
6301 struct weston_output *output = (struct weston_output *)data;
6302
6303 create_shell_output(shell, output);
6304}
6305
6306static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006307handle_output_move_layer(struct desktop_shell *shell,
6308 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006309{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006310 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006311 struct weston_view *view;
6312 float x, y;
6313
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006314 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006315 if (view->output != output)
6316 continue;
6317
6318 x = view->geometry.x + output->move_x;
6319 y = view->geometry.y + output->move_y;
6320 weston_view_set_position(view, x, y);
6321 }
6322}
6323
6324static void
6325handle_output_move(struct wl_listener *listener, void *data)
6326{
6327 struct desktop_shell *shell;
6328
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006329 shell = container_of(listener, struct desktop_shell,
6330 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006331
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006332 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006333}
6334
6335static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006336setup_output_destroy_handler(struct weston_compositor *ec,
6337 struct desktop_shell *shell)
6338{
6339 struct weston_output *output;
6340
6341 wl_list_init(&shell->output_list);
6342 wl_list_for_each(output, &ec->output_list, link)
6343 create_shell_output(shell, output);
6344
6345 shell->output_create_listener.notify = handle_output_create;
6346 wl_signal_add(&ec->output_created_signal,
6347 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006348
6349 shell->output_move_listener.notify = handle_output_move;
6350 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006351}
6352
6353static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006354shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006355{
Tiago Vignattibe143262012-04-16 17:31:41 +03006356 struct desktop_shell *shell =
6357 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006358 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006359 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006360
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006361 /* Force state to unlocked so we don't try to fade */
6362 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006363
6364 if (shell->child.client) {
6365 /* disable respawn */
6366 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006367 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006368 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006369
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006370 wl_list_remove(&shell->idle_listener.link);
6371 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006372
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006373 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006374 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006375
Xiong Zhang6b481422013-10-23 13:58:32 +08006376 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6377 wl_list_remove(&shell_output->destroy_listener.link);
6378 wl_list_remove(&shell_output->link);
6379 free(shell_output);
6380 }
6381
6382 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006383 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006384
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006385 wl_array_for_each(ws, &shell->workspaces.array)
6386 workspace_destroy(*ws);
6387 wl_array_release(&shell->workspaces.array);
6388
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006389 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006390 free(shell);
6391}
6392
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006393static void
6394shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6395{
6396 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006397 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006398
6399 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006400 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6401 MODIFIER_CTRL | MODIFIER_ALT,
6402 terminate_binding, ec);
6403 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6404 click_to_activate_binding,
6405 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006406 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6407 click_to_activate_binding,
6408 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006409 weston_compositor_add_touch_binding(ec, 0,
6410 touch_to_activate_binding,
6411 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006412 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6413 MODIFIER_SUPER | MODIFIER_ALT,
6414 surface_opacity_binding, NULL);
6415 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6416 MODIFIER_SUPER, zoom_axis_binding,
6417 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006418
6419 /* configurable bindings */
6420 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006421 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6422 zoom_key_binding, NULL);
6423 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6424 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006425 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6426 maximize_binding, NULL);
6427 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6428 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006429 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6430 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006431 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006432 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6433 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006434 weston_compositor_add_button_binding(ec, BTN_LEFT,
6435 mod | MODIFIER_SHIFT,
6436 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006437
6438 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6439 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6440 rotate_binding, NULL);
6441
Daniel Stone325fc2d2012-05-30 16:31:58 +01006442 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6443 shell);
6444 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6445 ec);
6446 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6447 backlight_binding, ec);
6448 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6449 ec);
6450 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6451 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006452 weston_compositor_add_key_binding(ec, KEY_K, mod,
6453 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006454 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6455 workspace_up_binding, shell);
6456 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6457 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006458 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6459 workspace_move_surface_up_binding,
6460 shell);
6461 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6462 workspace_move_surface_down_binding,
6463 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006464
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006465 if (shell->exposay_modifier)
6466 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6467 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006468
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006469 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6470 if (shell->workspaces.num > 1) {
6471 num_workspace_bindings = shell->workspaces.num;
6472 if (num_workspace_bindings > 6)
6473 num_workspace_bindings = 6;
6474 for (i = 0; i < num_workspace_bindings; i++)
6475 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6476 workspace_f_binding,
6477 shell);
6478 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006479
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006480 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006481}
6482
Jason Ekstrand024177c2014-04-21 19:42:58 -05006483static void
6484handle_seat_created(struct wl_listener *listener, void *data)
6485{
6486 struct weston_seat *seat = data;
6487
6488 create_shell_seat(seat);
6489}
6490
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006491WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006492module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006493 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006494{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006495 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006496 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006497 struct workspace **pws;
6498 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006499 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006500
Peter Huttererf3d62272013-08-08 11:57:05 +10006501 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006502 if (shell == NULL)
6503 return -1;
6504
Kristian Høgsberg75840622011-09-06 13:48:16 -04006505 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006506
6507 shell->destroy_listener.notify = shell_destroy;
6508 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006509 shell->idle_listener.notify = idle_handler;
6510 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6511 shell->wake_listener.notify = wake_handler;
6512 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006513
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006514 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006515 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006516 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006517 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006518 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006519 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006520 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006521 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006522 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006523 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006524 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006525 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006526 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006527
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006528 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6529 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006530 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6531 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006532 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006533
6534 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006535 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006536
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006537 if (input_panel_setup(shell) < 0)
6538 return -1;
6539
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006540 shell->text_backend = text_backend_init(ec);
6541 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006542 return -1;
6543
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006544 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006545
Daniel Stonedf8133b2013-11-19 11:37:14 +01006546 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6547 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6548
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006549 for (i = 0; i < shell->workspaces.num; i++) {
6550 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6551 if (pws == NULL)
6552 return -1;
6553
6554 *pws = workspace_create();
6555 if (*pws == NULL)
6556 return -1;
6557 }
6558 activate_workspace(shell, 0);
6559
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006560 weston_layer_init(&shell->minimized_layer, NULL);
6561
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006562 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006563 wl_list_init(&shell->workspaces.animation.link);
6564 shell->workspaces.animation.frame = animate_workspace_change_frame;
6565
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006566 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006567 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006568 return -1;
6569
Rafael Antognollie2a34552013-12-03 15:35:45 -02006570 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6571 shell, bind_xdg_shell) == NULL)
6572 return -1;
6573
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006574 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006575 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006576 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006577 return -1;
6578
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006579 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6580 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006581 return -1;
6582
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006583 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006584
Jonny Lamb765760d2014-08-20 15:53:19 +02006585 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6586
Xiong Zhang6b481422013-10-23 13:58:32 +08006587 setup_output_destroy_handler(ec, shell);
6588
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006589 loop = wl_display_get_event_loop(ec->wl_display);
6590 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006591
Jason Ekstrand024177c2014-04-21 19:42:58 -05006592 wl_list_for_each(seat, &ec->seat_list, link)
6593 handle_seat_created(NULL, seat);
6594 shell->seat_create_listener.notify = handle_seat_created;
6595 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006596
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006597 screenshooter_create(ec);
6598
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006599 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006600
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006601 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006602
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006603 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6604
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006605 return 0;
6606}