blob: d08b0d81c8035ef2934fb1d534386bf1fb7ec2b1 [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:
Jonny Lambd73c6942014-08-20 15:53:20 +0200427 weston_view_to_global_float(view,
428 view->surface->width, 0,
429 &x, &y);
430
Derek Foreman612341f2015-04-15 12:23:55 -0500431 *width = (int)x - output->x;
432 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200433 return;
434
435 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
436 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
437 weston_view_to_global_float(view,
438 0, view->surface->height,
439 &x, &y);
440
Derek Foreman612341f2015-04-15 12:23:55 -0500441 *width = view->surface->width + (int)x - output->x;
442 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200443 return;
444
445 default:
446 /* we've already set width and height to
447 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700448 break;
449 }
450 }
451
Jonny Lambd73c6942014-08-20 15:53:20 +0200452 /* the correct view wasn't found */
453}
454
455static void
456get_output_work_area(struct desktop_shell *shell,
457 struct weston_output *output,
458 pixman_rectangle32_t *area)
459{
460 int32_t panel_width = 0, panel_height = 0;
461
Derek Foremanf814c5d2015-04-15 12:23:54 -0500462 area->x = output->x;
463 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200464
465 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200466 switch (shell->panel_position) {
467 case DESKTOP_SHELL_PANEL_POSITION_TOP:
468 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500469 area->y += panel_height;
Jonny Lambd73c6942014-08-20 15:53:20 +0200470 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
471 area->width = output->width;
472 area->height = output->height - panel_height;
473 break;
474 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500475 area->x += panel_width;
Jonny Lambd73c6942014-08-20 15:53:20 +0200476 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
477 area->width = output->width - panel_width;
478 area->height = output->height;
479 break;
480 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700481}
482
483static void
484send_configure_for_surface(struct shell_surface *shsurf)
485{
486 int32_t width, height;
487 struct surface_state *state;
488
489 if (shsurf->state_requested)
490 state = &shsurf->requested_state;
491 else if (shsurf->state_changed)
492 state = &shsurf->next_state;
493 else
494 state = &shsurf->state;
495
496 if (state->fullscreen) {
497 width = shsurf->output->width;
498 height = shsurf->output->height;
499 } else if (state->maximized) {
500 struct desktop_shell *shell;
Jonny Lambd73c6942014-08-20 15:53:20 +0200501 pixman_rectangle32_t area;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700502
503 shell = shell_surface_get_shell(shsurf);
Jonny Lambd73c6942014-08-20 15:53:20 +0200504 get_output_work_area(shell, shsurf->output, &area);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700505
Jonny Lambd73c6942014-08-20 15:53:20 +0200506 width = area.width;
507 height = area.height;
Ryo Munakata79954ec2015-05-02 21:44:04 +0900508 } else if (shsurf->resize_edges) {
509 width = shsurf->geometry.width;
510 height = shsurf->geometry.height;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700511 } else {
512 width = 0;
513 height = 0;
514 }
515
516 shsurf->client->send_configure(shsurf->surface, width, height);
517}
518
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300519static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400520shell_surface_state_changed(struct shell_surface *shsurf)
521{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700522 if (shell_surface_is_xdg_surface(shsurf))
523 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400524}
525
526static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300527shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300528{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700529 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400530 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700531 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400532
533 if (grab->shsurf->resize_edges) {
534 grab->shsurf->resize_edges = 0;
535 shell_surface_state_changed(grab->shsurf);
536 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700537 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300538
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700539 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300540}
541
542static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700543shell_touch_grab_start(struct shell_touch_grab *grab,
544 const struct weston_touch_grab_interface *interface,
545 struct shell_surface *shsurf,
546 struct weston_touch *touch)
547{
548 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800549
Jonny Lamb76cf1fe2014-08-20 11:27:10 +0200550 touch_popup_grab_end(touch);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700551 if (touch->seat->pointer)
552 popup_grab_end(touch->seat->pointer);
553
Rusty Lynch1084da52013-08-15 09:10:08 -0700554 grab->grab.interface = interface;
555 grab->shsurf = shsurf;
556 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
557 wl_signal_add(&shsurf->destroy_signal,
558 &grab->shsurf_destroy_listener);
559
560 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700561 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700562
563 weston_touch_start_grab(touch, &grab->grab);
564 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500565 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500566 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700567}
568
569static void
570shell_touch_grab_end(struct shell_touch_grab *grab)
571{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700572 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700573 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700574 grab->shsurf->grabbed = 0;
575 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700576
577 weston_touch_end_grab(grab->touch);
578}
579
580static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500581center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800582 struct weston_output *output);
583
Daniel Stone496ca172012-05-30 16:31:42 +0100584static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300585get_modifier(char *modifier)
586{
587 if (!modifier)
588 return MODIFIER_SUPER;
589
590 if (!strcmp("ctrl", modifier))
591 return MODIFIER_CTRL;
592 else if (!strcmp("alt", modifier))
593 return MODIFIER_ALT;
594 else if (!strcmp("super", modifier))
595 return MODIFIER_SUPER;
596 else
597 return MODIFIER_SUPER;
598}
599
Juan Zhaoe10d2792012-04-25 19:09:52 +0800600static enum animation_type
601get_animation_type(char *animation)
602{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800603 if (!animation)
604 return ANIMATION_NONE;
605
Juan Zhaoe10d2792012-04-25 19:09:52 +0800606 if (!strcmp("zoom", animation))
607 return ANIMATION_ZOOM;
608 else if (!strcmp("fade", animation))
609 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100610 else if (!strcmp("dim-layer", animation))
611 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800612 else
613 return ANIMATION_NONE;
614}
615
Alex Wu4539b082012-03-01 12:57:46 +0800616static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400617shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200618{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400619 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500620 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300621 int ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200622
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400623 section = weston_config_get_section(shell->compositor->config,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400624 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300625 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
626 WESTON_SHELL_CLIENT);
627 if (ret < 0)
628 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400629 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500630 "client", &s, client);
631 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100632 shell->client = s;
633 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400634 "binding-modifier", &s, "super");
635 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200636 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800637
638 weston_config_section_get_string(section,
639 "exposay-modifier", &s, "none");
640 if (strcmp(s, "none") == 0)
641 shell->exposay_modifier = 0;
642 else
643 shell->exposay_modifier = get_modifier(s);
644 free(s);
645
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400646 weston_config_section_get_string(section, "animation", &s, "none");
647 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200648 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200649 weston_config_section_get_string(section, "close-animation", &s, "fade");
650 shell->win_close_animation_type = get_animation_type(s);
651 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700652 weston_config_section_get_string(section,
653 "startup-animation", &s, "fade");
654 shell->startup_animation_type = get_animation_type(s);
655 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700656 if (shell->startup_animation_type == ANIMATION_ZOOM)
657 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100658 weston_config_section_get_string(section, "focus-animation", &s, "none");
659 shell->focus_animation_type = get_animation_type(s);
660 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400661 weston_config_section_get_uint(section, "num-workspaces",
662 &shell->workspaces.num,
663 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200664}
665
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800666struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100667get_default_output(struct weston_compositor *compositor)
668{
669 return container_of(compositor->output_list.next,
670 struct weston_output, link);
671}
672
Pekka Paalanen8274d902014-08-06 19:36:51 +0300673static int
674focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
675{
676 return snprintf(buf, len, "focus highlight effect for output %s",
677 surface->output->name);
678}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100679
680/* no-op func for checking focus surface */
681static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600682focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100683{
684}
685
686static struct focus_surface *
687get_focus_surface(struct weston_surface *surface)
688{
689 if (surface->configure == focus_surface_configure)
690 return surface->configure_private;
691 else
692 return NULL;
693}
694
695static bool
696is_focus_surface (struct weston_surface *es)
697{
698 return (es->configure == focus_surface_configure);
699}
700
701static bool
702is_focus_view (struct weston_view *view)
703{
704 return is_focus_surface (view->surface);
705}
706
707static struct focus_surface *
708create_focus_surface(struct weston_compositor *ec,
709 struct weston_output *output)
710{
711 struct focus_surface *fsurf = NULL;
712 struct weston_surface *surface = NULL;
713
714 fsurf = malloc(sizeof *fsurf);
715 if (!fsurf)
716 return NULL;
717
718 fsurf->surface = weston_surface_create(ec);
719 surface = fsurf->surface;
720 if (surface == NULL) {
721 free(fsurf);
722 return NULL;
723 }
724
725 surface->configure = focus_surface_configure;
726 surface->output = output;
727 surface->configure_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300728 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100729
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800730 fsurf->view = weston_view_create(surface);
731 if (fsurf->view == NULL) {
732 weston_surface_destroy(surface);
733 free(fsurf);
734 return NULL;
735 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100736 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100737
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600738 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600739 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100740 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
741 pixman_region32_fini(&surface->opaque);
742 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
743 output->width, output->height);
744 pixman_region32_fini(&surface->input);
745 pixman_region32_init(&surface->input);
746
747 wl_list_init(&fsurf->workspace_transform.link);
748
749 return fsurf;
750}
751
752static void
753focus_surface_destroy(struct focus_surface *fsurf)
754{
755 weston_surface_destroy(fsurf->surface);
756 free(fsurf);
757}
758
759static void
760focus_animation_done(struct weston_view_animation *animation, void *data)
761{
762 struct workspace *ws = data;
763
764 ws->focus_animation = NULL;
765}
766
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200767static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200768focus_state_destroy(struct focus_state *state)
769{
770 wl_list_remove(&state->seat_destroy_listener.link);
771 wl_list_remove(&state->surface_destroy_listener.link);
772 free(state);
773}
774
775static void
776focus_state_seat_destroy(struct wl_listener *listener, void *data)
777{
778 struct focus_state *state = container_of(listener,
779 struct focus_state,
780 seat_destroy_listener);
781
782 wl_list_remove(&state->link);
783 focus_state_destroy(state);
784}
785
786static void
787focus_state_surface_destroy(struct wl_listener *listener, void *data)
788{
789 struct focus_state *state = container_of(listener,
790 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400791 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400792 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500793 struct weston_surface *main_surface, *next;
794 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200795
Pekka Paalanen01388e22013-04-25 13:57:44 +0300796 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
797
Kristian Høgsberge3778222012-07-31 17:29:30 -0400798 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300799 wl_list_for_each(view,
800 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400802 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100803 if (is_focus_view(view))
804 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400805
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400807 break;
808 }
809
Pekka Paalanen01388e22013-04-25 13:57:44 +0300810 /* if the focus was a sub-surface, activate its main surface */
811 if (main_surface != state->keyboard_focus)
812 next = main_surface;
813
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100814 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400815 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100816 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100817 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400818 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100819 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
820 if (state->ws->focus_animation)
821 weston_view_animation_destroy(state->ws->focus_animation);
822
823 state->ws->focus_animation = weston_fade_run(
824 state->ws->fsurf_front->view,
825 state->ws->fsurf_front->view->alpha, 0.0, 300,
826 focus_animation_done, state->ws);
827 }
828
Kristian Høgsberge3778222012-07-31 17:29:30 -0400829 wl_list_remove(&state->link);
830 focus_state_destroy(state);
831 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200832}
833
834static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400835focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200836{
Jonas Ådahl04769742012-06-13 00:01:24 +0200837 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200838
839 state = malloc(sizeof *state);
840 if (state == NULL)
841 return NULL;
842
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400844 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200845 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400846 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200847
848 state->seat_destroy_listener.notify = focus_state_seat_destroy;
849 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400850 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200851 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400852 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200853
854 return state;
855}
856
Jonas Ådahl8538b222012-08-29 22:13:03 +0200857static struct focus_state *
858ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
859{
860 struct workspace *ws = get_current_workspace(shell);
861 struct focus_state *state;
862
863 wl_list_for_each(state, &ws->focus_list, link)
864 if (state->seat == seat)
865 break;
866
867 if (&state->link == &ws->focus_list)
868 state = focus_state_create(seat, ws);
869
870 return state;
871}
872
Jonas Ådahl04769742012-06-13 00:01:24 +0200873static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800874focus_state_set_focus(struct focus_state *state,
875 struct weston_surface *surface)
876{
877 if (state->keyboard_focus) {
878 wl_list_remove(&state->surface_destroy_listener.link);
879 wl_list_init(&state->surface_destroy_listener.link);
880 }
881
882 state->keyboard_focus = surface;
883 if (surface)
884 wl_signal_add(&surface->destroy_signal,
885 &state->surface_destroy_listener);
886}
887
888static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400889restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200890{
891 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400892 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100893 struct wl_list pending_seat_list;
894 struct weston_seat *seat, *next_seat;
895
896 /* Temporarily steal the list of seats so that we can keep
897 * track of the seats we've already processed */
898 wl_list_init(&pending_seat_list);
899 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
900 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200901
902 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100903 wl_list_remove(&state->seat->link);
904 wl_list_insert(&shell->compositor->seat_list,
905 &state->seat->link);
906
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800907 if (state->seat->keyboard == NULL)
908 continue;
909
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400910 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200911
Kristian Høgsberge3148752013-05-06 23:19:49 -0400912 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200913 }
Neil Roberts4237f502014-04-09 16:33:31 +0100914
915 /* For any remaining seats that we don't have a focus state
916 * for we'll reset the keyboard focus to NULL */
917 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
918 wl_list_insert(&shell->compositor->seat_list, &seat->link);
919
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300920 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100921 continue;
922
923 weston_keyboard_set_focus(seat->keyboard, NULL);
924 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200925}
926
927static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200928replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
929 struct weston_seat *seat)
930{
931 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932
933 wl_list_for_each(state, &ws->focus_list, link) {
934 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800935 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200936 return;
937 }
938 }
939}
940
941static void
942drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
943 struct weston_surface *surface)
944{
945 struct focus_state *state;
946
947 wl_list_for_each(state, &ws->focus_list, link)
948 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800949 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200950}
951
952static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100953animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
954 struct weston_view *from, struct weston_view *to)
955{
956 struct weston_output *output;
957 bool focus_surface_created = false;
958
959 /* FIXME: Only support dim animation using two layers */
960 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
961 return;
962
963 output = get_default_output(shell->compositor);
964 if (ws->fsurf_front == NULL && (from || to)) {
965 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800966 if (ws->fsurf_front == NULL)
967 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100968 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800969
970 ws->fsurf_back = create_focus_surface(shell->compositor, output);
971 if (ws->fsurf_back == NULL) {
972 focus_surface_destroy(ws->fsurf_front);
973 return;
974 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100975 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800976
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 focus_surface_created = true;
978 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300979 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
980 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100981 }
982
983 if (ws->focus_animation) {
984 weston_view_animation_destroy(ws->focus_animation);
985 ws->focus_animation = NULL;
986 }
987
988 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300989 weston_layer_entry_insert(&to->layer_link,
990 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100991 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300992 weston_layer_entry_insert(&ws->layer.view_list,
993 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100994
995 if (focus_surface_created) {
996 ws->focus_animation = weston_fade_run(
997 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200998 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 focus_animation_done, ws);
1000 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001001 weston_layer_entry_insert(&from->layer_link,
1002 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 ws->focus_animation = weston_stable_fade_run(
1004 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001005 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001006 focus_animation_done, ws);
1007 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001008 weston_layer_entry_insert(&ws->layer.view_list,
1009 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001010 ws->focus_animation = weston_stable_fade_run(
1011 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +02001012 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001013 focus_animation_done, ws);
1014 }
1015}
1016
1017static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001018workspace_destroy(struct workspace *ws)
1019{
Jonas Ådahl04769742012-06-13 00:01:24 +02001020 struct focus_state *state, *next;
1021
1022 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1023 focus_state_destroy(state);
1024
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001025 if (ws->fsurf_front)
1026 focus_surface_destroy(ws->fsurf_front);
1027 if (ws->fsurf_back)
1028 focus_surface_destroy(ws->fsurf_back);
1029
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001030 free(ws);
1031}
1032
Jonas Ådahl04769742012-06-13 00:01:24 +02001033static void
1034seat_destroyed(struct wl_listener *listener, void *data)
1035{
1036 struct weston_seat *seat = data;
1037 struct focus_state *state, *next;
1038 struct workspace *ws = container_of(listener,
1039 struct workspace,
1040 seat_destroyed_listener);
1041
1042 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1043 if (state->seat == seat)
1044 wl_list_remove(&state->link);
1045}
1046
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001047static struct workspace *
1048workspace_create(void)
1049{
1050 struct workspace *ws = malloc(sizeof *ws);
1051 if (ws == NULL)
1052 return NULL;
1053
1054 weston_layer_init(&ws->layer, NULL);
1055
Jonas Ådahl04769742012-06-13 00:01:24 +02001056 wl_list_init(&ws->focus_list);
1057 wl_list_init(&ws->seat_destroyed_listener.link);
1058 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001059 ws->fsurf_front = NULL;
1060 ws->fsurf_back = NULL;
1061 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +02001062
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001063 return ws;
1064}
1065
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066static int
1067workspace_is_empty(struct workspace *ws)
1068{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001069 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070}
1071
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001072static struct workspace *
1073get_workspace(struct desktop_shell *shell, unsigned int index)
1074{
1075 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001076 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001077 pws += index;
1078 return *pws;
1079}
1080
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001081struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001082get_current_workspace(struct desktop_shell *shell)
1083{
1084 return get_workspace(shell, shell->workspaces.current);
1085}
1086
1087static void
1088activate_workspace(struct desktop_shell *shell, unsigned int index)
1089{
1090 struct workspace *ws;
1091
1092 ws = get_workspace(shell, index);
1093 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1094
1095 shell->workspaces.current = index;
1096}
1097
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098static unsigned int
1099get_output_height(struct weston_output *output)
1100{
1101 return abs(output->region.extents.y1 - output->region.extents.y2);
1102}
1103
1104static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001105view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106{
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 struct weston_transform *transform;
1108
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001109 if (is_focus_view(view)) {
1110 struct focus_surface *fsurf = get_focus_surface(view->surface);
1111 transform = &fsurf->workspace_transform;
1112 } else {
1113 struct shell_surface *shsurf = get_shell_surface(view->surface);
1114 transform = &shsurf->workspace_transform;
1115 }
1116
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001119 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001121 weston_matrix_init(&transform->matrix);
1122 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001123 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125}
1126
1127static void
1128workspace_translate_out(struct workspace *ws, double fraction)
1129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131 unsigned int height;
1132 double d;
1133
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001134 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136 d = height * fraction;
1137
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001138 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001139 }
1140}
1141
1142static void
1143workspace_translate_in(struct workspace *ws, double fraction)
1144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146 unsigned int height;
1147 double d;
1148
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001149 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151
1152 if (fraction > 0)
1153 d = -(height - height * fraction);
1154 else
1155 d = height + height * fraction;
1156
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001157 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001158 }
1159}
1160
1161static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001162broadcast_current_workspace_state(struct desktop_shell *shell)
1163{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001164 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001165
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001166 wl_resource_for_each(resource, &shell->workspaces.client_list)
1167 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001168 shell->workspaces.current,
1169 shell->workspaces.num);
1170}
1171
1172static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173reverse_workspace_change_animation(struct desktop_shell *shell,
1174 unsigned int index,
1175 struct workspace *from,
1176 struct workspace *to)
1177{
1178 shell->workspaces.current = index;
1179
1180 shell->workspaces.anim_to = to;
1181 shell->workspaces.anim_from = from;
1182 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1183 shell->workspaces.anim_timestamp = 0;
1184
Scott Moreau4272e632012-08-13 09:58:41 -06001185 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001186}
1187
1188static void
1189workspace_deactivate_transforms(struct workspace *ws)
1190{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001192 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001193
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001194 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001195 if (is_focus_view(view)) {
1196 struct focus_surface *fsurf = get_focus_surface(view->surface);
1197 transform = &fsurf->workspace_transform;
1198 } else {
1199 struct shell_surface *shsurf = get_shell_surface(view->surface);
1200 transform = &shsurf->workspace_transform;
1201 }
1202
1203 if (!wl_list_empty(&transform->link)) {
1204 wl_list_remove(&transform->link);
1205 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001206 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001208 }
1209}
1210
1211static void
1212finish_workspace_change_animation(struct desktop_shell *shell,
1213 struct workspace *from,
1214 struct workspace *to)
1215{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001216 struct weston_view *view;
1217
Scott Moreau4272e632012-08-13 09:58:41 -06001218 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001220 /* Views that extend past the bottom of the output are still
1221 * visible after the workspace animation ends but before its layer
1222 * is hidden. In that case, we need to damage below those views so
1223 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001224 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001225 weston_view_damage_below(view);
1226
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001227 wl_list_remove(&shell->workspaces.animation.link);
1228 workspace_deactivate_transforms(from);
1229 workspace_deactivate_transforms(to);
1230 shell->workspaces.anim_to = NULL;
1231
1232 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1233}
1234
1235static void
1236animate_workspace_change_frame(struct weston_animation *animation,
1237 struct weston_output *output, uint32_t msecs)
1238{
1239 struct desktop_shell *shell =
1240 container_of(animation, struct desktop_shell,
1241 workspaces.animation);
1242 struct workspace *from = shell->workspaces.anim_from;
1243 struct workspace *to = shell->workspaces.anim_to;
1244 uint32_t t;
1245 double x, y;
1246
1247 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1248 finish_workspace_change_animation(shell, from, to);
1249 return;
1250 }
1251
1252 if (shell->workspaces.anim_timestamp == 0) {
1253 if (shell->workspaces.anim_current == 0.0)
1254 shell->workspaces.anim_timestamp = msecs;
1255 else
1256 shell->workspaces.anim_timestamp =
1257 msecs -
1258 /* Invers of movement function 'y' below. */
1259 (asin(1.0 - shell->workspaces.anim_current) *
1260 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1261 M_2_PI);
1262 }
1263
1264 t = msecs - shell->workspaces.anim_timestamp;
1265
1266 /*
1267 * x = [0, π/2]
1268 * y(x) = sin(x)
1269 */
1270 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1271 y = sin(x);
1272
1273 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001274 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001275
1276 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1277 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1278 shell->workspaces.anim_current = y;
1279
Scott Moreau4272e632012-08-13 09:58:41 -06001280 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001281 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001282 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001283 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001284}
1285
1286static void
1287animate_workspace_change(struct desktop_shell *shell,
1288 unsigned int index,
1289 struct workspace *from,
1290 struct workspace *to)
1291{
1292 struct weston_output *output;
1293
1294 int dir;
1295
1296 if (index > shell->workspaces.current)
1297 dir = -1;
1298 else
1299 dir = 1;
1300
1301 shell->workspaces.current = index;
1302
1303 shell->workspaces.anim_dir = dir;
1304 shell->workspaces.anim_from = from;
1305 shell->workspaces.anim_to = to;
1306 shell->workspaces.anim_current = 0.0;
1307 shell->workspaces.anim_timestamp = 0;
1308
1309 output = container_of(shell->compositor->output_list.next,
1310 struct weston_output, link);
1311 wl_list_insert(&output->animation_list,
1312 &shell->workspaces.animation.link);
1313
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001314 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001315
1316 workspace_translate_in(to, 0);
1317
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001318 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001319
Scott Moreau4272e632012-08-13 09:58:41 -06001320 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001321}
1322
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001323static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001324update_workspace(struct desktop_shell *shell, unsigned int index,
1325 struct workspace *from, struct workspace *to)
1326{
1327 shell->workspaces.current = index;
1328 wl_list_insert(&from->layer.link, &to->layer.link);
1329 wl_list_remove(&from->layer.link);
1330}
1331
1332static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001333change_workspace(struct desktop_shell *shell, unsigned int index)
1334{
1335 struct workspace *from;
1336 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001337 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001338
1339 if (index == shell->workspaces.current)
1340 return;
1341
1342 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001343 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001344 return;
1345
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001346 from = get_current_workspace(shell);
1347 to = get_workspace(shell, index);
1348
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001349 if (shell->workspaces.anim_from == to &&
1350 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001351 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001352 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001353 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001354 return;
1355 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001356
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001357 if (shell->workspaces.anim_to != NULL)
1358 finish_workspace_change_animation(shell,
1359 shell->workspaces.anim_from,
1360 shell->workspaces.anim_to);
1361
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001362 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001363
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001364 if (shell->focus_animation_type != ANIMATION_NONE) {
1365 wl_list_for_each(state, &from->focus_list, link)
1366 if (state->keyboard_focus)
1367 animate_focus_change(shell, from,
1368 get_default_view(state->keyboard_focus), NULL);
1369
1370 wl_list_for_each(state, &to->focus_list, link)
1371 if (state->keyboard_focus)
1372 animate_focus_change(shell, to,
1373 NULL, get_default_view(state->keyboard_focus));
1374 }
1375
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001376 if (workspace_is_empty(to) && workspace_is_empty(from))
1377 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001378 else
1379 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001380
1381 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001382}
1383
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001384static bool
1385workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1386{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001387 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001388 struct wl_list *e;
1389
1390 if (wl_list_empty(list))
1391 return false;
1392
1393 e = list->next;
1394
1395 if (e->next != list)
1396 return false;
1397
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001398 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001399}
1400
1401static void
Philip Withnall659163d2013-11-25 18:01:36 +00001402move_surface_to_workspace(struct desktop_shell *shell,
1403 struct shell_surface *shsurf,
1404 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001405{
1406 struct workspace *from;
1407 struct workspace *to;
1408 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001409 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001410 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001411
1412 if (workspace == shell->workspaces.current)
1413 return;
1414
Philip Withnall659163d2013-11-25 18:01:36 +00001415 view = get_default_view(shsurf->surface);
1416 if (!view)
1417 return;
1418
1419 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1420
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001421 if (workspace >= shell->workspaces.num)
1422 workspace = shell->workspaces.num - 1;
1423
Jonas Ådahle9d22502012-08-29 22:13:01 +02001424 from = get_current_workspace(shell);
1425 to = get_workspace(shell, workspace);
1426
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001427 weston_layer_entry_remove(&view->layer_link);
1428 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001429
Philip Withnall648a4dd2013-11-25 18:01:44 +00001430 shell_surface_update_child_surface_layers(shsurf);
1431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001433 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1434 if (!seat->keyboard)
1435 continue;
1436
1437 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001439 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001440 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001441
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001443}
1444
1445static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001446take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001447 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001448 unsigned int index)
1449{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001450 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001452 struct shell_surface *shsurf;
1453 struct workspace *from;
1454 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001455 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001456
Pekka Paalanen01388e22013-04-25 13:57:44 +03001457 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 view = get_default_view(surface);
1459 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001460 index == shell->workspaces.current ||
1461 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001462 return;
1463
1464 from = get_current_workspace(shell);
1465 to = get_workspace(shell, index);
1466
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001467 weston_layer_entry_remove(&view->layer_link);
1468 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001469
Philip Withnall659163d2013-11-25 18:01:36 +00001470 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001471 if (shsurf != NULL)
1472 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001473
Jonas Ådahle9d22502012-08-29 22:13:01 +02001474 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001475 drop_focus_state(shell, from, surface);
1476
1477 if (shell->workspaces.anim_from == to &&
1478 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001479 wl_list_remove(&to->layer.link);
1480 wl_list_insert(from->layer.link.prev, &to->layer.link);
1481
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001482 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001483 broadcast_current_workspace_state(shell);
1484
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001485 return;
1486 }
1487
1488 if (shell->workspaces.anim_to != NULL)
1489 finish_workspace_change_animation(shell,
1490 shell->workspaces.anim_from,
1491 shell->workspaces.anim_to);
1492
1493 if (workspace_is_empty(from) &&
1494 workspace_has_only(to, surface))
1495 update_workspace(shell, index, from, to);
1496 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001497 if (shsurf != NULL &&
1498 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001499 wl_list_insert(&shell->workspaces.anim_sticky_list,
1500 &shsurf->workspace_transform.link);
1501
1502 animate_workspace_change(shell, index, from, to);
1503 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001504
1505 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001506
1507 state = ensure_focus_state(shell, seat);
1508 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001509 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001510}
1511
1512static void
1513workspace_manager_move_surface(struct wl_client *client,
1514 struct wl_resource *resource,
1515 struct wl_resource *surface_resource,
1516 uint32_t workspace)
1517{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001518 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001519 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001520 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001521 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001522 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001523
Pekka Paalanen01388e22013-04-25 13:57:44 +03001524 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001525 shell_surface = get_shell_surface(main_surface);
1526 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001528
1529 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001530}
1531
1532static const struct workspace_manager_interface workspace_manager_implementation = {
1533 workspace_manager_move_surface,
1534};
1535
1536static void
1537unbind_resource(struct wl_resource *resource)
1538{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001539 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001540}
1541
1542static void
1543bind_workspace_manager(struct wl_client *client,
1544 void *data, uint32_t version, uint32_t id)
1545{
1546 struct desktop_shell *shell = data;
1547 struct wl_resource *resource;
1548
Jason Ekstranda85118c2013-06-27 20:17:02 -05001549 resource = wl_resource_create(client,
1550 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001551
1552 if (resource == NULL) {
1553 weston_log("couldn't add workspace manager object");
1554 return;
1555 }
1556
Jason Ekstranda85118c2013-06-27 20:17:02 -05001557 wl_resource_set_implementation(resource,
1558 &workspace_manager_implementation,
1559 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001560 wl_list_insert(&shell->workspaces.client_list,
1561 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001562
1563 workspace_manager_send_state(resource,
1564 shell->workspaces.current,
1565 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001566}
1567
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001568static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001569touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1570 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1571{
1572}
1573
1574static void
1575touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1576{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001577 struct weston_touch_move_grab *move =
1578 (struct weston_touch_move_grab *) container_of(
1579 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001580
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001581 if (touch_id == 0)
1582 move->active = 0;
1583
Jonas Ådahl9484b692013-12-02 22:05:03 +01001584 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001585 shell_touch_grab_end(&move->base);
1586 free(move);
1587 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001588}
1589
1590static void
1591touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1592 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1593{
1594 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1595 struct shell_surface *shsurf = move->base.shsurf;
1596 struct weston_surface *es;
1597 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1598 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1599
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001600 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001601 return;
1602
1603 es = shsurf->surface;
1604
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001605 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001606
1607 weston_compositor_schedule_repaint(es->compositor);
1608}
1609
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001610static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001611touch_move_grab_frame(struct weston_touch_grab *grab)
1612{
1613}
1614
1615static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001616touch_move_grab_cancel(struct weston_touch_grab *grab)
1617{
1618 struct weston_touch_move_grab *move =
1619 (struct weston_touch_move_grab *) container_of(
1620 grab, struct shell_touch_grab, grab);
1621
1622 shell_touch_grab_end(&move->base);
1623 free(move);
1624}
1625
Rusty Lynch1084da52013-08-15 09:10:08 -07001626static const struct weston_touch_grab_interface touch_move_grab_interface = {
1627 touch_move_grab_down,
1628 touch_move_grab_up,
1629 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001630 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001631 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001632};
1633
1634static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001635surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001636{
1637 struct weston_touch_move_grab *move;
1638
1639 if (!shsurf)
1640 return -1;
1641
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001642 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001643 return 0;
1644
1645 move = malloc(sizeof *move);
1646 if (!move)
1647 return -1;
1648
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001649 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001651 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foremanb7674ae2015-07-15 13:00:37 -05001653 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001654
1655 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001656 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001657
1658 return 0;
1659}
1660
1661static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001662noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001663{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001664}
1665
1666static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001667constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1668{
1669 struct shell_surface *shsurf = move->base.shsurf;
1670 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001671 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001672 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001673 pixman_rectangle32_t area;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001674
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) {
Derek Foreman6feb0f92015-04-15 12:23:56 -05001679 get_output_work_area(shsurf->shell,
1680 shsurf->surface->output,
1681 &area);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001682
Derek Foreman6feb0f92015-04-15 12:23:56 -05001683 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1684 if (bottom - safety < area.y)
1685 y = area.y + safety - shsurf->geometry.height
1686 - shsurf->geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001687
1688 if (move->client_initiated &&
Derek Foreman6feb0f92015-04-15 12:23:56 -05001689 y + shsurf->geometry.y < area.y)
1690 y = area.y - shsurf->geometry.y;
1691
Jonny Lambd73c6942014-08-20 15:53:20 +02001692 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001693
1694 *cx = x;
1695 *cy = y;
1696}
1697
1698static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001699move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1700 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001701{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001702 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001703 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001704 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001705 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001706
1707 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001708 if (!shsurf)
1709 return;
1710
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001711 constrain_position(move, &cx, &cy);
1712
1713 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001714
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001716}
1717
1718static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001719move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001720 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001721{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001722 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1723 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001724 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001725 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001726
Daniel Stone4dbadb12012-05-30 16:31:51 +01001727 if (pointer->button_count == 0 &&
1728 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001729 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001730 free(grab);
1731 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001732}
1733
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001734static void
1735move_grab_cancel(struct weston_pointer_grab *grab)
1736{
1737 struct shell_grab *shell_grab =
1738 container_of(grab, struct shell_grab, grab);
1739
1740 shell_grab_end(shell_grab);
1741 free(grab);
1742}
1743
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001744static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001745 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001746 move_grab_motion,
1747 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001748 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001749};
1750
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001752surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001753 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754{
1755 struct weston_move_grab *move;
1756
1757 if (!shsurf)
1758 return -1;
1759
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001760 if (shsurf->grabbed ||
1761 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762 return 0;
1763
1764 move = malloc(sizeof *move);
1765 if (!move)
1766 return -1;
1767
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001769 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Derek Foreman794fa0e2015-07-15 13:00:38 -05001771 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001772 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001773
1774 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Derek Foreman794fa0e2015-07-15 13:00:38 -05001775 pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776
1777 return 0;
1778}
1779
1780static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001781common_surface_move(struct wl_resource *resource,
1782 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001783{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001784 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001785 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001786 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001787
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001788 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001789 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001790 seat->pointer->button_count > 0 &&
1791 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001793 if ((surface == shsurf->surface) &&
Derek Foreman794fa0e2015-07-15 13:00:38 -05001794 (surface_move(shsurf, seat->pointer, true) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001795 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001796 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001797 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001798 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001800 if ((surface == shsurf->surface) &&
Derek Foremanb7674ae2015-07-15 13:00:37 -05001801 (surface_touch_move(shsurf, seat->touch) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001802 wl_resource_post_no_memory(resource);
1803 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001804}
1805
Rafael Antognollie2a34552013-12-03 15:35:45 -02001806static void
1807shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1808 struct wl_resource *seat_resource, uint32_t serial)
1809{
1810 common_surface_move(resource, seat_resource, serial);
1811}
1812
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001813struct weston_resize_grab {
1814 struct shell_grab base;
1815 uint32_t edges;
1816 int32_t width, height;
1817};
1818
1819static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001820resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1821 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001822{
1823 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001824 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001825 struct shell_surface *shsurf = resize->base.shsurf;
1826 int32_t width, height;
1827 wl_fixed_t from_x, from_y;
1828 wl_fixed_t to_x, to_y;
1829
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001830 weston_pointer_move(pointer, x, y);
1831
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001832 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001833 return;
1834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 weston_view_from_global_fixed(shsurf->view,
1836 pointer->grab_x, pointer->grab_y,
1837 &from_x, &from_y);
1838 weston_view_from_global_fixed(shsurf->view,
1839 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001840
1841 width = resize->width;
1842 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1843 width += wl_fixed_to_int(from_x - to_x);
1844 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1845 width += wl_fixed_to_int(to_x - from_x);
1846 }
1847
1848 height = resize->height;
1849 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1850 height += wl_fixed_to_int(from_y - to_y);
1851 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1852 height += wl_fixed_to_int(to_y - from_y);
1853 }
1854
Derek Foreman70ac0ed2015-03-18 11:16:33 -05001855 if (width < 1)
1856 width = 1;
1857 if (height < 1)
1858 height = 1;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001859 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001860}
1861
1862static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001863send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001864{
1865 struct shell_surface *shsurf = get_shell_surface(surface);
1866
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001867 assert(shsurf);
1868
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001869 if (shsurf->resource)
1870 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001871 shsurf->resize_edges,
1872 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001873}
1874
1875static const struct weston_shell_client shell_client = {
1876 send_configure
1877};
1878
1879static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001880resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001881 uint32_t time, uint32_t button, uint32_t state_w)
1882{
1883 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001884 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001885 enum wl_pointer_button_state state = state_w;
1886
1887 if (pointer->button_count == 0 &&
1888 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1889 shell_grab_end(&resize->base);
1890 free(grab);
1891 }
1892}
1893
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001894static void
1895resize_grab_cancel(struct weston_pointer_grab *grab)
1896{
1897 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1898
1899 shell_grab_end(&resize->base);
1900 free(grab);
1901}
1902
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001903static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001904 noop_grab_focus,
1905 resize_grab_motion,
1906 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001907 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001908};
1909
Giulio Camuffob8366642013-04-25 13:57:46 +03001910/*
1911 * Returns the bounding box of a surface and all its sub-surfaces,
1912 * in the surface coordinates system. */
1913static void
1914surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1915 int32_t *y, int32_t *w, int32_t *h) {
1916 pixman_region32_t region;
1917 pixman_box32_t *box;
1918 struct weston_subsurface *subsurface;
1919
1920 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 surface->width,
1922 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001923
1924 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1925 pixman_region32_union_rect(&region, &region,
1926 subsurface->position.x,
1927 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 subsurface->surface->width,
1929 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001930 }
1931
1932 box = pixman_region32_extents(&region);
1933 if (x)
1934 *x = box->x1;
1935 if (y)
1936 *y = box->y1;
1937 if (w)
1938 *w = box->x2 - box->x1;
1939 if (h)
1940 *h = box->y2 - box->y1;
1941
1942 pixman_region32_fini(&region);
1943}
1944
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001945static int
1946surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001947 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001948{
1949 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001950 const unsigned resize_topbottom =
1951 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1952 const unsigned resize_leftright =
1953 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1954 const unsigned resize_any = resize_topbottom | resize_leftright;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001955
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001956 if (shsurf->grabbed ||
1957 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001958 return 0;
1959
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001960 /* Check for invalid edge combinations. */
1961 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1962 (edges & resize_topbottom) == resize_topbottom ||
1963 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001964 return 0;
1965
1966 resize = malloc(sizeof *resize);
1967 if (!resize)
1968 return -1;
1969
1970 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001971
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001972 resize->width = shsurf->geometry.width;
1973 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001974
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001975 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001976 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001977 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001978 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001979
1980 return 0;
1981}
1982
1983static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001984common_surface_resize(struct wl_resource *resource,
1985 struct wl_resource *seat_resource, uint32_t serial,
1986 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001987{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001988 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001989 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001990 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001991
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001992 if (seat->pointer == NULL ||
1993 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001994 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001995 seat->pointer->focus == NULL)
1996 return;
1997
1998 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1999 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002000 return;
2001
Derek Foreman8fbebbd2015-07-15 13:00:40 -05002002 if (surface_resize(shsurf, seat->pointer, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04002003 wl_resource_post_no_memory(resource);
2004}
2005
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002006static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002007shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2008 struct wl_resource *seat_resource, uint32_t serial,
2009 uint32_t edges)
2010{
2011 common_surface_resize(resource, seat_resource, serial, edges);
2012}
2013
2014static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002015busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002016{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002017 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002018 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002020 wl_fixed_t sx, sy;
2021
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 view = weston_compositor_pick_view(pointer->seat->compositor,
2023 pointer->x, pointer->y,
2024 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002025
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002026 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
2027 shell_grab_end(grab);
2028 free(grab);
2029 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002030}
2031
2032static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002033busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2034 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002035{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002036 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002037}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002038
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002039static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002040busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002041 uint32_t time, uint32_t button, uint32_t state)
2042{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002043 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04002044 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05002045 struct weston_pointer *pointer = grab->grab.pointer;
2046 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002047
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002048 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002049 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman794fa0e2015-07-15 13:00:38 -05002050 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002051 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002052 activate(shsurf->shell, shsurf->surface, seat, true);
Derek Foreman74de4692015-07-15 13:00:39 -05002053 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04002054 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002055}
2056
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002057static void
2058busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2059{
2060 struct shell_grab *grab = (struct shell_grab *) base;
2061
2062 shell_grab_end(grab);
2063 free(grab);
2064}
2065
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002066static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002067 busy_cursor_grab_focus,
2068 busy_cursor_grab_motion,
2069 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002070 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002071};
2072
2073static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002074set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002075{
2076 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002077
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002078 if (pointer->grab->interface == &busy_cursor_grab_interface)
2079 return;
2080
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002081 grab = malloc(sizeof *grab);
2082 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002083 return;
2084
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002085 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2086 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03002087 /* Mark the shsurf as ungrabbed so that button binding is able
2088 * to move it. */
2089 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002090}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002091
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002092static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002093end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002094{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002095 struct shell_grab *grab;
2096 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002097
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002098 wl_list_for_each(seat, &compositor->seat_list, link) {
2099 if (seat->pointer == NULL)
2100 continue;
2101
2102 grab = (struct shell_grab *) seat->pointer->grab;
2103 if (grab->grab.interface == &busy_cursor_grab_interface &&
2104 wl_resource_get_client(grab->shsurf->resource) == client) {
2105 shell_grab_end(grab);
2106 free(grab);
2107 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002108 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06002109}
2110
Scott Moreau9521d5e2012-04-19 13:06:17 -06002111static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002112handle_shell_client_destroy(struct wl_listener *listener, void *data);
2113
2114static int
2115xdg_ping_timeout_handler(void *data)
2116{
2117 struct shell_client *sc = data;
2118 struct weston_seat *seat;
2119 struct shell_surface *shsurf;
2120
2121 /* Client is not responding */
2122 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002123 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2124 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2125 continue;
2126 if (seat->pointer->focus->surface->resource == NULL)
2127 continue;
Michael Vetter2a18a522015-05-15 17:17:47 +02002128
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002129 shsurf = get_shell_surface(seat->pointer->focus->surface);
2130 if (shsurf &&
2131 wl_resource_get_client(shsurf->resource) == sc->client)
2132 set_busy_cursor(shsurf, seat->pointer);
2133 }
2134
2135 return 1;
2136}
2137
2138static void
2139handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2140{
2141 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002142 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002143 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002144 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002145
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002146 if (sc->unresponsive) {
2147 xdg_ping_timeout_handler(sc);
2148 return;
2149 }
2150
2151 sc->ping_serial = serial;
2152 loop = wl_display_get_event_loop(compositor->wl_display);
2153 if (sc->ping_timer == NULL)
2154 sc->ping_timer =
2155 wl_event_loop_add_timer(loop,
2156 xdg_ping_timeout_handler, sc);
2157 if (sc->ping_timer == NULL)
2158 return;
2159
2160 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2161
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002162 if (shell_surface_is_xdg_surface(shsurf) ||
2163 shell_surface_is_xdg_popup(shsurf))
2164 xdg_shell_send_ping(sc->resource, serial);
2165 else if (shell_surface_is_wl_shell_surface(shsurf))
2166 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002167}
2168
Scott Moreauff1db4a2012-04-17 19:06:18 -06002169static void
2170ping_handler(struct weston_surface *surface, uint32_t serial)
2171{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002172 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002173
2174 if (!shsurf)
2175 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002176 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002177 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002178 if (shsurf->surface == shsurf->shell->grab_surface)
2179 return;
2180
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002181 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002182}
2183
2184static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002185handle_pointer_focus(struct wl_listener *listener, void *data)
2186{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002187 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002189 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002190 uint32_t serial;
2191
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002193 return;
2194
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002196 serial = wl_display_next_serial(compositor->wl_display);
2197 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002198}
2199
2200static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002201shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2202{
2203 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002204 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002205}
2206
2207static void
2208shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2209{
2210 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002211 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002212}
2213
2214static void
2215handle_keyboard_focus(struct wl_listener *listener, void *data)
2216{
2217 struct weston_keyboard *keyboard = data;
2218 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2219
2220 if (seat->focused_surface) {
2221 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2222 if (shsurf)
2223 shell_surface_lose_keyboard_focus(shsurf);
2224 }
2225
2226 seat->focused_surface = keyboard->focus;
2227
2228 if (seat->focused_surface) {
2229 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2230 if (shsurf)
2231 shell_surface_gain_keyboard_focus(shsurf);
2232 }
2233}
2234
2235static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002236shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002237{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002238 if (sc->ping_serial != serial)
2239 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002240
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002241 sc->unresponsive = 0;
2242 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002243
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002244 if (sc->ping_timer) {
2245 wl_event_source_remove(sc->ping_timer);
2246 sc->ping_timer = NULL;
2247 }
2248
2249}
2250
2251static void
2252shell_surface_pong(struct wl_client *client,
2253 struct wl_resource *resource, uint32_t serial)
2254{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002255 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2256 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002257
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002258 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002259}
2260
2261static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002262set_title(struct shell_surface *shsurf, const char *title)
2263{
2264 free(shsurf->title);
2265 shsurf->title = strdup(title);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002266 shsurf->surface->timeline.force_refresh = 1;
2267}
2268
2269static void
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002270set_pid(struct shell_surface *shsurf, pid_t pid)
2271{
2272 /* We have no use for it */
2273}
2274
2275static void
Pekka Paalanenb5026542014-11-12 15:09:24 +02002276set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2277{
2278 shsurf->type = t;
2279 shsurf->surface->timeline.force_refresh = 1;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002280}
2281
2282static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002283set_window_geometry(struct shell_surface *shsurf,
2284 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002285{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002286 shsurf->next_geometry.x = x;
2287 shsurf->next_geometry.y = y;
2288 shsurf->next_geometry.width = width;
2289 shsurf->next_geometry.height = height;
2290 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002291}
2292
2293static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002294shell_surface_set_title(struct wl_client *client,
2295 struct wl_resource *resource, const char *title)
2296{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002297 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002298
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002299 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002300}
2301
2302static void
2303shell_surface_set_class(struct wl_client *client,
2304 struct wl_resource *resource, const char *class)
2305{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002306 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002307
2308 free(shsurf->class);
2309 shsurf->class = strdup(class);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002310 shsurf->surface->timeline.force_refresh = 1;
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002311}
2312
Alex Wu4539b082012-03-01 12:57:46 +08002313static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002314restore_output_mode(struct weston_output *output)
2315{
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002316 if (output->original_mode)
2317 weston_output_mode_switch_to_native(output);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002318}
2319
2320static void
2321restore_all_output_modes(struct weston_compositor *compositor)
2322{
2323 struct weston_output *output;
2324
2325 wl_list_for_each(output, &compositor->output_list, link)
2326 restore_output_mode(output);
2327}
2328
Philip Withnall07926d92013-11-25 18:01:40 +00002329/* The surface will be inserted into the list immediately after the link
2330 * returned by this function (i.e. will be stacked immediately above the
2331 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002332static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002333shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2334{
2335 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002336 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002337
2338 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002339 case SHELL_SURFACE_XWAYLAND:
2340 return &shsurf->shell->fullscreen_layer.view_list;
2341
2342 case SHELL_SURFACE_NONE:
2343 return NULL;
2344
Kristian Høgsbergead52422014-01-01 13:51:52 -08002345 case SHELL_SURFACE_POPUP:
2346 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002347 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002348 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002349 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002350 /* Move the surface to its parent layer so
2351 * that surfaces which are transient for
2352 * fullscreen surfaces don't get hidden by the
2353 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002354
2355 /* TODO: Handle a parent with multiple views */
2356 parent = get_default_view(shsurf->parent);
2357 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002358 return container_of(parent->layer_link.link.prev,
2359 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002360 }
Philip Withnall07926d92013-11-25 18:01:40 +00002361
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002362 /* Move the surface to a normal workspace layer so that surfaces
2363 * which were previously fullscreen or transient are no longer
2364 * rendered on top. */
2365 ws = get_current_workspace(shsurf->shell);
2366 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002367 }
2368
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002369 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002370}
2371
Philip Withnall648a4dd2013-11-25 18:01:44 +00002372static void
2373shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2374{
2375 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002376 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002377
2378 /* Move the child layers to the same workspace as shsurf. They will be
2379 * stacked above shsurf. */
2380 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002381 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002382 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002383 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002384 prev = container_of(shsurf->view->layer_link.link.prev,
2385 struct weston_layer_entry, link);
2386 weston_layer_entry_remove(&child->view->layer_link);
2387 weston_layer_entry_insert(prev,
2388 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002389 weston_view_geometry_dirty(child->view);
2390 weston_surface_damage(child->surface);
2391
2392 /* Recurse. We don’t expect this to recurse very far (if
2393 * at all) because that would imply we have transient
2394 * (or popup) children of transient surfaces, which
2395 * would be unusual. */
2396 shell_surface_update_child_surface_layers(child);
2397 }
2398 }
2399}
2400
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002401/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002402 * geometry to ensure the changes are redrawn.
2403 *
2404 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2405 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002406static void
2407shell_surface_update_layer(struct shell_surface *shsurf)
2408{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002409 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002410
2411 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2412
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002413 if (new_layer_link == NULL)
2414 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002415 if (new_layer_link == &shsurf->view->layer_link)
2416 return;
2417
2418 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002419 weston_layer_entry_remove(&shsurf->view->layer_link);
2420 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002421 weston_view_geometry_dirty(shsurf->view);
2422 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002423
2424 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002425}
2426
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002427static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002428shell_surface_set_parent(struct shell_surface *shsurf,
2429 struct weston_surface *parent)
2430{
2431 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002432
2433 wl_list_remove(&shsurf->children_link);
2434 wl_list_init(&shsurf->children_link);
2435
2436 /* Insert into the parent surface’s child list. */
2437 if (parent != NULL) {
2438 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2439 if (parent_shsurf != NULL)
2440 wl_list_insert(&parent_shsurf->children_list,
2441 &shsurf->children_link);
2442 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002443}
2444
2445static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002446shell_surface_set_output(struct shell_surface *shsurf,
2447 struct weston_output *output)
2448{
2449 struct weston_surface *es = shsurf->surface;
2450
2451 /* get the default output, if the client set it as NULL
2452 check whether the ouput is available */
2453 if (output)
2454 shsurf->output = output;
2455 else if (es->output)
2456 shsurf->output = es->output;
2457 else
2458 shsurf->output = get_default_output(es->compositor);
2459}
2460
2461static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002462surface_clear_next_states(struct shell_surface *shsurf)
2463{
2464 shsurf->next_state.maximized = false;
2465 shsurf->next_state.fullscreen = false;
2466
2467 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2468 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2469 shsurf->state_changed = true;
2470}
2471
2472static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002473set_toplevel(struct shell_surface *shsurf)
2474{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002475 shell_surface_set_parent(shsurf, NULL);
2476 surface_clear_next_states(shsurf);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002477 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002478
2479 /* The layer_link is updated in set_surface_type(),
2480 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002481}
2482
2483static void
2484shell_surface_set_toplevel(struct wl_client *client,
2485 struct wl_resource *resource)
2486{
2487 struct shell_surface *surface = wl_resource_get_user_data(resource);
2488
2489 set_toplevel(surface);
2490}
2491
2492static void
2493set_transient(struct shell_surface *shsurf,
2494 struct weston_surface *parent, int x, int y, uint32_t flags)
2495{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002496 assert(parent != NULL);
2497
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002498 shell_surface_set_parent(shsurf, parent);
2499
2500 surface_clear_next_states(shsurf);
2501
Philip Withnallbecb77e2013-11-25 18:01:30 +00002502 shsurf->transient.x = x;
2503 shsurf->transient.y = y;
2504 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002505
Rafael Antognollied207b42013-12-03 15:35:43 -02002506 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002507 shsurf->state_changed = true;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002508 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002509
2510 /* The layer_link is updated in set_surface_type(),
2511 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512}
2513
2514static void
2515shell_surface_set_transient(struct wl_client *client,
2516 struct wl_resource *resource,
2517 struct wl_resource *parent_resource,
2518 int x, int y, uint32_t flags)
2519{
2520 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2521 struct weston_surface *parent =
2522 wl_resource_get_user_data(parent_resource);
2523
2524 set_transient(shsurf, parent, x, y, flags);
2525}
2526
2527static void
2528set_fullscreen(struct shell_surface *shsurf,
2529 uint32_t method,
2530 uint32_t framerate,
2531 struct weston_output *output)
2532{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002533 shell_surface_set_output(shsurf, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002534 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002535
2536 shsurf->fullscreen_output = shsurf->output;
2537 shsurf->fullscreen.type = method;
2538 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002539
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002540 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541}
2542
2543static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002544weston_view_set_initial_position(struct weston_view *view,
2545 struct desktop_shell *shell);
2546
2547static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002548unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002549{
Philip Withnallf85fe842013-11-25 18:01:37 +00002550 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002551 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2552 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002553 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002554 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002555
Alex Wu4539b082012-03-01 12:57:46 +08002556 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2557 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002558
Alex Wu4539b082012-03-01 12:57:46 +08002559 wl_list_remove(&shsurf->fullscreen.transform.link);
2560 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002561
Jason Ekstranda7af7042013-10-12 22:38:11 -05002562 if (shsurf->fullscreen.black_view)
2563 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2564 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002565
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002566 if (shsurf->saved_position_valid)
2567 weston_view_set_position(shsurf->view,
2568 shsurf->saved_x, shsurf->saved_y);
2569 else
2570 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2571
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002572 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002575 shsurf->saved_rotation_valid = false;
2576 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002577
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002578 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002579}
2580
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002581static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582shell_surface_set_fullscreen(struct wl_client *client,
2583 struct wl_resource *resource,
2584 uint32_t method,
2585 uint32_t framerate,
2586 struct wl_resource *output_resource)
2587{
2588 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2589 struct weston_output *output;
2590
2591 if (output_resource)
2592 output = wl_resource_get_user_data(output_resource);
2593 else
2594 output = NULL;
2595
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002596 shell_surface_set_parent(shsurf, NULL);
2597
Rafael Antognolli03b16592013-12-03 15:35:42 -02002598 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002599 shsurf->next_state.fullscreen = true;
2600 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002601 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002602}
2603
2604static void
2605set_popup(struct shell_surface *shsurf,
2606 struct weston_surface *parent,
2607 struct weston_seat *seat,
2608 uint32_t serial,
2609 int32_t x,
2610 int32_t y)
2611{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002612 assert(parent != NULL);
2613
Philip Withnallbecb77e2013-11-25 18:01:30 +00002614 shsurf->popup.shseat = get_shell_seat(seat);
2615 shsurf->popup.serial = serial;
2616 shsurf->popup.x = x;
2617 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002618
Pekka Paalanenb5026542014-11-12 15:09:24 +02002619 set_type(shsurf, SHELL_SURFACE_POPUP);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002620}
2621
2622static void
2623shell_surface_set_popup(struct wl_client *client,
2624 struct wl_resource *resource,
2625 struct wl_resource *seat_resource,
2626 uint32_t serial,
2627 struct wl_resource *parent_resource,
2628 int32_t x, int32_t y, uint32_t flags)
2629{
2630 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002631 struct weston_surface *parent =
2632 wl_resource_get_user_data(parent_resource);
2633
2634 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002635
Rafael Antognolli03b16592013-12-03 15:35:42 -02002636 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002637 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002638 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002639 wl_resource_get_user_data(seat_resource),
2640 serial, x, y);
2641}
2642
2643static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002644unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002645{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002646 /* undo all maximized things here */
2647 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002648
2649 if (shsurf->saved_position_valid)
2650 weston_view_set_position(shsurf->view,
2651 shsurf->saved_x, shsurf->saved_y);
2652 else
2653 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002654
2655 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656 wl_list_insert(&shsurf->view->geometry.transformation_list,
2657 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002658 shsurf->saved_rotation_valid = false;
2659 }
2660
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002661 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002662}
2663
Philip Withnallbecb77e2013-11-25 18:01:30 +00002664static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002665set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002666{
2667 struct shell_surface *shsurf;
2668 struct workspace *current_ws;
2669 struct weston_seat *seat;
2670 struct weston_surface *focus;
2671 struct weston_view *view;
2672
2673 view = get_default_view(surface);
2674 if (!view)
2675 return;
2676
2677 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2678
2679 shsurf = get_shell_surface(surface);
2680 current_ws = get_current_workspace(shsurf->shell);
2681
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002682 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002683 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002684
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002685 drop_focus_state(shsurf->shell, current_ws, view->surface);
2686 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2687 if (!seat->keyboard)
2688 continue;
2689 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2690 if (focus == view->surface)
2691 weston_keyboard_set_focus(seat->keyboard, NULL);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002692 }
2693
2694 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002695 weston_view_damage_below(view);
2696}
2697
2698static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002699shell_surface_set_maximized(struct wl_client *client,
2700 struct wl_resource *resource,
2701 struct wl_resource *output_resource)
2702{
2703 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2704 struct weston_output *output;
2705
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002706 surface_clear_next_states(shsurf);
2707 shsurf->next_state.maximized = true;
2708 shsurf->state_changed = true;
2709
Pekka Paalanenb5026542014-11-12 15:09:24 +02002710 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002711 shell_surface_set_parent(shsurf, NULL);
2712
Philip Withnallbecb77e2013-11-25 18:01:30 +00002713 if (output_resource)
2714 output = wl_resource_get_user_data(output_resource);
2715 else
2716 output = NULL;
2717
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002718 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002719
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002720 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002721}
2722
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002723/* This is only ever called from set_surface_type(), so there’s no need to
2724 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002725static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002726reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002727{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002728 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002729 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002730 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002731 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002732
Pekka Paalanen98262232011-12-01 10:42:22 +02002733 return 0;
2734}
2735
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002736static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002737set_full_output(struct shell_surface *shsurf)
2738{
2739 shsurf->saved_x = shsurf->view->geometry.x;
2740 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002741 shsurf->saved_width = shsurf->surface->width;
2742 shsurf->saved_height = shsurf->surface->height;
2743 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002744 shsurf->saved_position_valid = true;
2745
2746 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2747 wl_list_remove(&shsurf->rotation.transform.link);
2748 wl_list_init(&shsurf->rotation.transform.link);
2749 weston_view_geometry_dirty(shsurf->view);
2750 shsurf->saved_rotation_valid = true;
2751 }
2752}
2753
2754static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002755set_surface_type(struct shell_surface *shsurf)
2756{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002757 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002759
Philip Withnallbecb77e2013-11-25 18:01:30 +00002760 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002761
Rafael Antognolli03b16592013-12-03 15:35:42 -02002762 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002763 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002764
2765 switch (shsurf->type) {
2766 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002767 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002768 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002769 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770 weston_view_set_position(shsurf->view,
2771 pev->geometry.x + shsurf->transient.x,
2772 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002773 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002774 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002775
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002776 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002777 weston_view_set_position(shsurf->view, shsurf->transient.x,
2778 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002779 break;
2780
Philip Withnall0f640e22013-11-25 18:01:31 +00002781 case SHELL_SURFACE_POPUP:
2782 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002783 default:
2784 break;
2785 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002786
2787 /* Update the surface’s layer. */
2788 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002789}
2790
Tiago Vignattibe143262012-04-16 17:31:41 +03002791static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002792shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002793{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002794 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002795}
2796
Pekka Paalanen8274d902014-08-06 19:36:51 +03002797static int
2798black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2799{
2800 struct weston_surface *fs_surface = surface->configure_private;
2801 int n;
2802 int rem;
2803 int ret;
2804
2805 n = snprintf(buf, len, "black background surface for ");
2806 if (n < 0)
2807 return n;
2808
2809 rem = (int)len - n;
2810 if (rem < 0)
2811 rem = 0;
2812
2813 if (fs_surface->get_label)
2814 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2815 else
2816 ret = snprintf(buf + n, rem, "<unknown>");
2817
2818 if (ret < 0)
2819 return n;
2820
2821 return n + ret;
2822}
2823
Alex Wu21858432012-04-01 20:13:08 +08002824static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002825black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002826
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002828create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002829 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002830 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002831{
2832 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002834
2835 surface = weston_surface_create(ec);
2836 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002837 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002838 return NULL;
2839 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002840 view = weston_view_create(surface);
2841 if (surface == NULL) {
2842 weston_log("no memory\n");
2843 weston_surface_destroy(surface);
2844 return NULL;
2845 }
Alex Wu4539b082012-03-01 12:57:46 +08002846
Alex Wu21858432012-04-01 20:13:08 +08002847 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002848 surface->configure_private = fs_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002849 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002850 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002851 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002852 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002853 pixman_region32_fini(&surface->input);
2854 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002855
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002856 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002857 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858
2859 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002860}
2861
Philip Withnalled8f1a92013-11-25 18:01:43 +00002862static void
2863shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2864{
2865 struct weston_output *output = shsurf->fullscreen_output;
2866
Rafael Antognolli03b16592013-12-03 15:35:42 -02002867 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002868
2869 if (!shsurf->fullscreen.black_view)
2870 shsurf->fullscreen.black_view =
2871 create_black_surface(shsurf->surface->compositor,
2872 shsurf->surface,
2873 output->x, output->y,
2874 output->width,
2875 output->height);
2876
2877 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002878 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2879 weston_layer_entry_insert(&shsurf->view->layer_link,
2880 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002881 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2882 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002883
2884 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002885}
2886
Alex Wu4539b082012-03-01 12:57:46 +08002887/* Create black surface and append it to the associated fullscreen surface.
2888 * Handle size dismatch and positioning according to the method. */
2889static void
2890shell_configure_fullscreen(struct shell_surface *shsurf)
2891{
2892 struct weston_output *output = shsurf->fullscreen_output;
2893 struct weston_surface *surface = shsurf->surface;
2894 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002895 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002896 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002897
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002898 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2899 restore_output_mode(output);
2900
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002901 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002902 weston_layer_entry_remove(&shsurf->view->layer_link);
2903 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002904
Philip Withnalled8f1a92013-11-25 18:01:43 +00002905 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002906
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002908 &surf_width, &surf_height);
2909
Alex Wu4539b082012-03-01 12:57:46 +08002910 switch (shsurf->fullscreen.type) {
2911 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002912 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002913 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002914 break;
2915 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002916 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002917 if (output->width == surf_width &&
2918 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002919 weston_view_set_position(shsurf->view,
2920 output->x - surf_x,
2921 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002922 break;
2923 }
2924
Alex Wu4539b082012-03-01 12:57:46 +08002925 matrix = &shsurf->fullscreen.transform.matrix;
2926 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002927
Scott Moreau1bad5db2012-08-18 01:04:05 -06002928 output_aspect = (float) output->width /
2929 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002930 /* XXX: Use surf_width and surf_height here? */
2931 surface_aspect = (float) surface->width /
2932 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002933 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002934 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002935 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002936 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002937 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002938 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002939
Alex Wu4539b082012-03-01 12:57:46 +08002940 weston_matrix_scale(matrix, scale, scale, 1);
2941 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002943 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002944 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2945 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002946 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002947
Alex Wu4539b082012-03-01 12:57:46 +08002948 break;
2949 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002950 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002951 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002952 surf_width * surface->buffer_viewport.buffer.scale,
2953 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002954 shsurf->fullscreen.framerate};
2955
Derek Foreman6ae7bc92014-11-04 10:47:33 -06002956 if (weston_output_mode_switch_to_temporary(output, &mode,
2957 surface->buffer_viewport.buffer.scale) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002958 weston_view_set_position(shsurf->view,
2959 output->x - surf_x,
2960 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002961 shsurf->fullscreen.black_view->surface->width = output->width;
2962 shsurf->fullscreen.black_view->surface->height = output->height;
2963 weston_view_set_position(shsurf->fullscreen.black_view,
2964 output->x - surf_x,
2965 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002966 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002967 } else {
Mario Kleiner492c12f2015-06-21 21:25:12 +02002968 weston_log("shell: Can't switch to temporary mode.\n");
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002969 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002970 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002971 }
Alex Wubd3354b2012-04-17 17:20:49 +08002972 }
Alex Wu4539b082012-03-01 12:57:46 +08002973 break;
2974 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002975 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002976 break;
2977 default:
2978 break;
2979 }
2980}
2981
Alex Wu4539b082012-03-01 12:57:46 +08002982static void
2983shell_map_fullscreen(struct shell_surface *shsurf)
2984{
Alex Wubd3354b2012-04-17 17:20:49 +08002985 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002986}
2987
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002988static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002989set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2990{
2991 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002992 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002993 shsurf->transient.x = x;
2994 shsurf->transient.y = y;
2995 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002996
2997 shell_surface_set_parent(shsurf, NULL);
2998
Pekka Paalanenb5026542014-11-12 15:09:24 +02002999 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08003000 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003001}
3002
Kristian Høgsberg47792412014-05-04 13:47:06 -07003003static void
3004shell_interface_set_fullscreen(struct shell_surface *shsurf,
3005 uint32_t method,
3006 uint32_t framerate,
3007 struct weston_output *output)
3008{
3009 surface_clear_next_states(shsurf);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003010 shsurf->next_state.fullscreen = true;
3011 shsurf->state_changed = true;
Marek Chalupae9fe4672014-10-29 13:44:44 +01003012
3013 set_fullscreen(shsurf, method, framerate, output);
Kristian Høgsberg47792412014-05-04 13:47:06 -07003014}
3015
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02003016static struct weston_output *
3017get_focused_output(struct weston_compositor *compositor)
3018{
3019 struct weston_seat *seat;
3020 struct weston_output *output = NULL;
3021
3022 wl_list_for_each(seat, &compositor->seat_list, link) {
3023 /* Priority has touch focus, then pointer and
3024 * then keyboard focus. We should probably have
3025 * three for loops and check frist for touch,
3026 * then for pointer, etc. but unless somebody has some
3027 * objections, I think this is sufficient. */
3028 if (seat->touch && seat->touch->focus)
3029 output = seat->touch->focus->output;
3030 else if (seat->pointer && seat->pointer->focus)
3031 output = seat->pointer->focus->output;
3032 else if (seat->keyboard && seat->keyboard->focus)
3033 output = seat->keyboard->focus->output;
3034
3035 if (output)
3036 break;
3037 }
3038
3039 return output;
3040}
3041
3042static void
3043shell_interface_set_maximized(struct shell_surface *shsurf)
3044{
3045 struct weston_output *output;
3046
3047 surface_clear_next_states(shsurf);
3048 shsurf->next_state.maximized = true;
3049 shsurf->state_changed = true;
3050 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3051
3052 if (!weston_surface_is_mapped(shsurf->surface))
3053 output = get_focused_output(shsurf->surface->compositor);
3054 else
3055 output = shsurf->surface->output;
3056
3057 shell_surface_set_output(shsurf, output);
3058 send_configure_for_surface(shsurf);
3059}
3060
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003061static int
3062shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
3063{
Derek Foreman794fa0e2015-07-15 13:00:38 -05003064 return surface_move(shsurf, ws->pointer, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003065}
3066
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003067static int
3068shell_interface_resize(struct shell_surface *shsurf,
3069 struct weston_seat *ws,
3070 uint32_t edges)
3071{
3072 return surface_resize(shsurf, ws->pointer, edges);
3073}
3074
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003075static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076
3077static void
3078destroy_shell_seat(struct wl_listener *listener, void *data)
3079{
3080 struct shell_seat *shseat =
3081 container_of(listener,
3082 struct shell_seat, seat_destroy_listener);
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003083 struct shell_surface *shsurf, *next;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003084
3085 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003086 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003087 shseat->popup_grab.client = NULL;
3088
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003089 wl_list_for_each_safe(shsurf, next,
3090 &shseat->popup_grab.surfaces_list,
3091 popup.grab_link) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003092 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003093 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003094 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003095 }
3096
3097 wl_list_remove(&shseat->seat_destroy_listener.link);
3098 free(shseat);
3099}
3100
Jason Ekstrand024177c2014-04-21 19:42:58 -05003101static void
3102shell_seat_caps_changed(struct wl_listener *listener, void *data)
3103{
3104 struct shell_seat *seat;
3105
3106 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3107
3108 if (seat->seat->keyboard &&
3109 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3110 wl_signal_add(&seat->seat->keyboard->focus_signal,
3111 &seat->keyboard_focus_listener);
3112 } else if (!seat->seat->keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05003113 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003114 wl_list_init(&seat->keyboard_focus_listener.link);
3115 }
3116
3117 if (seat->seat->pointer &&
3118 wl_list_empty(&seat->pointer_focus_listener.link)) {
3119 wl_signal_add(&seat->seat->pointer->focus_signal,
3120 &seat->pointer_focus_listener);
3121 } else if (!seat->seat->pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05003122 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003123 wl_list_init(&seat->pointer_focus_listener.link);
3124 }
3125}
3126
Giulio Camuffo5085a752013-03-25 21:42:45 +01003127static struct shell_seat *
3128create_shell_seat(struct weston_seat *seat)
3129{
3130 struct shell_seat *shseat;
3131
3132 shseat = calloc(1, sizeof *shseat);
3133 if (!shseat) {
3134 weston_log("no memory to allocate shell seat\n");
3135 return NULL;
3136 }
3137
3138 shseat->seat = seat;
3139 wl_list_init(&shseat->popup_grab.surfaces_list);
3140
3141 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3142 wl_signal_add(&seat->destroy_signal,
3143 &shseat->seat_destroy_listener);
3144
Jason Ekstrand024177c2014-04-21 19:42:58 -05003145 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3146 wl_list_init(&shseat->keyboard_focus_listener.link);
3147
3148 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3149 wl_list_init(&shseat->pointer_focus_listener.link);
3150
3151 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3152 wl_signal_add(&seat->updated_caps_signal,
3153 &shseat->caps_changed_listener);
3154 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3155
Giulio Camuffo5085a752013-03-25 21:42:45 +01003156 return shseat;
3157}
3158
3159static struct shell_seat *
3160get_shell_seat(struct weston_seat *seat)
3161{
3162 struct wl_listener *listener;
3163
3164 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05003165 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003166
3167 return container_of(listener,
3168 struct shell_seat, seat_destroy_listener);
3169}
3170
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05003171static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003172popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003173{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003174 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003175 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003176 struct shell_seat *shseat =
3177 container_of(grab, struct shell_seat, popup_grab.grab);
3178 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04003179 wl_fixed_t sx, sy;
3180
Jason Ekstranda7af7042013-10-12 22:38:11 -05003181 view = weston_compositor_pick_view(pointer->seat->compositor,
3182 pointer->x, pointer->y,
3183 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003184
Jason Ekstranda7af7042013-10-12 22:38:11 -05003185 if (view && view->surface->resource &&
3186 wl_resource_get_client(view->surface->resource) == client) {
3187 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003188 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003189 weston_pointer_set_focus(pointer, NULL,
3190 wl_fixed_from_int(0),
3191 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003192 }
3193}
3194
3195static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003196popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3197 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003198{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003199 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01003200 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003201 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003202
Jonas Ådahl61917c82014-08-11 22:44:02 +02003203 if (pointer->focus) {
3204 weston_view_from_global_fixed(pointer->focus, x, y,
3205 &pointer->sx, &pointer->sy);
3206 }
3207
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003208 weston_pointer_move(pointer, x, y);
3209
Neil Roberts96d790e2013-09-19 17:32:00 +01003210 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003211 weston_view_from_global_fixed(pointer->focus,
3212 pointer->x, pointer->y,
3213 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01003214 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04003215 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003216}
3217
3218static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003219popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003220 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003221{
3222 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003223 struct shell_seat *shseat =
3224 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003225 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003226 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003227 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003228 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003229
Neil Roberts96d790e2013-09-19 17:32:00 +01003230 resource_list = &grab->pointer->focus_resource_list;
3231 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003232 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003233 wl_resource_for_each(resource, resource_list) {
3234 wl_pointer_send_button(resource, serial,
3235 time, button, state);
3236 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003237 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003238 (shseat->popup_grab.initial_up ||
Derek Foremanf7b2f8b2015-07-15 13:00:41 -05003239 time - grab->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003240 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003241 }
3242
Daniel Stone4dbadb12012-05-30 16:31:51 +01003243 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003244 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003245}
3246
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003247static void
3248popup_grab_cancel(struct weston_pointer_grab *grab)
3249{
3250 popup_grab_end(grab->pointer);
3251}
3252
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003253static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003254 popup_grab_focus,
3255 popup_grab_motion,
3256 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003257 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003258};
3259
3260static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003261touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3262 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3263{
3264 struct wl_resource *resource;
3265 struct shell_seat *shseat =
3266 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3267 struct wl_display *display = shseat->seat->compositor->wl_display;
3268 uint32_t serial;
3269 struct wl_list *resource_list;
3270
3271 resource_list = &grab->touch->focus_resource_list;
3272 if (!wl_list_empty(resource_list)) {
3273 serial = wl_display_get_serial(display);
3274 wl_resource_for_each(resource, resource_list) {
3275 wl_touch_send_down(resource, serial, time,
3276 grab->touch->focus->surface->resource,
3277 touch_id, sx, sy);
3278 }
3279 }
3280}
3281
3282static void
3283touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3284{
3285 struct wl_resource *resource;
3286 struct shell_seat *shseat =
3287 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3288 struct wl_display *display = shseat->seat->compositor->wl_display;
3289 uint32_t serial;
3290 struct wl_list *resource_list;
3291
3292 resource_list = &grab->touch->focus_resource_list;
3293 if (!wl_list_empty(resource_list)) {
3294 serial = wl_display_get_serial(display);
3295 wl_resource_for_each(resource, resource_list) {
3296 wl_touch_send_up(resource, serial, time, touch_id);
3297 }
3298 }
3299}
3300
3301static void
3302touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3303 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3304{
3305 struct wl_resource *resource;
3306 struct wl_list *resource_list;
3307
3308 resource_list = &grab->touch->focus_resource_list;
3309 if (!wl_list_empty(resource_list)) {
3310 wl_resource_for_each(resource, resource_list) {
3311 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3312 }
3313 }
3314}
3315
3316static void
3317touch_popup_grab_frame(struct weston_touch_grab *grab)
3318{
3319}
3320
3321static void
3322touch_popup_grab_cancel(struct weston_touch_grab *grab)
3323{
3324 touch_popup_grab_end(grab->touch);
3325}
3326
3327static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3328 touch_popup_grab_down,
3329 touch_popup_grab_up,
3330 touch_popup_grab_motion,
3331 touch_popup_grab_frame,
3332 touch_popup_grab_cancel,
3333};
3334
3335static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003336shell_surface_send_popup_done(struct shell_surface *shsurf)
3337{
3338 if (shell_surface_is_wl_shell_surface(shsurf))
3339 wl_shell_surface_send_popup_done(shsurf->resource);
3340 else if (shell_surface_is_xdg_popup(shsurf))
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08003341 xdg_popup_send_popup_done(shsurf->resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003342}
3343
3344static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003345popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003346{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003347 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003348 struct shell_seat *shseat =
3349 container_of(grab, struct shell_seat, popup_grab.grab);
3350 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003351 struct shell_surface *next;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003352
3353 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003354 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003355 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003356 shseat->popup_grab.grab.interface = NULL;
3357 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003358 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003359 wl_list_for_each_safe(shsurf, next,
3360 &shseat->popup_grab.surfaces_list,
3361 popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003363 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003364 wl_list_init(&shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003365 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003366 wl_list_init(&shseat->popup_grab.surfaces_list);
3367 }
3368}
3369
3370static void
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003371touch_popup_grab_end(struct weston_touch *touch)
3372{
3373 struct weston_touch_grab *grab = touch->grab;
3374 struct shell_seat *shseat =
3375 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3376 struct shell_surface *shsurf;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003377 struct shell_surface *next;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003378
3379 if (touch->grab->interface == &touch_popup_grab_interface) {
3380 weston_touch_end_grab(grab->touch);
3381 shseat->popup_grab.client = NULL;
3382 shseat->popup_grab.touch_grab.interface = NULL;
3383 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3384 /* Send the popup_done event to all the popups open */
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003385 wl_list_for_each_safe(shsurf, next,
3386 &shseat->popup_grab.surfaces_list,
3387 popup.grab_link) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003388 shell_surface_send_popup_done(shsurf);
3389 shsurf->popup.shseat = NULL;
Jonas Ådahlc2b10112015-02-13 14:01:53 +08003390 wl_list_init(&shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003391 }
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003392 wl_list_init(&shseat->popup_grab.surfaces_list);
3393 }
3394}
3395
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003396static struct shell_surface *
3397get_top_popup(struct shell_seat *shseat)
3398{
3399 struct shell_surface *shsurf;
3400
3401 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3402 return NULL;
3403 } else {
3404 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3405 struct shell_surface,
3406 popup.grab_link);
3407 return shsurf;
3408 }
3409}
3410
3411static int
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003412add_popup_grab(struct shell_surface *shsurf,
3413 struct shell_seat *shseat,
3414 int32_t type)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003415{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003416 struct weston_seat *seat = shseat->seat;
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003417 struct shell_surface *parent, *top_surface;
3418
3419 parent = get_shell_surface(shsurf->parent);
3420 top_surface = get_top_popup(shseat);
3421 if (shell_surface_is_xdg_popup(shsurf) &&
Dima Ryazanov497f25d2015-04-08 11:51:57 -07003422 (!parent ||
3423 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003424 (top_surface != NULL && parent != top_surface))) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003425 wl_resource_post_error(shsurf->owner_resource,
3426 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003427 "xdg_popup was not created on the "
3428 "topmost popup");
3429 return -1;
3430 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003431
3432 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003433 shseat->popup_grab.type = type;
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003434 shseat->popup_grab.client =
3435 wl_resource_get_client(shsurf->resource);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003436
3437 if (type == POINTER) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003438 shseat->popup_grab.grab.interface =
3439 &popup_grab_interface;
3440
3441 /* We must make sure here that this popup was opened
3442 * after a mouse press, and not just by moving around
3443 * with other popups already open. */
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003444 if (shseat->seat->pointer->button_count > 0)
3445 shseat->popup_grab.initial_up = 0;
3446 } else if (type == TOUCH) {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003447 shseat->popup_grab.touch_grab.interface =
3448 &touch_popup_grab_interface;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003449 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003450
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003451 wl_list_insert(&shseat->popup_grab.surfaces_list,
3452 &shsurf->popup.grab_link);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003453
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003454 if (type == POINTER) {
3455 weston_pointer_start_grab(seat->pointer,
3456 &shseat->popup_grab.grab);
3457 } else if (type == TOUCH) {
3458 weston_touch_start_grab(seat->touch,
3459 &shseat->popup_grab.touch_grab);
3460 }
Rob Bradforddfe31052013-06-26 19:49:11 +01003461 } else {
Jonas Ådahl01193ae2015-02-13 14:01:54 +08003462 wl_list_insert(&shseat->popup_grab.surfaces_list,
3463 &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003464 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003465
3466 return 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003467}
3468
3469static void
3470remove_popup_grab(struct shell_surface *shsurf)
3471{
3472 struct shell_seat *shseat = shsurf->popup.shseat;
3473
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003474 if (shell_surface_is_xdg_popup(shsurf) &&
3475 get_top_popup(shseat) != shsurf) {
Jonas Ådahlee45a552015-03-18 16:37:47 +08003476 wl_resource_post_error(shsurf->owner_resource,
3477 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003478 "xdg_popup was destroyed while it was "
3479 "not the topmost popup.");
3480 return;
3481 }
3482
Giulio Camuffo5085a752013-03-25 21:42:45 +01003483 wl_list_remove(&shsurf->popup.grab_link);
3484 wl_list_init(&shsurf->popup.grab_link);
3485 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003486 if (shseat->popup_grab.type == POINTER) {
3487 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3488 shseat->popup_grab.grab.interface = NULL;
3489 } else if (shseat->popup_grab.type == TOUCH) {
3490 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3491 shseat->popup_grab.touch_grab.interface = NULL;
3492 }
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003493 }
3494}
3495
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003496static int
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003497shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003498{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003499 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003500 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003501
Jason Ekstranda7af7042013-10-12 22:38:11 -05003502 shsurf->surface->output = parent_view->output;
3503 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003504
Jason Ekstranda7af7042013-10-12 22:38:11 -05003505 weston_view_set_transform_parent(shsurf->view, parent_view);
3506 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3507 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003508
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003509 if (shseat->seat->pointer &&
3510 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003511 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3512 return -1;
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02003513 } else if (shseat->seat->touch &&
3514 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003515 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3516 return -1;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003517 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003518 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003519 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003520 }
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08003521
3522 return 0;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003523}
3524
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003525static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003526 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003527 shell_surface_move,
3528 shell_surface_resize,
3529 shell_surface_set_toplevel,
3530 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003531 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003532 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003533 shell_surface_set_maximized,
3534 shell_surface_set_title,
3535 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003536};
3537
3538static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003539destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003540{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003541 struct shell_surface *child, *next;
3542
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003543 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3544
Giulio Camuffo5085a752013-03-25 21:42:45 +01003545 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3546 remove_popup_grab(shsurf);
3547 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003548
Alex Wubd3354b2012-04-17 17:20:49 +08003549 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003550 shell_surface_is_top_fullscreen(shsurf))
3551 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003552
Jason Ekstranda7af7042013-10-12 22:38:11 -05003553 if (shsurf->fullscreen.black_view)
3554 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003555
Alex Wubd3354b2012-04-17 17:20:49 +08003556 /* As destroy_resource() use wl_list_for_each_safe(),
3557 * we can always remove the listener.
3558 */
3559 wl_list_remove(&shsurf->surface_destroy_listener.link);
3560 shsurf->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003561 weston_surface_set_label_func(shsurf->surface, NULL);
Scott Moreau976a0502013-03-07 10:15:17 -07003562 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003563
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564 weston_view_destroy(shsurf->view);
3565
Philip Withnall648a4dd2013-11-25 18:01:44 +00003566 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003567 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3568 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003569
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003570 wl_list_remove(&shsurf->link);
3571 free(shsurf);
3572}
3573
3574static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003575shell_destroy_shell_surface(struct wl_resource *resource)
3576{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003577 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003578
Bryan Caina46b9462014-04-04 17:41:24 -05003579 if (!wl_list_empty(&shsurf->popup.grab_link))
3580 remove_popup_grab(shsurf);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003581 wl_list_remove(wl_resource_get_link(shsurf->resource));
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003582 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003583}
3584
3585static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003586shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003587{
3588 struct shell_surface *shsurf = container_of(listener,
3589 struct shell_surface,
3590 surface_destroy_listener);
3591
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003592 if (shsurf->resource)
3593 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003594
3595 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003596}
3597
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003598static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003599fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003600{
3601 struct shell_surface *shsurf = data;
3602
3603 weston_surface_destroy(shsurf->surface);
3604}
3605
3606static void
Derek Foreman039e9be2015-04-14 17:09:06 -05003607fade_out_done(struct weston_view_animation *animation, void *data)
3608{
3609 struct shell_surface *shsurf = data;
3610 struct wl_event_loop *loop;
3611
3612 loop = wl_display_get_event_loop(
3613 shsurf->surface->compositor->wl_display);
3614
3615 if (!shsurf->destroying) {
3616 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3617 shsurf->destroying = true;
3618 }
3619}
3620
3621static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003622handle_resource_destroy(struct wl_listener *listener, void *data)
3623{
3624 struct shell_surface *shsurf =
3625 container_of(listener, struct shell_surface,
3626 resource_destroy_listener);
3627
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003628 if (!weston_surface_is_mapped(shsurf->surface))
3629 return;
3630
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003631 shsurf->surface->ref_count++;
3632
3633 pixman_region32_fini(&shsurf->surface->pending.input);
3634 pixman_region32_init(&shsurf->surface->pending.input);
3635 pixman_region32_fini(&shsurf->surface->input);
3636 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003637 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3638 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3639 fade_out_done, shsurf);
3640 } else {
3641 weston_surface_destroy(shsurf->surface);
3642 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003643}
3644
3645static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003646shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003647
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003648struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003649get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003650{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003651 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003652 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003653 else
3654 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003655}
3656
Rafael Antognollie2a34552013-12-03 15:35:45 -02003657static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003658create_common_surface(struct shell_client *owner, void *shell,
3659 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003660 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003661{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003662 struct shell_surface *shsurf;
3663
Pekka Paalanen50b67472014-10-01 15:02:41 +03003664 assert(surface->configure == NULL);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003665
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003666 shsurf = calloc(1, sizeof *shsurf);
3667 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003668 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003669 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003670 }
3671
Jason Ekstranda7af7042013-10-12 22:38:11 -05003672 shsurf->view = weston_view_create(surface);
3673 if (!shsurf->view) {
3674 weston_log("no memory to allocate shell surface\n");
3675 free(shsurf);
3676 return NULL;
3677 }
3678
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003679 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003680 surface->configure_private = shsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003681 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003682
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003683 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3684 wl_resource_add_destroy_listener(surface->resource,
3685 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003686 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003687
Tiago Vignattibc052c92012-04-19 16:18:18 +03003688 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003689 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003690 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003691 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003692 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003693 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003694 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3695 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003696 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003697 wl_list_init(&shsurf->fullscreen.transform.link);
3698
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003699 shsurf->output = get_default_output(shsurf->shell->compositor);
3700
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003701 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003702 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003703 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003704 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003705
3706 /* init link so its safe to always remove it in destroy_shell_surface */
3707 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003708 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003709
Pekka Paalanen460099f2012-01-20 16:48:25 +02003710 /* empty when not in use */
3711 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003712 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003713
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003714 wl_list_init(&shsurf->workspace_transform.link);
3715
Philip Withnall648a4dd2013-11-25 18:01:44 +00003716 wl_list_init(&shsurf->children_link);
3717 wl_list_init(&shsurf->children_list);
3718 shsurf->parent = NULL;
3719
Pekka Paalanenb5026542014-11-12 15:09:24 +02003720 set_type(shsurf, SHELL_SURFACE_NONE);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003721
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003722 shsurf->client = client;
3723
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003724 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003725}
3726
Rafael Antognollie2a34552013-12-03 15:35:45 -02003727static struct shell_surface *
3728create_shell_surface(void *shell, struct weston_surface *surface,
3729 const struct weston_shell_client *client)
3730{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003731 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003732}
3733
Jason Ekstranda7af7042013-10-12 22:38:11 -05003734static struct weston_view *
3735get_primary_view(void *shell, struct shell_surface *shsurf)
3736{
3737 return shsurf->view;
3738}
3739
Tiago Vignattibc052c92012-04-19 16:18:18 +03003740static void
3741shell_get_shell_surface(struct wl_client *client,
3742 struct wl_resource *resource,
3743 uint32_t id,
3744 struct wl_resource *surface_resource)
3745{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003746 struct weston_surface *surface =
3747 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003748 struct shell_client *sc = wl_resource_get_user_data(resource);
3749 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003750 struct shell_surface *shsurf;
3751
Pekka Paalanen50b67472014-10-01 15:02:41 +03003752 if (weston_surface_set_role(surface, "wl_shell_surface",
3753 resource, WL_SHELL_ERROR_ROLE) < 0)
Tiago Vignattibc052c92012-04-19 16:18:18 +03003754 return;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003755
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003756 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003757 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03003758 wl_resource_post_no_memory(surface_resource);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003759 return;
3760 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003761
Jason Ekstranda85118c2013-06-27 20:17:02 -05003762 shsurf->resource =
3763 wl_resource_create(client,
3764 &wl_shell_surface_interface, 1, id);
3765 wl_resource_set_implementation(shsurf->resource,
3766 &shell_surface_implementation,
3767 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08003768 wl_list_init(wl_resource_get_link(shsurf->resource));
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003769}
3770
Rafael Antognollie2a34552013-12-03 15:35:45 -02003771static bool
3772shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3773{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003774 /* A shell surface without a resource is created from xwayland
3775 * and is considered a wl_shell surface for now. */
3776
3777 return shsurf->resource == NULL ||
3778 wl_resource_instance_of(shsurf->resource,
3779 &wl_shell_surface_interface,
3780 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003781}
3782
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003783static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003784 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003785};
3786
Rafael Antognollie2a34552013-12-03 15:35:45 -02003787/****************************
3788 * xdg-shell implementation */
3789
3790static void
3791xdg_surface_destroy(struct wl_client *client,
3792 struct wl_resource *resource)
3793{
3794 wl_resource_destroy(resource);
3795}
3796
3797static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003798xdg_surface_set_parent(struct wl_client *client,
3799 struct wl_resource *resource,
3800 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003801{
3802 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003803 struct shell_surface *parent;
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003804
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003805 if (parent_resource) {
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003806 parent = wl_resource_get_user_data(parent_resource);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08003807 shell_surface_set_parent(shsurf, parent->surface);
3808 } else {
3809 shell_surface_set_parent(shsurf, NULL);
3810 }
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003811}
3812
3813static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003814xdg_surface_set_app_id(struct wl_client *client,
3815 struct wl_resource *resource,
3816 const char *app_id)
3817{
3818 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3819
3820 free(shsurf->class);
3821 shsurf->class = strdup(app_id);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003822 shsurf->surface->timeline.force_refresh = 1;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003823}
3824
3825static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003826xdg_surface_show_window_menu(struct wl_client *client,
3827 struct wl_resource *surface_resource,
3828 struct wl_resource *seat_resource,
3829 uint32_t serial,
3830 int32_t x,
3831 int32_t y)
3832{
3833 /* TODO */
3834}
3835
3836static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003837xdg_surface_set_title(struct wl_client *client,
3838 struct wl_resource *resource, const char *title)
3839{
3840 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3841
3842 set_title(shsurf, title);
3843}
3844
3845static void
3846xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3847 struct wl_resource *seat_resource, uint32_t serial)
3848{
3849 common_surface_move(resource, seat_resource, serial);
3850}
3851
3852static void
3853xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3854 struct wl_resource *seat_resource, uint32_t serial,
3855 uint32_t edges)
3856{
3857 common_surface_resize(resource, seat_resource, serial, edges);
3858}
3859
3860static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003861xdg_surface_ack_configure(struct wl_client *client,
3862 struct wl_resource *resource,
3863 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003864{
3865 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3866
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003867 if (shsurf->state_requested) {
3868 shsurf->next_state = shsurf->requested_state;
3869 shsurf->state_changed = true;
3870 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003871 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003872}
3873
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003874static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003875xdg_surface_set_window_geometry(struct wl_client *client,
3876 struct wl_resource *resource,
3877 int32_t x,
3878 int32_t y,
3879 int32_t width,
3880 int32_t height)
3881{
3882 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3883
3884 set_window_geometry(shsurf, x, y, width, height);
3885}
3886
3887static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003888xdg_surface_set_maximized(struct wl_client *client,
3889 struct wl_resource *resource)
3890{
3891 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003892 struct weston_output *output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003893
3894 shsurf->state_requested = true;
3895 shsurf->requested_state.maximized = true;
Marek Chalupabfbb64b2014-09-08 12:34:20 +02003896
3897 if (!weston_surface_is_mapped(shsurf->surface))
3898 output = get_focused_output(shsurf->surface->compositor);
3899 else
3900 output = shsurf->surface->output;
3901
3902 shell_surface_set_output(shsurf, output);
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003903 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003904}
3905
3906static void
3907xdg_surface_unset_maximized(struct wl_client *client,
3908 struct wl_resource *resource)
3909{
3910 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3911
3912 shsurf->state_requested = true;
3913 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003914 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003915}
3916
3917static void
3918xdg_surface_set_fullscreen(struct wl_client *client,
3919 struct wl_resource *resource,
3920 struct wl_resource *output_resource)
3921{
3922 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3923 struct weston_output *output;
3924
3925 shsurf->state_requested = true;
3926 shsurf->requested_state.fullscreen = true;
3927
3928 if (output_resource)
3929 output = wl_resource_get_user_data(output_resource);
3930 else
3931 output = NULL;
3932
Marek Chalupa052917a2014-09-02 11:35:12 +02003933 /* handle clients launching in fullscreen */
3934 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3935 /* Set the output to the one that has focus currently. */
3936 assert(shsurf->surface);
3937 output = get_focused_output(shsurf->surface->compositor);
3938 }
3939
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003940 shell_surface_set_output(shsurf, output);
3941 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003942
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003943 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003944}
3945
3946static void
3947xdg_surface_unset_fullscreen(struct wl_client *client,
3948 struct wl_resource *resource)
3949{
3950 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3951
3952 shsurf->state_requested = true;
3953 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003954 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003955}
3956
3957static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003958xdg_surface_set_minimized(struct wl_client *client,
3959 struct wl_resource *resource)
3960{
3961 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3962
3963 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3964 return;
3965
3966 /* apply compositor's own minimization logic (hide) */
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01003967 set_minimized(shsurf->surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003968}
3969
Rafael Antognollie2a34552013-12-03 15:35:45 -02003970static const struct xdg_surface_interface xdg_surface_implementation = {
3971 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003972 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003973 xdg_surface_set_title,
3974 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003975 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003976 xdg_surface_move,
3977 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003978 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003979 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003980 xdg_surface_set_maximized,
3981 xdg_surface_unset_maximized,
3982 xdg_surface_set_fullscreen,
3983 xdg_surface_unset_fullscreen,
3984 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003985};
3986
3987static void
3988xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003989 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003990{
3991 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003992 uint32_t *s;
3993 struct wl_array states;
3994 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003995
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003996 assert(shsurf);
3997
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003998 if (!shsurf->resource)
3999 return;
4000
4001 wl_array_init(&states);
4002 if (shsurf->requested_state.fullscreen) {
4003 s = wl_array_add(&states, sizeof *s);
4004 *s = XDG_SURFACE_STATE_FULLSCREEN;
4005 } else if (shsurf->requested_state.maximized) {
4006 s = wl_array_add(&states, sizeof *s);
4007 *s = XDG_SURFACE_STATE_MAXIMIZED;
4008 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004009 if (shsurf->resize_edges != 0) {
4010 s = wl_array_add(&states, sizeof *s);
4011 *s = XDG_SURFACE_STATE_RESIZING;
4012 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004013 if (shsurf->focus_count > 0) {
4014 s = wl_array_add(&states, sizeof *s);
4015 *s = XDG_SURFACE_STATE_ACTIVATED;
4016 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004017
4018 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4019 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4020
4021 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004022}
4023
4024static const struct weston_shell_client xdg_client = {
4025 xdg_send_configure
4026};
4027
4028static void
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004029xdg_shell_destroy(struct wl_client *client,
4030 struct wl_resource *resource)
4031{
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004032 struct shell_client *sc = wl_resource_get_user_data(resource);
4033 struct wl_resource *shsurf_resource;
4034 struct shell_surface *shsurf;
4035
4036 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4037 shsurf = wl_resource_get_user_data(shsurf_resource);
4038 if (shsurf->owner_resource == resource) {
4039 wl_resource_post_error(
4040 resource,
4041 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4042 "not all child surface objects destroyed");
4043 return;
4044 }
4045 }
4046
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004047 wl_resource_destroy(resource);
4048}
4049
4050static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02004051xdg_use_unstable_version(struct wl_client *client,
4052 struct wl_resource *resource,
4053 int32_t version)
4054{
4055 if (version > 1) {
4056 wl_resource_post_error(resource,
4057 1,
4058 "xdg-shell:: version not implemented yet.");
4059 return;
4060 }
4061}
4062
4063static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004064create_xdg_surface(struct shell_client *owner, void *shell,
4065 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004066 const struct weston_shell_client *client)
4067{
4068 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004069
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004070 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004071 if (!shsurf)
4072 return NULL;
4073
Pekka Paalanenb5026542014-11-12 15:09:24 +02004074 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004075
4076 return shsurf;
4077}
4078
4079static void
4080xdg_get_xdg_surface(struct wl_client *client,
4081 struct wl_resource *resource,
4082 uint32_t id,
4083 struct wl_resource *surface_resource)
4084{
4085 struct weston_surface *surface =
4086 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004087 struct shell_client *sc = wl_resource_get_user_data(resource);
4088 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004089 struct shell_surface *shsurf;
4090
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004091 shsurf = get_shell_surface(surface);
4092 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4093 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4094 "This wl_surface is already an "
4095 "xdg_surface");
4096 return;
4097 }
4098
Pekka Paalanen50b67472014-10-01 15:02:41 +03004099 if (weston_surface_set_role(surface, "xdg_surface",
4100 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004101 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004102
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004103 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004104 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004105 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004106 return;
4107 }
4108
4109 shsurf->resource =
4110 wl_resource_create(client,
4111 &xdg_surface_interface, 1, id);
4112 wl_resource_set_implementation(shsurf->resource,
4113 &xdg_surface_implementation,
4114 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004115 shsurf->owner_resource = resource;
4116 wl_list_insert(&sc->surface_list,
4117 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004118}
4119
4120static bool
4121shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4122{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08004123 return shsurf->resource &&
4124 wl_resource_instance_of(shsurf->resource,
4125 &xdg_surface_interface,
4126 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004127}
4128
4129/* xdg-popup implementation */
4130
4131static void
4132xdg_popup_destroy(struct wl_client *client,
4133 struct wl_resource *resource)
4134{
4135 wl_resource_destroy(resource);
4136}
4137
Rafael Antognollie2a34552013-12-03 15:35:45 -02004138static const struct xdg_popup_interface xdg_popup_implementation = {
4139 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004140};
4141
4142static void
4143xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04004144 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004145{
4146}
4147
4148static const struct weston_shell_client xdg_popup_client = {
4149 xdg_popup_send_configure
4150};
4151
4152static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004153create_xdg_popup(struct shell_client *owner, void *shell,
4154 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004155 const struct weston_shell_client *client,
4156 struct weston_surface *parent,
4157 struct shell_seat *seat,
4158 uint32_t serial,
4159 int32_t x, int32_t y)
4160{
Jonas Ådahlee45a552015-03-18 16:37:47 +08004161 struct shell_surface *shsurf;
Jonas Ådahl24185e22015-02-27 18:37:41 +08004162
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004163 shsurf = create_common_surface(owner, shell, surface, client);
Pekka Paalanene972b272014-10-01 14:03:56 +03004164 if (!shsurf)
4165 return NULL;
4166
Pekka Paalanenb5026542014-11-12 15:09:24 +02004167 set_type(shsurf, SHELL_SURFACE_POPUP);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004168 shsurf->popup.shseat = seat;
4169 shsurf->popup.serial = serial;
4170 shsurf->popup.x = x;
4171 shsurf->popup.y = y;
4172 shell_surface_set_parent(shsurf, parent);
4173
4174 return shsurf;
4175}
4176
4177static void
4178xdg_get_xdg_popup(struct wl_client *client,
4179 struct wl_resource *resource,
4180 uint32_t id,
4181 struct wl_resource *surface_resource,
4182 struct wl_resource *parent_resource,
4183 struct wl_resource *seat_resource,
4184 uint32_t serial,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004185 int32_t x, int32_t y)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004186{
4187 struct weston_surface *surface =
4188 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004189 struct shell_client *sc = wl_resource_get_user_data(resource);
4190 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004191 struct shell_surface *shsurf;
Jonas Ådahlee45a552015-03-18 16:37:47 +08004192 struct shell_surface *parent_shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004193 struct weston_surface *parent;
4194 struct shell_seat *seat;
4195
Jonas Ådahlbf48e212015-02-06 10:15:28 +08004196 shsurf = get_shell_surface(surface);
4197 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4198 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4199 "This wl_surface is already an "
4200 "xdg_popup");
4201 return;
4202 }
4203
Pekka Paalanen50b67472014-10-01 15:02:41 +03004204 if (weston_surface_set_role(surface, "xdg_popup",
4205 resource, XDG_SHELL_ERROR_ROLE) < 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004206 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004207
4208 if (!parent_resource) {
4209 wl_resource_post_error(surface_resource,
4210 WL_DISPLAY_ERROR_INVALID_OBJECT,
4211 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04004212 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004213 }
4214
4215 parent = wl_resource_get_user_data(parent_resource);
4216 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4217
Jonas Ådahlee45a552015-03-18 16:37:47 +08004218 /* Verify that we are creating the top most popup when mapping,
4219 * as it's not until then we know whether it was mapped as most
4220 * top level or not. */
4221
4222 parent_shsurf = get_shell_surface(parent);
4223 if (!shell_surface_is_xdg_popup(parent_shsurf) &&
4224 !shell_surface_is_xdg_surface(parent_shsurf)) {
4225 wl_resource_post_error(resource,
4226 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4227 "xdg_popup parent was invalid");
4228 return;
4229 }
4230
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05004231 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004232 parent, seat, serial, x, y);
4233 if (!shsurf) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03004234 wl_resource_post_no_memory(surface_resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004235 return;
4236 }
4237
4238 shsurf->resource =
4239 wl_resource_create(client,
4240 &xdg_popup_interface, 1, id);
4241 wl_resource_set_implementation(shsurf->resource,
4242 &xdg_popup_implementation,
4243 shsurf, shell_destroy_shell_surface);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08004244 shsurf->owner_resource = resource;
4245 wl_list_insert(&sc->surface_list,
4246 wl_resource_get_link(shsurf->resource));
Rafael Antognollie2a34552013-12-03 15:35:45 -02004247}
4248
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004249static void
4250xdg_pong(struct wl_client *client,
4251 struct wl_resource *resource, uint32_t serial)
4252{
4253 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004254
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004255 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004256}
4257
Rafael Antognollie2a34552013-12-03 15:35:45 -02004258static bool
4259shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4260{
4261 return wl_resource_instance_of(shsurf->resource,
4262 &xdg_popup_interface,
4263 &xdg_popup_implementation);
4264}
4265
4266static const struct xdg_shell_interface xdg_implementation = {
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004267 xdg_shell_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02004268 xdg_use_unstable_version,
4269 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004270 xdg_get_xdg_popup,
4271 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02004272};
4273
4274static int
4275xdg_shell_unversioned_dispatch(const void *implementation,
4276 void *_target, uint32_t opcode,
4277 const struct wl_message *message,
4278 union wl_argument *args)
4279{
4280 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004281 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004282
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08004283 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02004284 wl_resource_post_error(resource,
4285 WL_DISPLAY_ERROR_INVALID_OBJECT,
4286 "must call use_unstable_version first");
4287 return 0;
4288 }
4289
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08004290#define XDG_SERVER_VERSION 5
Rafael Antognollie2a34552013-12-03 15:35:45 -02004291
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08004292 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4293 "shell implementation doesn't match protocol version");
4294
Rafael Antognollie2a34552013-12-03 15:35:45 -02004295 if (args[0].i != XDG_SERVER_VERSION) {
4296 wl_resource_post_error(resource,
4297 WL_DISPLAY_ERROR_INVALID_OBJECT,
4298 "incompatible version, server is %d "
4299 "client wants %d",
4300 XDG_SERVER_VERSION, args[0].i);
4301 return 0;
4302 }
4303
4304 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004305 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02004306
4307 return 1;
4308}
4309
4310/* end of xdg-shell implementation */
4311/***********************************/
4312
Kristian Høgsberg07937562011-04-12 17:25:42 -04004313static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004314shell_fade(struct desktop_shell *shell, enum fade_type type);
4315
Pekka Paalanen77346a62011-11-30 16:26:35 +02004316static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004317configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004318{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004319 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004320
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004321 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004322 if (v->output == ev->output && v != ev) {
4323 weston_view_unmap(v);
4324 v->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004325 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004326 }
4327 }
4328
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004329 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004330
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004331 if (wl_list_empty(&ev->layer_link.link)) {
4332 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004333 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004334 }
4335}
4336
Pekka Paalanen8274d902014-08-06 19:36:51 +03004337static int
4338background_get_label(struct weston_surface *surface, char *buf, size_t len)
4339{
4340 return snprintf(buf, len, "background for output %s",
4341 surface->output->name);
4342}
4343
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004344static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004345background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004346{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004347 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004349
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 view = container_of(es->views.next, struct weston_view, surface_link);
4351
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004352 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004353}
4354
4355static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004356desktop_shell_set_background(struct wl_client *client,
4357 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004358 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004359 struct wl_resource *surface_resource)
4360{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004361 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004362 struct weston_surface *surface =
4363 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004364 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004365
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004366 if (surface->configure) {
4367 wl_resource_post_error(surface_resource,
4368 WL_DISPLAY_ERROR_INVALID_OBJECT,
4369 "surface role already assigned");
4370 return;
4371 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004372
Jason Ekstranda7af7042013-10-12 22:38:11 -05004373 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4374 weston_view_destroy(view);
4375 view = weston_view_create(surface);
4376
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004377 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004378 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004379 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004380 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004381 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004382 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004383 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004384 surface->output->width,
4385 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004386}
4387
Pekka Paalanen8274d902014-08-06 19:36:51 +03004388static int
4389panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4390{
4391 return snprintf(buf, len, "panel for output %s",
4392 surface->output->name);
4393}
4394
Kristian Høgsberg75840622011-09-06 13:48:16 -04004395static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004396panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004397{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004398 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004399 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004400
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 view = container_of(es->views.next, struct weston_view, surface_link);
4402
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004403 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004404}
4405
4406static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004407desktop_shell_set_panel(struct wl_client *client,
4408 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004409 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04004410 struct wl_resource *surface_resource)
4411{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004412 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004413 struct weston_surface *surface =
4414 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004416
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004417 if (surface->configure) {
4418 wl_resource_post_error(surface_resource,
4419 WL_DISPLAY_ERROR_INVALID_OBJECT,
4420 "surface role already assigned");
4421 return;
4422 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004423
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4425 weston_view_destroy(view);
4426 view = weston_view_create(surface);
4427
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004428 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004429 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004430 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004431 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004432 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004433 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004434 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004435 surface->output->width,
4436 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004437}
4438
Pekka Paalanen8274d902014-08-06 19:36:51 +03004439static int
4440lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4441{
4442 return snprintf(buf, len, "lock window");
4443}
4444
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004445static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004446lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004447{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004448 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 struct weston_view *view;
4450
4451 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004452
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004453 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004454 return;
4455
Jason Ekstranda7af7042013-10-12 22:38:11 -05004456 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004457
4458 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004459 weston_layer_entry_insert(&shell->lock_layer.view_list,
4460 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004461 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004462 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004463 }
4464}
4465
4466static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004467handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004468{
Tiago Vignattibe143262012-04-16 17:31:41 +03004469 struct desktop_shell *shell =
4470 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004471
Martin Minarik6d118362012-06-07 18:01:59 +02004472 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004473 shell->lock_surface = NULL;
4474}
4475
4476static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004477desktop_shell_set_lock_surface(struct wl_client *client,
4478 struct wl_resource *resource,
4479 struct wl_resource *surface_resource)
4480{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004481 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004482 struct weston_surface *surface =
4483 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004484
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004485 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004486
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004487 if (!shell->locked)
4488 return;
4489
Pekka Paalanen98262232011-12-01 10:42:22 +02004490 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004491
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004492 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004493 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004494 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004495
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004496 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004497 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004498 surface->configure_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004499 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004500}
4501
4502static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004503resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004504{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004505 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004506
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004507 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004508 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004509 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004510 &shell->input_panel_layer.link);
4511 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004512 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004513 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004514 wl_list_insert(&shell->compositor->cursor_layer.link,
4515 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004516 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004517 wl_list_insert(&shell->fullscreen_layer.link,
4518 &shell->panel_layer.link);
4519 wl_list_insert(&shell->panel_layer.link,
4520 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004521
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004522 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004523
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004524 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004525 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004526 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004527}
4528
4529static void
4530desktop_shell_unlock(struct wl_client *client,
4531 struct wl_resource *resource)
4532{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004533 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004534
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004535 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004536
4537 if (shell->locked)
4538 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004539}
4540
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004541static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004542desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004543 struct wl_resource *resource,
4544 struct wl_resource *surface_resource)
4545{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004546 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004547
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004548 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004549 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004550}
4551
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004552static void
4553desktop_shell_desktop_ready(struct wl_client *client,
4554 struct wl_resource *resource)
4555{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004556 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004557
4558 shell_fade_startup(shell);
4559}
4560
Jonny Lamb765760d2014-08-20 15:53:19 +02004561static void
4562desktop_shell_set_panel_position(struct wl_client *client,
4563 struct wl_resource *resource,
4564 uint32_t position)
4565{
4566 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4567
4568 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4569 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4570 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4571 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4572 wl_resource_post_error(resource,
4573 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4574 "bad position argument");
4575 return;
4576 }
4577
4578 shell->panel_position = position;
4579}
4580
Kristian Høgsberg75840622011-09-06 13:48:16 -04004581static const struct desktop_shell_interface desktop_shell_implementation = {
4582 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004583 desktop_shell_set_panel,
4584 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004585 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004586 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004587 desktop_shell_desktop_ready,
4588 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004589};
4590
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004591static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004592get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004593{
4594 struct shell_surface *shsurf;
4595
4596 shsurf = get_shell_surface(surface);
4597 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004598 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004599 return shsurf->type;
4600}
4601
Kristian Høgsberg75840622011-09-06 13:48:16 -04004602static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004603move_binding(struct weston_pointer *pointer, uint32_t time,
4604 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004605{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004606 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004607 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004608 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004609
Derek Foreman8ae2db52015-07-15 13:00:36 -05004610 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004611 return;
4612
Derek Foreman8ae2db52015-07-15 13:00:36 -05004613 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004614
Pekka Paalanen01388e22013-04-25 13:57:44 +03004615 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004616 if (surface == NULL)
4617 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004618
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004619 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004620 if (shsurf == NULL || shsurf->state.fullscreen ||
4621 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004622 return;
4623
Derek Foreman794fa0e2015-07-15 13:00:38 -05004624 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004625}
4626
4627static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004628maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4629 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004630{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004631 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004632 struct weston_surface *surface;
4633 struct shell_surface *shsurf;
4634
4635 surface = weston_surface_get_main_surface(focus);
4636 if (surface == NULL)
4637 return;
4638
4639 shsurf = get_shell_surface(surface);
4640 if (shsurf == NULL)
4641 return;
4642
4643 if (!shell_surface_is_xdg_surface(shsurf))
4644 return;
4645
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004646 shsurf->state_requested = true;
4647 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004648 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004649}
4650
4651static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004652fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4653 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004654{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004655 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004656 struct weston_surface *surface;
4657 struct shell_surface *shsurf;
4658
4659 surface = weston_surface_get_main_surface(focus);
4660 if (surface == NULL)
4661 return;
4662
4663 shsurf = get_shell_surface(surface);
4664 if (shsurf == NULL)
4665 return;
4666
4667 if (!shell_surface_is_xdg_surface(shsurf))
4668 return;
4669
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004670 shsurf->state_requested = true;
4671 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004672 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004673 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004674}
4675
4676static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004677touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01004678{
Derek Foreman362656b2014-09-04 10:23:05 -05004679 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01004680 struct weston_surface *surface;
4681 struct shell_surface *shsurf;
4682
Derek Foreman8ae2db52015-07-15 13:00:36 -05004683 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05004684 return;
4685
Derek Foreman8ae2db52015-07-15 13:00:36 -05004686 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004687 surface = weston_surface_get_main_surface(focus);
4688 if (surface == NULL)
4689 return;
4690
4691 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004692 if (shsurf == NULL || shsurf->state.fullscreen ||
4693 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004694 return;
4695
Derek Foremanb7674ae2015-07-15 13:00:37 -05004696 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01004697}
4698
4699static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004700resize_binding(struct weston_pointer *pointer, uint32_t time,
4701 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004702{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004703 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004704 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004705 uint32_t edges = 0;
4706 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004707 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004708
Derek Foreman8ae2db52015-07-15 13:00:36 -05004709 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004710 return;
4711
Derek Foreman8ae2db52015-07-15 13:00:36 -05004712 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004713
Pekka Paalanen01388e22013-04-25 13:57:44 +03004714 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004715 if (surface == NULL)
4716 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004717
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004718 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004719 if (shsurf == NULL || shsurf->state.fullscreen ||
4720 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004721 return;
4722
Jason Ekstranda7af7042013-10-12 22:38:11 -05004723 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004724 wl_fixed_to_int(pointer->grab_x),
4725 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05004726 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004727
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004728 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004729 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004730 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004731 edges |= 0;
4732 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004733 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004734
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004735 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004736 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004737 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004738 edges |= 0;
4739 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004740 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004741
Derek Foreman8fbebbd2015-07-15 13:00:40 -05004742 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004743}
4744
4745static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004746surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4747 uint32_t axis, wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004748{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004749 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004750 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004751 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004752 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004753
Pekka Paalanen01388e22013-04-25 13:57:44 +03004754 /* XXX: broken for windows containing sub-surfaces */
4755 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004756 if (surface == NULL)
4757 return;
4758
4759 shsurf = get_shell_surface(surface);
4760 if (!shsurf)
4761 return;
4762
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004764
Jason Ekstranda7af7042013-10-12 22:38:11 -05004765 if (shsurf->view->alpha > 1.0)
4766 shsurf->view->alpha = 1.0;
4767 if (shsurf->view->alpha < step)
4768 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004769
Jason Ekstranda7af7042013-10-12 22:38:11 -05004770 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004771 weston_surface_damage(surface);
4772}
4773
4774static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004775do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004776 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004777{
Derek Foreman8aeeac82015-01-30 13:24:36 -06004778 struct weston_compositor *compositor = seat->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004779 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004780 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004781
Derek Foreman7ef3bed2015-01-06 14:28:13 -06004782 if (!seat->pointer) {
4783 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4784 return;
4785 }
4786
Scott Moreauccbf29d2012-02-22 14:21:41 -07004787 wl_list_for_each(output, &compositor->output_list, link) {
4788 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004789 wl_fixed_to_double(seat->pointer->x),
4790 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004791 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004792 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004793 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004794 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004795 increment = -output->zoom.increment;
4796 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004797 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004798 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004799 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004800 else
4801 increment = 0;
4802
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004803 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004804
Scott Moreaue6603982012-06-11 13:07:51 -06004805 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004806 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004807 else if (output->zoom.level > output->zoom.max_level)
4808 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05004809
4810 if (!output->zoom.active) {
4811 if (output->zoom.level <= 0.0)
4812 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05004813 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004814 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004815
Scott Moreaue6603982012-06-11 13:07:51 -06004816 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004817
Jason Ekstranda7af7042013-10-12 22:38:11 -05004818 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004819 }
4820 }
4821}
4822
Scott Moreauccbf29d2012-02-22 14:21:41 -07004823static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004824zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4825 uint32_t axis, wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004827 do_zoom(pointer->seat, time, 0, axis, value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004828}
4829
4830static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004831zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4832 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004833{
Derek Foreman8ae2db52015-07-15 13:00:36 -05004834 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004835}
4836
4837static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004838terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4839 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004840{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004841 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004842
Daniel Stone325fc2d2012-05-30 16:31:58 +01004843 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004844}
4845
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004846static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004847rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4848 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004849{
4850 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004851 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004852 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004853 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004854 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004855
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004856 weston_pointer_move(pointer, x, y);
4857
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004858 if (!shsurf)
4859 return;
4860
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004861 cx = 0.5f * shsurf->surface->width;
4862 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004863
Daniel Stone37816df2012-05-16 18:45:18 +01004864 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4865 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004866 r = sqrtf(dx * dx + dy * dy);
4867
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004868 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004870
4871 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004872 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004873 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004874
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004875 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004876 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004877
4878 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004879 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004880 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004881 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004882 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004883
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004884 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004886 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004887 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004888 wl_list_init(&shsurf->rotation.transform.link);
4889 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004890 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004891 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004892
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004893 /* We need to adjust the position of the surface
4894 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004895 cposx = shsurf->view->geometry.x + cx;
4896 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004897 dposx = rotate->center.x - cposx;
4898 dposy = rotate->center.y - cposy;
4899 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004900 weston_view_set_position(shsurf->view,
4901 shsurf->view->geometry.x + dposx,
4902 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004903 }
4904
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004905 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004906 * lazily applies the damage due to rotation update.
4907 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004908 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004909}
4910
4911static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004912rotate_grab_button(struct weston_pointer_grab *grab,
4913 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004914{
4915 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004916 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004917 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004918 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004919 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004920
Daniel Stone4dbadb12012-05-30 16:31:51 +01004921 if (pointer->button_count == 0 &&
4922 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004923 if (shsurf)
4924 weston_matrix_multiply(&shsurf->rotation.rotation,
4925 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004926 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004927 free(rotate);
4928 }
4929}
4930
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004931static void
4932rotate_grab_cancel(struct weston_pointer_grab *grab)
4933{
4934 struct rotate_grab *rotate =
4935 container_of(grab, struct rotate_grab, base.grab);
4936
4937 shell_grab_end(&rotate->base);
4938 free(rotate);
4939}
4940
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004941static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004942 noop_grab_focus,
4943 rotate_grab_motion,
4944 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004945 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004946};
4947
4948static void
Derek Foreman74de4692015-07-15 13:00:39 -05004949surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004950{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004951 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004952 float dx, dy;
4953 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004954
Pekka Paalanen460099f2012-01-20 16:48:25 +02004955 rotate = malloc(sizeof *rotate);
4956 if (!rotate)
4957 return;
4958
Jason Ekstranda7af7042013-10-12 22:38:11 -05004959 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004960 surface->surface->width * 0.5f,
4961 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004962 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004963
Derek Foreman74de4692015-07-15 13:00:39 -05004964 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4965 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004966 r = sqrtf(dx * dx + dy * dy);
4967 if (r > 20.0f) {
4968 struct weston_matrix inverse;
4969
4970 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004971 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004972 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004973
4974 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004975 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004976 } else {
4977 weston_matrix_init(&surface->rotation.rotation);
4978 weston_matrix_init(&rotate->rotation);
4979 }
4980
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004981 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
Derek Foreman74de4692015-07-15 13:00:39 -05004982 pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004983}
4984
4985static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004986rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004987 void *data)
4988{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004989 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004990 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004991 struct shell_surface *surface;
4992
Derek Foreman8ae2db52015-07-15 13:00:36 -05004993 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004994 return;
4995
Derek Foreman8ae2db52015-07-15 13:00:36 -05004996 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004997
Pekka Paalanen01388e22013-04-25 13:57:44 +03004998 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004999 if (base_surface == NULL)
5000 return;
5001
5002 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005003 if (surface == NULL || surface->state.fullscreen ||
5004 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005005 return;
5006
Derek Foreman74de4692015-07-15 13:00:39 -05005007 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05005008}
5009
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005010/* Move all fullscreen layers down to the current workspace and hide their
5011 * black views. The surfaces' state is set to both fullscreen and lowered,
5012 * and this is reversed when such a surface is re-configured, see
5013 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5014 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005015 * lowering_output = NULL - Lower on all outputs, else only lower on the
5016 * specified output.
5017 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005018 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005019 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005020void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005021lower_fullscreen_layer(struct desktop_shell *shell,
5022 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005023{
5024 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005025 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005026
5027 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005028 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005029 &shell->fullscreen_layer.view_list.link,
5030 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005031 struct shell_surface *shsurf = get_shell_surface(view->surface);
5032
5033 if (!shsurf)
5034 continue;
5035
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005036 /* Only lower surfaces which have lowering_output as their fullscreen
5037 * output, unless a NULL output asks for lowering on all outputs.
5038 */
5039 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5040 continue;
5041
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005042 /* We can have a non-fullscreen popup for a fullscreen surface
5043 * in the fullscreen layer. */
5044 if (shsurf->state.fullscreen) {
5045 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005046 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5047 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07005048 weston_view_damage_below(shsurf->fullscreen.black_view);
5049
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005050 }
5051
5052 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005053 weston_layer_entry_remove(&view->layer_link);
5054 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005055 weston_view_damage_below(view);
5056 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005057
5058 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005059 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005060}
5061
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08005062void
Tiago Vignattibe143262012-04-16 17:31:41 +03005063activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005064 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005065{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005066 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005067 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02005068 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005069 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02005070 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005071
Pekka Paalanen01388e22013-04-25 13:57:44 +03005072 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02005073 shsurf = get_shell_surface(main_surface);
5074 assert(shsurf);
5075
5076 /* Only demote fullscreen surfaces on the output of activated shsurf.
5077 * Leave fullscreen surfaces on unrelated outputs alone. */
5078 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03005079
Daniel Stone37816df2012-05-16 18:45:18 +01005080 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005081
Jonas Ådahl8538b222012-08-29 22:13:03 +02005082 state = ensure_focus_state(shell, seat);
5083 if (state == NULL)
5084 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005085
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005086 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08005087 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04005088
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005089 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02005090 shell_configure_fullscreen(shsurf);
5091 else
Mario Kleiner492c12f2015-06-21 21:25:12 +02005092 restore_output_mode(shsurf->output);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005093
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01005094 /* Update the surface’s layer. This brings it to the top of the stacking
5095 * order as appropriate. */
5096 shell_surface_update_layer(shsurf);
5097
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005098 if (shell->focus_animation_type != ANIMATION_NONE) {
5099 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005100 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00005101 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005102}
5103
Alex Wu21858432012-04-01 20:13:08 +08005104/* no-op func for checking black surface */
5105static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005106black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08005107{
5108}
5109
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01005110static bool
Alex Wu21858432012-04-01 20:13:08 +08005111is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
5112{
5113 if (es->configure == black_surface_configure) {
5114 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005115 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08005116 return true;
5117 }
5118 return false;
5119}
5120
Kristian Høgsberg75840622011-09-06 13:48:16 -04005121static void
Neil Robertsa28c6932013-10-03 16:43:04 +01005122activate_binding(struct weston_seat *seat,
5123 struct desktop_shell *shell,
5124 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005125{
Pekka Paalanen01388e22013-04-25 13:57:44 +03005126 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005127
Alex Wu9c35e6b2012-03-05 14:13:13 +08005128 if (!focus)
5129 return;
5130
Pekka Paalanen01388e22013-04-25 13:57:44 +03005131 if (is_black_surface(focus, &main_surface))
5132 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08005133
Pekka Paalanen01388e22013-04-25 13:57:44 +03005134 main_surface = weston_surface_get_main_surface(focus);
5135 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005136 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04005137
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005138 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01005139}
5140
5141static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005142click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5143 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005144{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005145 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005146 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005147 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08005148 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005149
Derek Foreman8ae2db52015-07-15 13:00:36 -05005150 activate_binding(pointer->seat, data, pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01005151}
5152
5153static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005154touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5155 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01005156{
Derek Foreman8ae2db52015-07-15 13:00:36 -05005157 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01005158 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05005159 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08005160 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01005161
Derek Foreman8ae2db52015-07-15 13:00:36 -05005162 activate_binding(touch->seat, data, touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05005163}
5164
5165static void
Neil Robertsb4a91702014-04-09 16:33:32 +01005166unfocus_all_seats(struct desktop_shell *shell)
5167{
5168 struct weston_seat *seat, *next;
5169
5170 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5171 if (seat->keyboard == NULL)
5172 continue;
5173
5174 weston_keyboard_set_focus(seat->keyboard, NULL);
5175 }
5176}
5177
5178static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005179lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005180{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005181 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005182
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005183 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005184 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005185 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005186 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005187
5188 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005189
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005190 /* Hide all surfaces by removing the fullscreen, panel and
5191 * toplevel layers. This way nothing else can show or receive
5192 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005193
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005194 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005195 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005196 if (shell->showing_input_panels)
5197 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005198 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005199 wl_list_insert(&shell->compositor->cursor_layer.link,
5200 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005201
Derek Foreman004b4a12015-07-20 16:28:13 -05005202 weston_compositor_sleep(shell->compositor);
5203
Neil Robertsb4a91702014-04-09 16:33:32 +01005204 /* Remove the keyboard focus on all seats. This will be
5205 * restored to the workspace's saved state via
5206 * restore_focus_state when the compositor is unlocked */
5207 unfocus_all_seats(shell);
5208
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005209 /* TODO: disable bindings that should not work while locked. */
5210
5211 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005212}
5213
5214static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005215unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005216{
Pekka Paalanend81c2162011-11-16 13:47:34 +02005217 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005218 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005219 return;
5220 }
5221
5222 /* If desktop-shell client has gone away, unlock immediately. */
5223 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005224 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005225 return;
5226 }
5227
5228 if (shell->prepare_event_sent)
5229 return;
5230
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005231 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005232 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005233}
5234
5235static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005236shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005237{
5238 struct desktop_shell *shell = data;
5239
5240 shell->fade.animation = NULL;
5241
5242 switch (shell->fade.type) {
5243 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005244 weston_surface_destroy(shell->fade.view->surface);
5245 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005246 break;
5247 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005248 lock(shell);
5249 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005250 default:
5251 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005252 }
5253}
5254
Jason Ekstranda7af7042013-10-12 22:38:11 -05005255static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005256shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005257{
5258 struct weston_compositor *compositor = shell->compositor;
5259 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005260 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005261
5262 surface = weston_surface_create(compositor);
5263 if (!surface)
5264 return NULL;
5265
Jason Ekstranda7af7042013-10-12 22:38:11 -05005266 view = weston_view_create(surface);
5267 if (!view) {
5268 weston_surface_destroy(surface);
5269 return NULL;
5270 }
5271
Jason Ekstrand5c11a332013-12-04 20:32:03 -06005272 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005273 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005274 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005275 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5276 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005277 pixman_region32_init(&surface->input);
5278
Jason Ekstranda7af7042013-10-12 22:38:11 -05005279 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005280}
5281
5282static void
5283shell_fade(struct desktop_shell *shell, enum fade_type type)
5284{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005285 float tint;
5286
5287 switch (type) {
5288 case FADE_IN:
5289 tint = 0.0;
5290 break;
5291 case FADE_OUT:
5292 tint = 1.0;
5293 break;
5294 default:
5295 weston_log("shell: invalid fade type\n");
5296 return;
5297 }
5298
5299 shell->fade.type = type;
5300
Jason Ekstranda7af7042013-10-12 22:38:11 -05005301 if (shell->fade.view == NULL) {
5302 shell->fade.view = shell_fade_create_surface(shell);
5303 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005304 return;
5305
Jason Ekstranda7af7042013-10-12 22:38:11 -05005306 shell->fade.view->alpha = 1.0 - tint;
5307 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005308 }
5309
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005310 if (shell->fade.view->output == NULL) {
5311 /* If the black view gets a NULL output, we lost the
5312 * last output and we'll just cancel the fade. This
5313 * happens when you close the last window under the
5314 * X11 or Wayland backends. */
5315 shell->locked = false;
5316 weston_surface_destroy(shell->fade.view->surface);
5317 shell->fade.view = NULL;
5318 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005319 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005320 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005321 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05005322 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04005323 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005324 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08005325 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005326}
5327
5328static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005329do_shell_fade_startup(void *data)
5330{
5331 struct desktop_shell *shell = data;
5332
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005333 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005334 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005335 } else {
5336 weston_log("desktop shell: "
5337 "unexpected fade-in animation type %d\n",
5338 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005339 weston_surface_destroy(shell->fade.view->surface);
5340 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07005341 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005342}
5343
5344static void
5345shell_fade_startup(struct desktop_shell *shell)
5346{
5347 struct wl_event_loop *loop;
5348
5349 if (!shell->fade.startup_timer)
5350 return;
5351
5352 wl_event_source_remove(shell->fade.startup_timer);
5353 shell->fade.startup_timer = NULL;
5354
5355 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5356 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5357}
5358
5359static int
5360fade_startup_timeout(void *data)
5361{
5362 struct desktop_shell *shell = data;
5363
5364 shell_fade_startup(shell);
5365 return 0;
5366}
5367
5368static void
5369shell_fade_init(struct desktop_shell *shell)
5370{
5371 /* Make compositor output all black, and wait for the desktop-shell
5372 * client to signal it is ready, then fade in. The timer triggers a
5373 * fade-in, in case the desktop-shell client takes too long.
5374 */
5375
5376 struct wl_event_loop *loop;
5377
Jason Ekstranda7af7042013-10-12 22:38:11 -05005378 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005379 weston_log("%s: warning: fade surface already exists\n",
5380 __func__);
5381 return;
5382 }
5383
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03005384 if (shell->startup_animation_type == ANIMATION_NONE)
5385 return;
5386
Jason Ekstranda7af7042013-10-12 22:38:11 -05005387 shell->fade.view = shell_fade_create_surface(shell);
5388 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005389 return;
5390
Jason Ekstranda7af7042013-10-12 22:38:11 -05005391 weston_view_update_transform(shell->fade.view);
5392 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005393
5394 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5395 shell->fade.startup_timer =
5396 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5397 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5398}
5399
5400static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005401idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005402{
5403 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005404 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005405 struct weston_seat *seat;
5406
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005407 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08005408 if (seat->pointer)
5409 popup_grab_end(seat->pointer);
Jonny Lamb76cf1fe2014-08-20 11:27:10 +02005410 if (seat->touch)
5411 touch_popup_grab_end(seat->touch);
5412 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005413
5414 shell_fade(shell, FADE_OUT);
5415 /* lock() is called from shell_fade_done() */
5416}
5417
5418static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005419wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005420{
5421 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005422 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005423
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005424 unlock(shell);
5425}
5426
5427static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005428center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02005429{
Giulio Camuffob8366642013-04-25 13:57:46 +03005430 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005431 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005432
Jason Ekstranda7af7042013-10-12 22:38:11 -05005433 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03005434
5435 x = output->x + (output->width - width) / 2 - surf_x / 2;
5436 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02005437
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005438 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005439}
5440
5441static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005442weston_view_set_initial_position(struct weston_view *view,
5443 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005444{
5445 struct weston_compositor *compositor = shell->compositor;
5446 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02005447 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05005448 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005449 struct weston_output *output, *target_output = NULL;
5450 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02005451 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005452
5453 /* As a heuristic place the new window on the same output as the
5454 * pointer. Falling back to the output containing 0, 0.
5455 *
5456 * TODO: Do something clever for touch too?
5457 */
5458 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04005459 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04005460 ix = wl_fixed_to_int(seat->pointer->x);
5461 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005462 break;
5463 }
5464 }
5465
5466 wl_list_for_each(output, &compositor->output_list, link) {
5467 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5468 target_output = output;
5469 break;
5470 }
5471 }
5472
5473 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005474 weston_view_set_position(view, 10 + random() % 400,
5475 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005476 return;
5477 }
5478
5479 /* Valid range within output where the surface will still be onscreen.
5480 * If this is negative it means that the surface is bigger than
5481 * output.
5482 */
Jonny Lambd73c6942014-08-20 15:53:20 +02005483 get_output_work_area(shell, target_output, &area);
5484
Derek Foremanf814c5d2015-04-15 12:23:54 -05005485 x = area.x;
5486 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02005487 range_x = area.width - view->surface->width;
5488 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005489
Rob Bradford4cb88c72012-08-13 15:18:44 +01005490 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005491 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005492
5493 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05005494 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005495
Jason Ekstranda7af7042013-10-12 22:38:11 -05005496 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005497}
5498
5499static void
Jonny Lambd73c6942014-08-20 15:53:20 +02005500set_maximized_position(struct desktop_shell *shell,
5501 struct shell_surface *shsurf)
5502{
5503 int32_t surf_x, surf_y;
5504 pixman_rectangle32_t area;
Marek Chalupa8b771af2014-09-01 17:20:33 +02005505 pixman_box32_t *e;
Jonny Lambd73c6942014-08-20 15:53:20 +02005506
Jonny Lambd73c6942014-08-20 15:53:20 +02005507 get_output_work_area(shell, shsurf->output, &area);
Giulio Camuffo7a8d67d2015-01-09 20:10:45 +02005508 if (shsurf->has_set_geometry) {
5509 surf_x = shsurf->geometry.x;
5510 surf_y = shsurf->geometry.y;
5511 } else {
5512 surface_subsurfaces_boundingbox(shsurf->surface,
5513 &surf_x, &surf_y, NULL, NULL);
5514 }
Marek Chalupa8b771af2014-09-01 17:20:33 +02005515 e = pixman_region32_extents(&shsurf->output->region);
Jonny Lambd73c6942014-08-20 15:53:20 +02005516
5517 weston_view_set_position(shsurf->view,
Marek Chalupa8b771af2014-09-01 17:20:33 +02005518 e->x1 + area.x - surf_x,
5519 e->y1 + area.y - surf_y);
Jonny Lambd73c6942014-08-20 15:53:20 +02005520}
5521
5522static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005523map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005524 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005525{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005526 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005527 struct weston_seat *seat;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005528
Pekka Paalanen77346a62011-11-30 16:26:35 +02005529 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005530 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005531 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005532 if (shsurf->state.fullscreen) {
5533 center_on_output(shsurf->view, shsurf->fullscreen_output);
5534 shell_map_fullscreen(shsurf);
5535 } else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005536 set_maximized_position(shell, shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02005537 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005538 weston_view_set_initial_position(shsurf->view, shell);
5539 }
Juan Zhao96879df2012-02-07 08:45:41 +08005540 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005541 case SHELL_SURFACE_POPUP:
Jasper St. Pierreda6ecd02015-02-27 18:35:45 +08005542 if (shell_map_popup(shsurf) != 0)
5543 return;
Rob Bradforddb999382012-12-06 12:07:48 +00005544 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005545 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005546 weston_view_set_position(shsurf->view,
5547 shsurf->view->geometry.x + sx,
5548 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005549 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005550 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005551 default:
5552 ;
5553 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005554
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005555 /* Surface stacking order, see also activate(). */
5556 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005557
Jason Ekstranda7af7042013-10-12 22:38:11 -05005558 if (shsurf->type != SHELL_SURFACE_NONE) {
5559 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005560 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005561 shsurf->surface->output = shsurf->output;
5562 shsurf->view->output = shsurf->output;
5563 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005564 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005565
Jason Ekstranda7af7042013-10-12 22:38:11 -05005566 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005567 /* XXX: xwayland's using the same fields for transient type */
5568 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005569 if (shsurf->transient.flags ==
5570 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5571 break;
5572 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005573 if (shsurf->state.relative &&
5574 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5575 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005576 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005577 break;
5578 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005579 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005580 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005581 case SHELL_SURFACE_POPUP:
5582 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005583 default:
5584 break;
5585 }
5586
Rafael Antognolli03b16592013-12-03 15:35:42 -02005587 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5588 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005589 {
5590 switch (shell->win_animation_type) {
5591 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005592 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005593 break;
5594 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005595 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005596 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005597 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005598 default:
5599 break;
5600 }
5601 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005602}
5603
5604static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005605configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005606 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005607{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005608 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005609 struct weston_view *view;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005610
Pekka Paalanen77346a62011-11-30 16:26:35 +02005611 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005612
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005613 assert(shsurf);
5614
Rafael Antognolli03b16592013-12-03 15:35:42 -02005615 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005616 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005617 else if (shsurf->state.maximized) {
Jonny Lambd73c6942014-08-20 15:53:20 +02005618 set_maximized_position(shell, shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005619 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005620 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005621 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005622
Alex Wu4539b082012-03-01 12:57:46 +08005623 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005624 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005625 wl_list_for_each(view, &surface->views, surface_link)
5626 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005627
Rafael Antognolli03b16592013-12-03 15:35:42 -02005628 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005629 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005630 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005631}
5632
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005633static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005634shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005635{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005636 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005637 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005638 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005639
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005640 assert(shsurf);
5641
5642 shell = shsurf->shell;
5643
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005644 if (!weston_surface_is_mapped(es) &&
5645 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005646 remove_popup_grab(shsurf);
5647 }
5648
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005649 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005650 return;
5651
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005652 if (shsurf->has_next_geometry) {
5653 shsurf->geometry = shsurf->next_geometry;
5654 shsurf->has_next_geometry = false;
5655 shsurf->has_set_geometry = true;
5656 } else if (!shsurf->has_set_geometry) {
5657 surface_subsurfaces_boundingbox(shsurf->surface,
5658 &shsurf->geometry.x,
5659 &shsurf->geometry.y,
5660 &shsurf->geometry.width,
5661 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005662 }
5663
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005664 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005665 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005666 type_changed = 1;
5667 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005668
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005669 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005670 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005671 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005672 shsurf->last_width != es->width ||
5673 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005674 float from_x, from_y;
5675 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005676
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005677 if (shsurf->resize_edges) {
5678 sx = 0;
5679 sy = 0;
5680 }
5681
5682 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5683 sx = shsurf->last_width - es->width;
5684 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5685 sy = shsurf->last_height - es->height;
5686
5687 shsurf->last_width = es->width;
5688 shsurf->last_height = es->height;
5689
Jason Ekstranda7af7042013-10-12 22:38:11 -05005690 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5691 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005692 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005693 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005694 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005695 }
5696}
5697
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005698static bool
5699check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5700{
5701 struct timespec now;
5702
5703 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5704 return false;
5705
5706 /*
5707 * If the shell helper client dies before the session has been
5708 * up for roughly 30 seconds, better just make Weston shut down,
5709 * because the user likely has no way to interact with the desktop
5710 * anyway.
5711 */
5712 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5713 weston_log("Error: %s apparently cannot run at all.\n",
5714 shell->client);
5715 weston_log_continue(STAMP_SPACE "Quitting...");
5716 wl_display_terminate(shell->compositor->wl_display);
5717
5718 return true;
5719 }
5720
5721 return false;
5722}
5723
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005724static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005725
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005726static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03005727respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005728{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005729 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005730
5731 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5732 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005733 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005734 shell->child.deathstamp = time;
5735 shell->child.deathcount = 0;
5736 }
5737
5738 shell->child.deathcount++;
5739 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03005740 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005741 return;
5742 }
5743
Pekka Paalanen826dc142014-08-27 12:11:53 +03005744 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005745 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005746}
5747
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005748static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005749desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5750{
5751 struct desktop_shell *shell;
5752
5753 shell = container_of(listener, struct desktop_shell,
5754 child.client_destroy_listener);
5755
Pekka Paalanen826dc142014-08-27 12:11:53 +03005756 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005757 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005758 /*
5759 * unbind_desktop_shell() will reset shell->child.desktop_shell
5760 * before the respawned process has a chance to create a new
5761 * desktop_shell object, because we are being called from the
5762 * wl_client destructor which destroys all wl_resources before
5763 * returning.
5764 */
5765
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005766 if (!check_desktop_shell_crash_too_early(shell))
5767 respawn_desktop_shell_process(shell);
5768
Pekka Paalanen826dc142014-08-27 12:11:53 +03005769 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005770}
5771
5772static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005773launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005774{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005775 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005776
Pekka Paalanen826dc142014-08-27 12:11:53 +03005777 shell->child.client = weston_client_start(shell->compositor,
5778 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005779
Arnaud Vrac42631192014-08-25 20:56:46 +02005780 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005781 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02005782 return;
5783 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005784
5785 shell->child.client_destroy_listener.notify =
5786 desktop_shell_client_destroy;
5787 wl_client_add_destroy_listener(shell->child.client,
5788 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005789}
5790
5791static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005792handle_shell_client_destroy(struct wl_listener *listener, void *data)
5793{
5794 struct shell_client *sc =
5795 container_of(listener, struct shell_client, destroy_listener);
5796
5797 if (sc->ping_timer)
5798 wl_event_source_remove(sc->ping_timer);
5799 free(sc);
5800}
5801
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005802static struct shell_client *
5803shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5804 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005805{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005806 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005807
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005808 sc = zalloc(sizeof *sc);
5809 if (sc == NULL) {
5810 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005811 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005812 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005813
5814 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005815 if (sc->resource == NULL) {
5816 free(sc);
5817 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005818 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005819 }
5820
5821 sc->client = client;
5822 sc->shell = shell;
5823 sc->destroy_listener.notify = handle_shell_client_destroy;
5824 wl_client_add_destroy_listener(client, &sc->destroy_listener);
Jonas Ådahl49d77d22015-03-18 16:20:51 +08005825 wl_list_init(&sc->surface_list);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005826
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005827 return sc;
5828}
5829
5830static void
5831bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5832{
5833 struct desktop_shell *shell = data;
5834 struct shell_client *sc;
5835
5836 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5837 if (sc)
5838 wl_resource_set_implementation(sc->resource,
5839 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005840 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005841}
5842
5843static void
5844bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5845{
5846 struct desktop_shell *shell = data;
5847 struct shell_client *sc;
5848
5849 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5850 if (sc)
5851 wl_resource_set_dispatcher(sc->resource,
5852 xdg_shell_unversioned_dispatch,
5853 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005854}
5855
5856static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005857unbind_desktop_shell(struct wl_resource *resource)
5858{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005859 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005860
5861 if (shell->locked)
5862 resume_desktop(shell);
5863
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005864 shell->child.desktop_shell = NULL;
5865 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005866}
5867
5868static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005869bind_desktop_shell(struct wl_client *client,
5870 void *data, uint32_t version, uint32_t id)
5871{
Tiago Vignattibe143262012-04-16 17:31:41 +03005872 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005873 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005874
Jason Ekstranda85118c2013-06-27 20:17:02 -05005875 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005876 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005877
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005878 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005879 wl_resource_set_implementation(resource,
5880 &desktop_shell_implementation,
5881 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005882 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005883
5884 if (version < 2)
5885 shell_fade_startup(shell);
5886
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005887 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005888 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005889
5890 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5891 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005892}
5893
Kristian Høgsberg07045392012-02-19 18:52:44 -05005894struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005895 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005896 struct weston_surface *current;
5897 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005898 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005899 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005900};
5901
5902static void
5903switcher_next(struct switcher *switcher)
5904{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005905 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005906 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005907 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005908 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005909
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005910 /* temporary re-display minimized surfaces */
5911 struct weston_view *tmp;
5912 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005913 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5914 weston_layer_entry_remove(&view->layer_link);
5915 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005916 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5917 *minimized = view;
5918 }
5919
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005920 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005921 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005922 if (shsurf &&
5923 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5924 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005925 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005926 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005927 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005928 next = view->surface;
5929 prev = view->surface;
5930 view->alpha = 0.25;
5931 weston_view_geometry_dirty(view);
5932 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005933 }
Alex Wu1659daa2012-04-01 20:13:09 +08005934
Jason Ekstranda7af7042013-10-12 22:38:11 -05005935 if (is_black_surface(view->surface, NULL)) {
5936 view->alpha = 0.25;
5937 weston_view_geometry_dirty(view);
5938 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005939 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005940 }
5941
5942 if (next == NULL)
5943 next = first;
5944
Alex Wu07b26062012-03-12 16:06:01 +08005945 if (next == NULL)
5946 return;
5947
Kristian Høgsberg07045392012-02-19 18:52:44 -05005948 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005949 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005950
5951 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005952 wl_list_for_each(view, &next->views, surface_link)
5953 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005954
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005955 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005956 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005957 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005958}
5959
5960static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005961switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005962{
5963 struct switcher *switcher =
5964 container_of(listener, struct switcher, listener);
5965
5966 switcher_next(switcher);
5967}
5968
5969static void
Daniel Stone351eb612012-05-31 15:27:47 -04005970switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005971{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005972 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005973 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005974 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005975
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005976 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005977 if (is_focus_view(view))
5978 continue;
5979
Jason Ekstranda7af7042013-10-12 22:38:11 -05005980 view->alpha = 1.0;
5981 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005982 }
5983
Alex Wu07b26062012-03-12 16:06:01 +08005984 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005985 activate(switcher->shell, switcher->current,
Derek Foreman8aeeac82015-01-30 13:24:36 -06005986 keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005987 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005988 weston_keyboard_end_grab(keyboard);
5989 if (keyboard->input_method_resource)
5990 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005991
5992 /* re-hide surfaces that were temporary shown during the switch */
5993 struct weston_view **minimized;
5994 wl_array_for_each(minimized, &switcher->minimized_array) {
5995 /* with the exception of the current selected */
5996 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005997 weston_layer_entry_remove(&(*minimized)->layer_link);
5998 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005999 weston_view_damage_below(*minimized);
6000 }
6001 }
6002 wl_array_release(&switcher->minimized_array);
6003
Kristian Høgsberg07045392012-02-19 18:52:44 -05006004 free(switcher);
6005}
6006
6007static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006008switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01006009 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006010{
6011 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01006012 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04006013
Daniel Stonec9785ea2012-05-30 16:31:52 +01006014 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04006015 switcher_next(switcher);
6016}
6017
6018static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006019switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04006020 uint32_t mods_depressed, uint32_t mods_latched,
6021 uint32_t mods_locked, uint32_t group)
6022{
6023 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06006024 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006025
Daniel Stone351eb612012-05-31 15:27:47 -04006026 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6027 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04006028}
Kristian Høgsberg07045392012-02-19 18:52:44 -05006029
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006030static void
6031switcher_cancel(struct weston_keyboard_grab *grab)
6032{
6033 struct switcher *switcher = container_of(grab, struct switcher, grab);
6034
6035 switcher_destroy(switcher);
6036}
6037
Kristian Høgsberg29139d42013-04-18 15:25:39 -04006038static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04006039 switcher_key,
6040 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02006041 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05006042};
6043
6044static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006045switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6046 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05006047{
Tiago Vignattibe143262012-04-16 17:31:41 +03006048 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006049 struct switcher *switcher;
6050
6051 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006052 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006053 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04006054 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05006055 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01006056 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006057
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02006058 restore_all_output_modes(shell->compositor);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02006059 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006060 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05006061 weston_keyboard_start_grab(keyboard, &switcher->grab);
6062 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05006063 switcher_next(switcher);
6064}
6065
Pekka Paalanen3c647232011-12-22 13:43:43 +02006066static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006067backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6068 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006069{
6070 struct weston_compositor *compositor = data;
6071 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006072 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006073
6074 /* TODO: we're limiting to simple use cases, where we assume just
6075 * control on the primary display. We'd have to extend later if we
6076 * ever get support for setting backlights on random desktop LCD
6077 * panels though */
6078 output = get_default_output(compositor);
6079 if (!output)
6080 return;
6081
6082 if (!output->set_backlight)
6083 return;
6084
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006085 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6086 backlight_new = output->backlight_current - 25;
6087 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6088 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006089
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03006090 if (backlight_new < 5)
6091 backlight_new = 5;
6092 if (backlight_new > 255)
6093 backlight_new = 255;
6094
6095 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02006096 output->set_backlight(output, output->backlight_current);
6097}
6098
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05006099static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006100force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6101 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006102{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04006103 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006104 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006105 struct desktop_shell *shell = data;
6106 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006107 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006108
Derek Foreman8ae2db52015-07-15 13:00:36 -05006109 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02006110 if (!focus_surface)
6111 return;
6112
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006113 wl_signal_emit(&compositor->kill_signal, focus_surface);
6114
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05006115 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03006116 wl_client_get_credentials(client, &pid, NULL, NULL);
6117
6118 /* Skip clients that we launched ourselves (the credentials of
6119 * the socketpair is ours) */
6120 if (pid == getpid())
6121 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006122
Daniel Stone325fc2d2012-05-30 16:31:58 +01006123 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04006124}
6125
6126static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006127workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006128 uint32_t key, void *data)
6129{
6130 struct desktop_shell *shell = data;
6131 unsigned int new_index = shell->workspaces.current;
6132
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006133 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006134 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006135 if (new_index != 0)
6136 new_index--;
6137
6138 change_workspace(shell, new_index);
6139}
6140
6141static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006142workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006143 uint32_t key, void *data)
6144{
6145 struct desktop_shell *shell = data;
6146 unsigned int new_index = shell->workspaces.current;
6147
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006148 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006149 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006150 if (new_index < shell->workspaces.num - 1)
6151 new_index++;
6152
6153 change_workspace(shell, new_index);
6154}
6155
6156static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006157workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006158 uint32_t key, void *data)
6159{
6160 struct desktop_shell *shell = data;
6161 unsigned int new_index;
6162
Kristian Høgsbergce345b02012-06-25 21:35:29 -04006163 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04006164 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006165 new_index = key - KEY_F1;
6166 if (new_index >= shell->workspaces.num)
6167 new_index = shell->workspaces.num - 1;
6168
6169 change_workspace(shell, new_index);
6170}
6171
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006172static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006173workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6174 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006175{
6176 struct desktop_shell *shell = data;
6177 unsigned int new_index = shell->workspaces.current;
6178
6179 if (shell->locked)
6180 return;
6181
6182 if (new_index != 0)
6183 new_index--;
6184
Derek Foreman8ae2db52015-07-15 13:00:36 -05006185 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006186}
6187
6188static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05006189workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6190 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006191{
6192 struct desktop_shell *shell = data;
6193 unsigned int new_index = shell->workspaces.current;
6194
6195 if (shell->locked)
6196 return;
6197
6198 if (new_index < shell->workspaces.num - 1)
6199 new_index++;
6200
Derek Foreman8ae2db52015-07-15 13:00:36 -05006201 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006202}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006203
6204static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006205shell_reposition_view_on_output_destroy(struct weston_view *view)
6206{
6207 struct weston_output *output, *first_output;
6208 struct weston_compositor *ec = view->surface->compositor;
6209 struct shell_surface *shsurf;
6210 float x, y;
6211 int visible;
6212
6213 x = view->geometry.x;
6214 y = view->geometry.y;
6215
6216 /* At this point the destroyed output is not in the list anymore.
6217 * If the view is still visible somewhere, we leave where it is,
6218 * otherwise, move it to the first output. */
6219 visible = 0;
6220 wl_list_for_each(output, &ec->output_list, link) {
6221 if (pixman_region32_contains_point(&output->region,
6222 x, y, NULL)) {
6223 visible = 1;
6224 break;
6225 }
6226 }
6227
6228 if (!visible) {
6229 first_output = container_of(ec->output_list.next,
6230 struct weston_output, link);
6231
6232 x = first_output->x + first_output->width / 4;
6233 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08006234
6235 weston_view_set_position(view, x, y);
6236 } else {
6237 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006238 }
6239
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006240
6241 shsurf = get_shell_surface(view->surface);
6242
6243 if (shsurf) {
6244 shsurf->saved_position_valid = false;
6245 shsurf->next_state.maximized = false;
6246 shsurf->next_state.fullscreen = false;
6247 shsurf->state_changed = true;
6248 }
6249}
6250
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006251void
6252shell_for_each_layer(struct desktop_shell *shell,
6253 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006254{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006255 struct workspace **ws;
6256
6257 func(shell, &shell->fullscreen_layer, data);
6258 func(shell, &shell->panel_layer, data);
6259 func(shell, &shell->background_layer, data);
6260 func(shell, &shell->lock_layer, data);
6261 func(shell, &shell->input_panel_layer, data);
6262
6263 wl_array_for_each(ws, &shell->workspaces.array)
6264 func(shell, &(*ws)->layer, data);
6265}
6266
6267static void
6268shell_output_destroy_move_layer(struct desktop_shell *shell,
6269 struct weston_layer *layer,
6270 void *data)
6271{
6272 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006273 struct weston_view *view;
6274
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006275 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006276 if (view->output != output)
6277 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006278
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006279 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006280 }
6281}
6282
6283static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006284handle_output_destroy(struct wl_listener *listener, void *data)
6285{
6286 struct shell_output *output_listener =
6287 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006288 struct weston_output *output = output_listener->output;
6289 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08006290
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006291 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02006292
Xiong Zhang6b481422013-10-23 13:58:32 +08006293 wl_list_remove(&output_listener->destroy_listener.link);
6294 wl_list_remove(&output_listener->link);
6295 free(output_listener);
6296}
6297
6298static void
6299create_shell_output(struct desktop_shell *shell,
6300 struct weston_output *output)
6301{
6302 struct shell_output *shell_output;
6303
6304 shell_output = zalloc(sizeof *shell_output);
6305 if (shell_output == NULL)
6306 return;
6307
6308 shell_output->output = output;
6309 shell_output->shell = shell;
6310 shell_output->destroy_listener.notify = handle_output_destroy;
6311 wl_signal_add(&output->destroy_signal,
6312 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006313 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006314}
6315
6316static void
6317handle_output_create(struct wl_listener *listener, void *data)
6318{
6319 struct desktop_shell *shell =
6320 container_of(listener, struct desktop_shell, output_create_listener);
6321 struct weston_output *output = (struct weston_output *)data;
6322
6323 create_shell_output(shell, output);
6324}
6325
6326static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006327handle_output_move_layer(struct desktop_shell *shell,
6328 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006329{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006330 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006331 struct weston_view *view;
6332 float x, y;
6333
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006334 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006335 if (view->output != output)
6336 continue;
6337
6338 x = view->geometry.x + output->move_x;
6339 y = view->geometry.y + output->move_y;
6340 weston_view_set_position(view, x, y);
6341 }
6342}
6343
6344static void
6345handle_output_move(struct wl_listener *listener, void *data)
6346{
6347 struct desktop_shell *shell;
6348
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006349 shell = container_of(listener, struct desktop_shell,
6350 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006351
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006352 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006353}
6354
6355static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006356setup_output_destroy_handler(struct weston_compositor *ec,
6357 struct desktop_shell *shell)
6358{
6359 struct weston_output *output;
6360
6361 wl_list_init(&shell->output_list);
6362 wl_list_for_each(output, &ec->output_list, link)
6363 create_shell_output(shell, output);
6364
6365 shell->output_create_listener.notify = handle_output_create;
6366 wl_signal_add(&ec->output_created_signal,
6367 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006368
6369 shell->output_move_listener.notify = handle_output_move;
6370 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006371}
6372
6373static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006374shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006375{
Tiago Vignattibe143262012-04-16 17:31:41 +03006376 struct desktop_shell *shell =
6377 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006378 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006379 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006380
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006381 /* Force state to unlocked so we don't try to fade */
6382 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03006383
6384 if (shell->child.client) {
6385 /* disable respawn */
6386 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006387 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03006388 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006389
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006390 wl_list_remove(&shell->idle_listener.link);
6391 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006392
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006393 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006394 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006395
Xiong Zhang6b481422013-10-23 13:58:32 +08006396 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6397 wl_list_remove(&shell_output->destroy_listener.link);
6398 wl_list_remove(&shell_output->link);
6399 free(shell_output);
6400 }
6401
6402 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006403 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006404
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006405 wl_array_for_each(ws, &shell->workspaces.array)
6406 workspace_destroy(*ws);
6407 wl_array_release(&shell->workspaces.array);
6408
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006409 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006410 free(shell);
6411}
6412
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006413static void
6414shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6415{
6416 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006417 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006418
6419 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006420 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6421 MODIFIER_CTRL | MODIFIER_ALT,
6422 terminate_binding, ec);
6423 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6424 click_to_activate_binding,
6425 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006426 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6427 click_to_activate_binding,
6428 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006429 weston_compositor_add_touch_binding(ec, 0,
6430 touch_to_activate_binding,
6431 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006432 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6433 MODIFIER_SUPER | MODIFIER_ALT,
6434 surface_opacity_binding, NULL);
6435 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6436 MODIFIER_SUPER, zoom_axis_binding,
6437 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006438
6439 /* configurable bindings */
6440 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006441 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6442 zoom_key_binding, NULL);
6443 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6444 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006445 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6446 maximize_binding, NULL);
6447 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6448 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006449 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6450 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006451 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05006452 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01006453 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006454 weston_compositor_add_button_binding(ec, BTN_LEFT,
6455 mod | MODIFIER_SHIFT,
6456 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006457
6458 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05006459 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006460 rotate_binding, NULL);
6461
Daniel Stone325fc2d2012-05-30 16:31:58 +01006462 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6463 shell);
6464 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6465 ec);
6466 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6467 backlight_binding, ec);
6468 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6469 ec);
6470 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6471 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006472 weston_compositor_add_key_binding(ec, KEY_K, mod,
6473 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006474 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6475 workspace_up_binding, shell);
6476 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6477 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006478 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6479 workspace_move_surface_up_binding,
6480 shell);
6481 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6482 workspace_move_surface_down_binding,
6483 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006484
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006485 if (shell->exposay_modifier)
6486 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6487 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006488
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006489 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6490 if (shell->workspaces.num > 1) {
6491 num_workspace_bindings = shell->workspaces.num;
6492 if (num_workspace_bindings > 6)
6493 num_workspace_bindings = 6;
6494 for (i = 0; i < num_workspace_bindings; i++)
6495 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6496 workspace_f_binding,
6497 shell);
6498 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006499
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02006500 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006501}
6502
Jason Ekstrand024177c2014-04-21 19:42:58 -05006503static void
6504handle_seat_created(struct wl_listener *listener, void *data)
6505{
6506 struct weston_seat *seat = data;
6507
6508 create_shell_seat(seat);
6509}
6510
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006511WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006512module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006513 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006514{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006515 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006516 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006517 struct workspace **pws;
6518 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006519 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006520
Peter Huttererf3d62272013-08-08 11:57:05 +10006521 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006522 if (shell == NULL)
6523 return -1;
6524
Kristian Høgsberg75840622011-09-06 13:48:16 -04006525 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006526
6527 shell->destroy_listener.notify = shell_destroy;
6528 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006529 shell->idle_listener.notify = idle_handler;
6530 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6531 shell->wake_listener.notify = wake_handler;
6532 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006533
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006534 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006535 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006536 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006537 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006538 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006539 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006540 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006541 ec->shell_interface.move = shell_interface_move;
Derek Foreman8fbebbd2015-07-15 13:00:40 -05006542 ec->shell_interface.resize = shell_interface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006543 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006544 ec->shell_interface.set_window_geometry = set_window_geometry;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02006545 ec->shell_interface.set_maximized = shell_interface_set_maximized;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02006546 ec->shell_interface.set_pid = set_pid;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006547
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006548 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6549 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006550 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6551 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006552 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006553
6554 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006555 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006556
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006557 if (input_panel_setup(shell) < 0)
6558 return -1;
6559
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03006560 shell->text_backend = text_backend_init(ec);
6561 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00006562 return -1;
6563
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006564 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006565
Daniel Stonedf8133b2013-11-19 11:37:14 +01006566 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6567 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6568
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006569 for (i = 0; i < shell->workspaces.num; i++) {
6570 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6571 if (pws == NULL)
6572 return -1;
6573
6574 *pws = workspace_create();
6575 if (*pws == NULL)
6576 return -1;
6577 }
6578 activate_workspace(shell, 0);
6579
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006580 weston_layer_init(&shell->minimized_layer, NULL);
6581
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006582 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006583 wl_list_init(&shell->workspaces.animation.link);
6584 shell->workspaces.animation.frame = animate_workspace_change_frame;
6585
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006586 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006587 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006588 return -1;
6589
Rafael Antognollie2a34552013-12-03 15:35:45 -02006590 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6591 shell, bind_xdg_shell) == NULL)
6592 return -1;
6593
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006594 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006595 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006596 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006597 return -1;
6598
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006599 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6600 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006601 return -1;
6602
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006603 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006604
Jonny Lamb765760d2014-08-20 15:53:19 +02006605 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6606
Xiong Zhang6b481422013-10-23 13:58:32 +08006607 setup_output_destroy_handler(ec, shell);
6608
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006609 loop = wl_display_get_event_loop(ec->wl_display);
6610 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006611
Jason Ekstrand024177c2014-04-21 19:42:58 -05006612 wl_list_for_each(seat, &ec->seat_list, link)
6613 handle_seat_created(NULL, seat);
6614 shell->seat_create_listener.notify = handle_seat_created;
6615 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006616
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006617 screenshooter_create(ec);
6618
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006619 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006620
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006621 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006622
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03006623 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6624
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006625 return 0;
6626}