blob: f87b6bbe0b2b045984373cc04d2cbe05a79f8496 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040029#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050030#include <string.h>
31#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040032#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020034#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020035#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040036#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080038#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040039#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070041#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070042#include "shared/helpers.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020043#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050044
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020046#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010048#ifndef static_assert
49#define static_assert(cond, msg)
50#endif
51
Jonas Ådahl04769742012-06-13 00:01:24 +020052struct focus_state {
53 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020062 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050063 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030064 SHELL_SURFACE_POPUP,
65 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020066};
67
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050068struct shell_client;
69
Philip Withnall648a4dd2013-11-25 18:01:44 +000070/*
71 * Surface stacking and ordering.
72 *
73 * This is handled using several linked lists of surfaces, organised into
74 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
75 * above all of the surfaces in the layer below. The set of layers is static and
76 * in the following order (top-most first):
77 * • Lock layer (only ever displayed on its own)
78 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010079 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Fullscreen layer
81 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000082 * • Workspace layers
83 * • Background layer
84 *
85 * The list of layers may be manipulated to remove whole layers of surfaces from
86 * display. For example, when locking the screen, all layers except the lock
87 * layer are removed.
88 *
89 * A surface’s layer is modified on configuring the surface, in
90 * set_surface_type() (which is only called when the surface’s type change is
91 * _committed_). If a surface’s type changes (e.g. when making a window
92 * fullscreen) its layer changes too.
93 *
94 * In order to allow popup and transient surfaces to be correctly stacked above
95 * their parent surfaces, each surface tracks both its parent surface, and a
96 * linked list of its children. When a surface’s layer is updated, so are the
97 * layers of its children. Note that child surfaces are *not* the same as
98 * subsurfaces — child/parent surfaces are purely for maintaining stacking
99 * order.
100 *
101 * The children_link list of siblings of a surface (i.e. those surfaces which
102 * have the same parent) only contains weston_surfaces which have a
103 * shell_surface. Stacking is not implemented for non-shell_surface
104 * weston_surfaces. This means that the following implication does *not* hold:
105 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
106 */
107
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200108struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500109 struct wl_resource *resource;
110 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500111 struct shell_client *owner;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800112 struct wl_resource *owner_resource;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500115 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600116 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700118 struct wl_listener resource_destroy_listener;
119
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400120 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000121 struct wl_list children_list; /* child surfaces of this one */
122 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300123 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200124
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800125 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400126 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500127 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200128 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800129 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200130 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800131 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700132 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800133 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100134
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200136 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500137 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200138 } rotation;
139
140 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100141 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100143 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400144 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 } popup;
146
Alex Wu4539b082012-03-01 12:57:46 +0800147 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300148 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400149 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300150 } transient;
151
152 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800153 enum wl_shell_surface_fullscreen_method type;
154 struct weston_transform transform; /* matrix from x, y */
155 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500156 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800157 } fullscreen;
158
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200159 struct weston_transform workspace_transform;
160
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500161 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500162 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100163 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400164
165 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700167 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200168 bool maximized;
169 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200170 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100171 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200173 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500174 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500175
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700176 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400177 int32_t x, y, width, height;
178 } geometry, next_geometry;
179 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700180
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500181 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500182
183 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400187 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188 struct shell_surface *shsurf;
189 struct wl_listener shsurf_destroy_listener;
190};
191
Rusty Lynch1084da52013-08-15 09:10:08 -0700192struct shell_touch_grab {
193 struct weston_touch_grab grab;
194 struct shell_surface *shsurf;
195 struct wl_listener shsurf_destroy_listener;
196 struct weston_touch *touch;
197};
198
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199struct weston_move_grab {
200 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100201 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500202 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203};
204
Rusty Lynch1084da52013-08-15 09:10:08 -0700205struct weston_touch_move_grab {
206 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800207 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700208 wl_fixed_t dx, dy;
209};
210
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300212 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500213 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200215 float x;
216 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200217 } center;
218};
219
Giulio Camuffo5085a752013-03-25 21:42:45 +0100220struct shell_seat {
221 struct weston_seat *seat;
222 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500223 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224
Jason Ekstrand024177c2014-04-21 19:42:58 -0500225 struct wl_listener caps_changed_listener;
226 struct wl_listener pointer_focus_listener;
227 struct wl_listener keyboard_focus_listener;
228
Giulio Camuffo5085a752013-03-25 21:42:45 +0100229 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400230 struct weston_pointer_grab grab;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200231 struct weston_touch_grab touch_grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100232 struct wl_list surfaces_list;
233 struct wl_client *client;
234 int32_t initial_up;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200235 enum { POINTER, TOUCH } type;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100236 } popup_grab;
237};
238
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800239struct shell_client {
240 struct wl_resource *resource;
241 struct wl_client *client;
242 struct desktop_shell *shell;
243 struct wl_listener destroy_listener;
244 struct wl_event_source *ping_timer;
245 uint32_t ping_serial;
246 int unresponsive;
Jonas Ådahl49d77d22015-03-18 16:20:51 +0800247 struct wl_list surface_list;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800248};
249
Alex Wubd3354b2012-04-17 17:20:49 +0800250static struct desktop_shell *
251shell_surface_get_shell(struct shell_surface *shsurf);
252
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500253static void
Derek Foreman74de4692015-07-15 13:00:39 -0500254surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500255
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300256static void
257shell_fade_startup(struct desktop_shell *shell);
258
Philip Withnallbecb77e2013-11-25 18:01:30 +0000259static struct shell_seat *
260get_shell_seat(struct weston_seat *seat);
261
Jonny Lambd73c6942014-08-20 15:53:20 +0200262static void
263get_output_panel_size(struct desktop_shell *shell,
264 struct weston_output *output,
265 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700266
Philip Withnall648a4dd2013-11-25 18:01:44 +0000267static void
268shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
269
Alex Wubd3354b2012-04-17 17:20:49 +0800270static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200271shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
272
273static bool
274shell_surface_is_xdg_surface(struct shell_surface *shsurf);
275
276static bool
277shell_surface_is_xdg_popup(struct shell_surface *shsurf);
278
279static void
280shell_surface_set_parent(struct shell_surface *shsurf,
281 struct weston_surface *parent);
282
Pekka Paalanen8274d902014-08-06 19:36:51 +0300283static int
284shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
285{
286 struct shell_surface *shsurf;
287 const char *typestr[] = {
288 [SHELL_SURFACE_NONE] = "unidentified",
289 [SHELL_SURFACE_TOPLEVEL] = "top-level",
290 [SHELL_SURFACE_POPUP] = "popup",
291 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
292 };
293 const char *t, *c;
294
295 shsurf = get_shell_surface(surface);
296 if (!shsurf)
297 return snprintf(buf, len, "unidentified window");
298
299 t = shsurf->title;
300 c = shsurf->class;
301
302 return snprintf(buf, len, "%s window%s%s%s%s%s",
303 typestr[shsurf->type],
304 t ? " '" : "", t ?: "", t ? "'" : "",
305 c ? " of " : "", c ?: "");
306}
307
Rafael Antognollie2a34552013-12-03 15:35:45 -0200308static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800309shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
310{
311 struct desktop_shell *shell;
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200312 struct weston_view *view;
313 struct shell_surface *top_fs_shsurf = NULL;
Alex Wubd3354b2012-04-17 17:20:49 +0800314
315 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100316
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300317 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800318 return false;
319
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200320 /* Find topmost shsurf on the same fullscreen output on which shsurf
321 * is displaying. We don't care about other outputs.
322 */
323 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
324 layer_link.link) {
325 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
326
327 if (cand_shsurf &&
328 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
329 top_fs_shsurf = cand_shsurf;
330 break;
331 }
332 }
333
334 return (shsurf == top_fs_shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800335}
336
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500337static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300339{
340 struct shell_grab *grab;
341
342 grab = container_of(listener, struct shell_grab,
343 shsurf_destroy_listener);
344
345 grab->shsurf = NULL;
346}
347
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800348struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349get_default_view(struct weston_surface *surface)
350{
351 struct shell_surface *shsurf;
352 struct weston_view *view;
353
354 if (!surface || wl_list_empty(&surface->views))
355 return NULL;
356
357 shsurf = get_shell_surface(surface);
358 if (shsurf)
359 return shsurf->view;
360
361 wl_list_for_each(view, &surface->views, surface_link)
362 if (weston_view_is_mapped(view))
363 return view;
364
365 return container_of(surface->views.next, struct weston_view, surface_link);
366}
367
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400369popup_grab_end(struct weston_pointer *pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200370static void
371touch_popup_grab_end(struct weston_touch *touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400372
373static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400375 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400377 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380 struct desktop_shell *shell = shsurf->shell;
381
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400382 popup_grab_end(pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200383 if (pointer->seat->touch)
384 touch_popup_grab_end(pointer->seat->touch);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400385
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300386 grab->grab.interface = interface;
387 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400388 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500389 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400390 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300391
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700392 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400393 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400394 if (shell->child.desktop_shell) {
395 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
396 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397 weston_pointer_set_focus(pointer,
398 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400399 wl_fixed_from_int(0),
400 wl_fixed_from_int(0));
401 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300402}
403
Jonny Lambd73c6942014-08-20 15:53:20 +0200404static void
405get_output_panel_size(struct desktop_shell *shell,
406 struct weston_output *output,
407 int *width,
408 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700409{
410 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200411
412 *width = 0;
413 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700414
415 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200416 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700417
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300418 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200419 float x, y;
420
421 if (view->surface->output != output)
422 continue;
423
424 switch (shell->panel_position) {
425 case DESKTOP_SHELL_PANEL_POSITION_TOP:
426 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
427
428 weston_view_to_global_float(view,
429 view->surface->width, 0,
430 &x, &y);
431
432 *width = (int) x;
433 *height = view->surface->height + (int) y;
434 return;
435
436 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
437 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
438 weston_view_to_global_float(view,
439 0, view->surface->height,
440 &x, &y);
441
442 *width = view->surface->width + (int) x;
443 *height = (int) y;
444 return;
445
446 default:
447 /* we've already set width and height to
448 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700449 break;
450 }
451 }
452
Jonny Lambd73c6942014-08-20 15:53:20 +0200453 /* the correct view wasn't found */
454}
455
456static void
457get_output_work_area(struct desktop_shell *shell,
458 struct weston_output *output,
459 pixman_rectangle32_t *area)
460{
461 int32_t panel_width = 0, panel_height = 0;
462
Derek Foremanf814c5d2015-04-15 12:23:54 -0500463 area->x = output->x;
464 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200465
466 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200467 switch (shell->panel_position) {
468 case DESKTOP_SHELL_PANEL_POSITION_TOP:
469 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500470 area->y += panel_height;
Jonny Lambd73c6942014-08-20 15:53:20 +0200471 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
472 area->width = output->width;
473 area->height = output->height - panel_height;
474 break;
475 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500476 area->x += panel_width;
Jonny Lambd73c6942014-08-20 15:53:20 +0200477 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
478 area->width = output->width - panel_width;
479 area->height = output->height;
480 break;
481 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700482}
483
484static void
485send_configure_for_surface(struct shell_surface *shsurf)
486{
487 int32_t width, height;
488 struct surface_state *state;
489
490 if (shsurf->state_requested)
491 state = &shsurf->requested_state;
492 else if (shsurf->state_changed)
493 state = &shsurf->next_state;
494 else
495 state = &shsurf->state;
496
497 if (state->fullscreen) {
498 width = shsurf->output->width;
499 height = shsurf->output->height;
500 } else if (state->maximized) {
501 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200502 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700503
504 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200505 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700506
Jonny Lambd73c6942014-08-20 15:53:20 +0200507 width = area.width;
508 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900509 } else if (shsurf->resize_edges) {
510 width = shsurf->geometry.width;
511 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700512 } else {
513 width = 0;
514 height = 0;
515 }
516
517 shsurf->client->send_configure(shsurf->surface, width, height);
518}
519
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300520static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400521shell_surface_state_changed(struct shell_surface *shsurf)
522{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700523 if (shell_surface_is_xdg_surface(shsurf))
524 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400525}
526
527static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300528shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300529{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700530 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400531 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700532 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400533
534 if (grab->shsurf->resize_edges) {
535 grab->shsurf->resize_edges = 0;
536 shell_surface_state_changed(grab->shsurf);
537 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700538 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300539
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700540 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300541}
542
543static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700544shell_touch_grab_start(struct shell_touch_grab *grab,
545 const struct weston_touch_grab_interface *interface,
546 struct shell_surface *shsurf,
547 struct weston_touch *touch)
548{
549 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800550
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200551 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700552 if (touch->seat->pointer)
553 popup_grab_end(touch->seat->pointer);
554
Rusty Lynch1084da52013-08-15 09:10:08 -0700555 grab->grab.interface = interface;
556 grab->shsurf = shsurf;
557 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
558 wl_signal_add(&shsurf->destroy_signal,
559 &grab->shsurf_destroy_listener);
560
561 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700562 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700563
564 weston_touch_start_grab(touch, &grab->grab);
565 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500566 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500567 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700568}
569
570static void
571shell_touch_grab_end(struct shell_touch_grab *grab)
572{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700573 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700574 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700575 grab->shsurf->grabbed = 0;
576 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700577
578 weston_touch_end_grab(grab->touch);
579}
580
581static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500582center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800583 struct weston_output *output);
584
Daniel Stone496ca172012-05-30 16:31:42 +0100585static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300586get_modifier(char *modifier)
587{
588 if (!modifier)
589 return MODIFIER_SUPER;
590
591 if (!strcmp("ctrl", modifier))
592 return MODIFIER_CTRL;
593 else if (!strcmp("alt", modifier))
594 return MODIFIER_ALT;
595 else if (!strcmp("super", modifier))
596 return MODIFIER_SUPER;
597 else
598 return MODIFIER_SUPER;
599}
600
Juan Zhaoe10d2792012-04-25 19:09:52 +0800601static enum animation_type
602get_animation_type(char *animation)
603{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800604 if (!animation)
605 return ANIMATION_NONE;
606
Juan Zhaoe10d2792012-04-25 19:09:52 +0800607 if (!strcmp("zoom", animation))
608 return ANIMATION_ZOOM;
609 else if (!strcmp("fade", animation))
610 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100611 else if (!strcmp("dim-layer", animation))
612 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800613 else
614 return ANIMATION_NONE;
615}
616
Alex Wu4539b082012-03-01 12:57:46 +0800617static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400618shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200619{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400620 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500621 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300622 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200623
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400624 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400625 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300626 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
627 WESTON_SHELL_CLIENT);
628 if (ret < 0)
629 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400630 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500631 "client", &s, client);
632 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100633 shell->client = s;
634 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400635 "binding-modifier", &s, "super");
636 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200637 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800638
639 weston_config_section_get_string(section,
640 "exposay-modifier", &s, "none");
641 if (strcmp(s, "none") == 0)
642 shell->exposay_modifier = 0;
643 else
644 shell->exposay_modifier = get_modifier(s);
645 free(s);
646
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400647 weston_config_section_get_string(section, "animation", &s, "none");
648 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200649 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200650 weston_config_section_get_string(section, "close-animation", &s, "fade");
651 shell->win_close_animation_type = get_animation_type(s);
652 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700653 weston_config_section_get_string(section,
654 "startup-animation", &s, "fade");
655 shell->startup_animation_type = get_animation_type(s);
656 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700657 if (shell->startup_animation_type == ANIMATION_ZOOM)
658 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100659 weston_config_section_get_string(section, "focus-animation", &s, "none");
660 shell->focus_animation_type = get_animation_type(s);
661 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400662 weston_config_section_get_uint(section, "num-workspaces",
663 &shell->workspaces.num,
664 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200665}
666
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800667struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100668get_default_output(struct weston_compositor *compositor)
669{
670 return container_of(compositor->output_list.next,
671 struct weston_output, link);
672}
673
Pekka Paalanen8274d902014-08-06 19:36:51 +0300674static int
675focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
676{
677 return snprintf(buf, len, "focus highlight effect for output %s",
678 surface->output->name);
679}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100680
681/* no-op func for checking focus surface */
682static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600683focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100684{
685}
686
687static struct focus_surface *
688get_focus_surface(struct weston_surface *surface)
689{
690 if (surface->configure == focus_surface_configure)
691 return surface->configure_private;
692 else
693 return NULL;
694}
695
696static bool
697is_focus_surface (struct weston_surface *es)
698{
699 return (es->configure == focus_surface_configure);
700}
701
702static bool
703is_focus_view (struct weston_view *view)
704{
705 return is_focus_surface (view->surface);
706}
707
708static struct focus_surface *
709create_focus_surface(struct weston_compositor *ec,
710 struct weston_output *output)
711{
712 struct focus_surface *fsurf = NULL;
713 struct weston_surface *surface = NULL;
714
715 fsurf = malloc(sizeof *fsurf);
716 if (!fsurf)
717 return NULL;
718
719 fsurf->surface = weston_surface_create(ec);
720 surface = fsurf->surface;
721 if (surface == NULL) {
722 free(fsurf);
723 return NULL;
724 }
725
726 surface->configure = focus_surface_configure;
727 surface->output = output;
728 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300729 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100730
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800731 fsurf->view = weston_view_create(surface);
732 if (fsurf->view == NULL) {
733 weston_surface_destroy(surface);
734 free(fsurf);
735 return NULL;
736 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100737 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100738
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600739 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600740 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100741 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
742 pixman_region32_fini(&surface->opaque);
743 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
744 output->width, output->height);
745 pixman_region32_fini(&surface->input);
746 pixman_region32_init(&surface->input);
747
748 wl_list_init(&fsurf->workspace_transform.link);
749
750 return fsurf;
751}
752
753static void
754focus_surface_destroy(struct focus_surface *fsurf)
755{
756 weston_surface_destroy(fsurf->surface);
757 free(fsurf);
758}
759
760static void
761focus_animation_done(struct weston_view_animation *animation, void *data)
762{
763 struct workspace *ws = data;
764
765 ws->focus_animation = NULL;
766}
767
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200768static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200769focus_state_destroy(struct focus_state *state)
770{
771 wl_list_remove(&state->seat_destroy_listener.link);
772 wl_list_remove(&state->surface_destroy_listener.link);
773 free(state);
774}
775
776static void
777focus_state_seat_destroy(struct wl_listener *listener, void *data)
778{
779 struct focus_state *state = container_of(listener,
780 struct focus_state,
781 seat_destroy_listener);
782
783 wl_list_remove(&state->link);
784 focus_state_destroy(state);
785}
786
787static void
788focus_state_surface_destroy(struct wl_listener *listener, void *data)
789{
790 struct focus_state *state = container_of(listener,
791 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400792 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400793 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500794 struct weston_surface *main_surface, *next;
795 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200796
Pekka Paalanen01388e22013-04-25 13:57:44 +0300797 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
798
Kristian Høgsberge3778222012-07-31 17:29:30 -0400799 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300800 wl_list_for_each(view,
801 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500802 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400803 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100804 if (is_focus_view(view))
805 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400806
Jason Ekstranda7af7042013-10-12 22:38:11 -0500807 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400808 break;
809 }
810
Pekka Paalanen01388e22013-04-25 13:57:44 +0300811 /* if the focus was a sub-surface, activate its main surface */
812 if (main_surface != state->keyboard_focus)
813 next = main_surface;
814
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100815 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400816 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100817 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100818 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400819 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100820 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
821 if (state->ws->focus_animation)
822 weston_view_animation_destroy(state->ws->focus_animation);
823
824 state->ws->focus_animation = weston_fade_run(
825 state->ws->fsurf_front->view,
826 state->ws->fsurf_front->view->alpha, 0.0, 300,
827 focus_animation_done, state->ws);
828 }
829
Kristian Høgsberge3778222012-07-31 17:29:30 -0400830 wl_list_remove(&state->link);
831 focus_state_destroy(state);
832 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200833}
834
835static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400836focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200837{
Jonas Ådahl04769742012-06-13 00:01:24 +0200838 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200839
840 state = malloc(sizeof *state);
841 if (state == NULL)
842 return NULL;
843
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400845 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200846 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400847 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200848
849 state->seat_destroy_listener.notify = focus_state_seat_destroy;
850 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400851 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200852 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400853 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200854
855 return state;
856}
857
Jonas Ådahl8538b222012-08-29 22:13:03 +0200858static struct focus_state *
859ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
860{
861 struct workspace *ws = get_current_workspace(shell);
862 struct focus_state *state;
863
864 wl_list_for_each(state, &ws->focus_list, link)
865 if (state->seat == seat)
866 break;
867
868 if (&state->link == &ws->focus_list)
869 state = focus_state_create(seat, ws);
870
871 return state;
872}
873
Jonas Ådahl04769742012-06-13 00:01:24 +0200874static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800875focus_state_set_focus(struct focus_state *state,
876 struct weston_surface *surface)
877{
878 if (state->keyboard_focus) {
879 wl_list_remove(&state->surface_destroy_listener.link);
880 wl_list_init(&state->surface_destroy_listener.link);
881 }
882
883 state->keyboard_focus = surface;
884 if (surface)
885 wl_signal_add(&surface->destroy_signal,
886 &state->surface_destroy_listener);
887}
888
889static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400890restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200891{
892 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400893 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100894 struct wl_list pending_seat_list;
895 struct weston_seat *seat, *next_seat;
896
897 /* Temporarily steal the list of seats so that we can keep
898 * track of the seats we've already processed */
899 wl_list_init(&pending_seat_list);
900 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
901 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200902
903 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100904 wl_list_remove(&state->seat->link);
905 wl_list_insert(&shell->compositor->seat_list,
906 &state->seat->link);
907
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800908 if (state->seat->keyboard == NULL)
909 continue;
910
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400911 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200912
Kristian Høgsberge3148752013-05-06 23:19:49 -0400913 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200914 }
Neil Roberts4237f502014-04-09 16:33:31 +0100915
916 /* For any remaining seats that we don't have a focus state
917 * for we'll reset the keyboard focus to NULL */
918 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
919 wl_list_insert(&shell->compositor->seat_list, &seat->link);
920
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300921 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100922 continue;
923
924 weston_keyboard_set_focus(seat->keyboard, NULL);
925 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200926}
927
928static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200929replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
930 struct weston_seat *seat)
931{
932 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200933
934 wl_list_for_each(state, &ws->focus_list, link) {
935 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800936 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200937 return;
938 }
939 }
940}
941
942static void
943drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
944 struct weston_surface *surface)
945{
946 struct focus_state *state;
947
948 wl_list_for_each(state, &ws->focus_list, link)
949 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800950 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200951}
952
953static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100954animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
955 struct weston_view *from, struct weston_view *to)
956{
957 struct weston_output *output;
958 bool focus_surface_created = false;
959
960 /* FIXME: Only support dim animation using two layers */
961 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
962 return;
963
964 output = get_default_output(shell->compositor);
965 if (ws->fsurf_front == NULL && (from || to)) {
966 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800967 if (ws->fsurf_front == NULL)
968 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100969 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800970
971 ws->fsurf_back = create_focus_surface(shell->compositor, output);
972 if (ws->fsurf_back == NULL) {
973 focus_surface_destroy(ws->fsurf_front);
974 return;
975 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800977
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978 focus_surface_created = true;
979 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300980 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
981 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100982 }
983
984 if (ws->focus_animation) {
985 weston_view_animation_destroy(ws->focus_animation);
986 ws->focus_animation = NULL;
987 }
988
989 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300990 weston_layer_entry_insert(&to->layer_link,
991 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300993 weston_layer_entry_insert(&ws->layer.view_list,
994 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100995
996 if (focus_surface_created) {
997 ws->focus_animation = weston_fade_run(
998 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200999 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 focus_animation_done, ws);
1001 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001002 weston_layer_entry_insert(&from->layer_link,
1003 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 ws->focus_animation = weston_stable_fade_run(
1005 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001006 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001007 focus_animation_done, ws);
1008 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001009 weston_layer_entry_insert(&ws->layer.view_list,
1010 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001011 ws->focus_animation = weston_stable_fade_run(
1012 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001013 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001014 focus_animation_done, ws);
1015 }
1016}
1017
1018static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001019workspace_destroy(struct workspace *ws)
1020{
Jonas Ådahl04769742012-06-13 00:01:24 +02001021 struct focus_state *state, *next;
1022
1023 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1024 focus_state_destroy(state);
1025
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001026 if (ws->fsurf_front)
1027 focus_surface_destroy(ws->fsurf_front);
1028 if (ws->fsurf_back)
1029 focus_surface_destroy(ws->fsurf_back);
1030
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001031 free(ws);
1032}
1033
Jonas Ådahl04769742012-06-13 00:01:24 +02001034static void
1035seat_destroyed(struct wl_listener *listener, void *data)
1036{
1037 struct weston_seat *seat = data;
1038 struct focus_state *state, *next;
1039 struct workspace *ws = container_of(listener,
1040 struct workspace,
1041 seat_destroyed_listener);
1042
1043 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1044 if (state->seat == seat)
1045 wl_list_remove(&state->link);
1046}
1047
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001048static struct workspace *
1049workspace_create(void)
1050{
1051 struct workspace *ws = malloc(sizeof *ws);
1052 if (ws == NULL)
1053 return NULL;
1054
1055 weston_layer_init(&ws->layer, NULL);
1056
Jonas Ådahl04769742012-06-13 00:01:24 +02001057 wl_list_init(&ws->focus_list);
1058 wl_list_init(&ws->seat_destroyed_listener.link);
1059 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001060 ws->fsurf_front = NULL;
1061 ws->fsurf_back = NULL;
1062 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001063
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001064 return ws;
1065}
1066
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067static int
1068workspace_is_empty(struct workspace *ws)
1069{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001070 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001071}
1072
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001073static struct workspace *
1074get_workspace(struct desktop_shell *shell, unsigned int index)
1075{
1076 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001077 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001078 pws += index;
1079 return *pws;
1080}
1081
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001082struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001083get_current_workspace(struct desktop_shell *shell)
1084{
1085 return get_workspace(shell, shell->workspaces.current);
1086}
1087
1088static void
1089activate_workspace(struct desktop_shell *shell, unsigned int index)
1090{
1091 struct workspace *ws;
1092
1093 ws = get_workspace(shell, index);
1094 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1095
1096 shell->workspaces.current = index;
1097}
1098
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001099static unsigned int
1100get_output_height(struct weston_output *output)
1101{
1102 return abs(output->region.extents.y1 - output->region.extents.y2);
1103}
1104
1105static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001106view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001108 struct weston_transform *transform;
1109
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001110 if (is_focus_view(view)) {
1111 struct focus_surface *fsurf = get_focus_surface(view->surface);
1112 transform = &fsurf->workspace_transform;
1113 } else {
1114 struct shell_surface *shsurf = get_shell_surface(view->surface);
1115 transform = &shsurf->workspace_transform;
1116 }
1117
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001118 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001120 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001122 weston_matrix_init(&transform->matrix);
1123 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126}
1127
1128static void
1129workspace_translate_out(struct workspace *ws, double fraction)
1130{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001132 unsigned int height;
1133 double d;
1134
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001135 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137 d = height * fraction;
1138
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001139 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001140 }
1141}
1142
1143static void
1144workspace_translate_in(struct workspace *ws, double fraction)
1145{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 unsigned int height;
1148 double d;
1149
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001150 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152
1153 if (fraction > 0)
1154 d = -(height - height * fraction);
1155 else
1156 d = height + height * fraction;
1157
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001158 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159 }
1160}
1161
1162static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001163broadcast_current_workspace_state(struct desktop_shell *shell)
1164{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001165 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001166
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001167 wl_resource_for_each(resource, &shell->workspaces.client_list)
1168 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001169 shell->workspaces.current,
1170 shell->workspaces.num);
1171}
1172
1173static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174reverse_workspace_change_animation(struct desktop_shell *shell,
1175 unsigned int index,
1176 struct workspace *from,
1177 struct workspace *to)
1178{
1179 shell->workspaces.current = index;
1180
1181 shell->workspaces.anim_to = to;
1182 shell->workspaces.anim_from = from;
1183 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1184 shell->workspaces.anim_timestamp = 0;
1185
Scott Moreau4272e632012-08-13 09:58:41 -06001186 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187}
1188
1189static void
1190workspace_deactivate_transforms(struct workspace *ws)
1191{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001193 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001194
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001195 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001196 if (is_focus_view(view)) {
1197 struct focus_surface *fsurf = get_focus_surface(view->surface);
1198 transform = &fsurf->workspace_transform;
1199 } else {
1200 struct shell_surface *shsurf = get_shell_surface(view->surface);
1201 transform = &shsurf->workspace_transform;
1202 }
1203
1204 if (!wl_list_empty(&transform->link)) {
1205 wl_list_remove(&transform->link);
1206 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001207 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001209 }
1210}
1211
1212static void
1213finish_workspace_change_animation(struct desktop_shell *shell,
1214 struct workspace *from,
1215 struct workspace *to)
1216{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001217 struct weston_view *view;
1218
Scott Moreau4272e632012-08-13 09:58:41 -06001219 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001220
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001221 /* Views that extend past the bottom of the output are still
1222 * visible after the workspace animation ends but before its layer
1223 * is hidden. In that case, we need to damage below those views so
1224 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001225 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001226 weston_view_damage_below(view);
1227
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228 wl_list_remove(&shell->workspaces.animation.link);
1229 workspace_deactivate_transforms(from);
1230 workspace_deactivate_transforms(to);
1231 shell->workspaces.anim_to = NULL;
1232
1233 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1234}
1235
1236static void
1237animate_workspace_change_frame(struct weston_animation *animation,
1238 struct weston_output *output, uint32_t msecs)
1239{
1240 struct desktop_shell *shell =
1241 container_of(animation, struct desktop_shell,
1242 workspaces.animation);
1243 struct workspace *from = shell->workspaces.anim_from;
1244 struct workspace *to = shell->workspaces.anim_to;
1245 uint32_t t;
1246 double x, y;
1247
1248 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1249 finish_workspace_change_animation(shell, from, to);
1250 return;
1251 }
1252
1253 if (shell->workspaces.anim_timestamp == 0) {
1254 if (shell->workspaces.anim_current == 0.0)
1255 shell->workspaces.anim_timestamp = msecs;
1256 else
1257 shell->workspaces.anim_timestamp =
1258 msecs -
1259 /* Invers of movement function 'y' below. */
1260 (asin(1.0 - shell->workspaces.anim_current) *
1261 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1262 M_2_PI);
1263 }
1264
1265 t = msecs - shell->workspaces.anim_timestamp;
1266
1267 /*
1268 * x = [0, π/2]
1269 * y(x) = sin(x)
1270 */
1271 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1272 y = sin(x);
1273
1274 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001275 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001276
1277 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1278 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1279 shell->workspaces.anim_current = y;
1280
Scott Moreau4272e632012-08-13 09:58:41 -06001281 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001282 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001283 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001284 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001285}
1286
1287static void
1288animate_workspace_change(struct desktop_shell *shell,
1289 unsigned int index,
1290 struct workspace *from,
1291 struct workspace *to)
1292{
1293 struct weston_output *output;
1294
1295 int dir;
1296
1297 if (index > shell->workspaces.current)
1298 dir = -1;
1299 else
1300 dir = 1;
1301
1302 shell->workspaces.current = index;
1303
1304 shell->workspaces.anim_dir = dir;
1305 shell->workspaces.anim_from = from;
1306 shell->workspaces.anim_to = to;
1307 shell->workspaces.anim_current = 0.0;
1308 shell->workspaces.anim_timestamp = 0;
1309
1310 output = container_of(shell->compositor->output_list.next,
1311 struct weston_output, link);
1312 wl_list_insert(&output->animation_list,
1313 &shell->workspaces.animation.link);
1314
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001315 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001316
1317 workspace_translate_in(to, 0);
1318
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001319 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001320
Scott Moreau4272e632012-08-13 09:58:41 -06001321 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001322}
1323
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001324static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001325update_workspace(struct desktop_shell *shell, unsigned int index,
1326 struct workspace *from, struct workspace *to)
1327{
1328 shell->workspaces.current = index;
1329 wl_list_insert(&from->layer.link, &to->layer.link);
1330 wl_list_remove(&from->layer.link);
1331}
1332
1333static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001334change_workspace(struct desktop_shell *shell, unsigned int index)
1335{
1336 struct workspace *from;
1337 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001338 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001339
1340 if (index == shell->workspaces.current)
1341 return;
1342
1343 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001344 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001345 return;
1346
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001347 from = get_current_workspace(shell);
1348 to = get_workspace(shell, index);
1349
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001350 if (shell->workspaces.anim_from == to &&
1351 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001352 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001353 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001354 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001355 return;
1356 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001357
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001358 if (shell->workspaces.anim_to != NULL)
1359 finish_workspace_change_animation(shell,
1360 shell->workspaces.anim_from,
1361 shell->workspaces.anim_to);
1362
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001363 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001364
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001365 if (shell->focus_animation_type != ANIMATION_NONE) {
1366 wl_list_for_each(state, &from->focus_list, link)
1367 if (state->keyboard_focus)
1368 animate_focus_change(shell, from,
1369 get_default_view(state->keyboard_focus), NULL);
1370
1371 wl_list_for_each(state, &to->focus_list, link)
1372 if (state->keyboard_focus)
1373 animate_focus_change(shell, to,
1374 NULL, get_default_view(state->keyboard_focus));
1375 }
1376
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001377 if (workspace_is_empty(to) && workspace_is_empty(from))
1378 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001379 else
1380 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001381
1382 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001383}
1384
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001385static bool
1386workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1387{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001388 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001389 struct wl_list *e;
1390
1391 if (wl_list_empty(list))
1392 return false;
1393
1394 e = list->next;
1395
1396 if (e->next != list)
1397 return false;
1398
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001399 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001400}
1401
1402static void
Philip Withnall659163d2013-11-25 18:01:36 +00001403move_surface_to_workspace(struct desktop_shell *shell,
1404 struct shell_surface *shsurf,
1405 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001406{
1407 struct workspace *from;
1408 struct workspace *to;
1409 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001410 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001411 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001412
1413 if (workspace == shell->workspaces.current)
1414 return;
1415
Philip Withnall659163d2013-11-25 18:01:36 +00001416 view = get_default_view(shsurf->surface);
1417 if (!view)
1418 return;
1419
1420 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1421
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001422 if (workspace >= shell->workspaces.num)
1423 workspace = shell->workspaces.num - 1;
1424
Jonas Ådahle9d22502012-08-29 22:13:01 +02001425 from = get_current_workspace(shell);
1426 to = get_workspace(shell, workspace);
1427
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001428 weston_layer_entry_remove(&view->layer_link);
1429 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001430
Philip Withnall648a4dd2013-11-25 18:01:44 +00001431 shell_surface_update_child_surface_layers(shsurf);
1432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001434 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1435 if (!seat->keyboard)
1436 continue;
1437
1438 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001440 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001441 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001442
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001444}
1445
1446static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001447take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001448 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001449 unsigned int index)
1450{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001451 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001453 struct shell_surface *shsurf;
1454 struct workspace *from;
1455 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001456 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001457
Pekka Paalanen01388e22013-04-25 13:57:44 +03001458 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 view = get_default_view(surface);
1460 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001461 index == shell->workspaces.current ||
1462 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001463 return;
1464
1465 from = get_current_workspace(shell);
1466 to = get_workspace(shell, index);
1467
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001468 weston_layer_entry_remove(&view->layer_link);
1469 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001470
Philip Withnall659163d2013-11-25 18:01:36 +00001471 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001472 if (shsurf != NULL)
1473 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001474
Jonas Ådahle9d22502012-08-29 22:13:01 +02001475 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001476 drop_focus_state(shell, from, surface);
1477
1478 if (shell->workspaces.anim_from == to &&
1479 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001480 wl_list_remove(&to->layer.link);
1481 wl_list_insert(from->layer.link.prev, &to->layer.link);
1482
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001483 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001484 broadcast_current_workspace_state(shell);
1485
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001486 return;
1487 }
1488
1489 if (shell->workspaces.anim_to != NULL)
1490 finish_workspace_change_animation(shell,
1491 shell->workspaces.anim_from,
1492 shell->workspaces.anim_to);
1493
1494 if (workspace_is_empty(from) &&
1495 workspace_has_only(to, surface))
1496 update_workspace(shell, index, from, to);
1497 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001498 if (shsurf != NULL &&
1499 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001500 wl_list_insert(&shell->workspaces.anim_sticky_list,
1501 &shsurf->workspace_transform.link);
1502
1503 animate_workspace_change(shell, index, from, to);
1504 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001505
1506 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001507
1508 state = ensure_focus_state(shell, seat);
1509 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001510 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001511}
1512
1513static void
1514workspace_manager_move_surface(struct wl_client *client,
1515 struct wl_resource *resource,
1516 struct wl_resource *surface_resource,
1517 uint32_t workspace)
1518{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001519 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001520 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001521 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001522 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001523 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001524
Pekka Paalanen01388e22013-04-25 13:57:44 +03001525 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001526 shell_surface = get_shell_surface(main_surface);
1527 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001529
1530 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001531}
1532
1533static const struct workspace_manager_interface workspace_manager_implementation = {
1534 workspace_manager_move_surface,
1535};
1536
1537static void
1538unbind_resource(struct wl_resource *resource)
1539{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001540 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001541}
1542
1543static void
1544bind_workspace_manager(struct wl_client *client,
1545 void *data, uint32_t version, uint32_t id)
1546{
1547 struct desktop_shell *shell = data;
1548 struct wl_resource *resource;
1549
Jason Ekstranda85118c2013-06-27 20:17:02 -05001550 resource = wl_resource_create(client,
1551 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001552
1553 if (resource == NULL) {
1554 weston_log("couldn't add workspace manager object");
1555 return;
1556 }
1557
Jason Ekstranda85118c2013-06-27 20:17:02 -05001558 wl_resource_set_implementation(resource,
1559 &workspace_manager_implementation,
1560 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001561 wl_list_insert(&shell->workspaces.client_list,
1562 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001563
1564 workspace_manager_send_state(resource,
1565 shell->workspaces.current,
1566 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001567}
1568
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001569static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001570touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1571 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1572{
1573}
1574
1575static void
1576touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1577{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001578 struct weston_touch_move_grab *move =
1579 (struct weston_touch_move_grab *) container_of(
1580 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001581
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001582 if (touch_id == 0)
1583 move->active = 0;
1584
Jonas Ådahl9484b692013-12-02 22:05:03 +01001585 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001586 shell_touch_grab_end(&move->base);
1587 free(move);
1588 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001589}
1590
1591static void
1592touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1593 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1594{
1595 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1596 struct shell_surface *shsurf = move->base.shsurf;
1597 struct weston_surface *es;
1598 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1599 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1600
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001601 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001602 return;
1603
1604 es = shsurf->surface;
1605
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001606 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001607
1608 weston_compositor_schedule_repaint(es->compositor);
1609}
1610
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001611static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001612touch_move_grab_frame(struct weston_touch_grab *grab)
1613{
1614}
1615
1616static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001617touch_move_grab_cancel(struct weston_touch_grab *grab)
1618{
1619 struct weston_touch_move_grab *move =
1620 (struct weston_touch_move_grab *) container_of(
1621 grab, struct shell_touch_grab, grab);
1622
1623 shell_touch_grab_end(&move->base);
1624 free(move);
1625}
1626
Rusty Lynch1084da52013-08-15 09:10:08 -07001627static const struct weston_touch_grab_interface touch_move_grab_interface = {
1628 touch_move_grab_down,
1629 touch_move_grab_up,
1630 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001631 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001632 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001633};
1634
1635static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001636surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001637{
1638 struct weston_touch_move_grab *move;
1639
1640 if (!shsurf)
1641 return -1;
1642
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001643 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001644 return 0;
1645
1646 move = malloc(sizeof *move);
1647 if (!move)
1648 return -1;
1649
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001650 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001652 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001654 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001655
1656 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001657 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001658
1659 return 0;
1660}
1661
1662static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001663noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001664{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001665}
1666
1667static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001668constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1669{
1670 struct shell_surface *shsurf = move->base.shsurf;
1671 struct weston_pointer *pointer = move->base.grab.pointer;
Jonny Lambd73c6942014-08-20 15:53:20 +02001672 int x, y, panel_width, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001673 const int safety = 50;
1674
1675 x = wl_fixed_to_int(pointer->x + move->dx);
1676 y = wl_fixed_to_int(pointer->y + move->dy);
1677
Jonny Lambd73c6942014-08-20 15:53:20 +02001678 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1679 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1680 &panel_width, &panel_height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001681
Jonny Lambd73c6942014-08-20 15:53:20 +02001682 bottom = y + shsurf->geometry.height;
1683 if (bottom - panel_height < safety)
1684 y = panel_height + safety -
1685 shsurf->geometry.height;
1686
1687 if (move->client_initiated &&
1688 y + shsurf->geometry.y < panel_height)
1689 y = panel_height - shsurf->geometry.y;
1690 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001691
1692 *cx = x;
1693 *cy = y;
1694}
1695
1696static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001697move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1698 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001699{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001700 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001701 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001702 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001703 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001704
1705 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001706 if (!shsurf)
1707 return;
1708
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001709 constrain_position(move, &cx, &cy);
1710
1711 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001712
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001714}
1715
1716static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001717move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001718 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001719{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001720 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1721 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001722 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001723 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001724
Daniel Stone4dbadb12012-05-30 16:31:51 +01001725 if (pointer->button_count == 0 &&
1726 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001727 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001728 free(grab);
1729 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001730}
1731
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001732static void
1733move_grab_cancel(struct weston_pointer_grab *grab)
1734{
1735 struct shell_grab *shell_grab =
1736 container_of(grab, struct shell_grab, grab);
1737
1738 shell_grab_end(shell_grab);
1739 free(grab);
1740}
1741
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001742static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001743 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001744 move_grab_motion,
1745 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001746 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001747};
1748
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001749static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001750surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001751 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752{
1753 struct weston_move_grab *move;
1754
1755 if (!shsurf)
1756 return -1;
1757
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001758 if (shsurf->grabbed ||
1759 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760 return 0;
1761
1762 move = malloc(sizeof *move);
1763 if (!move)
1764 return -1;
1765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001767 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001769 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001770 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771
1772 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Derek Foreman794fa0e2015-07-15 13:00:38 -05001773 pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774
1775 return 0;
1776}
1777
1778static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001779common_surface_move(struct wl_resource *resource,
1780 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001781{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001782 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001783 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001784 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001785
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001786 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001787 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001788 seat->pointer->button_count > 0 &&
1789 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001791 if ((surface == shsurf->surface) &&
Derek Foreman794fa0e2015-07-15 13:00:38 -05001792 (surface_move(shsurf, seat->pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001793 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001794 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001795 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001796 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001798 if ((surface == shsurf->surface) &&
Derek Foremanb7674ae2015-07-15 13:00:37 -05001799 (surface_touch_move(shsurf, seat->touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001800 wl_resource_post_no_memory(resource);
1801 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001802}
1803
Rafael Antognollie2a34552013-12-03 15:35:45 -02001804static void
1805shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1806 struct wl_resource *seat_resource, uint32_t serial)
1807{
1808 common_surface_move(resource, seat_resource, serial);
1809}
1810
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001811struct weston_resize_grab {
1812 struct shell_grab base;
1813 uint32_t edges;
1814 int32_t width, height;
1815};
1816
1817static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001818resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1819 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820{
1821 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001822 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001823 struct shell_surface *shsurf = resize->base.shsurf;
1824 int32_t width, height;
1825 wl_fixed_t from_x, from_y;
1826 wl_fixed_t to_x, to_y;
1827
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001828 weston_pointer_move(pointer, x, y);
1829
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001830 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001831 return;
1832
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 weston_view_from_global_fixed(shsurf->view,
1834 pointer->grab_x, pointer->grab_y,
1835 &from_x, &from_y);
1836 weston_view_from_global_fixed(shsurf->view,
1837 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001838
1839 width = resize->width;
1840 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1841 width += wl_fixed_to_int(from_x - to_x);
1842 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1843 width += wl_fixed_to_int(to_x - from_x);
1844 }
1845
1846 height = resize->height;
1847 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1848 height += wl_fixed_to_int(from_y - to_y);
1849 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1850 height += wl_fixed_to_int(to_y - from_y);
1851 }
1852
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001853 if (width < 1)
1854 width = 1;
1855 if (height < 1)
1856 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001857 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001858}
1859
1860static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001861send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001862{
1863 struct shell_surface *shsurf = get_shell_surface(surface);
1864
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001865 assert(shsurf);
1866
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001867 if (shsurf->resource)
1868 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001869 shsurf->resize_edges,
1870 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001871}
1872
1873static const struct weston_shell_client shell_client = {
1874 send_configure
1875};
1876
1877static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001878resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001879 uint32_t time, uint32_t button, uint32_t state_w)
1880{
1881 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001882 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001883 enum wl_pointer_button_state state = state_w;
1884
1885 if (pointer->button_count == 0 &&
1886 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1887 shell_grab_end(&resize->base);
1888 free(grab);
1889 }
1890}
1891
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001892static void
1893resize_grab_cancel(struct weston_pointer_grab *grab)
1894{
1895 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1896
1897 shell_grab_end(&resize->base);
1898 free(grab);
1899}
1900
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001901static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001902 noop_grab_focus,
1903 resize_grab_motion,
1904 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001905 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001906};
1907
Giulio Camuffob8366642013-04-25 13:57:46 +03001908/*
1909 * Returns the bounding box of a surface and all its sub-surfaces,
1910 * in the surface coordinates system. */
1911static void
1912surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1913 int32_t *y, int32_t *w, int32_t *h) {
1914 pixman_region32_t region;
1915 pixman_box32_t *box;
1916 struct weston_subsurface *subsurface;
1917
1918 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919 surface->width,
1920 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001921
1922 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1923 pixman_region32_union_rect(&region, &region,
1924 subsurface->position.x,
1925 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001926 subsurface->surface->width,
1927 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001928 }
1929
1930 box = pixman_region32_extents(&region);
1931 if (x)
1932 *x = box->x1;
1933 if (y)
1934 *y = box->y1;
1935 if (w)
1936 *w = box->x2 - box->x1;
1937 if (h)
1938 *h = box->y2 - box->y1;
1939
1940 pixman_region32_fini(&region);
1941}
1942
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001943static int
1944surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001945 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001946{
1947 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001948 const unsigned resize_topbottom =
1949 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1950 const unsigned resize_leftright =
1951 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1952 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001953
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001954 if (shsurf->grabbed ||
1955 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001956 return 0;
1957
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001958 /* Check for invalid edge combinations. */
1959 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1960 (edges & resize_topbottom) == resize_topbottom ||
1961 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001962 return 0;
1963
1964 resize = malloc(sizeof *resize);
1965 if (!resize)
1966 return -1;
1967
1968 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001969
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001970 resize->width = shsurf->geometry.width;
1971 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001972
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001973 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001974 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001975 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001976 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001977
1978 return 0;
1979}
1980
1981static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001982common_surface_resize(struct wl_resource *resource,
1983 struct wl_resource *seat_resource, uint32_t serial,
1984 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001985{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001986 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001987 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001988 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001989
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001990 if (seat->pointer == NULL ||
1991 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001992 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001993 seat->pointer->focus == NULL)
1994 return;
1995
1996 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1997 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001998 return;
1999
Derek Foreman8fbebbd2015-07-15 13:00:40 -05002000 if (surface_resize(shsurf, seat->pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002001 wl_resource_post_no_memory(resource);
2002}
2003
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002004static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002005shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2006 struct wl_resource *seat_resource, uint32_t serial,
2007 uint32_t edges)
2008{
2009 common_surface_resize(resource, seat_resource, serial, edges);
2010}
2011
2012static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002013busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002014{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002015 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002016 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002017 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002018 wl_fixed_t sx, sy;
2019
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 view = weston_compositor_pick_view(pointer->seat->compositor,
2021 pointer->x, pointer->y,
2022 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002023
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002024 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2025 shell_grab_end(grab);
2026 free(grab);
2027 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002028}
2029
2030static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002031busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2032 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002033{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002034 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002035}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002036
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002037static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002038busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002039 uint32_t time, uint32_t button, uint32_t state)
2040{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002041 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002042 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002043 struct weston_pointer *pointer = grab->grab.pointer;
2044 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002045
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002046 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002047 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002048 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002049 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002050 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002051 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002052 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002053}
2054
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002055static void
2056busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2057{
2058 struct shell_grab *grab = (struct shell_grab *) base;
2059
2060 shell_grab_end(grab);
2061 free(grab);
2062}
2063
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002064static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002065 busy_cursor_grab_focus,
2066 busy_cursor_grab_motion,
2067 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002068 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002069};
2070
2071static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002072set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002073{
2074 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002075
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002076 if (pointer->grab->interface == &busy_cursor_grab_interface)
2077 return;
2078
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002079 grab = malloc(sizeof *grab);
2080 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002081 return;
2082
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002083 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2084 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002085 /* Mark the shsurf as ungrabbed so that button binding is able
2086 * to move it. */
2087 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002088}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002089
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002090static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002091end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002092{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002093 struct shell_grab *grab;
2094 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002095
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002096 wl_list_for_each(seat, &compositor->seat_list, link) {
2097 if (seat->pointer == NULL)
2098 continue;
2099
2100 grab = (struct shell_grab *) seat->pointer->grab;
2101 if (grab->grab.interface == &busy_cursor_grab_interface &&
2102 wl_resource_get_client(grab->shsurf->resource) == client) {
2103 shell_grab_end(grab);
2104 free(grab);
2105 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002106 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002107}
2108
Scott Moreau9521d5e2012-04-19 13:06:17 -06002109static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002110handle_shell_client_destroy(struct wl_listener *listener, void *data);
2111
2112static int
2113xdg_ping_timeout_handler(void *data)
2114{
2115 struct shell_client *sc = data;
2116 struct weston_seat *seat;
2117 struct shell_surface *shsurf;
2118
2119 /* Client is not responding */
2120 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002121 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2122 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2123 continue;
2124 if (seat->pointer->focus->surface->resource == NULL)
2125 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002126
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002127 shsurf = get_shell_surface(seat->pointer->focus->surface);
2128 if (shsurf &&
2129 wl_resource_get_client(shsurf->resource) == sc->client)
2130 set_busy_cursor(shsurf, seat->pointer);
2131 }
2132
2133 return 1;
2134}
2135
2136static void
2137handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2138{
2139 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002140 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002141 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002142 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002143
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002144 if (sc->unresponsive) {
2145 xdg_ping_timeout_handler(sc);
2146 return;
2147 }
2148
2149 sc->ping_serial = serial;
2150 loop = wl_display_get_event_loop(compositor->wl_display);
2151 if (sc->ping_timer == NULL)
2152 sc->ping_timer =
2153 wl_event_loop_add_timer(loop,
2154 xdg_ping_timeout_handler, sc);
2155 if (sc->ping_timer == NULL)
2156 return;
2157
2158 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2159
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002160 if (shell_surface_is_xdg_surface(shsurf) ||
2161 shell_surface_is_xdg_popup(shsurf))
2162 xdg_shell_send_ping(sc->resource, serial);
2163 else if (shell_surface_is_wl_shell_surface(shsurf))
2164 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002165}
2166
Scott Moreauff1db4a2012-04-17 19:06:18 -06002167static void
2168ping_handler(struct weston_surface *surface, uint32_t serial)
2169{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002170 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002171
2172 if (!shsurf)
2173 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002174 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002175 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002176 if (shsurf->surface == shsurf->shell->grab_surface)
2177 return;
2178
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002179 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002180}
2181
2182static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002183handle_pointer_focus(struct wl_listener *listener, void *data)
2184{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002185 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002186 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002187 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002188 uint32_t serial;
2189
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002191 return;
2192
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002194 serial = wl_display_next_serial(compositor->wl_display);
2195 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002196}
2197
2198static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002199shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2200{
2201 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002202 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002203}
2204
2205static void
2206shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2207{
2208 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002209 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002210}
2211
2212static void
2213handle_keyboard_focus(struct wl_listener *listener, void *data)
2214{
2215 struct weston_keyboard *keyboard = data;
2216 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2217
2218 if (seat->focused_surface) {
2219 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2220 if (shsurf)
2221 shell_surface_lose_keyboard_focus(shsurf);
2222 }
2223
2224 seat->focused_surface = keyboard->focus;
2225
2226 if (seat->focused_surface) {
2227 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2228 if (shsurf)
2229 shell_surface_gain_keyboard_focus(shsurf);
2230 }
2231}
2232
2233static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002234shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002235{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002236 if (sc->ping_serial != serial)
2237 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002238
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002239 sc->unresponsive = 0;
2240 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002241
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002242 if (sc->ping_timer) {
2243 wl_event_source_remove(sc->ping_timer);
2244 sc->ping_timer = NULL;
2245 }
2246
2247}
2248
2249static void
2250shell_surface_pong(struct wl_client *client,
2251 struct wl_resource *resource, uint32_t serial)
2252{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002253 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2254 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002255
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002256 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002257}
2258
2259static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002260set_title(struct shell_surface *shsurf, const char *title)
2261{
2262 free(shsurf->title);
2263 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002264 shsurf->surface->timeline.force_refresh = 1;
2265}
2266
2267static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002268set_pid(struct shell_surface *shsurf, pid_t pid)
2269{
2270 /* We have no use for it */
2271}
2272
2273static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002274set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2275{
2276 shsurf->type = t;
2277 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002278}
2279
2280static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002281set_window_geometry(struct shell_surface *shsurf,
2282 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002283{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002284 shsurf->next_geometry.x = x;
2285 shsurf->next_geometry.y = y;
2286 shsurf->next_geometry.width = width;
2287 shsurf->next_geometry.height = height;
2288 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002289}
2290
2291static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002292shell_surface_set_title(struct wl_client *client,
2293 struct wl_resource *resource, const char *title)
2294{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002295 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002296
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002297 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002298}
2299
2300static void
2301shell_surface_set_class(struct wl_client *client,
2302 struct wl_resource *resource, const char *class)
2303{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002304 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002305
2306 free(shsurf->class);
2307 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002308 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002309}
2310
Alex Wu4539b082012-03-01 12:57:46 +08002311static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002312restore_output_mode(struct weston_output *output)
2313{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002314 if (output->original_mode)
2315 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002316}
2317
2318static void
2319restore_all_output_modes(struct weston_compositor *compositor)
2320{
2321 struct weston_output *output;
2322
2323 wl_list_for_each(output, &compositor->output_list, link)
2324 restore_output_mode(output);
2325}
2326
Philip Withnall07926d92013-11-25 18:01:40 +00002327/* The surface will be inserted into the list immediately after the link
2328 * returned by this function (i.e. will be stacked immediately above the
2329 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002330static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002331shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2332{
2333 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002334 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002335
2336 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002337 case SHELL_SURFACE_XWAYLAND:
2338 return &shsurf->shell->fullscreen_layer.view_list;
2339
2340 case SHELL_SURFACE_NONE:
2341 return NULL;
2342
Kristian Høgsbergead52422014-01-01 13:51:52 -08002343 case SHELL_SURFACE_POPUP:
2344 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002345 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002346 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002347 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002348 /* Move the surface to its parent layer so
2349 * that surfaces which are transient for
2350 * fullscreen surfaces don't get hidden by the
2351 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002352
2353 /* TODO: Handle a parent with multiple views */
2354 parent = get_default_view(shsurf->parent);
2355 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002356 return container_of(parent->layer_link.link.prev,
2357 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002358 }
Philip Withnall07926d92013-11-25 18:01:40 +00002359
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002360 /* Move the surface to a normal workspace layer so that surfaces
2361 * which were previously fullscreen or transient are no longer
2362 * rendered on top. */
2363 ws = get_current_workspace(shsurf->shell);
2364 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002365 }
2366
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002367 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002368}
2369
Philip Withnall648a4dd2013-11-25 18:01:44 +00002370static void
2371shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2372{
2373 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002375
2376 /* Move the child layers to the same workspace as shsurf. They will be
2377 * stacked above shsurf. */
2378 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002379 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002380 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002381 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002382 prev = container_of(shsurf->view->layer_link.link.prev,
2383 struct weston_layer_entry, link);
2384 weston_layer_entry_remove(&child->view->layer_link);
2385 weston_layer_entry_insert(prev,
2386 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002387 weston_view_geometry_dirty(child->view);
2388 weston_surface_damage(child->surface);
2389
2390 /* Recurse. We don’t expect this to recurse very far (if
2391 * at all) because that would imply we have transient
2392 * (or popup) children of transient surfaces, which
2393 * would be unusual. */
2394 shell_surface_update_child_surface_layers(child);
2395 }
2396 }
2397}
2398
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002399/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002400 * geometry to ensure the changes are redrawn.
2401 *
2402 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2403 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002404static void
2405shell_surface_update_layer(struct shell_surface *shsurf)
2406{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002407 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002408
2409 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2410
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002411 if (new_layer_link == NULL)
2412 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002413 if (new_layer_link == &shsurf->view->layer_link)
2414 return;
2415
2416 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002417 weston_layer_entry_remove(&shsurf->view->layer_link);
2418 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002419 weston_view_geometry_dirty(shsurf->view);
2420 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002421
2422 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002423}
2424
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002425static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002426shell_surface_set_parent(struct shell_surface *shsurf,
2427 struct weston_surface *parent)
2428{
2429 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002430
2431 wl_list_remove(&shsurf->children_link);
2432 wl_list_init(&shsurf->children_link);
2433
2434 /* Insert into the parent surface’s child list. */
2435 if (parent != NULL) {
2436 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2437 if (parent_shsurf != NULL)
2438 wl_list_insert(&parent_shsurf->children_list,
2439 &shsurf->children_link);
2440 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002441}
2442
2443static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002444shell_surface_set_output(struct shell_surface *shsurf,
2445 struct weston_output *output)
2446{
2447 struct weston_surface *es = shsurf->surface;
2448
2449 /* get the default output, if the client set it as NULL
2450 check whether the ouput is available */
2451 if (output)
2452 shsurf->output = output;
2453 else if (es->output)
2454 shsurf->output = es->output;
2455 else
2456 shsurf->output = get_default_output(es->compositor);
2457}
2458
2459static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002460surface_clear_next_states(struct shell_surface *shsurf)
2461{
2462 shsurf->next_state.maximized = false;
2463 shsurf->next_state.fullscreen = false;
2464
2465 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2466 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2467 shsurf->state_changed = true;
2468}
2469
2470static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002471set_toplevel(struct shell_surface *shsurf)
2472{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002473 shell_surface_set_parent(shsurf, NULL);
2474 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002475 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002476
2477 /* The layer_link is updated in set_surface_type(),
2478 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002479}
2480
2481static void
2482shell_surface_set_toplevel(struct wl_client *client,
2483 struct wl_resource *resource)
2484{
2485 struct shell_surface *surface = wl_resource_get_user_data(resource);
2486
2487 set_toplevel(surface);
2488}
2489
2490static void
2491set_transient(struct shell_surface *shsurf,
2492 struct weston_surface *parent, int x, int y, uint32_t flags)
2493{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002494 assert(parent != NULL);
2495
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002496 shell_surface_set_parent(shsurf, parent);
2497
2498 surface_clear_next_states(shsurf);
2499
Philip Withnallbecb77e2013-11-25 18:01:30 +00002500 shsurf->transient.x = x;
2501 shsurf->transient.y = y;
2502 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002503
Rafael Antognollied207b42013-12-03 15:35:43 -02002504 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002505 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002506 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002507
2508 /* The layer_link is updated in set_surface_type(),
2509 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002510}
2511
2512static void
2513shell_surface_set_transient(struct wl_client *client,
2514 struct wl_resource *resource,
2515 struct wl_resource *parent_resource,
2516 int x, int y, uint32_t flags)
2517{
2518 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2519 struct weston_surface *parent =
2520 wl_resource_get_user_data(parent_resource);
2521
2522 set_transient(shsurf, parent, x, y, flags);
2523}
2524
2525static void
2526set_fullscreen(struct shell_surface *shsurf,
2527 uint32_t method,
2528 uint32_t framerate,
2529 struct weston_output *output)
2530{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002531 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002532 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002533
2534 shsurf->fullscreen_output = shsurf->output;
2535 shsurf->fullscreen.type = method;
2536 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002537
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002538 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002539}
2540
2541static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002542weston_view_set_initial_position(struct weston_view *view,
2543 struct desktop_shell *shell);
2544
2545static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002546unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002547{
Philip Withnallf85fe842013-11-25 18:01:37 +00002548 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002549 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2550 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002551 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002552 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002553
Alex Wu4539b082012-03-01 12:57:46 +08002554 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2555 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002556
Alex Wu4539b082012-03-01 12:57:46 +08002557 wl_list_remove(&shsurf->fullscreen.transform.link);
2558 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002559
Jason Ekstranda7af7042013-10-12 22:38:11 -05002560 if (shsurf->fullscreen.black_view)
2561 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2562 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002563
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002564 if (shsurf->saved_position_valid)
2565 weston_view_set_position(shsurf->view,
2566 shsurf->saved_x, shsurf->saved_y);
2567 else
2568 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2569
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002570 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002572 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002573 shsurf->saved_rotation_valid = false;
2574 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002575
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002576 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002577}
2578
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002579static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002580shell_surface_set_fullscreen(struct wl_client *client,
2581 struct wl_resource *resource,
2582 uint32_t method,
2583 uint32_t framerate,
2584 struct wl_resource *output_resource)
2585{
2586 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2587 struct weston_output *output;
2588
2589 if (output_resource)
2590 output = wl_resource_get_user_data(output_resource);
2591 else
2592 output = NULL;
2593
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002594 shell_surface_set_parent(shsurf, NULL);
2595
Rafael Antognolli03b16592013-12-03 15:35:42 -02002596 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002597 shsurf->next_state.fullscreen = true;
2598 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002599 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002600}
2601
2602static void
2603set_popup(struct shell_surface *shsurf,
2604 struct weston_surface *parent,
2605 struct weston_seat *seat,
2606 uint32_t serial,
2607 int32_t x,
2608 int32_t y)
2609{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002610 assert(parent != NULL);
2611
Philip Withnallbecb77e2013-11-25 18:01:30 +00002612 shsurf->popup.shseat = get_shell_seat(seat);
2613 shsurf->popup.serial = serial;
2614 shsurf->popup.x = x;
2615 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002616
Pekka Paalanenb5026542014-11-12 15:09:24 +02002617 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002618}
2619
2620static void
2621shell_surface_set_popup(struct wl_client *client,
2622 struct wl_resource *resource,
2623 struct wl_resource *seat_resource,
2624 uint32_t serial,
2625 struct wl_resource *parent_resource,
2626 int32_t x, int32_t y, uint32_t flags)
2627{
2628 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002629 struct weston_surface *parent =
2630 wl_resource_get_user_data(parent_resource);
2631
2632 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002633
Rafael Antognolli03b16592013-12-03 15:35:42 -02002634 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002635 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002636 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002637 wl_resource_get_user_data(seat_resource),
2638 serial, x, y);
2639}
2640
2641static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002642unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002643{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002644 /* undo all maximized things here */
2645 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002646
2647 if (shsurf->saved_position_valid)
2648 weston_view_set_position(shsurf->view,
2649 shsurf->saved_x, shsurf->saved_y);
2650 else
2651 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002652
2653 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 wl_list_insert(&shsurf->view->geometry.transformation_list,
2655 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002656 shsurf->saved_rotation_valid = false;
2657 }
2658
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002659 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002660}
2661
Philip Withnallbecb77e2013-11-25 18:01:30 +00002662static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002663set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002664{
2665 struct shell_surface *shsurf;
2666 struct workspace *current_ws;
2667 struct weston_seat *seat;
2668 struct weston_surface *focus;
2669 struct weston_view *view;
2670
2671 view = get_default_view(surface);
2672 if (!view)
2673 return;
2674
2675 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2676
2677 shsurf = get_shell_surface(surface);
2678 current_ws = get_current_workspace(shsurf->shell);
2679
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002680 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002681 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002682
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002683 drop_focus_state(shsurf->shell, current_ws, view->surface);
2684 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2685 if (!seat->keyboard)
2686 continue;
2687 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2688 if (focus == view->surface)
2689 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002690 }
2691
2692 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002693 weston_view_damage_below(view);
2694}
2695
2696static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002697shell_surface_set_maximized(struct wl_client *client,
2698 struct wl_resource *resource,
2699 struct wl_resource *output_resource)
2700{
2701 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2702 struct weston_output *output;
2703
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002704 surface_clear_next_states(shsurf);
2705 shsurf->next_state.maximized = true;
2706 shsurf->state_changed = true;
2707
Pekka Paalanenb5026542014-11-12 15:09:24 +02002708 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002709 shell_surface_set_parent(shsurf, NULL);
2710
Philip Withnallbecb77e2013-11-25 18:01:30 +00002711 if (output_resource)
2712 output = wl_resource_get_user_data(output_resource);
2713 else
2714 output = NULL;
2715
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002716 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002717
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002718 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002719}
2720
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002721/* This is only ever called from set_surface_type(), so there’s no need to
2722 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002723static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002724reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002725{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002726 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002727 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002728 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002729 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002730
Pekka Paalanen98262232011-12-01 10:42:22 +02002731 return 0;
2732}
2733
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002734static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002735set_full_output(struct shell_surface *shsurf)
2736{
2737 shsurf->saved_x = shsurf->view->geometry.x;
2738 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002739 shsurf->saved_width = shsurf->surface->width;
2740 shsurf->saved_height = shsurf->surface->height;
2741 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002742 shsurf->saved_position_valid = true;
2743
2744 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2745 wl_list_remove(&shsurf->rotation.transform.link);
2746 wl_list_init(&shsurf->rotation.transform.link);
2747 weston_view_geometry_dirty(shsurf->view);
2748 shsurf->saved_rotation_valid = true;
2749 }
2750}
2751
2752static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002753set_surface_type(struct shell_surface *shsurf)
2754{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002755 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002757
Philip Withnallbecb77e2013-11-25 18:01:30 +00002758 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002759
Rafael Antognolli03b16592013-12-03 15:35:42 -02002760 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002761 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002762
2763 switch (shsurf->type) {
2764 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002765 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002766 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002767 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002768 weston_view_set_position(shsurf->view,
2769 pev->geometry.x + shsurf->transient.x,
2770 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002771 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002772 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002773
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002774 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 weston_view_set_position(shsurf->view, shsurf->transient.x,
2776 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002777 break;
2778
Philip Withnall0f640e22013-11-25 18:01:31 +00002779 case SHELL_SURFACE_POPUP:
2780 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002781 default:
2782 break;
2783 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002784
2785 /* Update the surface’s layer. */
2786 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002787}
2788
Tiago Vignattibe143262012-04-16 17:31:41 +03002789static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002790shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002791{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002792 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002793}
2794
Pekka Paalanen8274d902014-08-06 19:36:51 +03002795static int
2796black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2797{
2798 struct weston_surface *fs_surface = surface->configure_private;
2799 int n;
2800 int rem;
2801 int ret;
2802
2803 n = snprintf(buf, len, "black background surface for ");
2804 if (n < 0)
2805 return n;
2806
2807 rem = (int)len - n;
2808 if (rem < 0)
2809 rem = 0;
2810
2811 if (fs_surface->get_label)
2812 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2813 else
2814 ret = snprintf(buf + n, rem, "<unknown>");
2815
2816 if (ret < 0)
2817 return n;
2818
2819 return n + ret;
2820}
2821
Alex Wu21858432012-04-01 20:13:08 +08002822static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002823black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002826create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002827 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002828 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002829{
2830 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002831 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002832
2833 surface = weston_surface_create(ec);
2834 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002835 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002836 return NULL;
2837 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002838 view = weston_view_create(surface);
2839 if (surface == NULL) {
2840 weston_log("no memory\n");
2841 weston_surface_destroy(surface);
2842 return NULL;
2843 }
Alex Wu4539b082012-03-01 12:57:46 +08002844
Alex Wu21858432012-04-01 20:13:08 +08002845 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002846 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002847 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002848 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002849 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002850 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002851 pixman_region32_fini(&surface->input);
2852 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002853
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002854 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002855 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856
2857 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002858}
2859
Philip Withnalled8f1a92013-11-25 18:01:43 +00002860static void
2861shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2862{
2863 struct weston_output *output = shsurf->fullscreen_output;
2864
Rafael Antognolli03b16592013-12-03 15:35:42 -02002865 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002866
2867 if (!shsurf->fullscreen.black_view)
2868 shsurf->fullscreen.black_view =
2869 create_black_surface(shsurf->surface->compositor,
2870 shsurf->surface,
2871 output->x, output->y,
2872 output->width,
2873 output->height);
2874
2875 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002876 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2877 weston_layer_entry_insert(&shsurf->view->layer_link,
2878 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002879 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2880 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002881
2882 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002883}
2884
Alex Wu4539b082012-03-01 12:57:46 +08002885/* Create black surface and append it to the associated fullscreen surface.
2886 * Handle size dismatch and positioning according to the method. */
2887static void
2888shell_configure_fullscreen(struct shell_surface *shsurf)
2889{
2890 struct weston_output *output = shsurf->fullscreen_output;
2891 struct weston_surface *surface = shsurf->surface;
2892 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002893 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002894 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002895
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002896 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2897 restore_output_mode(output);
2898
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002899 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002900 weston_layer_entry_remove(&shsurf->view->layer_link);
2901 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002902
Philip Withnalled8f1a92013-11-25 18:01:43 +00002903 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002904
Jason Ekstranda7af7042013-10-12 22:38:11 -05002905 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002906 &surf_width, &surf_height);
2907
Alex Wu4539b082012-03-01 12:57:46 +08002908 switch (shsurf->fullscreen.type) {
2909 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002910 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002911 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002912 break;
2913 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002914 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002915 if (output->width == surf_width &&
2916 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002917 weston_view_set_position(shsurf->view,
2918 output->x - surf_x,
2919 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002920 break;
2921 }
2922
Alex Wu4539b082012-03-01 12:57:46 +08002923 matrix = &shsurf->fullscreen.transform.matrix;
2924 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002925
Scott Moreau1bad5db2012-08-18 01:04:05 -06002926 output_aspect = (float) output->width /
2927 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 /* XXX: Use surf_width and surf_height here? */
2929 surface_aspect = (float) surface->width /
2930 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002931 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002932 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002933 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002934 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002935 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002936 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002937
Alex Wu4539b082012-03-01 12:57:46 +08002938 weston_matrix_scale(matrix, scale, scale, 1);
2939 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002941 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002942 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2943 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002944 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002945
Alex Wu4539b082012-03-01 12:57:46 +08002946 break;
2947 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002948 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002949 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002950 surf_width * surface->buffer_viewport.buffer.scale,
2951 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002952 shsurf->fullscreen.framerate};
2953
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002954 if (weston_output_mode_switch_to_temporary(output, &mode,
2955 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 weston_view_set_position(shsurf->view,
2957 output->x - surf_x,
2958 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002959 shsurf->fullscreen.black_view->surface->width = output->width;
2960 shsurf->fullscreen.black_view->surface->height = output->height;
2961 weston_view_set_position(shsurf->fullscreen.black_view,
2962 output->x - surf_x,
2963 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002964 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002965 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002966 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002967 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002969 }
Alex Wubd3354b2012-04-17 17:20:49 +08002970 }
Alex Wu4539b082012-03-01 12:57:46 +08002971 break;
2972 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002973 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002974 break;
2975 default:
2976 break;
2977 }
2978}
2979
Alex Wu4539b082012-03-01 12:57:46 +08002980static void
2981shell_map_fullscreen(struct shell_surface *shsurf)
2982{
Alex Wubd3354b2012-04-17 17:20:49 +08002983 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002984}
2985
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002986static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002987set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2988{
2989 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002990 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002991 shsurf->transient.x = x;
2992 shsurf->transient.y = y;
2993 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002994
2995 shell_surface_set_parent(shsurf, NULL);
2996
Pekka Paalanenb5026542014-11-12 15:09:24 +02002997 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002998 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002999}
3000
Kristian Høgsberg47792412014-05-04 13:47:06 -07003001static void
3002shell_interface_set_fullscreen(struct shell_surface *shsurf,
3003 uint32_t method,
3004 uint32_t framerate,
3005 struct weston_output *output)
3006{
3007 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003008 shsurf->next_state.fullscreen = true;
3009 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003010
3011 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003012}
3013
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003014static struct weston_output *
3015get_focused_output(struct weston_compositor *compositor)
3016{
3017 struct weston_seat *seat;
3018 struct weston_output *output = NULL;
3019
3020 wl_list_for_each(seat, &compositor->seat_list, link) {
3021 /* Priority has touch focus, then pointer and
3022 * then keyboard focus. We should probably have
3023 * three for loops and check frist for touch,
3024 * then for pointer, etc. but unless somebody has some
3025 * objections, I think this is sufficient. */
3026 if (seat->touch && seat->touch->focus)
3027 output = seat->touch->focus->output;
3028 else if (seat->pointer && seat->pointer->focus)
3029 output = seat->pointer->focus->output;
3030 else if (seat->keyboard && seat->keyboard->focus)
3031 output = seat->keyboard->focus->output;
3032
3033 if (output)
3034 break;
3035 }
3036
3037 return output;
3038}
3039
3040static void
3041shell_interface_set_maximized(struct shell_surface *shsurf)
3042{
3043 struct weston_output *output;
3044
3045 surface_clear_next_states(shsurf);
3046 shsurf->next_state.maximized = true;
3047 shsurf->state_changed = true;
3048 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3049
3050 if (!weston_surface_is_mapped(shsurf->surface))
3051 output = get_focused_output(shsurf->surface->compositor);
3052 else
3053 output = shsurf->surface->output;
3054
3055 shell_surface_set_output(shsurf, output);
3056 send_configure_for_surface(shsurf);
3057}
3058
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003059static int
3060shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3061{
Derek Foreman794fa0e2015-07-15 13:00:38 -05003062 return surface_move(shsurf, ws->pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003063}
3064
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003065static int
3066shell_interface_resize(struct shell_surface *shsurf,
3067 struct weston_seat *ws,
3068 uint32_t edges)
3069{
3070 return surface_resize(shsurf, ws->pointer, edges);
3071}
3072
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003073static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074
3075static void
3076destroy_shell_seat(struct wl_listener *listener, void *data)
3077{
3078 struct shell_seat *shseat =
3079 container_of(listener,
3080 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003081 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003082
3083 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003084 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003085 shseat->popup_grab.client = NULL;
3086
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003087 wl_list_for_each_safe(shsurf, next,
3088 &shseat->popup_grab.surfaces_list,
3089 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003090 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003091 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003092 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003093 }
3094
3095 wl_list_remove(&shseat->seat_destroy_listener.link);
3096 free(shseat);
3097}
3098
Jason Ekstrand024177c2014-04-21 19:42:58 -05003099static void
3100shell_seat_caps_changed(struct wl_listener *listener, void *data)
3101{
3102 struct shell_seat *seat;
3103
3104 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3105
3106 if (seat->seat->keyboard &&
3107 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3108 wl_signal_add(&seat->seat->keyboard->focus_signal,
3109 &seat->keyboard_focus_listener);
3110 } else if (!seat->seat->keyboard) {
3111 wl_list_init(&seat->keyboard_focus_listener.link);
3112 }
3113
3114 if (seat->seat->pointer &&
3115 wl_list_empty(&seat->pointer_focus_listener.link)) {
3116 wl_signal_add(&seat->seat->pointer->focus_signal,
3117 &seat->pointer_focus_listener);
3118 } else if (!seat->seat->pointer) {
3119 wl_list_init(&seat->pointer_focus_listener.link);
3120 }
3121}
3122
Giulio Camuffo5085a752013-03-25 21:42:45 +01003123static struct shell_seat *
3124create_shell_seat(struct weston_seat *seat)
3125{
3126 struct shell_seat *shseat;
3127
3128 shseat = calloc(1, sizeof *shseat);
3129 if (!shseat) {
3130 weston_log("no memory to allocate shell seat\n");
3131 return NULL;
3132 }
3133
3134 shseat->seat = seat;
3135 wl_list_init(&shseat->popup_grab.surfaces_list);
3136
3137 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3138 wl_signal_add(&seat->destroy_signal,
3139 &shseat->seat_destroy_listener);
3140
Jason Ekstrand024177c2014-04-21 19:42:58 -05003141 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3142 wl_list_init(&shseat->keyboard_focus_listener.link);
3143
3144 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3145 wl_list_init(&shseat->pointer_focus_listener.link);
3146
3147 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3148 wl_signal_add(&seat->updated_caps_signal,
3149 &shseat->caps_changed_listener);
3150 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3151
Giulio Camuffo5085a752013-03-25 21:42:45 +01003152 return shseat;
3153}
3154
3155static struct shell_seat *
3156get_shell_seat(struct weston_seat *seat)
3157{
3158 struct wl_listener *listener;
3159
3160 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003161 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003162
3163 return container_of(listener,
3164 struct shell_seat, seat_destroy_listener);
3165}
3166
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003167static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003168popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003169{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003170 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003171 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003172 struct shell_seat *shseat =
3173 container_of(grab, struct shell_seat, popup_grab.grab);
3174 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003175 wl_fixed_t sx, sy;
3176
Jason Ekstranda7af7042013-10-12 22:38:11 -05003177 view = weston_compositor_pick_view(pointer->seat->compositor,
3178 pointer->x, pointer->y,
3179 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003180
Jason Ekstranda7af7042013-10-12 22:38:11 -05003181 if (view && view->surface->resource &&
3182 wl_resource_get_client(view->surface->resource) == client) {
3183 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003184 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003185 weston_pointer_set_focus(pointer, NULL,
3186 wl_fixed_from_int(0),
3187 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003188 }
3189}
3190
3191static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003192popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3193 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003194{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003195 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003196 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003197 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003198
Jonas Ådahl61917c82014-08-11 22:44:02 +02003199 if (pointer->focus) {
3200 weston_view_from_global_fixed(pointer->focus, x, y,
3201 &pointer->sx, &pointer->sy);
3202 }
3203
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003204 weston_pointer_move(pointer, x, y);
3205
Neil Roberts96d790e2013-09-19 17:32:00 +01003206 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003207 weston_view_from_global_fixed(pointer->focus,
3208 pointer->x, pointer->y,
3209 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003210 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003211 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212}
3213
3214static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003215popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003216 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003217{
3218 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003219 struct shell_seat *shseat =
3220 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003221 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003222 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003223 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003224 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003225
Neil Roberts96d790e2013-09-19 17:32:00 +01003226 resource_list = &grab->pointer->focus_resource_list;
3227 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003228 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003229 wl_resource_for_each(resource, resource_list) {
3230 wl_pointer_send_button(resource, serial,
3231 time, button, state);
3232 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003233 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003234 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003235 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003236 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003237 }
3238
Daniel Stone4dbadb12012-05-30 16:31:51 +01003239 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003240 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003241}
3242
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003243static void
3244popup_grab_cancel(struct weston_pointer_grab *grab)
3245{
3246 popup_grab_end(grab->pointer);
3247}
3248
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003249static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003250 popup_grab_focus,
3251 popup_grab_motion,
3252 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003253 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003254};
3255
3256static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003257touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3258 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3259{
3260 struct wl_resource *resource;
3261 struct shell_seat *shseat =
3262 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3263 struct wl_display *display = shseat->seat->compositor->wl_display;
3264 uint32_t serial;
3265 struct wl_list *resource_list;
3266
3267 resource_list = &grab->touch->focus_resource_list;
3268 if (!wl_list_empty(resource_list)) {
3269 serial = wl_display_get_serial(display);
3270 wl_resource_for_each(resource, resource_list) {
3271 wl_touch_send_down(resource, serial, time,
3272 grab->touch->focus->surface->resource,
3273 touch_id, sx, sy);
3274 }
3275 }
3276}
3277
3278static void
3279touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3280{
3281 struct wl_resource *resource;
3282 struct shell_seat *shseat =
3283 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3284 struct wl_display *display = shseat->seat->compositor->wl_display;
3285 uint32_t serial;
3286 struct wl_list *resource_list;
3287
3288 resource_list = &grab->touch->focus_resource_list;
3289 if (!wl_list_empty(resource_list)) {
3290 serial = wl_display_get_serial(display);
3291 wl_resource_for_each(resource, resource_list) {
3292 wl_touch_send_up(resource, serial, time, touch_id);
3293 }
3294 }
3295}
3296
3297static void
3298touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3299 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3300{
3301 struct wl_resource *resource;
3302 struct wl_list *resource_list;
3303
3304 resource_list = &grab->touch->focus_resource_list;
3305 if (!wl_list_empty(resource_list)) {
3306 wl_resource_for_each(resource, resource_list) {
3307 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3308 }
3309 }
3310}
3311
3312static void
3313touch_popup_grab_frame(struct weston_touch_grab *grab)
3314{
3315}
3316
3317static void
3318touch_popup_grab_cancel(struct weston_touch_grab *grab)
3319{
3320 touch_popup_grab_end(grab->touch);
3321}
3322
3323static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3324 touch_popup_grab_down,
3325 touch_popup_grab_up,
3326 touch_popup_grab_motion,
3327 touch_popup_grab_frame,
3328 touch_popup_grab_cancel,
3329};
3330
3331static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003332shell_surface_send_popup_done(struct shell_surface *shsurf)
3333{
3334 if (shell_surface_is_wl_shell_surface(shsurf))
3335 wl_shell_surface_send_popup_done(shsurf->resource);
3336 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003337 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003338}
3339
3340static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003341popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003342{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003343 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003344 struct shell_seat *shseat =
3345 container_of(grab, struct shell_seat, popup_grab.grab);
3346 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003347 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003348
3349 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003350 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003351 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003352 shseat->popup_grab.grab.interface = NULL;
3353 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003354 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003355 wl_list_for_each_safe(shsurf, next,
3356 &shseat->popup_grab.surfaces_list,
3357 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003358 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003359 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003360 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003361 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003362 wl_list_init(&shseat->popup_grab.surfaces_list);
3363 }
3364}
3365
3366static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003367touch_popup_grab_end(struct weston_touch *touch)
3368{
3369 struct weston_touch_grab *grab = touch->grab;
3370 struct shell_seat *shseat =
3371 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3372 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003373 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003374
3375 if (touch->grab->interface == &touch_popup_grab_interface) {
3376 weston_touch_end_grab(grab->touch);
3377 shseat->popup_grab.client = NULL;
3378 shseat->popup_grab.touch_grab.interface = NULL;
3379 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3380 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003381 wl_list_for_each_safe(shsurf, next,
3382 &shseat->popup_grab.surfaces_list,
3383 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003384 shell_surface_send_popup_done(shsurf);
3385 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003386 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003387 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003388 wl_list_init(&shseat->popup_grab.surfaces_list);
3389 }
3390}
3391
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003392static struct shell_surface *
3393get_top_popup(struct shell_seat *shseat)
3394{
3395 struct shell_surface *shsurf;
3396
3397 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3398 return NULL;
3399 } else {
3400 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3401 struct shell_surface,
3402 popup.grab_link);
3403 return shsurf;
3404 }
3405}
3406
3407static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003408add_popup_grab(struct shell_surface *shsurf,
3409 struct shell_seat *shseat,
3410 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003411{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003412 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003413 struct shell_surface *parent, *top_surface;
3414
3415 parent = get_shell_surface(shsurf->parent);
3416 top_surface = get_top_popup(shseat);
3417 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003418 (!parent ||
3419 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003420 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003421 wl_resource_post_error(shsurf->owner_resource,
3422 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003423 "xdg_popup was not created on the "
3424 "topmost popup");
3425 return -1;
3426 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003427
3428 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003429 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003430 shseat->popup_grab.client =
3431 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003432
3433 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003434 shseat->popup_grab.grab.interface =
3435 &popup_grab_interface;
3436
3437 /* We must make sure here that this popup was opened
3438 * after a mouse press, and not just by moving around
3439 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003440 if (shseat->seat->pointer->button_count > 0)
3441 shseat->popup_grab.initial_up = 0;
3442 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003443 shseat->popup_grab.touch_grab.interface =
3444 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003445 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003446
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003447 wl_list_insert(&shseat->popup_grab.surfaces_list,
3448 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003449
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003450 if (type == POINTER) {
3451 weston_pointer_start_grab(seat->pointer,
3452 &shseat->popup_grab.grab);
3453 } else if (type == TOUCH) {
3454 weston_touch_start_grab(seat->touch,
3455 &shseat->popup_grab.touch_grab);
3456 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003457 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003458 wl_list_insert(&shseat->popup_grab.surfaces_list,
3459 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003460 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003461
3462 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003463}
3464
3465static void
3466remove_popup_grab(struct shell_surface *shsurf)
3467{
3468 struct shell_seat *shseat = shsurf->popup.shseat;
3469
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003470 if (shell_surface_is_xdg_popup(shsurf) &&
3471 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003472 wl_resource_post_error(shsurf->owner_resource,
3473 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003474 "xdg_popup was destroyed while it was "
3475 "not the topmost popup.");
3476 return;
3477 }
3478
Giulio Camuffo5085a752013-03-25 21:42:45 +01003479 wl_list_remove(&shsurf->popup.grab_link);
3480 wl_list_init(&shsurf->popup.grab_link);
3481 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003482 if (shseat->popup_grab.type == POINTER) {
3483 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3484 shseat->popup_grab.grab.interface = NULL;
3485 } else if (shseat->popup_grab.type == TOUCH) {
3486 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3487 shseat->popup_grab.touch_grab.interface = NULL;
3488 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003489 }
3490}
3491
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003492static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003493shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003494{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003495 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003496 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003497
Jason Ekstranda7af7042013-10-12 22:38:11 -05003498 shsurf->surface->output = parent_view->output;
3499 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003500
Jason Ekstranda7af7042013-10-12 22:38:11 -05003501 weston_view_set_transform_parent(shsurf->view, parent_view);
3502 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3503 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003504
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003505 if (shseat->seat->pointer &&
3506 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003507 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3508 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003509 } else if (shseat->seat->touch &&
3510 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003511 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3512 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003513 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003514 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003515 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003516 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003517
3518 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003519}
3520
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003521static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003522 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003523 shell_surface_move,
3524 shell_surface_resize,
3525 shell_surface_set_toplevel,
3526 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003527 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003528 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003529 shell_surface_set_maximized,
3530 shell_surface_set_title,
3531 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003532};
3533
3534static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003535destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003536{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003537 struct shell_surface *child, *next;
3538
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003539 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3540
Giulio Camuffo5085a752013-03-25 21:42:45 +01003541 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3542 remove_popup_grab(shsurf);
3543 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003544
Alex Wubd3354b2012-04-17 17:20:49 +08003545 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003546 shell_surface_is_top_fullscreen(shsurf))
3547 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003548
Jason Ekstranda7af7042013-10-12 22:38:11 -05003549 if (shsurf->fullscreen.black_view)
3550 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003551
Alex Wubd3354b2012-04-17 17:20:49 +08003552 /* As destroy_resource() use wl_list_for_each_safe(),
3553 * we can always remove the listener.
3554 */
3555 wl_list_remove(&shsurf->surface_destroy_listener.link);
3556 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003557 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003558 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003559
Jason Ekstranda7af7042013-10-12 22:38:11 -05003560 weston_view_destroy(shsurf->view);
3561
Philip Withnall648a4dd2013-11-25 18:01:44 +00003562 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003563 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3564 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003565
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003566 wl_list_remove(&shsurf->link);
3567 free(shsurf);
3568}
3569
3570static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003571shell_destroy_shell_surface(struct wl_resource *resource)
3572{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003573 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003574
Bryan Caina46b9462014-04-04 17:41:24 -05003575 if (!wl_list_empty(&shsurf->popup.grab_link))
3576 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003577 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003578 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003579}
3580
3581static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003582shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003583{
3584 struct shell_surface *shsurf = container_of(listener,
3585 struct shell_surface,
3586 surface_destroy_listener);
3587
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003588 if (shsurf->resource)
3589 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003590
3591 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003592}
3593
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003594static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003595fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003596{
3597 struct shell_surface *shsurf = data;
3598
3599 weston_surface_destroy(shsurf->surface);
3600}
3601
3602static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003603fade_out_done(struct weston_view_animation *animation, void *data)
3604{
3605 struct shell_surface *shsurf = data;
3606 struct wl_event_loop *loop;
3607
3608 loop = wl_display_get_event_loop(
3609 shsurf->surface->compositor->wl_display);
3610
3611 if (!shsurf->destroying) {
3612 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3613 shsurf->destroying = true;
3614 }
3615}
3616
3617static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003618handle_resource_destroy(struct wl_listener *listener, void *data)
3619{
3620 struct shell_surface *shsurf =
3621 container_of(listener, struct shell_surface,
3622 resource_destroy_listener);
3623
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003624 if (!weston_surface_is_mapped(shsurf->surface))
3625 return;
3626
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003627 shsurf->surface->ref_count++;
3628
3629 pixman_region32_fini(&shsurf->surface->pending.input);
3630 pixman_region32_init(&shsurf->surface->pending.input);
3631 pixman_region32_fini(&shsurf->surface->input);
3632 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003633 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3634 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3635 fade_out_done, shsurf);
3636 } else {
3637 weston_surface_destroy(shsurf->surface);
3638 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003639}
3640
3641static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003642shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003643
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003644struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003645get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003646{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003647 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003648 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003649 else
3650 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003651}
3652
Rafael Antognollie2a34552013-12-03 15:35:45 -02003653static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003654create_common_surface(struct shell_client *owner, void *shell,
3655 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003656 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003657{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003658 struct shell_surface *shsurf;
3659
Pekka Paalanen50b67472014-10-01 15:02:41 +03003660 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003661
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003662 shsurf = calloc(1, sizeof *shsurf);
3663 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003664 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003665 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003666 }
3667
Jason Ekstranda7af7042013-10-12 22:38:11 -05003668 shsurf->view = weston_view_create(surface);
3669 if (!shsurf->view) {
3670 weston_log("no memory to allocate shell surface\n");
3671 free(shsurf);
3672 return NULL;
3673 }
3674
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003675 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003676 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003677 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003678
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003679 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3680 wl_resource_add_destroy_listener(surface->resource,
3681 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003682 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003683
Tiago Vignattibc052c92012-04-19 16:18:18 +03003684 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003685 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003686 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003687 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003688 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003689 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003690 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3691 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003692 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003693 wl_list_init(&shsurf->fullscreen.transform.link);
3694
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003695 shsurf->output = get_default_output(shsurf->shell->compositor);
3696
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003697 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003698 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003699 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003700 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003701
3702 /* init link so its safe to always remove it in destroy_shell_surface */
3703 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003704 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003705
Pekka Paalanen460099f2012-01-20 16:48:25 +02003706 /* empty when not in use */
3707 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003708 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003709
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003710 wl_list_init(&shsurf->workspace_transform.link);
3711
Philip Withnall648a4dd2013-11-25 18:01:44 +00003712 wl_list_init(&shsurf->children_link);
3713 wl_list_init(&shsurf->children_list);
3714 shsurf->parent = NULL;
3715
Pekka Paalanenb5026542014-11-12 15:09:24 +02003716 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003717
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003718 shsurf->client = client;
3719
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003720 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003721}
3722
Rafael Antognollie2a34552013-12-03 15:35:45 -02003723static struct shell_surface *
3724create_shell_surface(void *shell, struct weston_surface *surface,
3725 const struct weston_shell_client *client)
3726{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003727 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003728}
3729
Jason Ekstranda7af7042013-10-12 22:38:11 -05003730static struct weston_view *
3731get_primary_view(void *shell, struct shell_surface *shsurf)
3732{
3733 return shsurf->view;
3734}
3735
Tiago Vignattibc052c92012-04-19 16:18:18 +03003736static void
3737shell_get_shell_surface(struct wl_client *client,
3738 struct wl_resource *resource,
3739 uint32_t id,
3740 struct wl_resource *surface_resource)
3741{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003742 struct weston_surface *surface =
3743 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003744 struct shell_client *sc = wl_resource_get_user_data(resource);
3745 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003746 struct shell_surface *shsurf;
3747
Pekka Paalanen50b67472014-10-01 15:02:41 +03003748 if (weston_surface_set_role(surface, "wl_shell_surface",
3749 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003750 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003751
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003752 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003753 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003754 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003755 return;
3756 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003757
Jason Ekstranda85118c2013-06-27 20:17:02 -05003758 shsurf->resource =
3759 wl_resource_create(client,
3760 &wl_shell_surface_interface, 1, id);
3761 wl_resource_set_implementation(shsurf->resource,
3762 &shell_surface_implementation,
3763 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003764 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003765}
3766
Rafael Antognollie2a34552013-12-03 15:35:45 -02003767static bool
3768shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3769{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003770 /* A shell surface without a resource is created from xwayland
3771 * and is considered a wl_shell surface for now. */
3772
3773 return shsurf->resource == NULL ||
3774 wl_resource_instance_of(shsurf->resource,
3775 &wl_shell_surface_interface,
3776 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003777}
3778
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003779static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003780 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003781};
3782
Rafael Antognollie2a34552013-12-03 15:35:45 -02003783/****************************
3784 * xdg-shell implementation */
3785
3786static void
3787xdg_surface_destroy(struct wl_client *client,
3788 struct wl_resource *resource)
3789{
3790 wl_resource_destroy(resource);
3791}
3792
3793static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003794xdg_surface_set_parent(struct wl_client *client,
3795 struct wl_resource *resource,
3796 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003797{
3798 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003799 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003800
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003801 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003802 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003803 shell_surface_set_parent(shsurf, parent->surface);
3804 } else {
3805 shell_surface_set_parent(shsurf, NULL);
3806 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003807}
3808
3809static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003810xdg_surface_set_app_id(struct wl_client *client,
3811 struct wl_resource *resource,
3812 const char *app_id)
3813{
3814 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3815
3816 free(shsurf->class);
3817 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003818 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003819}
3820
3821static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003822xdg_surface_show_window_menu(struct wl_client *client,
3823 struct wl_resource *surface_resource,
3824 struct wl_resource *seat_resource,
3825 uint32_t serial,
3826 int32_t x,
3827 int32_t y)
3828{
3829 /* TODO */
3830}
3831
3832static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003833xdg_surface_set_title(struct wl_client *client,
3834 struct wl_resource *resource, const char *title)
3835{
3836 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3837
3838 set_title(shsurf, title);
3839}
3840
3841static void
3842xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3843 struct wl_resource *seat_resource, uint32_t serial)
3844{
3845 common_surface_move(resource, seat_resource, serial);
3846}
3847
3848static void
3849xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3850 struct wl_resource *seat_resource, uint32_t serial,
3851 uint32_t edges)
3852{
3853 common_surface_resize(resource, seat_resource, serial, edges);
3854}
3855
3856static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003857xdg_surface_ack_configure(struct wl_client *client,
3858 struct wl_resource *resource,
3859 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003860{
3861 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3862
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003863 if (shsurf->state_requested) {
3864 shsurf->next_state = shsurf->requested_state;
3865 shsurf->state_changed = true;
3866 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003867 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003868}
3869
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003870static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003871xdg_surface_set_window_geometry(struct wl_client *client,
3872 struct wl_resource *resource,
3873 int32_t x,
3874 int32_t y,
3875 int32_t width,
3876 int32_t height)
3877{
3878 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3879
3880 set_window_geometry(shsurf, x, y, width, height);
3881}
3882
3883static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003884xdg_surface_set_maximized(struct wl_client *client,
3885 struct wl_resource *resource)
3886{
3887 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003888 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003889
3890 shsurf->state_requested = true;
3891 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003892
3893 if (!weston_surface_is_mapped(shsurf->surface))
3894 output = get_focused_output(shsurf->surface->compositor);
3895 else
3896 output = shsurf->surface->output;
3897
3898 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003899 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003900}
3901
3902static void
3903xdg_surface_unset_maximized(struct wl_client *client,
3904 struct wl_resource *resource)
3905{
3906 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3907
3908 shsurf->state_requested = true;
3909 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003910 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003911}
3912
3913static void
3914xdg_surface_set_fullscreen(struct wl_client *client,
3915 struct wl_resource *resource,
3916 struct wl_resource *output_resource)
3917{
3918 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3919 struct weston_output *output;
3920
3921 shsurf->state_requested = true;
3922 shsurf->requested_state.fullscreen = true;
3923
3924 if (output_resource)
3925 output = wl_resource_get_user_data(output_resource);
3926 else
3927 output = NULL;
3928
Marek Chalupa052917a2014-09-02 11:35:12 +02003929 /* handle clients launching in fullscreen */
3930 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3931 /* Set the output to the one that has focus currently. */
3932 assert(shsurf->surface);
3933 output = get_focused_output(shsurf->surface->compositor);
3934 }
3935
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003936 shell_surface_set_output(shsurf, output);
3937 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003938
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003939 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003940}
3941
3942static void
3943xdg_surface_unset_fullscreen(struct wl_client *client,
3944 struct wl_resource *resource)
3945{
3946 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3947
3948 shsurf->state_requested = true;
3949 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003950 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003951}
3952
3953static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003954xdg_surface_set_minimized(struct wl_client *client,
3955 struct wl_resource *resource)
3956{
3957 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3958
3959 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3960 return;
3961
3962 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003963 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003964}
3965
Rafael Antognollie2a34552013-12-03 15:35:45 -02003966static const struct xdg_surface_interface xdg_surface_implementation = {
3967 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003968 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003969 xdg_surface_set_title,
3970 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003971 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003972 xdg_surface_move,
3973 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003974 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003975 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003976 xdg_surface_set_maximized,
3977 xdg_surface_unset_maximized,
3978 xdg_surface_set_fullscreen,
3979 xdg_surface_unset_fullscreen,
3980 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003981};
3982
3983static void
3984xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003985 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003986{
3987 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003988 uint32_t *s;
3989 struct wl_array states;
3990 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003991
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003992 assert(shsurf);
3993
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003994 if (!shsurf->resource)
3995 return;
3996
3997 wl_array_init(&states);
3998 if (shsurf->requested_state.fullscreen) {
3999 s = wl_array_add(&states, sizeof *s);
4000 *s = XDG_SURFACE_STATE_FULLSCREEN;
4001 } else if (shsurf->requested_state.maximized) {
4002 s = wl_array_add(&states, sizeof *s);
4003 *s = XDG_SURFACE_STATE_MAXIMIZED;
4004 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004005 if (shsurf->resize_edges != 0) {
4006 s = wl_array_add(&states, sizeof *s);
4007 *s = XDG_SURFACE_STATE_RESIZING;
4008 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004009 if (shsurf->focus_count > 0) {
4010 s = wl_array_add(&states, sizeof *s);
4011 *s = XDG_SURFACE_STATE_ACTIVATED;
4012 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004013
4014 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4015 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4016
4017 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004018}
4019
4020static const struct weston_shell_client xdg_client = {
4021 xdg_send_configure
4022};
4023
4024static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004025xdg_shell_destroy(struct wl_client *client,
4026 struct wl_resource *resource)
4027{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004028 struct shell_client *sc = wl_resource_get_user_data(resource);
4029 struct wl_resource *shsurf_resource;
4030 struct shell_surface *shsurf;
4031
4032 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4033 shsurf = wl_resource_get_user_data(shsurf_resource);
4034 if (shsurf->owner_resource == resource) {
4035 wl_resource_post_error(
4036 resource,
4037 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4038 "not all child surface objects destroyed");
4039 return;
4040 }
4041 }
4042
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004043 wl_resource_destroy(resource);
4044}
4045
4046static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004047xdg_use_unstable_version(struct wl_client *client,
4048 struct wl_resource *resource,
4049 int32_t version)
4050{
4051 if (version > 1) {
4052 wl_resource_post_error(resource,
4053 1,
4054 "xdg-shell:: version not implemented yet.");
4055 return;
4056 }
4057}
4058
4059static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004060create_xdg_surface(struct shell_client *owner, void *shell,
4061 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004062 const struct weston_shell_client *client)
4063{
4064 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004065
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004066 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004067 if (!shsurf)
4068 return NULL;
4069
Pekka Paalanenb5026542014-11-12 15:09:24 +02004070 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004071
4072 return shsurf;
4073}
4074
4075static void
4076xdg_get_xdg_surface(struct wl_client *client,
4077 struct wl_resource *resource,
4078 uint32_t id,
4079 struct wl_resource *surface_resource)
4080{
4081 struct weston_surface *surface =
4082 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004083 struct shell_client *sc = wl_resource_get_user_data(resource);
4084 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004085 struct shell_surface *shsurf;
4086
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004087 shsurf = get_shell_surface(surface);
4088 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4089 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4090 "This wl_surface is already an "
4091 "xdg_surface");
4092 return;
4093 }
4094
Pekka Paalanen50b67472014-10-01 15:02:41 +03004095 if (weston_surface_set_role(surface, "xdg_surface",
4096 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004097 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004098
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004099 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004100 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004101 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102 return;
4103 }
4104
4105 shsurf->resource =
4106 wl_resource_create(client,
4107 &xdg_surface_interface, 1, id);
4108 wl_resource_set_implementation(shsurf->resource,
4109 &xdg_surface_implementation,
4110 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004111 shsurf->owner_resource = resource;
4112 wl_list_insert(&sc->surface_list,
4113 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004114}
4115
4116static bool
4117shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4118{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004119 return shsurf->resource &&
4120 wl_resource_instance_of(shsurf->resource,
4121 &xdg_surface_interface,
4122 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004123}
4124
4125/* xdg-popup implementation */
4126
4127static void
4128xdg_popup_destroy(struct wl_client *client,
4129 struct wl_resource *resource)
4130{
4131 wl_resource_destroy(resource);
4132}
4133
Rafael Antognollie2a34552013-12-03 15:35:45 -02004134static const struct xdg_popup_interface xdg_popup_implementation = {
4135 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004136};
4137
4138static void
4139xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004140 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004141{
4142}
4143
4144static const struct weston_shell_client xdg_popup_client = {
4145 xdg_popup_send_configure
4146};
4147
4148static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004149create_xdg_popup(struct shell_client *owner, void *shell,
4150 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004151 const struct weston_shell_client *client,
4152 struct weston_surface *parent,
4153 struct shell_seat *seat,
4154 uint32_t serial,
4155 int32_t x, int32_t y)
4156{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004157 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004158
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004159 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004160 if (!shsurf)
4161 return NULL;
4162
Pekka Paalanenb5026542014-11-12 15:09:24 +02004163 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004164 shsurf->popup.shseat = seat;
4165 shsurf->popup.serial = serial;
4166 shsurf->popup.x = x;
4167 shsurf->popup.y = y;
4168 shell_surface_set_parent(shsurf, parent);
4169
4170 return shsurf;
4171}
4172
4173static void
4174xdg_get_xdg_popup(struct wl_client *client,
4175 struct wl_resource *resource,
4176 uint32_t id,
4177 struct wl_resource *surface_resource,
4178 struct wl_resource *parent_resource,
4179 struct wl_resource *seat_resource,
4180 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004181 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004182{
4183 struct weston_surface *surface =
4184 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004185 struct shell_client *sc = wl_resource_get_user_data(resource);
4186 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004187 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004188 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004189 struct weston_surface *parent;
4190 struct shell_seat *seat;
4191
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004192 shsurf = get_shell_surface(surface);
4193 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4194 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4195 "This wl_surface is already an "
4196 "xdg_popup");
4197 return;
4198 }
4199
Pekka Paalanen50b67472014-10-01 15:02:41 +03004200 if (weston_surface_set_role(surface, "xdg_popup",
4201 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004202 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004203
4204 if (!parent_resource) {
4205 wl_resource_post_error(surface_resource,
4206 WL_DISPLAY_ERROR_INVALID_OBJECT,
4207 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004208 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004209 }
4210
4211 parent = wl_resource_get_user_data(parent_resource);
4212 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4213
Jonas Ådahlee45a552015-03-18 16:37:47 +08004214 /* Verify that we are creating the top most popup when mapping,
4215 * as it's not until then we know whether it was mapped as most
4216 * top level or not. */
4217
4218 parent_shsurf = get_shell_surface(parent);
4219 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4220 !shell_surface_is_xdg_surface(parent_shsurf)) {
4221 wl_resource_post_error(resource,
4222 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4223 "xdg_popup parent was invalid");
4224 return;
4225 }
4226
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004227 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004228 parent, seat, serial, x, y);
4229 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004230 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004231 return;
4232 }
4233
4234 shsurf->resource =
4235 wl_resource_create(client,
4236 &xdg_popup_interface, 1, id);
4237 wl_resource_set_implementation(shsurf->resource,
4238 &xdg_popup_implementation,
4239 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004240 shsurf->owner_resource = resource;
4241 wl_list_insert(&sc->surface_list,
4242 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004243}
4244
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004245static void
4246xdg_pong(struct wl_client *client,
4247 struct wl_resource *resource, uint32_t serial)
4248{
4249 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004250
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004251 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004252}
4253
Rafael Antognollie2a34552013-12-03 15:35:45 -02004254static bool
4255shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4256{
4257 return wl_resource_instance_of(shsurf->resource,
4258 &xdg_popup_interface,
4259 &xdg_popup_implementation);
4260}
4261
4262static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004263 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004264 xdg_use_unstable_version,
4265 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004266 xdg_get_xdg_popup,
4267 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004268};
4269
4270static int
4271xdg_shell_unversioned_dispatch(const void *implementation,
4272 void *_target, uint32_t opcode,
4273 const struct wl_message *message,
4274 union wl_argument *args)
4275{
4276 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004277 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004278
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004279 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004280 wl_resource_post_error(resource,
4281 WL_DISPLAY_ERROR_INVALID_OBJECT,
4282 "must call use_unstable_version first");
4283 return 0;
4284 }
4285
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004286#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004287
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004288 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4289 "shell implementation doesn't match protocol version");
4290
Rafael Antognollie2a34552013-12-03 15:35:45 -02004291 if (args[0].i != XDG_SERVER_VERSION) {
4292 wl_resource_post_error(resource,
4293 WL_DISPLAY_ERROR_INVALID_OBJECT,
4294 "incompatible version, server is %d "
4295 "client wants %d",
4296 XDG_SERVER_VERSION, args[0].i);
4297 return 0;
4298 }
4299
4300 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004301 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004302
4303 return 1;
4304}
4305
4306/* end of xdg-shell implementation */
4307/***********************************/
4308
Kristian Høgsberg07937562011-04-12 17:25:42 -04004309static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004310shell_fade(struct desktop_shell *shell, enum fade_type type);
4311
Pekka Paalanen77346a62011-11-30 16:26:35 +02004312static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004313configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004314{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004315 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004316
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004317 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004318 if (v->output == ev->output && v != ev) {
4319 weston_view_unmap(v);
4320 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004321 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004322 }
4323 }
4324
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004325 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004326
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004327 if (wl_list_empty(&ev->layer_link.link)) {
4328 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004330 }
4331}
4332
Pekka Paalanen8274d902014-08-06 19:36:51 +03004333static int
4334background_get_label(struct weston_surface *surface, char *buf, size_t len)
4335{
4336 return snprintf(buf, len, "background for output %s",
4337 surface->output->name);
4338}
4339
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004340static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004341background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004342{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004343 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004344 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004345
Jason Ekstranda7af7042013-10-12 22:38:11 -05004346 view = container_of(es->views.next, struct weston_view, surface_link);
4347
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004348 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004349}
4350
4351static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004352desktop_shell_set_background(struct wl_client *client,
4353 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004354 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004355 struct wl_resource *surface_resource)
4356{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004357 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004358 struct weston_surface *surface =
4359 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004361
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004362 if (surface->configure) {
4363 wl_resource_post_error(surface_resource,
4364 WL_DISPLAY_ERROR_INVALID_OBJECT,
4365 "surface role already assigned");
4366 return;
4367 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004368
Jason Ekstranda7af7042013-10-12 22:38:11 -05004369 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4370 weston_view_destroy(view);
4371 view = weston_view_create(surface);
4372
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004373 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004374 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004375 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004376 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004377 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004378 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004379 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004380 surface->output->width,
4381 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004382}
4383
Pekka Paalanen8274d902014-08-06 19:36:51 +03004384static int
4385panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4386{
4387 return snprintf(buf, len, "panel for output %s",
4388 surface->output->name);
4389}
4390
Kristian Høgsberg75840622011-09-06 13:48:16 -04004391static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004392panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004393{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004394 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004395 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004396
Jason Ekstranda7af7042013-10-12 22:38:11 -05004397 view = container_of(es->views.next, struct weston_view, surface_link);
4398
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004399 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004400}
4401
4402static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004403desktop_shell_set_panel(struct wl_client *client,
4404 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004405 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004406 struct wl_resource *surface_resource)
4407{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004408 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004409 struct weston_surface *surface =
4410 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004412
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004413 if (surface->configure) {
4414 wl_resource_post_error(surface_resource,
4415 WL_DISPLAY_ERROR_INVALID_OBJECT,
4416 "surface role already assigned");
4417 return;
4418 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004419
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4421 weston_view_destroy(view);
4422 view = weston_view_create(surface);
4423
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004424 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004425 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004426 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004427 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004428 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004429 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004430 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004431 surface->output->width,
4432 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004433}
4434
Pekka Paalanen8274d902014-08-06 19:36:51 +03004435static int
4436lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4437{
4438 return snprintf(buf, len, "lock window");
4439}
4440
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004441static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004442lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004443{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004444 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004445 struct weston_view *view;
4446
4447 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004448
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004449 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004450 return;
4451
Jason Ekstranda7af7042013-10-12 22:38:11 -05004452 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004453
4454 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004455 weston_layer_entry_insert(&shell->lock_layer.view_list,
4456 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004458 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004459 }
4460}
4461
4462static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004463handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004464{
Tiago Vignattibe143262012-04-16 17:31:41 +03004465 struct desktop_shell *shell =
4466 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004467
Martin Minarik6d118362012-06-07 18:01:59 +02004468 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004469 shell->lock_surface = NULL;
4470}
4471
4472static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004473desktop_shell_set_lock_surface(struct wl_client *client,
4474 struct wl_resource *resource,
4475 struct wl_resource *surface_resource)
4476{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004477 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004478 struct weston_surface *surface =
4479 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004480
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004481 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004482
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004483 if (!shell->locked)
4484 return;
4485
Pekka Paalanen98262232011-12-01 10:42:22 +02004486 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004487
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004488 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004489 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004490 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004491
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004492 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004493 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004494 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004495 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004496}
4497
4498static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004499resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004500{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004501 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004502
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004503 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004504 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004505 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004506 &shell->input_panel_layer.link);
4507 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004508 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004509 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004510 wl_list_insert(&shell->compositor->cursor_layer.link,
4511 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004512 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004513 wl_list_insert(&shell->fullscreen_layer.link,
4514 &shell->panel_layer.link);
4515 wl_list_insert(&shell->panel_layer.link,
4516 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004517
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004518 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004519
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004520 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004521 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004522 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004523}
4524
4525static void
4526desktop_shell_unlock(struct wl_client *client,
4527 struct wl_resource *resource)
4528{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004529 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004530
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004531 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004532
4533 if (shell->locked)
4534 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004535}
4536
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004537static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004538desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004539 struct wl_resource *resource,
4540 struct wl_resource *surface_resource)
4541{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004542 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004543
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004544 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004545 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004546}
4547
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004548static void
4549desktop_shell_desktop_ready(struct wl_client *client,
4550 struct wl_resource *resource)
4551{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004552 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004553
4554 shell_fade_startup(shell);
4555}
4556
Jonny Lamb765760d2014-08-20 15:53:19 +02004557static void
4558desktop_shell_set_panel_position(struct wl_client *client,
4559 struct wl_resource *resource,
4560 uint32_t position)
4561{
4562 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4563
4564 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4565 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4566 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4567 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4568 wl_resource_post_error(resource,
4569 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4570 "bad position argument");
4571 return;
4572 }
4573
4574 shell->panel_position = position;
4575}
4576
Kristian Høgsberg75840622011-09-06 13:48:16 -04004577static const struct desktop_shell_interface desktop_shell_implementation = {
4578 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004579 desktop_shell_set_panel,
4580 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004581 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004582 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004583 desktop_shell_desktop_ready,
4584 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004585};
4586
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004587static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004588get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004589{
4590 struct shell_surface *shsurf;
4591
4592 shsurf = get_shell_surface(surface);
4593 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004594 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004595 return shsurf->type;
4596}
4597
Kristian Høgsberg75840622011-09-06 13:48:16 -04004598static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004599move_binding(struct weston_pointer *pointer, uint32_t time,
4600 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004601{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004602 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004603 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004604 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004605
Derek Foreman8ae2db52015-07-15 13:00:36 -05004606 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004607 return;
4608
Derek Foreman8ae2db52015-07-15 13:00:36 -05004609 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004610
Pekka Paalanen01388e22013-04-25 13:57:44 +03004611 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004612 if (surface == NULL)
4613 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004614
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004615 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004616 if (shsurf == NULL || shsurf->state.fullscreen ||
4617 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004618 return;
4619
Derek Foreman794fa0e2015-07-15 13:00:38 -05004620 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004621}
4622
4623static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004624maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4625 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004626{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004627 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004628 struct weston_surface *surface;
4629 struct shell_surface *shsurf;
4630
4631 surface = weston_surface_get_main_surface(focus);
4632 if (surface == NULL)
4633 return;
4634
4635 shsurf = get_shell_surface(surface);
4636 if (shsurf == NULL)
4637 return;
4638
4639 if (!shell_surface_is_xdg_surface(shsurf))
4640 return;
4641
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004642 shsurf->state_requested = true;
4643 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004644 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004645}
4646
4647static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004648fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4649 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004650{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004651 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004652 struct weston_surface *surface;
4653 struct shell_surface *shsurf;
4654
4655 surface = weston_surface_get_main_surface(focus);
4656 if (surface == NULL)
4657 return;
4658
4659 shsurf = get_shell_surface(surface);
4660 if (shsurf == NULL)
4661 return;
4662
4663 if (!shell_surface_is_xdg_surface(shsurf))
4664 return;
4665
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004666 shsurf->state_requested = true;
4667 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004668 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004669 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004670}
4671
4672static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004673touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004674{
Derek Foreman362656b2014-09-04 10:23:05 -05004675 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004676 struct weston_surface *surface;
4677 struct shell_surface *shsurf;
4678
Derek Foreman8ae2db52015-07-15 13:00:36 -05004679 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004680 return;
4681
Derek Foreman8ae2db52015-07-15 13:00:36 -05004682 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004683 surface = weston_surface_get_main_surface(focus);
4684 if (surface == NULL)
4685 return;
4686
4687 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004688 if (shsurf == NULL || shsurf->state.fullscreen ||
4689 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004690 return;
4691
Derek Foremanb7674ae2015-07-15 13:00:37 -05004692 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004693}
4694
4695static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004696resize_binding(struct weston_pointer *pointer, uint32_t time,
4697 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004698{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004699 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004700 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004701 uint32_t edges = 0;
4702 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004703 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004704
Derek Foreman8ae2db52015-07-15 13:00:36 -05004705 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004706 return;
4707
Derek Foreman8ae2db52015-07-15 13:00:36 -05004708 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004709
Pekka Paalanen01388e22013-04-25 13:57:44 +03004710 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004711 if (surface == NULL)
4712 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004713
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004714 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004715 if (shsurf == NULL || shsurf->state.fullscreen ||
4716 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004717 return;
4718
Jason Ekstranda7af7042013-10-12 22:38:11 -05004719 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004720 wl_fixed_to_int(pointer->grab_x),
4721 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004722 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004723
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004724 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004725 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004726 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004727 edges |= 0;
4728 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004729 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004730
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004731 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004732 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004733 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004734 edges |= 0;
4735 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004736 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004737
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004738 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004739}
4740
4741static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004742surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4743 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004744{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004745 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004746 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004747 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004748 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004749
Pekka Paalanen01388e22013-04-25 13:57:44 +03004750 /* XXX: broken for windows containing sub-surfaces */
4751 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004752 if (surface == NULL)
4753 return;
4754
4755 shsurf = get_shell_surface(surface);
4756 if (!shsurf)
4757 return;
4758
Jason Ekstranda7af7042013-10-12 22:38:11 -05004759 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004760
Jason Ekstranda7af7042013-10-12 22:38:11 -05004761 if (shsurf->view->alpha > 1.0)
4762 shsurf->view->alpha = 1.0;
4763 if (shsurf->view->alpha < step)
4764 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004765
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004767 weston_surface_damage(surface);
4768}
4769
4770static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004771do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004772 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004773{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004774 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004775 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004776 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004777
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004778 if (!seat->pointer) {
4779 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4780 return;
4781 }
4782
Scott Moreauccbf29d2012-02-22 14:21:41 -07004783 wl_list_for_each(output, &compositor->output_list, link) {
4784 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004785 wl_fixed_to_double(seat->pointer->x),
4786 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004787 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004788 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004789 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004790 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004791 increment = -output->zoom.increment;
4792 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004793 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004794 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004795 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004796 else
4797 increment = 0;
4798
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004799 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004800
Scott Moreaue6603982012-06-11 13:07:51 -06004801 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004802 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004803 else if (output->zoom.level > output->zoom.max_level)
4804 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004805 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004806 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004807 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004808
Scott Moreaue6603982012-06-11 13:07:51 -06004809 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004810
Jason Ekstranda7af7042013-10-12 22:38:11 -05004811 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004812 }
4813 }
4814}
4815
Scott Moreauccbf29d2012-02-22 14:21:41 -07004816static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004817zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4818 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004819{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004820 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004821}
4822
4823static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004824zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4825 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004827 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004828}
4829
4830static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004831terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4832 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004833{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004834 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004835
Daniel Stone325fc2d2012-05-30 16:31:58 +01004836 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004837}
4838
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004839static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004840rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4841 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004842{
4843 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004844 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004845 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004846 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004847 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004848
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004849 weston_pointer_move(pointer, x, y);
4850
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004851 if (!shsurf)
4852 return;
4853
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004854 cx = 0.5f * shsurf->surface->width;
4855 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004856
Daniel Stone37816df2012-05-16 18:45:18 +01004857 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4858 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004859 r = sqrtf(dx * dx + dy * dy);
4860
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004861 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004862 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004863
4864 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004865 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004866 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004867
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004868 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004869 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004870
4871 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004872 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004873 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004874 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004875 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004876
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004877 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004878 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004879 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004880 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004881 wl_list_init(&shsurf->rotation.transform.link);
4882 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004883 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004884 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004885
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004886 /* We need to adjust the position of the surface
4887 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004888 cposx = shsurf->view->geometry.x + cx;
4889 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004890 dposx = rotate->center.x - cposx;
4891 dposy = rotate->center.y - cposy;
4892 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893 weston_view_set_position(shsurf->view,
4894 shsurf->view->geometry.x + dposx,
4895 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004896 }
4897
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004898 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004899 * lazily applies the damage due to rotation update.
4900 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004901 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004902}
4903
4904static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004905rotate_grab_button(struct weston_pointer_grab *grab,
4906 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004907{
4908 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004909 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004910 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004911 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004912 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004913
Daniel Stone4dbadb12012-05-30 16:31:51 +01004914 if (pointer->button_count == 0 &&
4915 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004916 if (shsurf)
4917 weston_matrix_multiply(&shsurf->rotation.rotation,
4918 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004919 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004920 free(rotate);
4921 }
4922}
4923
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004924static void
4925rotate_grab_cancel(struct weston_pointer_grab *grab)
4926{
4927 struct rotate_grab *rotate =
4928 container_of(grab, struct rotate_grab, base.grab);
4929
4930 shell_grab_end(&rotate->base);
4931 free(rotate);
4932}
4933
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004934static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004935 noop_grab_focus,
4936 rotate_grab_motion,
4937 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004938 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004939};
4940
4941static void
Derek Foreman74de4692015-07-15 13:00:39 -05004942surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004943{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004944 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004945 float dx, dy;
4946 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004947
Pekka Paalanen460099f2012-01-20 16:48:25 +02004948 rotate = malloc(sizeof *rotate);
4949 if (!rotate)
4950 return;
4951
Jason Ekstranda7af7042013-10-12 22:38:11 -05004952 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004953 surface->surface->width * 0.5f,
4954 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004955 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004956
Derek Foreman74de4692015-07-15 13:00:39 -05004957 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4958 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004959 r = sqrtf(dx * dx + dy * dy);
4960 if (r > 20.0f) {
4961 struct weston_matrix inverse;
4962
4963 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004964 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004965 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004966
4967 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004968 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004969 } else {
4970 weston_matrix_init(&surface->rotation.rotation);
4971 weston_matrix_init(&rotate->rotation);
4972 }
4973
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004974 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Derek Foreman74de4692015-07-15 13:00:39 -05004975 pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004976}
4977
4978static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004979rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004980 void *data)
4981{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004982 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004983 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004984 struct shell_surface *surface;
4985
Derek Foreman8ae2db52015-07-15 13:00:36 -05004986 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004987 return;
4988
Derek Foreman8ae2db52015-07-15 13:00:36 -05004989 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004990
Pekka Paalanen01388e22013-04-25 13:57:44 +03004991 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004992 if (base_surface == NULL)
4993 return;
4994
4995 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004996 if (surface == NULL || surface->state.fullscreen ||
4997 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004998 return;
4999
Derek Foreman74de4692015-07-15 13:00:39 -05005000 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005001}
5002
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005003/* Move all fullscreen layers down to the current workspace and hide their
5004 * black views. The surfaces' state is set to both fullscreen and lowered,
5005 * and this is reversed when such a surface is re-configured, see
5006 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5007 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005008 * lowering_output = NULL - Lower on all outputs, else only lower on the
5009 * specified output.
5010 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005011 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005012 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005013void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005014lower_fullscreen_layer(struct desktop_shell *shell,
5015 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005016{
5017 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005018 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005019
5020 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005021 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005022 &shell->fullscreen_layer.view_list.link,
5023 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005024 struct shell_surface *shsurf = get_shell_surface(view->surface);
5025
5026 if (!shsurf)
5027 continue;
5028
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005029 /* Only lower surfaces which have lowering_output as their fullscreen
5030 * output, unless a NULL output asks for lowering on all outputs.
5031 */
5032 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5033 continue;
5034
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005035 /* We can have a non-fullscreen popup for a fullscreen surface
5036 * in the fullscreen layer. */
5037 if (shsurf->state.fullscreen) {
5038 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005039 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5040 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005041 weston_view_damage_below(shsurf->fullscreen.black_view);
5042
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005043 }
5044
5045 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005046 weston_layer_entry_remove(&view->layer_link);
5047 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005048 weston_view_damage_below(view);
5049 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005050
5051 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005052 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005053}
5054
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005055void
Tiago Vignattibe143262012-04-16 17:31:41 +03005056activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005057 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005058{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005059 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005060 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005061 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005062 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005063 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005064
Pekka Paalanen01388e22013-04-25 13:57:44 +03005065 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005066 shsurf = get_shell_surface(main_surface);
5067 assert(shsurf);
5068
5069 /* Only demote fullscreen surfaces on the output of activated shsurf.
5070 * Leave fullscreen surfaces on unrelated outputs alone. */
5071 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005072
Daniel Stone37816df2012-05-16 18:45:18 +01005073 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005074
Jonas Ådahl8538b222012-08-29 22:13:03 +02005075 state = ensure_focus_state(shell, seat);
5076 if (state == NULL)
5077 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005078
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005079 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005080 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005081
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005082 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005083 shell_configure_fullscreen(shsurf);
5084 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005085 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005086
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005087 /* Update the surface’s layer. This brings it to the top of the stacking
5088 * order as appropriate. */
5089 shell_surface_update_layer(shsurf);
5090
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005091 if (shell->focus_animation_type != ANIMATION_NONE) {
5092 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005093 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005094 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005095}
5096
Alex Wu21858432012-04-01 20:13:08 +08005097/* no-op func for checking black surface */
5098static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005099black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005100{
5101}
5102
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005103static bool
Alex Wu21858432012-04-01 20:13:08 +08005104is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5105{
5106 if (es->configure == black_surface_configure) {
5107 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005108 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005109 return true;
5110 }
5111 return false;
5112}
5113
Kristian Høgsberg75840622011-09-06 13:48:16 -04005114static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005115activate_binding(struct weston_seat *seat,
5116 struct desktop_shell *shell,
5117 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005118{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005119 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005120
Alex Wu9c35e6b2012-03-05 14:13:13 +08005121 if (!focus)
5122 return;
5123
Pekka Paalanen01388e22013-04-25 13:57:44 +03005124 if (is_black_surface(focus, &main_surface))
5125 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005126
Pekka Paalanen01388e22013-04-25 13:57:44 +03005127 main_surface = weston_surface_get_main_surface(focus);
5128 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005129 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005130
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005131 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005132}
5133
5134static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005135click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5136 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005137{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005138 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005139 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005140 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005141 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005142
Derek Foreman8ae2db52015-07-15 13:00:36 -05005143 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005144}
5145
5146static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005147touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5148 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005149{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005150 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005151 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005152 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005153 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005154
Derek Foreman8ae2db52015-07-15 13:00:36 -05005155 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005156}
5157
5158static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005159unfocus_all_seats(struct desktop_shell *shell)
5160{
5161 struct weston_seat *seat, *next;
5162
5163 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5164 if (seat->keyboard == NULL)
5165 continue;
5166
5167 weston_keyboard_set_focus(seat->keyboard, NULL);
5168 }
5169}
5170
5171static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005172lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005173{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005174 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005175
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005176 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005177 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005178 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005179 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005180
5181 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005182
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005183 /* Hide all surfaces by removing the fullscreen, panel and
5184 * toplevel layers. This way nothing else can show or receive
5185 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005186
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005187 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005188 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005189 if (shell->showing_input_panels)
5190 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005191 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005192 wl_list_insert(&shell->compositor->cursor_layer.link,
5193 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005194
Neil Robertsb4a91702014-04-09 16:33:32 +01005195 /* Remove the keyboard focus on all seats. This will be
5196 * restored to the workspace's saved state via
5197 * restore_focus_state when the compositor is unlocked */
5198 unfocus_all_seats(shell);
5199
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005200 /* TODO: disable bindings that should not work while locked. */
5201
5202 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005203}
5204
5205static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005206unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005207{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005208 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005209 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005210 return;
5211 }
5212
5213 /* If desktop-shell client has gone away, unlock immediately. */
5214 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005215 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005216 return;
5217 }
5218
5219 if (shell->prepare_event_sent)
5220 return;
5221
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005222 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005223 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005224}
5225
5226static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005227shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005228{
5229 struct desktop_shell *shell = data;
5230
5231 shell->fade.animation = NULL;
5232
5233 switch (shell->fade.type) {
5234 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005235 weston_surface_destroy(shell->fade.view->surface);
5236 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005237 break;
5238 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005239 lock(shell);
5240 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005241 default:
5242 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005243 }
5244}
5245
Jason Ekstranda7af7042013-10-12 22:38:11 -05005246static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005247shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005248{
5249 struct weston_compositor *compositor = shell->compositor;
5250 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005251 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005252
5253 surface = weston_surface_create(compositor);
5254 if (!surface)
5255 return NULL;
5256
Jason Ekstranda7af7042013-10-12 22:38:11 -05005257 view = weston_view_create(surface);
5258 if (!view) {
5259 weston_surface_destroy(surface);
5260 return NULL;
5261 }
5262
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005263 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005264 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005265 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005266 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5267 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005268 pixman_region32_init(&surface->input);
5269
Jason Ekstranda7af7042013-10-12 22:38:11 -05005270 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005271}
5272
5273static void
5274shell_fade(struct desktop_shell *shell, enum fade_type type)
5275{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005276 float tint;
5277
5278 switch (type) {
5279 case FADE_IN:
5280 tint = 0.0;
5281 break;
5282 case FADE_OUT:
5283 tint = 1.0;
5284 break;
5285 default:
5286 weston_log("shell: invalid fade type\n");
5287 return;
5288 }
5289
5290 shell->fade.type = type;
5291
Jason Ekstranda7af7042013-10-12 22:38:11 -05005292 if (shell->fade.view == NULL) {
5293 shell->fade.view = shell_fade_create_surface(shell);
5294 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005295 return;
5296
Jason Ekstranda7af7042013-10-12 22:38:11 -05005297 shell->fade.view->alpha = 1.0 - tint;
5298 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005299 }
5300
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005301 if (shell->fade.view->output == NULL) {
5302 /* If the black view gets a NULL output, we lost the
5303 * last output and we'll just cancel the fade. This
5304 * happens when you close the last window under the
5305 * X11 or Wayland backends. */
5306 shell->locked = false;
5307 weston_surface_destroy(shell->fade.view->surface);
5308 shell->fade.view = NULL;
5309 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005310 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005311 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005312 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005313 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005314 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005315 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005316 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005317}
5318
5319static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005320do_shell_fade_startup(void *data)
5321{
5322 struct desktop_shell *shell = data;
5323
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005324 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005325 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005326 } else {
5327 weston_log("desktop shell: "
5328 "unexpected fade-in animation type %d\n",
5329 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005330 weston_surface_destroy(shell->fade.view->surface);
5331 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005332 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005333}
5334
5335static void
5336shell_fade_startup(struct desktop_shell *shell)
5337{
5338 struct wl_event_loop *loop;
5339
5340 if (!shell->fade.startup_timer)
5341 return;
5342
5343 wl_event_source_remove(shell->fade.startup_timer);
5344 shell->fade.startup_timer = NULL;
5345
5346 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5347 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5348}
5349
5350static int
5351fade_startup_timeout(void *data)
5352{
5353 struct desktop_shell *shell = data;
5354
5355 shell_fade_startup(shell);
5356 return 0;
5357}
5358
5359static void
5360shell_fade_init(struct desktop_shell *shell)
5361{
5362 /* Make compositor output all black, and wait for the desktop-shell
5363 * client to signal it is ready, then fade in. The timer triggers a
5364 * fade-in, in case the desktop-shell client takes too long.
5365 */
5366
5367 struct wl_event_loop *loop;
5368
Jason Ekstranda7af7042013-10-12 22:38:11 -05005369 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005370 weston_log("%s: warning: fade surface already exists\n",
5371 __func__);
5372 return;
5373 }
5374
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005375 if (shell->startup_animation_type == ANIMATION_NONE)
5376 return;
5377
Jason Ekstranda7af7042013-10-12 22:38:11 -05005378 shell->fade.view = shell_fade_create_surface(shell);
5379 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005380 return;
5381
Jason Ekstranda7af7042013-10-12 22:38:11 -05005382 weston_view_update_transform(shell->fade.view);
5383 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005384
5385 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5386 shell->fade.startup_timer =
5387 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5388 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5389}
5390
5391static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005392idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005393{
5394 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005395 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005396 struct weston_seat *seat;
5397
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005398 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005399 if (seat->pointer)
5400 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005401 if (seat->touch)
5402 touch_popup_grab_end(seat->touch);
5403 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005404
5405 shell_fade(shell, FADE_OUT);
5406 /* lock() is called from shell_fade_done() */
5407}
5408
5409static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005410wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005411{
5412 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005413 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005414
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005415 unlock(shell);
5416}
5417
5418static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005419center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005420{
Giulio Camuffob8366642013-04-25 13:57:46 +03005421 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005422 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005423
Jason Ekstranda7af7042013-10-12 22:38:11 -05005424 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005425
5426 x = output->x + (output->width - width) / 2 - surf_x / 2;
5427 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005428
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005429 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005430}
5431
5432static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005433weston_view_set_initial_position(struct weston_view *view,
5434 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005435{
5436 struct weston_compositor *compositor = shell->compositor;
5437 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005438 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005439 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005440 struct weston_output *output, *target_output = NULL;
5441 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005442 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005443
5444 /* As a heuristic place the new window on the same output as the
5445 * pointer. Falling back to the output containing 0, 0.
5446 *
5447 * TODO: Do something clever for touch too?
5448 */
5449 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005450 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005451 ix = wl_fixed_to_int(seat->pointer->x);
5452 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005453 break;
5454 }
5455 }
5456
5457 wl_list_for_each(output, &compositor->output_list, link) {
5458 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5459 target_output = output;
5460 break;
5461 }
5462 }
5463
5464 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005465 weston_view_set_position(view, 10 + random() % 400,
5466 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005467 return;
5468 }
5469
5470 /* Valid range within output where the surface will still be onscreen.
5471 * If this is negative it means that the surface is bigger than
5472 * output.
5473 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005474 get_output_work_area(shell, target_output, &area);
5475
Derek Foremanf814c5d2015-04-15 12:23:54 -05005476 x = area.x;
5477 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005478 range_x = area.width - view->surface->width;
5479 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005480
Rob Bradford4cb88c72012-08-13 15:18:44 +01005481 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005482 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005483
5484 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005485 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005486
Jason Ekstranda7af7042013-10-12 22:38:11 -05005487 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005488}
5489
5490static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005491set_maximized_position(struct desktop_shell *shell,
5492 struct shell_surface *shsurf)
5493{
5494 int32_t surf_x, surf_y;
5495 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005496 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005497
Jonny Lambd73c6942014-08-20 15:53:20 +02005498 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005499 if (shsurf->has_set_geometry) {
5500 surf_x = shsurf->geometry.x;
5501 surf_y = shsurf->geometry.y;
5502 } else {
5503 surface_subsurfaces_boundingbox(shsurf->surface,
5504 &surf_x, &surf_y, NULL, NULL);
5505 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005506 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005507
5508 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005509 e->x1 + area.x - surf_x,
5510 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005511}
5512
5513static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005514map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005515 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005516{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005517 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005518 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005519
Pekka Paalanen77346a62011-11-30 16:26:35 +02005520 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005521 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005522 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005523 if (shsurf->state.fullscreen) {
5524 center_on_output(shsurf->view, shsurf->fullscreen_output);
5525 shell_map_fullscreen(shsurf);
5526 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005527 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005528 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005529 weston_view_set_initial_position(shsurf->view, shell);
5530 }
Juan Zhao96879df2012-02-07 08:45:41 +08005531 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005532 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005533 if (shell_map_popup(shsurf) != 0)
5534 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005535 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005536 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005537 weston_view_set_position(shsurf->view,
5538 shsurf->view->geometry.x + sx,
5539 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005540 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005541 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005542 default:
5543 ;
5544 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005545
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005546 /* Surface stacking order, see also activate(). */
5547 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005548
Jason Ekstranda7af7042013-10-12 22:38:11 -05005549 if (shsurf->type != SHELL_SURFACE_NONE) {
5550 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005551 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005552 shsurf->surface->output = shsurf->output;
5553 shsurf->view->output = shsurf->output;
5554 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005555 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005556
Jason Ekstranda7af7042013-10-12 22:38:11 -05005557 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005558 /* XXX: xwayland's using the same fields for transient type */
5559 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005560 if (shsurf->transient.flags ==
5561 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5562 break;
5563 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005564 if (shsurf->state.relative &&
5565 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5566 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005567 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005568 break;
5569 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005570 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005571 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005572 case SHELL_SURFACE_POPUP:
5573 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005574 default:
5575 break;
5576 }
5577
Rafael Antognolli03b16592013-12-03 15:35:42 -02005578 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5579 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005580 {
5581 switch (shell->win_animation_type) {
5582 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005583 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005584 break;
5585 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005586 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005587 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005588 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005589 default:
5590 break;
5591 }
5592 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005593}
5594
5595static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005596configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005597 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005598{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005599 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005600 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005601
Pekka Paalanen77346a62011-11-30 16:26:35 +02005602 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005603
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005604 assert(shsurf);
5605
Rafael Antognolli03b16592013-12-03 15:35:42 -02005606 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005607 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005608 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005609 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005610 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005611 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005612 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005613
Alex Wu4539b082012-03-01 12:57:46 +08005614 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005615 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005616 wl_list_for_each(view, &surface->views, surface_link)
5617 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005618
Rafael Antognolli03b16592013-12-03 15:35:42 -02005619 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005620 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005621 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005622}
5623
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005624static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005625shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005626{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005627 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005628 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005629 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005630
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005631 assert(shsurf);
5632
5633 shell = shsurf->shell;
5634
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005635 if (!weston_surface_is_mapped(es) &&
5636 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005637 remove_popup_grab(shsurf);
5638 }
5639
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005640 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005641 return;
5642
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005643 if (shsurf->has_next_geometry) {
5644 shsurf->geometry = shsurf->next_geometry;
5645 shsurf->has_next_geometry = false;
5646 shsurf->has_set_geometry = true;
5647 } else if (!shsurf->has_set_geometry) {
5648 surface_subsurfaces_boundingbox(shsurf->surface,
5649 &shsurf->geometry.x,
5650 &shsurf->geometry.y,
5651 &shsurf->geometry.width,
5652 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005653 }
5654
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005655 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005656 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005657 type_changed = 1;
5658 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005659
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005660 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005661 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005662 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005663 shsurf->last_width != es->width ||
5664 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005665 float from_x, from_y;
5666 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005667
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005668 if (shsurf->resize_edges) {
5669 sx = 0;
5670 sy = 0;
5671 }
5672
5673 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5674 sx = shsurf->last_width - es->width;
5675 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5676 sy = shsurf->last_height - es->height;
5677
5678 shsurf->last_width = es->width;
5679 shsurf->last_height = es->height;
5680
Jason Ekstranda7af7042013-10-12 22:38:11 -05005681 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5682 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005683 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005684 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005685 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005686 }
5687}
5688
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005689static bool
5690check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5691{
5692 struct timespec now;
5693
5694 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5695 return false;
5696
5697 /*
5698 * If the shell helper client dies before the session has been
5699 * up for roughly 30 seconds, better just make Weston shut down,
5700 * because the user likely has no way to interact with the desktop
5701 * anyway.
5702 */
5703 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5704 weston_log("Error: %s apparently cannot run at all.\n",
5705 shell->client);
5706 weston_log_continue(STAMP_SPACE "Quitting...");
5707 wl_display_terminate(shell->compositor->wl_display);
5708
5709 return true;
5710 }
5711
5712 return false;
5713}
5714
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005715static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005716
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005717static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005718respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005719{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005720 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005721
5722 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5723 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005724 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005725 shell->child.deathstamp = time;
5726 shell->child.deathcount = 0;
5727 }
5728
5729 shell->child.deathcount++;
5730 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005731 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005732 return;
5733 }
5734
Pekka Paalanen826dc142014-08-27 12:11:53 +03005735 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005736 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005737}
5738
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005739static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005740desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5741{
5742 struct desktop_shell *shell;
5743
5744 shell = container_of(listener, struct desktop_shell,
5745 child.client_destroy_listener);
5746
Pekka Paalanen826dc142014-08-27 12:11:53 +03005747 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005748 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005749 /*
5750 * unbind_desktop_shell() will reset shell->child.desktop_shell
5751 * before the respawned process has a chance to create a new
5752 * desktop_shell object, because we are being called from the
5753 * wl_client destructor which destroys all wl_resources before
5754 * returning.
5755 */
5756
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005757 if (!check_desktop_shell_crash_too_early(shell))
5758 respawn_desktop_shell_process(shell);
5759
Pekka Paalanen826dc142014-08-27 12:11:53 +03005760 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005761}
5762
5763static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005764launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005765{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005766 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005767
Pekka Paalanen826dc142014-08-27 12:11:53 +03005768 shell->child.client = weston_client_start(shell->compositor,
5769 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005770
Arnaud Vrac42631192014-08-25 20:56:46 +02005771 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005772 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005773 return;
5774 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005775
5776 shell->child.client_destroy_listener.notify =
5777 desktop_shell_client_destroy;
5778 wl_client_add_destroy_listener(shell->child.client,
5779 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005780}
5781
5782static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005783handle_shell_client_destroy(struct wl_listener *listener, void *data)
5784{
5785 struct shell_client *sc =
5786 container_of(listener, struct shell_client, destroy_listener);
5787
5788 if (sc->ping_timer)
5789 wl_event_source_remove(sc->ping_timer);
5790 free(sc);
5791}
5792
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005793static struct shell_client *
5794shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5795 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005796{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005797 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005798
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005799 sc = zalloc(sizeof *sc);
5800 if (sc == NULL) {
5801 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005802 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005803 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005804
5805 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005806 if (sc->resource == NULL) {
5807 free(sc);
5808 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005809 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005810 }
5811
5812 sc->client = client;
5813 sc->shell = shell;
5814 sc->destroy_listener.notify = handle_shell_client_destroy;
5815 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005816 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005817
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005818 return sc;
5819}
5820
5821static void
5822bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5823{
5824 struct desktop_shell *shell = data;
5825 struct shell_client *sc;
5826
5827 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5828 if (sc)
5829 wl_resource_set_implementation(sc->resource,
5830 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005831 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005832}
5833
5834static void
5835bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5836{
5837 struct desktop_shell *shell = data;
5838 struct shell_client *sc;
5839
5840 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5841 if (sc)
5842 wl_resource_set_dispatcher(sc->resource,
5843 xdg_shell_unversioned_dispatch,
5844 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005845}
5846
5847static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005848unbind_desktop_shell(struct wl_resource *resource)
5849{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005850 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005851
5852 if (shell->locked)
5853 resume_desktop(shell);
5854
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005855 shell->child.desktop_shell = NULL;
5856 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005857}
5858
5859static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005860bind_desktop_shell(struct wl_client *client,
5861 void *data, uint32_t version, uint32_t id)
5862{
Tiago Vignattibe143262012-04-16 17:31:41 +03005863 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005864 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005865
Jason Ekstranda85118c2013-06-27 20:17:02 -05005866 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005867 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005868
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005869 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005870 wl_resource_set_implementation(resource,
5871 &desktop_shell_implementation,
5872 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005873 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005874
5875 if (version < 2)
5876 shell_fade_startup(shell);
5877
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005878 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005879 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005880
5881 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5882 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005883}
5884
Kristian Høgsberg07045392012-02-19 18:52:44 -05005885struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005886 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005887 struct weston_surface *current;
5888 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005889 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005890 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005891};
5892
5893static void
5894switcher_next(struct switcher *switcher)
5895{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005896 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005897 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005898 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005899 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005900
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005901 /* temporary re-display minimized surfaces */
5902 struct weston_view *tmp;
5903 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005904 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5905 weston_layer_entry_remove(&view->layer_link);
5906 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005907 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5908 *minimized = view;
5909 }
5910
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005911 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005912 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005913 if (shsurf &&
5914 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5915 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005916 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005917 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005918 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005919 next = view->surface;
5920 prev = view->surface;
5921 view->alpha = 0.25;
5922 weston_view_geometry_dirty(view);
5923 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005924 }
Alex Wu1659daa2012-04-01 20:13:09 +08005925
Jason Ekstranda7af7042013-10-12 22:38:11 -05005926 if (is_black_surface(view->surface, NULL)) {
5927 view->alpha = 0.25;
5928 weston_view_geometry_dirty(view);
5929 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005930 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005931 }
5932
5933 if (next == NULL)
5934 next = first;
5935
Alex Wu07b26062012-03-12 16:06:01 +08005936 if (next == NULL)
5937 return;
5938
Kristian Høgsberg07045392012-02-19 18:52:44 -05005939 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005940 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005941
5942 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005943 wl_list_for_each(view, &next->views, surface_link)
5944 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005945
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005946 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005947 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005948 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005949}
5950
5951static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005952switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005953{
5954 struct switcher *switcher =
5955 container_of(listener, struct switcher, listener);
5956
5957 switcher_next(switcher);
5958}
5959
5960static void
Daniel Stone351eb612012-05-31 15:27:47 -04005961switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005962{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005963 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005964 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005965 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005966
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005967 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005968 if (is_focus_view(view))
5969 continue;
5970
Jason Ekstranda7af7042013-10-12 22:38:11 -05005971 view->alpha = 1.0;
5972 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005973 }
5974
Alex Wu07b26062012-03-12 16:06:01 +08005975 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005976 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005977 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005978 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005979 weston_keyboard_end_grab(keyboard);
5980 if (keyboard->input_method_resource)
5981 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005982
5983 /* re-hide surfaces that were temporary shown during the switch */
5984 struct weston_view **minimized;
5985 wl_array_for_each(minimized, &switcher->minimized_array) {
5986 /* with the exception of the current selected */
5987 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005988 weston_layer_entry_remove(&(*minimized)->layer_link);
5989 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005990 weston_view_damage_below(*minimized);
5991 }
5992 }
5993 wl_array_release(&switcher->minimized_array);
5994
Kristian Høgsberg07045392012-02-19 18:52:44 -05005995 free(switcher);
5996}
5997
5998static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005999switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006000 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006001{
6002 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006003 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006004
Daniel Stonec9785ea2012-05-30 16:31:52 +01006005 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006006 switcher_next(switcher);
6007}
6008
6009static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006010switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006011 uint32_t mods_depressed, uint32_t mods_latched,
6012 uint32_t mods_locked, uint32_t group)
6013{
6014 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006015 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006016
Daniel Stone351eb612012-05-31 15:27:47 -04006017 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6018 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006019}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006020
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006021static void
6022switcher_cancel(struct weston_keyboard_grab *grab)
6023{
6024 struct switcher *switcher = container_of(grab, struct switcher, grab);
6025
6026 switcher_destroy(switcher);
6027}
6028
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006029static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006030 switcher_key,
6031 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006032 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006033};
6034
6035static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006036switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6037 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006038{
Tiago Vignattibe143262012-04-16 17:31:41 +03006039 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006040 struct switcher *switcher;
6041
6042 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006043 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006044 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006045 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006046 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006047 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006048
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006049 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006050 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006051 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006052 weston_keyboard_start_grab(keyboard, &switcher->grab);
6053 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006054 switcher_next(switcher);
6055}
6056
Pekka Paalanen3c647232011-12-22 13:43:43 +02006057static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006058backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6059 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006060{
6061 struct weston_compositor *compositor = data;
6062 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006063 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006064
6065 /* TODO: we're limiting to simple use cases, where we assume just
6066 * control on the primary display. We'd have to extend later if we
6067 * ever get support for setting backlights on random desktop LCD
6068 * panels though */
6069 output = get_default_output(compositor);
6070 if (!output)
6071 return;
6072
6073 if (!output->set_backlight)
6074 return;
6075
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006076 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6077 backlight_new = output->backlight_current - 25;
6078 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6079 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006080
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006081 if (backlight_new < 5)
6082 backlight_new = 5;
6083 if (backlight_new > 255)
6084 backlight_new = 255;
6085
6086 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006087 output->set_backlight(output, output->backlight_current);
6088}
6089
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006090static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006091force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6092 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006093{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006094 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006095 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006096 struct desktop_shell *shell = data;
6097 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006098 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006099
Derek Foreman8ae2db52015-07-15 13:00:36 -05006100 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006101 if (!focus_surface)
6102 return;
6103
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006104 wl_signal_emit(&compositor->kill_signal, focus_surface);
6105
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006106 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006107 wl_client_get_credentials(client, &pid, NULL, NULL);
6108
6109 /* Skip clients that we launched ourselves (the credentials of
6110 * the socketpair is ours) */
6111 if (pid == getpid())
6112 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006113
Daniel Stone325fc2d2012-05-30 16:31:58 +01006114 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006115}
6116
6117static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006118workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006119 uint32_t key, void *data)
6120{
6121 struct desktop_shell *shell = data;
6122 unsigned int new_index = shell->workspaces.current;
6123
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006124 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006125 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006126 if (new_index != 0)
6127 new_index--;
6128
6129 change_workspace(shell, new_index);
6130}
6131
6132static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006133workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006134 uint32_t key, void *data)
6135{
6136 struct desktop_shell *shell = data;
6137 unsigned int new_index = shell->workspaces.current;
6138
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006139 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006140 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006141 if (new_index < shell->workspaces.num - 1)
6142 new_index++;
6143
6144 change_workspace(shell, new_index);
6145}
6146
6147static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006148workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006149 uint32_t key, void *data)
6150{
6151 struct desktop_shell *shell = data;
6152 unsigned int new_index;
6153
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006154 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006155 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006156 new_index = key - KEY_F1;
6157 if (new_index >= shell->workspaces.num)
6158 new_index = shell->workspaces.num - 1;
6159
6160 change_workspace(shell, new_index);
6161}
6162
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006163static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006164workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6165 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006166{
6167 struct desktop_shell *shell = data;
6168 unsigned int new_index = shell->workspaces.current;
6169
6170 if (shell->locked)
6171 return;
6172
6173 if (new_index != 0)
6174 new_index--;
6175
Derek Foreman8ae2db52015-07-15 13:00:36 -05006176 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006177}
6178
6179static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006180workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6181 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006182{
6183 struct desktop_shell *shell = data;
6184 unsigned int new_index = shell->workspaces.current;
6185
6186 if (shell->locked)
6187 return;
6188
6189 if (new_index < shell->workspaces.num - 1)
6190 new_index++;
6191
Derek Foreman8ae2db52015-07-15 13:00:36 -05006192 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006193}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006194
6195static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006196shell_reposition_view_on_output_destroy(struct weston_view *view)
6197{
6198 struct weston_output *output, *first_output;
6199 struct weston_compositor *ec = view->surface->compositor;
6200 struct shell_surface *shsurf;
6201 float x, y;
6202 int visible;
6203
6204 x = view->geometry.x;
6205 y = view->geometry.y;
6206
6207 /* At this point the destroyed output is not in the list anymore.
6208 * If the view is still visible somewhere, we leave where it is,
6209 * otherwise, move it to the first output. */
6210 visible = 0;
6211 wl_list_for_each(output, &ec->output_list, link) {
6212 if (pixman_region32_contains_point(&output->region,
6213 x, y, NULL)) {
6214 visible = 1;
6215 break;
6216 }
6217 }
6218
6219 if (!visible) {
6220 first_output = container_of(ec->output_list.next,
6221 struct weston_output, link);
6222
6223 x = first_output->x + first_output->width / 4;
6224 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006225
6226 weston_view_set_position(view, x, y);
6227 } else {
6228 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006229 }
6230
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006231
6232 shsurf = get_shell_surface(view->surface);
6233
6234 if (shsurf) {
6235 shsurf->saved_position_valid = false;
6236 shsurf->next_state.maximized = false;
6237 shsurf->next_state.fullscreen = false;
6238 shsurf->state_changed = true;
6239 }
6240}
6241
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006242void
6243shell_for_each_layer(struct desktop_shell *shell,
6244 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006245{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006246 struct workspace **ws;
6247
6248 func(shell, &shell->fullscreen_layer, data);
6249 func(shell, &shell->panel_layer, data);
6250 func(shell, &shell->background_layer, data);
6251 func(shell, &shell->lock_layer, data);
6252 func(shell, &shell->input_panel_layer, data);
6253
6254 wl_array_for_each(ws, &shell->workspaces.array)
6255 func(shell, &(*ws)->layer, data);
6256}
6257
6258static void
6259shell_output_destroy_move_layer(struct desktop_shell *shell,
6260 struct weston_layer *layer,
6261 void *data)
6262{
6263 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006264 struct weston_view *view;
6265
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006266 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006267 if (view->output != output)
6268 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006269
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006270 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006271 }
6272}
6273
6274static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006275handle_output_destroy(struct wl_listener *listener, void *data)
6276{
6277 struct shell_output *output_listener =
6278 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006279 struct weston_output *output = output_listener->output;
6280 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006281
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006282 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006283
Xiong Zhang6b481422013-10-23 13:58:32 +08006284 wl_list_remove(&output_listener->destroy_listener.link);
6285 wl_list_remove(&output_listener->link);
6286 free(output_listener);
6287}
6288
6289static void
6290create_shell_output(struct desktop_shell *shell,
6291 struct weston_output *output)
6292{
6293 struct shell_output *shell_output;
6294
6295 shell_output = zalloc(sizeof *shell_output);
6296 if (shell_output == NULL)
6297 return;
6298
6299 shell_output->output = output;
6300 shell_output->shell = shell;
6301 shell_output->destroy_listener.notify = handle_output_destroy;
6302 wl_signal_add(&output->destroy_signal,
6303 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006304 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006305}
6306
6307static void
6308handle_output_create(struct wl_listener *listener, void *data)
6309{
6310 struct desktop_shell *shell =
6311 container_of(listener, struct desktop_shell, output_create_listener);
6312 struct weston_output *output = (struct weston_output *)data;
6313
6314 create_shell_output(shell, output);
6315}
6316
6317static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006318handle_output_move_layer(struct desktop_shell *shell,
6319 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006320{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006321 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006322 struct weston_view *view;
6323 float x, y;
6324
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006325 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006326 if (view->output != output)
6327 continue;
6328
6329 x = view->geometry.x + output->move_x;
6330 y = view->geometry.y + output->move_y;
6331 weston_view_set_position(view, x, y);
6332 }
6333}
6334
6335static void
6336handle_output_move(struct wl_listener *listener, void *data)
6337{
6338 struct desktop_shell *shell;
6339
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006340 shell = container_of(listener, struct desktop_shell,
6341 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006342
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006343 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006344}
6345
6346static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006347setup_output_destroy_handler(struct weston_compositor *ec,
6348 struct desktop_shell *shell)
6349{
6350 struct weston_output *output;
6351
6352 wl_list_init(&shell->output_list);
6353 wl_list_for_each(output, &ec->output_list, link)
6354 create_shell_output(shell, output);
6355
6356 shell->output_create_listener.notify = handle_output_create;
6357 wl_signal_add(&ec->output_created_signal,
6358 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006359
6360 shell->output_move_listener.notify = handle_output_move;
6361 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006362}
6363
6364static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006365shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006366{
Tiago Vignattibe143262012-04-16 17:31:41 +03006367 struct desktop_shell *shell =
6368 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006369 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006370 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006371
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006372 /* Force state to unlocked so we don't try to fade */
6373 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006374
6375 if (shell->child.client) {
6376 /* disable respawn */
6377 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006378 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006379 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006380
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006381 wl_list_remove(&shell->idle_listener.link);
6382 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006383
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006384 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006385 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006386
Xiong Zhang6b481422013-10-23 13:58:32 +08006387 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6388 wl_list_remove(&shell_output->destroy_listener.link);
6389 wl_list_remove(&shell_output->link);
6390 free(shell_output);
6391 }
6392
6393 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006394 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006395
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006396 wl_array_for_each(ws, &shell->workspaces.array)
6397 workspace_destroy(*ws);
6398 wl_array_release(&shell->workspaces.array);
6399
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006400 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006401 free(shell);
6402}
6403
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006404static void
6405shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6406{
6407 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006408 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006409
6410 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006411 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6412 MODIFIER_CTRL | MODIFIER_ALT,
6413 terminate_binding, ec);
6414 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6415 click_to_activate_binding,
6416 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006417 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6418 click_to_activate_binding,
6419 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006420 weston_compositor_add_touch_binding(ec, 0,
6421 touch_to_activate_binding,
6422 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006423 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6424 MODIFIER_SUPER | MODIFIER_ALT,
6425 surface_opacity_binding, NULL);
6426 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6427 MODIFIER_SUPER, zoom_axis_binding,
6428 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006429
6430 /* configurable bindings */
6431 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006432 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6433 zoom_key_binding, NULL);
6434 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6435 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006436 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6437 maximize_binding, NULL);
6438 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6439 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006440 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6441 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006442 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006443 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006444 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006445 weston_compositor_add_button_binding(ec, BTN_LEFT,
6446 mod | MODIFIER_SHIFT,
6447 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006448
6449 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006450 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006451 rotate_binding, NULL);
6452
Daniel Stone325fc2d2012-05-30 16:31:58 +01006453 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6454 shell);
6455 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6456 ec);
6457 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6458 backlight_binding, ec);
6459 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6460 ec);
6461 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6462 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006463 weston_compositor_add_key_binding(ec, KEY_K, mod,
6464 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006465 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6466 workspace_up_binding, shell);
6467 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6468 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006469 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6470 workspace_move_surface_up_binding,
6471 shell);
6472 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6473 workspace_move_surface_down_binding,
6474 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006475
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006476 if (shell->exposay_modifier)
6477 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6478 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006479
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006480 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6481 if (shell->workspaces.num > 1) {
6482 num_workspace_bindings = shell->workspaces.num;
6483 if (num_workspace_bindings > 6)
6484 num_workspace_bindings = 6;
6485 for (i = 0; i < num_workspace_bindings; i++)
6486 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6487 workspace_f_binding,
6488 shell);
6489 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006490
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006491 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006492}
6493
Jason Ekstrand024177c2014-04-21 19:42:58 -05006494static void
6495handle_seat_created(struct wl_listener *listener, void *data)
6496{
6497 struct weston_seat *seat = data;
6498
6499 create_shell_seat(seat);
6500}
6501
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006502WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006503module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006504 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006505{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006506 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006507 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006508 struct workspace **pws;
6509 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006510 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006511
Peter Huttererf3d62272013-08-08 11:57:05 +10006512 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006513 if (shell == NULL)
6514 return -1;
6515
Kristian Høgsberg75840622011-09-06 13:48:16 -04006516 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006517
6518 shell->destroy_listener.notify = shell_destroy;
6519 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006520 shell->idle_listener.notify = idle_handler;
6521 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6522 shell->wake_listener.notify = wake_handler;
6523 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006524
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006525 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006526 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006527 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006528 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006529 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006530 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006531 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006532 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006533 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006534 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006535 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006536 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006537 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006538
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006539 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6540 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006541 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6542 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006543 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006544
6545 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006546 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006547
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006548 if (input_panel_setup(shell) < 0)
6549 return -1;
6550
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006551 shell->text_backend = text_backend_init(ec);
6552 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006553 return -1;
6554
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006555 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006556
Daniel Stonedf8133b2013-11-19 11:37:14 +01006557 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6558 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6559
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006560 for (i = 0; i < shell->workspaces.num; i++) {
6561 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6562 if (pws == NULL)
6563 return -1;
6564
6565 *pws = workspace_create();
6566 if (*pws == NULL)
6567 return -1;
6568 }
6569 activate_workspace(shell, 0);
6570
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006571 weston_layer_init(&shell->minimized_layer, NULL);
6572
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006573 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006574 wl_list_init(&shell->workspaces.animation.link);
6575 shell->workspaces.animation.frame = animate_workspace_change_frame;
6576
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006577 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006578 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006579 return -1;
6580
Rafael Antognollie2a34552013-12-03 15:35:45 -02006581 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6582 shell, bind_xdg_shell) == NULL)
6583 return -1;
6584
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006585 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006586 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006587 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006588 return -1;
6589
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006590 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6591 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006592 return -1;
6593
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006594 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006595
Jonny Lamb765760d2014-08-20 15:53:19 +02006596 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6597
Xiong Zhang6b481422013-10-23 13:58:32 +08006598 setup_output_destroy_handler(ec, shell);
6599
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006600 loop = wl_display_get_event_loop(ec->wl_display);
6601 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006602
Jason Ekstrand024177c2014-04-21 19:42:58 -05006603 wl_list_for_each(seat, &ec->seat_list, link)
6604 handle_seat_created(NULL, seat);
6605 shell->seat_create_listener.notify = handle_seat_created;
6606 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006607
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006608 screenshooter_create(ec);
6609
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006610 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006611
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006612 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006613
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006614 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6615
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006616 return 0;
6617}