blob: bc4a25850fead08ce50c1b332df33c9b8be29b2f [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 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500112 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600113 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700115 struct wl_listener resource_destroy_listener;
116
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400117 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800122 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200125 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200160 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
165 struct {
166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500169 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200170 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500172
173 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200174};
175
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400177 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300178 struct shell_surface *shsurf;
179 struct wl_listener shsurf_destroy_listener;
180};
181
Rusty Lynch1084da52013-08-15 09:10:08 -0700182struct shell_touch_grab {
183 struct weston_touch_grab grab;
184 struct shell_surface *shsurf;
185 struct wl_listener shsurf_destroy_listener;
186 struct weston_touch *touch;
187};
188
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300189struct weston_move_grab {
190 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100191 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500192};
193
Rusty Lynch1084da52013-08-15 09:10:08 -0700194struct weston_touch_move_grab {
195 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800196 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700197 wl_fixed_t dx, dy;
198};
199
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300201 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500202 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200204 float x;
205 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206 } center;
207};
208
Giulio Camuffo5085a752013-03-25 21:42:45 +0100209struct shell_seat {
210 struct weston_seat *seat;
211 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500212 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213
214 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400215 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100216 struct wl_list surfaces_list;
217 struct wl_client *client;
218 int32_t initial_up;
219 } popup_grab;
220};
221
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800222struct shell_client {
223 struct wl_resource *resource;
224 struct wl_client *client;
225 struct desktop_shell *shell;
226 struct wl_listener destroy_listener;
227 struct wl_event_source *ping_timer;
228 uint32_t ping_serial;
229 int unresponsive;
230};
231
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100232void
233set_alpha_if_fullscreen(struct shell_surface *shsurf)
234{
235 if (shsurf && shsurf->state.fullscreen)
236 shsurf->fullscreen.black_view->alpha = 0.25;
237}
238
Alex Wubd3354b2012-04-17 17:20:49 +0800239static struct desktop_shell *
240shell_surface_get_shell(struct shell_surface *shsurf);
241
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500242static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400243surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500244
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300245static void
246shell_fade_startup(struct desktop_shell *shell);
247
Philip Withnallbecb77e2013-11-25 18:01:30 +0000248static struct shell_seat *
249get_shell_seat(struct weston_seat *seat);
250
Philip Withnall648a4dd2013-11-25 18:01:44 +0000251static void
252shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
253
Alex Wubd3354b2012-04-17 17:20:49 +0800254static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200255shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
256
257static bool
258shell_surface_is_xdg_surface(struct shell_surface *shsurf);
259
260static bool
261shell_surface_is_xdg_popup(struct shell_surface *shsurf);
262
263static void
264shell_surface_set_parent(struct shell_surface *shsurf,
265 struct weston_surface *parent);
266
267static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800268shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
269{
270 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800272
273 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800276 return false;
277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
279 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800280 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800282}
283
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500284static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400285destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286{
287 struct shell_grab *grab;
288
289 grab = container_of(listener, struct shell_grab,
290 shsurf_destroy_listener);
291
292 grab->shsurf = NULL;
293}
294
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800295struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296get_default_view(struct weston_surface *surface)
297{
298 struct shell_surface *shsurf;
299 struct weston_view *view;
300
301 if (!surface || wl_list_empty(&surface->views))
302 return NULL;
303
304 shsurf = get_shell_surface(surface);
305 if (shsurf)
306 return shsurf->view;
307
308 wl_list_for_each(view, &surface->views, surface_link)
309 if (weston_view_is_mapped(view))
310 return view;
311
312 return container_of(surface->views.next, struct weston_view, surface_link);
313}
314
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700335 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400336 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400337 if (shell->child.desktop_shell) {
338 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
339 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 weston_pointer_set_focus(pointer,
341 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400342 wl_fixed_from_int(0),
343 wl_fixed_from_int(0));
344 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345}
346
347static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700350 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400351 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700352 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800353 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700354 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300355
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700356 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357}
358
359static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700360shell_touch_grab_start(struct shell_touch_grab *grab,
361 const struct weston_touch_grab_interface *interface,
362 struct shell_surface *shsurf,
363 struct weston_touch *touch)
364{
365 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800366
Rusty Lynch1084da52013-08-15 09:10:08 -0700367 grab->grab.interface = interface;
368 grab->shsurf = shsurf;
369 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
370 wl_signal_add(&shsurf->destroy_signal,
371 &grab->shsurf_destroy_listener);
372
373 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700374 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700375
376 weston_touch_start_grab(touch, &grab->grab);
377 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500378 weston_touch_set_focus(touch->seat,
379 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700380}
381
382static void
383shell_touch_grab_end(struct shell_touch_grab *grab)
384{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700385 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700386 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 grab->shsurf->grabbed = 0;
388 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700389
390 weston_touch_end_grab(grab->touch);
391}
392
393static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800395 struct weston_output *output);
396
Daniel Stone496ca172012-05-30 16:31:42 +0100397static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300398get_modifier(char *modifier)
399{
400 if (!modifier)
401 return MODIFIER_SUPER;
402
403 if (!strcmp("ctrl", modifier))
404 return MODIFIER_CTRL;
405 else if (!strcmp("alt", modifier))
406 return MODIFIER_ALT;
407 else if (!strcmp("super", modifier))
408 return MODIFIER_SUPER;
409 else
410 return MODIFIER_SUPER;
411}
412
Juan Zhaoe10d2792012-04-25 19:09:52 +0800413static enum animation_type
414get_animation_type(char *animation)
415{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800416 if (!animation)
417 return ANIMATION_NONE;
418
Juan Zhaoe10d2792012-04-25 19:09:52 +0800419 if (!strcmp("zoom", animation))
420 return ANIMATION_ZOOM;
421 else if (!strcmp("fade", animation))
422 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100423 else if (!strcmp("dim-layer", animation))
424 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800425 else
426 return ANIMATION_NONE;
427}
428
Alex Wu4539b082012-03-01 12:57:46 +0800429static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400430shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200431{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400432 struct weston_config_section *section;
433 int duration;
434 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200435
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400436 section = weston_config_get_section(shell->compositor->config,
437 "screensaver", NULL, NULL);
438 weston_config_section_get_string(section,
439 "path", &shell->screensaver.path, NULL);
440 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200441 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442
443 section = weston_config_get_section(shell->compositor->config,
444 "shell", NULL, NULL);
445 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100446 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100447 shell->client = s;
448 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400449 "binding-modifier", &s, "super");
450 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200451 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800452
453 weston_config_section_get_string(section,
454 "exposay-modifier", &s, "none");
455 if (strcmp(s, "none") == 0)
456 shell->exposay_modifier = 0;
457 else
458 shell->exposay_modifier = get_modifier(s);
459 free(s);
460
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400461 weston_config_section_get_string(section, "animation", &s, "none");
462 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200463 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700464 weston_config_section_get_string(section,
465 "startup-animation", &s, "fade");
466 shell->startup_animation_type = get_animation_type(s);
467 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700468 if (shell->startup_animation_type == ANIMATION_ZOOM)
469 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100470 weston_config_section_get_string(section, "focus-animation", &s, "none");
471 shell->focus_animation_type = get_animation_type(s);
472 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 weston_config_section_get_uint(section, "num-workspaces",
474 &shell->workspaces.num,
475 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200476}
477
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800478struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100479get_default_output(struct weston_compositor *compositor)
480{
481 return container_of(compositor->output_list.next,
482 struct weston_output, link);
483}
484
485
486/* no-op func for checking focus surface */
487static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600488focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489{
490}
491
492static struct focus_surface *
493get_focus_surface(struct weston_surface *surface)
494{
495 if (surface->configure == focus_surface_configure)
496 return surface->configure_private;
497 else
498 return NULL;
499}
500
501static bool
502is_focus_surface (struct weston_surface *es)
503{
504 return (es->configure == focus_surface_configure);
505}
506
507static bool
508is_focus_view (struct weston_view *view)
509{
510 return is_focus_surface (view->surface);
511}
512
513static struct focus_surface *
514create_focus_surface(struct weston_compositor *ec,
515 struct weston_output *output)
516{
517 struct focus_surface *fsurf = NULL;
518 struct weston_surface *surface = NULL;
519
520 fsurf = malloc(sizeof *fsurf);
521 if (!fsurf)
522 return NULL;
523
524 fsurf->surface = weston_surface_create(ec);
525 surface = fsurf->surface;
526 if (surface == NULL) {
527 free(fsurf);
528 return NULL;
529 }
530
531 surface->configure = focus_surface_configure;
532 surface->output = output;
533 surface->configure_private = fsurf;
534
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800535 fsurf->view = weston_view_create(surface);
536 if (fsurf->view == NULL) {
537 weston_surface_destroy(surface);
538 free(fsurf);
539 return NULL;
540 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100541 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100542
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600543 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600544 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
546 pixman_region32_fini(&surface->opaque);
547 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
548 output->width, output->height);
549 pixman_region32_fini(&surface->input);
550 pixman_region32_init(&surface->input);
551
552 wl_list_init(&fsurf->workspace_transform.link);
553
554 return fsurf;
555}
556
557static void
558focus_surface_destroy(struct focus_surface *fsurf)
559{
560 weston_surface_destroy(fsurf->surface);
561 free(fsurf);
562}
563
564static void
565focus_animation_done(struct weston_view_animation *animation, void *data)
566{
567 struct workspace *ws = data;
568
569 ws->focus_animation = NULL;
570}
571
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200572static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200573focus_state_destroy(struct focus_state *state)
574{
575 wl_list_remove(&state->seat_destroy_listener.link);
576 wl_list_remove(&state->surface_destroy_listener.link);
577 free(state);
578}
579
580static void
581focus_state_seat_destroy(struct wl_listener *listener, void *data)
582{
583 struct focus_state *state = container_of(listener,
584 struct focus_state,
585 seat_destroy_listener);
586
587 wl_list_remove(&state->link);
588 focus_state_destroy(state);
589}
590
591static void
592focus_state_surface_destroy(struct wl_listener *listener, void *data)
593{
594 struct focus_state *state = container_of(listener,
595 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400596 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400597 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 struct weston_surface *main_surface, *next;
599 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200600
Pekka Paalanen01388e22013-04-25 13:57:44 +0300601 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
602
Kristian Høgsberge3778222012-07-31 17:29:30 -0400603 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
605 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400606 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100607 if (is_focus_view(view))
608 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400611 break;
612 }
613
Pekka Paalanen01388e22013-04-25 13:57:44 +0300614 /* if the focus was a sub-surface, activate its main surface */
615 if (main_surface != state->keyboard_focus)
616 next = main_surface;
617
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100620 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 activate(shell, next, state->seat);
622 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100623 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
624 if (state->ws->focus_animation)
625 weston_view_animation_destroy(state->ws->focus_animation);
626
627 state->ws->focus_animation = weston_fade_run(
628 state->ws->fsurf_front->view,
629 state->ws->fsurf_front->view->alpha, 0.0, 300,
630 focus_animation_done, state->ws);
631 }
632
Kristian Høgsberge3778222012-07-31 17:29:30 -0400633 wl_list_remove(&state->link);
634 focus_state_destroy(state);
635 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200636}
637
638static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400639focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200640{
Jonas Ådahl04769742012-06-13 00:01:24 +0200641 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
643 state = malloc(sizeof *state);
644 if (state == NULL)
645 return NULL;
646
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400648 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200649 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400650 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200651
652 state->seat_destroy_listener.notify = focus_state_seat_destroy;
653 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400654 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200655 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400656 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 return state;
659}
660
Jonas Ådahl8538b222012-08-29 22:13:03 +0200661static struct focus_state *
662ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
663{
664 struct workspace *ws = get_current_workspace(shell);
665 struct focus_state *state;
666
667 wl_list_for_each(state, &ws->focus_list, link)
668 if (state->seat == seat)
669 break;
670
671 if (&state->link == &ws->focus_list)
672 state = focus_state_create(seat, ws);
673
674 return state;
675}
676
Jonas Ådahl04769742012-06-13 00:01:24 +0200677static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800678focus_state_set_focus(struct focus_state *state,
679 struct weston_surface *surface)
680{
681 if (state->keyboard_focus) {
682 wl_list_remove(&state->surface_destroy_listener.link);
683 wl_list_init(&state->surface_destroy_listener.link);
684 }
685
686 state->keyboard_focus = surface;
687 if (surface)
688 wl_signal_add(&surface->destroy_signal,
689 &state->surface_destroy_listener);
690}
691
692static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400693restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200694{
695 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400696 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100697 struct wl_list pending_seat_list;
698 struct weston_seat *seat, *next_seat;
699
700 /* Temporarily steal the list of seats so that we can keep
701 * track of the seats we've already processed */
702 wl_list_init(&pending_seat_list);
703 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
704 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200705
706 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100707 wl_list_remove(&state->seat->link);
708 wl_list_insert(&shell->compositor->seat_list,
709 &state->seat->link);
710
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800711 if (state->seat->keyboard == NULL)
712 continue;
713
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400714 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200715
Kristian Høgsberge3148752013-05-06 23:19:49 -0400716 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200717 }
Neil Roberts4237f502014-04-09 16:33:31 +0100718
719 /* For any remaining seats that we don't have a focus state
720 * for we'll reset the keyboard focus to NULL */
721 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
722 wl_list_insert(&shell->compositor->seat_list, &seat->link);
723
724 if (state->seat->keyboard == NULL)
725 continue;
726
727 weston_keyboard_set_focus(seat->keyboard, NULL);
728 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200729}
730
731static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200732replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
733 struct weston_seat *seat)
734{
735 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200736
737 wl_list_for_each(state, &ws->focus_list, link) {
738 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800739 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200740 return;
741 }
742 }
743}
744
745static void
746drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
747 struct weston_surface *surface)
748{
749 struct focus_state *state;
750
751 wl_list_for_each(state, &ws->focus_list, link)
752 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800753 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200754}
755
756static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
758 struct weston_view *from, struct weston_view *to)
759{
760 struct weston_output *output;
761 bool focus_surface_created = false;
762
763 /* FIXME: Only support dim animation using two layers */
764 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
765 return;
766
767 output = get_default_output(shell->compositor);
768 if (ws->fsurf_front == NULL && (from || to)) {
769 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800770 if (ws->fsurf_front == NULL)
771 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100772 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800773
774 ws->fsurf_back = create_focus_surface(shell->compositor, output);
775 if (ws->fsurf_back == NULL) {
776 focus_surface_destroy(ws->fsurf_front);
777 return;
778 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100779 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800780
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100781 focus_surface_created = true;
782 } else {
783 wl_list_remove(&ws->fsurf_front->view->layer_link);
784 wl_list_remove(&ws->fsurf_back->view->layer_link);
785 }
786
787 if (ws->focus_animation) {
788 weston_view_animation_destroy(ws->focus_animation);
789 ws->focus_animation = NULL;
790 }
791
792 if (to)
793 wl_list_insert(&to->layer_link,
794 &ws->fsurf_front->view->layer_link);
795 else if (from)
796 wl_list_insert(&ws->layer.view_list,
797 &ws->fsurf_front->view->layer_link);
798
799 if (focus_surface_created) {
800 ws->focus_animation = weston_fade_run(
801 ws->fsurf_front->view,
802 ws->fsurf_front->view->alpha, 0.6, 300,
803 focus_animation_done, ws);
804 } else if (from) {
805 wl_list_insert(&from->layer_link,
806 &ws->fsurf_back->view->layer_link);
807 ws->focus_animation = weston_stable_fade_run(
808 ws->fsurf_front->view, 0.0,
809 ws->fsurf_back->view, 0.6,
810 focus_animation_done, ws);
811 } else if (to) {
812 wl_list_insert(&ws->layer.view_list,
813 &ws->fsurf_back->view->layer_link);
814 ws->focus_animation = weston_stable_fade_run(
815 ws->fsurf_front->view, 0.0,
816 ws->fsurf_back->view, 0.6,
817 focus_animation_done, ws);
818 }
819}
820
821static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200822workspace_destroy(struct workspace *ws)
823{
Jonas Ådahl04769742012-06-13 00:01:24 +0200824 struct focus_state *state, *next;
825
826 wl_list_for_each_safe(state, next, &ws->focus_list, link)
827 focus_state_destroy(state);
828
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100829 if (ws->fsurf_front)
830 focus_surface_destroy(ws->fsurf_front);
831 if (ws->fsurf_back)
832 focus_surface_destroy(ws->fsurf_back);
833
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200834 free(ws);
835}
836
Jonas Ådahl04769742012-06-13 00:01:24 +0200837static void
838seat_destroyed(struct wl_listener *listener, void *data)
839{
840 struct weston_seat *seat = data;
841 struct focus_state *state, *next;
842 struct workspace *ws = container_of(listener,
843 struct workspace,
844 seat_destroyed_listener);
845
846 wl_list_for_each_safe(state, next, &ws->focus_list, link)
847 if (state->seat == seat)
848 wl_list_remove(&state->link);
849}
850
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200851static struct workspace *
852workspace_create(void)
853{
854 struct workspace *ws = malloc(sizeof *ws);
855 if (ws == NULL)
856 return NULL;
857
858 weston_layer_init(&ws->layer, NULL);
859
Jonas Ådahl04769742012-06-13 00:01:24 +0200860 wl_list_init(&ws->focus_list);
861 wl_list_init(&ws->seat_destroyed_listener.link);
862 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 ws->fsurf_front = NULL;
864 ws->fsurf_back = NULL;
865 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200866
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200867 return ws;
868}
869
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200870static int
871workspace_is_empty(struct workspace *ws)
872{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500873 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200874}
875
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200876static struct workspace *
877get_workspace(struct desktop_shell *shell, unsigned int index)
878{
879 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200880 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200881 pws += index;
882 return *pws;
883}
884
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800885struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200886get_current_workspace(struct desktop_shell *shell)
887{
888 return get_workspace(shell, shell->workspaces.current);
889}
890
891static void
892activate_workspace(struct desktop_shell *shell, unsigned int index)
893{
894 struct workspace *ws;
895
896 ws = get_workspace(shell, index);
897 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
898
899 shell->workspaces.current = index;
900}
901
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200902static unsigned int
903get_output_height(struct weston_output *output)
904{
905 return abs(output->region.extents.y1 - output->region.extents.y2);
906}
907
908static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100909view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200910{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200911 struct weston_transform *transform;
912
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100913 if (is_focus_view(view)) {
914 struct focus_surface *fsurf = get_focus_surface(view->surface);
915 transform = &fsurf->workspace_transform;
916 } else {
917 struct shell_surface *shsurf = get_shell_surface(view->surface);
918 transform = &shsurf->workspace_transform;
919 }
920
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100923 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100925 weston_matrix_init(&transform->matrix);
926 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200929}
930
931static void
932workspace_translate_out(struct workspace *ws, double fraction)
933{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935 unsigned int height;
936 double d;
937
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
939 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940 d = height * fraction;
941
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100942 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200943 }
944}
945
946static void
947workspace_translate_in(struct workspace *ws, double fraction)
948{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950 unsigned int height;
951 double d;
952
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
954 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200955
956 if (fraction > 0)
957 d = -(height - height * fraction);
958 else
959 d = height + height * fraction;
960
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100961 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962 }
963}
964
965static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200966broadcast_current_workspace_state(struct desktop_shell *shell)
967{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700968 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200969
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700970 wl_resource_for_each(resource, &shell->workspaces.client_list)
971 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200972 shell->workspaces.current,
973 shell->workspaces.num);
974}
975
976static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977reverse_workspace_change_animation(struct desktop_shell *shell,
978 unsigned int index,
979 struct workspace *from,
980 struct workspace *to)
981{
982 shell->workspaces.current = index;
983
984 shell->workspaces.anim_to = to;
985 shell->workspaces.anim_from = from;
986 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
987 shell->workspaces.anim_timestamp = 0;
988
Scott Moreau4272e632012-08-13 09:58:41 -0600989 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990}
991
992static void
993workspace_deactivate_transforms(struct workspace *ws)
994{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 if (is_focus_view(view)) {
1000 struct focus_surface *fsurf = get_focus_surface(view->surface);
1001 transform = &fsurf->workspace_transform;
1002 } else {
1003 struct shell_surface *shsurf = get_shell_surface(view->surface);
1004 transform = &shsurf->workspace_transform;
1005 }
1006
1007 if (!wl_list_empty(&transform->link)) {
1008 wl_list_remove(&transform->link);
1009 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001010 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001012 }
1013}
1014
1015static void
1016finish_workspace_change_animation(struct desktop_shell *shell,
1017 struct workspace *from,
1018 struct workspace *to)
1019{
Scott Moreau4272e632012-08-13 09:58:41 -06001020 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001021
1022 wl_list_remove(&shell->workspaces.animation.link);
1023 workspace_deactivate_transforms(from);
1024 workspace_deactivate_transforms(to);
1025 shell->workspaces.anim_to = NULL;
1026
1027 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1028}
1029
1030static void
1031animate_workspace_change_frame(struct weston_animation *animation,
1032 struct weston_output *output, uint32_t msecs)
1033{
1034 struct desktop_shell *shell =
1035 container_of(animation, struct desktop_shell,
1036 workspaces.animation);
1037 struct workspace *from = shell->workspaces.anim_from;
1038 struct workspace *to = shell->workspaces.anim_to;
1039 uint32_t t;
1040 double x, y;
1041
1042 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1043 finish_workspace_change_animation(shell, from, to);
1044 return;
1045 }
1046
1047 if (shell->workspaces.anim_timestamp == 0) {
1048 if (shell->workspaces.anim_current == 0.0)
1049 shell->workspaces.anim_timestamp = msecs;
1050 else
1051 shell->workspaces.anim_timestamp =
1052 msecs -
1053 /* Invers of movement function 'y' below. */
1054 (asin(1.0 - shell->workspaces.anim_current) *
1055 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1056 M_2_PI);
1057 }
1058
1059 t = msecs - shell->workspaces.anim_timestamp;
1060
1061 /*
1062 * x = [0, π/2]
1063 * y(x) = sin(x)
1064 */
1065 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1066 y = sin(x);
1067
1068 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001069 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070
1071 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1072 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1073 shell->workspaces.anim_current = y;
1074
Scott Moreau4272e632012-08-13 09:58:41 -06001075 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001076 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001077 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001079}
1080
1081static void
1082animate_workspace_change(struct desktop_shell *shell,
1083 unsigned int index,
1084 struct workspace *from,
1085 struct workspace *to)
1086{
1087 struct weston_output *output;
1088
1089 int dir;
1090
1091 if (index > shell->workspaces.current)
1092 dir = -1;
1093 else
1094 dir = 1;
1095
1096 shell->workspaces.current = index;
1097
1098 shell->workspaces.anim_dir = dir;
1099 shell->workspaces.anim_from = from;
1100 shell->workspaces.anim_to = to;
1101 shell->workspaces.anim_current = 0.0;
1102 shell->workspaces.anim_timestamp = 0;
1103
1104 output = container_of(shell->compositor->output_list.next,
1105 struct weston_output, link);
1106 wl_list_insert(&output->animation_list,
1107 &shell->workspaces.animation.link);
1108
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001109 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001110
1111 workspace_translate_in(to, 0);
1112
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001113 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001114
Scott Moreau4272e632012-08-13 09:58:41 -06001115 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116}
1117
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001118static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001119update_workspace(struct desktop_shell *shell, unsigned int index,
1120 struct workspace *from, struct workspace *to)
1121{
1122 shell->workspaces.current = index;
1123 wl_list_insert(&from->layer.link, &to->layer.link);
1124 wl_list_remove(&from->layer.link);
1125}
1126
1127static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128change_workspace(struct desktop_shell *shell, unsigned int index)
1129{
1130 struct workspace *from;
1131 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001132 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001133
1134 if (index == shell->workspaces.current)
1135 return;
1136
1137 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001139 return;
1140
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001141 from = get_current_workspace(shell);
1142 to = get_workspace(shell, index);
1143
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144 if (shell->workspaces.anim_from == to &&
1145 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001146 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001148 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149 return;
1150 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001151
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 if (shell->workspaces.anim_to != NULL)
1153 finish_workspace_change_animation(shell,
1154 shell->workspaces.anim_from,
1155 shell->workspaces.anim_to);
1156
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001157 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001158
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001159 if (shell->focus_animation_type != ANIMATION_NONE) {
1160 wl_list_for_each(state, &from->focus_list, link)
1161 if (state->keyboard_focus)
1162 animate_focus_change(shell, from,
1163 get_default_view(state->keyboard_focus), NULL);
1164
1165 wl_list_for_each(state, &to->focus_list, link)
1166 if (state->keyboard_focus)
1167 animate_focus_change(shell, to,
1168 NULL, get_default_view(state->keyboard_focus));
1169 }
1170
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171 if (workspace_is_empty(to) && workspace_is_empty(from))
1172 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173 else
1174 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001175
1176 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001177}
1178
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001179static bool
1180workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1181{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001183 struct wl_list *e;
1184
1185 if (wl_list_empty(list))
1186 return false;
1187
1188 e = list->next;
1189
1190 if (e->next != list)
1191 return false;
1192
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001194}
1195
1196static void
Philip Withnall659163d2013-11-25 18:01:36 +00001197move_surface_to_workspace(struct desktop_shell *shell,
1198 struct shell_surface *shsurf,
1199 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001200{
1201 struct workspace *from;
1202 struct workspace *to;
1203 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001204 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001205 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206
1207 if (workspace == shell->workspaces.current)
1208 return;
1209
Philip Withnall659163d2013-11-25 18:01:36 +00001210 view = get_default_view(shsurf->surface);
1211 if (!view)
1212 return;
1213
1214 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1215
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001216 if (workspace >= shell->workspaces.num)
1217 workspace = shell->workspaces.num - 1;
1218
Jonas Ådahle9d22502012-08-29 22:13:01 +02001219 from = get_current_workspace(shell);
1220 to = get_workspace(shell, workspace);
1221
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 wl_list_remove(&view->layer_link);
1223 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001224
Philip Withnall648a4dd2013-11-25 18:01:44 +00001225 shell_surface_update_child_surface_layers(shsurf);
1226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001228 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1229 if (!seat->keyboard)
1230 continue;
1231
1232 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001234 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001235 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001236
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001238}
1239
1240static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001242 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001243 unsigned int index)
1244{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001245 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 struct shell_surface *shsurf;
1248 struct workspace *from;
1249 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001250 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251
Pekka Paalanen01388e22013-04-25 13:57:44 +03001252 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 view = get_default_view(surface);
1254 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001255 index == shell->workspaces.current ||
1256 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 return;
1258
1259 from = get_current_workspace(shell);
1260 to = get_workspace(shell, index);
1261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 wl_list_remove(&view->layer_link);
1263 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001264
Philip Withnall659163d2013-11-25 18:01:36 +00001265 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001266 if (shsurf != NULL)
1267 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001268
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270 drop_focus_state(shell, from, surface);
1271
1272 if (shell->workspaces.anim_from == to &&
1273 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001274 wl_list_remove(&to->layer.link);
1275 wl_list_insert(from->layer.link.prev, &to->layer.link);
1276
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001278 broadcast_current_workspace_state(shell);
1279
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 return;
1281 }
1282
1283 if (shell->workspaces.anim_to != NULL)
1284 finish_workspace_change_animation(shell,
1285 shell->workspaces.anim_from,
1286 shell->workspaces.anim_to);
1287
1288 if (workspace_is_empty(from) &&
1289 workspace_has_only(to, surface))
1290 update_workspace(shell, index, from, to);
1291 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001292 if (shsurf != NULL &&
1293 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294 wl_list_insert(&shell->workspaces.anim_sticky_list,
1295 &shsurf->workspace_transform.link);
1296
1297 animate_workspace_change(shell, index, from, to);
1298 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299
1300 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001301
1302 state = ensure_focus_state(shell, seat);
1303 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001304 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305}
1306
1307static void
1308workspace_manager_move_surface(struct wl_client *client,
1309 struct wl_resource *resource,
1310 struct wl_resource *surface_resource,
1311 uint32_t workspace)
1312{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001313 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001314 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001315 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001316 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001317 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318
Pekka Paalanen01388e22013-04-25 13:57:44 +03001319 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001320 shell_surface = get_shell_surface(main_surface);
1321 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001323
1324 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325}
1326
1327static const struct workspace_manager_interface workspace_manager_implementation = {
1328 workspace_manager_move_surface,
1329};
1330
1331static void
1332unbind_resource(struct wl_resource *resource)
1333{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001334 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001335}
1336
1337static void
1338bind_workspace_manager(struct wl_client *client,
1339 void *data, uint32_t version, uint32_t id)
1340{
1341 struct desktop_shell *shell = data;
1342 struct wl_resource *resource;
1343
Jason Ekstranda85118c2013-06-27 20:17:02 -05001344 resource = wl_resource_create(client,
1345 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001346
1347 if (resource == NULL) {
1348 weston_log("couldn't add workspace manager object");
1349 return;
1350 }
1351
Jason Ekstranda85118c2013-06-27 20:17:02 -05001352 wl_resource_set_implementation(resource,
1353 &workspace_manager_implementation,
1354 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001355 wl_list_insert(&shell->workspaces.client_list,
1356 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001357
1358 workspace_manager_send_state(resource,
1359 shell->workspaces.current,
1360 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001361}
1362
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001363static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001364touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1365 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1366{
1367}
1368
1369static void
1370touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1371{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001372 struct weston_touch_move_grab *move =
1373 (struct weston_touch_move_grab *) container_of(
1374 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001375
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001376 if (touch_id == 0)
1377 move->active = 0;
1378
Jonas Ådahl9484b692013-12-02 22:05:03 +01001379 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001380 shell_touch_grab_end(&move->base);
1381 free(move);
1382 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001383}
1384
1385static void
1386touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1387 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1388{
1389 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1390 struct shell_surface *shsurf = move->base.shsurf;
1391 struct weston_surface *es;
1392 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1393 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1394
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001395 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001396 return;
1397
1398 es = shsurf->surface;
1399
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001400 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001401
1402 weston_compositor_schedule_repaint(es->compositor);
1403}
1404
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001405static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001406touch_move_grab_frame(struct weston_touch_grab *grab)
1407{
1408}
1409
1410static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001411touch_move_grab_cancel(struct weston_touch_grab *grab)
1412{
1413 struct weston_touch_move_grab *move =
1414 (struct weston_touch_move_grab *) container_of(
1415 grab, struct shell_touch_grab, grab);
1416
1417 shell_touch_grab_end(&move->base);
1418 free(move);
1419}
1420
Rusty Lynch1084da52013-08-15 09:10:08 -07001421static const struct weston_touch_grab_interface touch_move_grab_interface = {
1422 touch_move_grab_down,
1423 touch_move_grab_up,
1424 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001425 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001426 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001427};
1428
1429static int
1430surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1431{
1432 struct weston_touch_move_grab *move;
1433
1434 if (!shsurf)
1435 return -1;
1436
Rafael Antognolli03b16592013-12-03 15:35:42 -02001437 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001438 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001439 if (shsurf->grabbed)
1440 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001441
1442 move = malloc(sizeof *move);
1443 if (!move)
1444 return -1;
1445
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001446 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001448 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001450 seat->touch->grab_y;
1451
1452 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1453 seat->touch);
1454
1455 return 0;
1456}
1457
1458static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001459noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001460{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001461}
1462
1463static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001464move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1465 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001466{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001467 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001468 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001469 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001470 int dx, dy;
1471
1472 weston_pointer_move(pointer, x, y);
1473 dx = wl_fixed_to_int(pointer->x + move->dx);
1474 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001475
1476 if (!shsurf)
1477 return;
1478
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001479 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001480
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001482}
1483
1484static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001485move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001486 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001487{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001488 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1489 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001490 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001491 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001492
Daniel Stone4dbadb12012-05-30 16:31:51 +01001493 if (pointer->button_count == 0 &&
1494 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001495 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001496 free(grab);
1497 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001498}
1499
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001500static void
1501move_grab_cancel(struct weston_pointer_grab *grab)
1502{
1503 struct shell_grab *shell_grab =
1504 container_of(grab, struct shell_grab, grab);
1505
1506 shell_grab_end(shell_grab);
1507 free(grab);
1508}
1509
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001510static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001511 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001512 move_grab_motion,
1513 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001514 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001515};
1516
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001517static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001518surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001519{
1520 struct weston_move_grab *move;
1521
1522 if (!shsurf)
1523 return -1;
1524
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001525 if (shsurf->grabbed)
1526 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001527 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001528 return 0;
1529
1530 move = malloc(sizeof *move);
1531 if (!move)
1532 return -1;
1533
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001535 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001537 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001538
1539 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001540 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541
1542 return 0;
1543}
1544
1545static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001546common_surface_move(struct wl_resource *resource,
1547 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001549 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001550 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001551 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001553 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001554 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001555 seat->pointer->button_count > 0 &&
1556 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001558 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001559 (surface_move(shsurf, seat) < 0))
1560 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001561 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001562 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001563 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001565 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001566 (surface_touch_move(shsurf, seat) < 0))
1567 wl_resource_post_no_memory(resource);
1568 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569}
1570
Rafael Antognollie2a34552013-12-03 15:35:45 -02001571static void
1572shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1573 struct wl_resource *seat_resource, uint32_t serial)
1574{
1575 common_surface_move(resource, seat_resource, serial);
1576}
1577
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578struct weston_resize_grab {
1579 struct shell_grab base;
1580 uint32_t edges;
1581 int32_t width, height;
1582};
1583
1584static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001585resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1586 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587{
1588 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001589 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590 struct shell_surface *shsurf = resize->base.shsurf;
1591 int32_t width, height;
1592 wl_fixed_t from_x, from_y;
1593 wl_fixed_t to_x, to_y;
1594
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001595 weston_pointer_move(pointer, x, y);
1596
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597 if (!shsurf)
1598 return;
1599
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 weston_view_from_global_fixed(shsurf->view,
1601 pointer->grab_x, pointer->grab_y,
1602 &from_x, &from_y);
1603 weston_view_from_global_fixed(shsurf->view,
1604 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605
1606 width = resize->width;
1607 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1608 width += wl_fixed_to_int(from_x - to_x);
1609 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1610 width += wl_fixed_to_int(to_x - from_x);
1611 }
1612
1613 height = resize->height;
1614 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1615 height += wl_fixed_to_int(from_y - to_y);
1616 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1617 height += wl_fixed_to_int(to_y - from_y);
1618 }
1619
1620 shsurf->client->send_configure(shsurf->surface,
1621 resize->edges, width, height);
1622}
1623
1624static void
1625send_configure(struct weston_surface *surface,
1626 uint32_t edges, int32_t width, int32_t height)
1627{
1628 struct shell_surface *shsurf = get_shell_surface(surface);
1629
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001630 assert(shsurf);
1631
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001632 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 edges, width, height);
1634}
1635
1636static const struct weston_shell_client shell_client = {
1637 send_configure
1638};
1639
1640static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001641resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001642 uint32_t time, uint32_t button, uint32_t state_w)
1643{
1644 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001645 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646 enum wl_pointer_button_state state = state_w;
1647
1648 if (pointer->button_count == 0 &&
1649 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1650 shell_grab_end(&resize->base);
1651 free(grab);
1652 }
1653}
1654
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001655static void
1656resize_grab_cancel(struct weston_pointer_grab *grab)
1657{
1658 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1659
1660 shell_grab_end(&resize->base);
1661 free(grab);
1662}
1663
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001664static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665 noop_grab_focus,
1666 resize_grab_motion,
1667 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001668 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669};
1670
Giulio Camuffob8366642013-04-25 13:57:46 +03001671/*
1672 * Returns the bounding box of a surface and all its sub-surfaces,
1673 * in the surface coordinates system. */
1674static void
1675surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1676 int32_t *y, int32_t *w, int32_t *h) {
1677 pixman_region32_t region;
1678 pixman_box32_t *box;
1679 struct weston_subsurface *subsurface;
1680
1681 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 surface->width,
1683 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001684
1685 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1686 pixman_region32_union_rect(&region, &region,
1687 subsurface->position.x,
1688 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 subsurface->surface->width,
1690 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001691 }
1692
1693 box = pixman_region32_extents(&region);
1694 if (x)
1695 *x = box->x1;
1696 if (y)
1697 *y = box->y1;
1698 if (w)
1699 *w = box->x2 - box->x1;
1700 if (h)
1701 *h = box->y2 - box->y1;
1702
1703 pixman_region32_fini(&region);
1704}
1705
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706static int
1707surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001708 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709{
1710 struct weston_resize_grab *resize;
1711
Rafael Antognolli03b16592013-12-03 15:35:42 -02001712 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713 return 0;
1714
1715 if (edges == 0 || edges > 15 ||
1716 (edges & 3) == 3 || (edges & 12) == 12)
1717 return 0;
1718
1719 resize = malloc(sizeof *resize);
1720 if (!resize)
1721 return -1;
1722
1723 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001724 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1725 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001727 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001729 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730
1731 return 0;
1732}
1733
1734static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001735common_surface_resize(struct wl_resource *resource,
1736 struct wl_resource *seat_resource, uint32_t serial,
1737 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001738{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001739 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001740 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001741 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742
Rafael Antognolli03b16592013-12-03 15:35:42 -02001743 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744 return;
1745
Kristian Høgsberge3148752013-05-06 23:19:49 -04001746 if (seat->pointer->button_count == 0 ||
1747 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001748 seat->pointer->focus == NULL)
1749 return;
1750
1751 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1752 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753 return;
1754
Kristian Høgsberge3148752013-05-06 23:19:49 -04001755 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001756 wl_resource_post_no_memory(resource);
1757}
1758
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001759static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001760shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1761 struct wl_resource *seat_resource, uint32_t serial,
1762 uint32_t edges)
1763{
1764 common_surface_resize(resource, seat_resource, serial, edges);
1765}
1766
1767static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001768busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001769{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001770 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001771 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001773 wl_fixed_t sx, sy;
1774
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 view = weston_compositor_pick_view(pointer->seat->compositor,
1776 pointer->x, pointer->y,
1777 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001778
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001779 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1780 shell_grab_end(grab);
1781 free(grab);
1782 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001783}
1784
1785static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001786busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1787 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001788{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001789 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001791
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001793busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794 uint32_t time, uint32_t button, uint32_t state)
1795{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001796 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001797 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001798 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001799
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001800 if (shsurf && button == BTN_LEFT && state) {
1801 activate(shsurf->shell, shsurf->surface, seat);
1802 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001803 } else if (shsurf && button == BTN_RIGHT && state) {
1804 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001805 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001806 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001807}
1808
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001809static void
1810busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1811{
1812 struct shell_grab *grab = (struct shell_grab *) base;
1813
1814 shell_grab_end(grab);
1815 free(grab);
1816}
1817
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001818static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001819 busy_cursor_grab_focus,
1820 busy_cursor_grab_motion,
1821 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001822 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001823};
1824
1825static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001826set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827{
1828 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001830 if (pointer->grab->interface == &busy_cursor_grab_interface)
1831 return;
1832
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001833 grab = malloc(sizeof *grab);
1834 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001835 return;
1836
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001837 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1838 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001839 /* Mark the shsurf as ungrabbed so that button binding is able
1840 * to move it. */
1841 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001843
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001844static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001845end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001847 struct shell_grab *grab;
1848 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001850 wl_list_for_each(seat, &compositor->seat_list, link) {
1851 if (seat->pointer == NULL)
1852 continue;
1853
1854 grab = (struct shell_grab *) seat->pointer->grab;
1855 if (grab->grab.interface == &busy_cursor_grab_interface &&
1856 wl_resource_get_client(grab->shsurf->resource) == client) {
1857 shell_grab_end(grab);
1858 free(grab);
1859 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001860 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001861}
1862
Scott Moreau9521d5e2012-04-19 13:06:17 -06001863static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001864handle_shell_client_destroy(struct wl_listener *listener, void *data);
1865
1866static int
1867xdg_ping_timeout_handler(void *data)
1868{
1869 struct shell_client *sc = data;
1870 struct weston_seat *seat;
1871 struct shell_surface *shsurf;
1872
1873 /* Client is not responding */
1874 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001875 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1876 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1877 continue;
1878 if (seat->pointer->focus->surface->resource == NULL)
1879 continue;
1880
1881 shsurf = get_shell_surface(seat->pointer->focus->surface);
1882 if (shsurf &&
1883 wl_resource_get_client(shsurf->resource) == sc->client)
1884 set_busy_cursor(shsurf, seat->pointer);
1885 }
1886
1887 return 1;
1888}
1889
1890static void
1891handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1892{
1893 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001894 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001895 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001896 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001897
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001898 if (sc->unresponsive) {
1899 xdg_ping_timeout_handler(sc);
1900 return;
1901 }
1902
1903 sc->ping_serial = serial;
1904 loop = wl_display_get_event_loop(compositor->wl_display);
1905 if (sc->ping_timer == NULL)
1906 sc->ping_timer =
1907 wl_event_loop_add_timer(loop,
1908 xdg_ping_timeout_handler, sc);
1909 if (sc->ping_timer == NULL)
1910 return;
1911
1912 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1913
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001914 if (shell_surface_is_xdg_surface(shsurf) ||
1915 shell_surface_is_xdg_popup(shsurf))
1916 xdg_shell_send_ping(sc->resource, serial);
1917 else if (shell_surface_is_wl_shell_surface(shsurf))
1918 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001919}
1920
Scott Moreauff1db4a2012-04-17 19:06:18 -06001921static void
1922ping_handler(struct weston_surface *surface, uint32_t serial)
1923{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001924 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001925
1926 if (!shsurf)
1927 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001928 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001929 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001930 if (shsurf->surface == shsurf->shell->grab_surface)
1931 return;
1932
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001933 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001934}
1935
1936static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001937handle_pointer_focus(struct wl_listener *listener, void *data)
1938{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001939 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001940 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001941 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001942 uint32_t serial;
1943
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001945 return;
1946
Jason Ekstranda7af7042013-10-12 22:38:11 -05001947 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001948 serial = wl_display_next_serial(compositor->wl_display);
1949 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001950}
1951
1952static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001953create_pointer_focus_listener(struct weston_seat *seat)
1954{
1955 struct wl_listener *listener;
1956
Kristian Høgsberge3148752013-05-06 23:19:49 -04001957 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001958 return;
1959
1960 listener = malloc(sizeof *listener);
1961 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001962 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001963}
1964
1965static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001966shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1967{
1968 if (--shsurf->focus_count == 0)
1969 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001970 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001971}
1972
1973static void
1974shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1975{
1976 if (shsurf->focus_count++ == 0)
1977 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001978 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001979}
1980
1981static void
1982handle_keyboard_focus(struct wl_listener *listener, void *data)
1983{
1984 struct weston_keyboard *keyboard = data;
1985 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1986
1987 if (seat->focused_surface) {
1988 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1989 if (shsurf)
1990 shell_surface_lose_keyboard_focus(shsurf);
1991 }
1992
1993 seat->focused_surface = keyboard->focus;
1994
1995 if (seat->focused_surface) {
1996 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1997 if (shsurf)
1998 shell_surface_gain_keyboard_focus(shsurf);
1999 }
2000}
2001
2002static void
2003create_keyboard_focus_listener(struct weston_seat *seat)
2004{
2005 struct wl_listener *listener;
2006
2007 if (!seat->keyboard)
2008 return;
2009
2010 listener = malloc(sizeof *listener);
2011 listener->notify = handle_keyboard_focus;
2012 wl_signal_add(&seat->keyboard->focus_signal, listener);
2013}
2014
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002015static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002016shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002017{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002018 if (sc->ping_serial != serial)
2019 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002020
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002021 sc->unresponsive = 0;
2022 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002023
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002024 if (sc->ping_timer) {
2025 wl_event_source_remove(sc->ping_timer);
2026 sc->ping_timer = NULL;
2027 }
2028
2029}
2030
2031static void
2032shell_surface_pong(struct wl_client *client,
2033 struct wl_resource *resource, uint32_t serial)
2034{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002035 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2036 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002037
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002038 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002039}
2040
2041static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002042set_title(struct shell_surface *shsurf, const char *title)
2043{
2044 free(shsurf->title);
2045 shsurf->title = strdup(title);
2046}
2047
2048static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002049shell_surface_set_title(struct wl_client *client,
2050 struct wl_resource *resource, const char *title)
2051{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002052 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002053
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002054 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002055}
2056
2057static void
2058shell_surface_set_class(struct wl_client *client,
2059 struct wl_resource *resource, const char *class)
2060{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002061 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002062
2063 free(shsurf->class);
2064 shsurf->class = strdup(class);
2065}
2066
Alex Wu4539b082012-03-01 12:57:46 +08002067static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002068restore_output_mode(struct weston_output *output)
2069{
Hardening57388e42013-09-18 23:56:36 +02002070 if (output->current_mode != output->original_mode ||
2071 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002072 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002073 output->original_mode,
2074 output->original_scale,
2075 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002076}
2077
2078static void
2079restore_all_output_modes(struct weston_compositor *compositor)
2080{
2081 struct weston_output *output;
2082
2083 wl_list_for_each(output, &compositor->output_list, link)
2084 restore_output_mode(output);
2085}
2086
Philip Withnallbecb77e2013-11-25 18:01:30 +00002087static int
2088get_output_panel_height(struct desktop_shell *shell,
2089 struct weston_output *output)
2090{
2091 struct weston_view *view;
2092 int panel_height = 0;
2093
2094 if (!output)
2095 return 0;
2096
2097 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2098 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002099 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002100 break;
2101 }
2102 }
2103
2104 return panel_height;
2105}
2106
Philip Withnall07926d92013-11-25 18:01:40 +00002107/* The surface will be inserted into the list immediately after the link
2108 * returned by this function (i.e. will be stacked immediately above the
2109 * returned link). */
2110static struct wl_list *
2111shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2112{
2113 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002114 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002115
2116 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002117 case SHELL_SURFACE_POPUP:
2118 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002119 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002120 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002121 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002122 /* Move the surface to its parent layer so
2123 * that surfaces which are transient for
2124 * fullscreen surfaces don't get hidden by the
2125 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002126
2127 /* TODO: Handle a parent with multiple views */
2128 parent = get_default_view(shsurf->parent);
2129 if (parent)
2130 return parent->layer_link.prev;
2131 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002132 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002133
2134 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002135 return &shsurf->shell->fullscreen_layer.view_list;
2136
Philip Withnall07926d92013-11-25 18:01:40 +00002137 case SHELL_SURFACE_NONE:
2138 default:
2139 /* Go to the fallback, below. */
2140 break;
2141 }
2142
2143 /* Move the surface to a normal workspace layer so that surfaces
2144 * which were previously fullscreen or transient are no longer
2145 * rendered on top. */
2146 ws = get_current_workspace(shsurf->shell);
2147 return &ws->layer.view_list;
2148}
2149
Philip Withnall648a4dd2013-11-25 18:01:44 +00002150static void
2151shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2152{
2153 struct shell_surface *child;
2154
2155 /* Move the child layers to the same workspace as shsurf. They will be
2156 * stacked above shsurf. */
2157 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2158 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002159 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002160 weston_view_geometry_dirty(child->view);
2161 wl_list_remove(&child->view->layer_link);
2162 wl_list_insert(shsurf->view->layer_link.prev,
2163 &child->view->layer_link);
2164 weston_view_geometry_dirty(child->view);
2165 weston_surface_damage(child->surface);
2166
2167 /* Recurse. We don’t expect this to recurse very far (if
2168 * at all) because that would imply we have transient
2169 * (or popup) children of transient surfaces, which
2170 * would be unusual. */
2171 shell_surface_update_child_surface_layers(child);
2172 }
2173 }
2174}
2175
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002176/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002177 * geometry to ensure the changes are redrawn.
2178 *
2179 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2180 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002181static void
2182shell_surface_update_layer(struct shell_surface *shsurf)
2183{
2184 struct wl_list *new_layer_link;
2185
2186 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2187
2188 if (new_layer_link == &shsurf->view->layer_link)
2189 return;
2190
2191 weston_view_geometry_dirty(shsurf->view);
2192 wl_list_remove(&shsurf->view->layer_link);
2193 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2194 weston_view_geometry_dirty(shsurf->view);
2195 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002196
2197 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002198}
2199
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002200static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002201shell_surface_set_parent(struct shell_surface *shsurf,
2202 struct weston_surface *parent)
2203{
2204 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002205
2206 wl_list_remove(&shsurf->children_link);
2207 wl_list_init(&shsurf->children_link);
2208
2209 /* Insert into the parent surface’s child list. */
2210 if (parent != NULL) {
2211 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2212 if (parent_shsurf != NULL)
2213 wl_list_insert(&parent_shsurf->children_list,
2214 &shsurf->children_link);
2215 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002216}
2217
2218static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002219shell_surface_set_output(struct shell_surface *shsurf,
2220 struct weston_output *output)
2221{
2222 struct weston_surface *es = shsurf->surface;
2223
2224 /* get the default output, if the client set it as NULL
2225 check whether the ouput is available */
2226 if (output)
2227 shsurf->output = output;
2228 else if (es->output)
2229 shsurf->output = es->output;
2230 else
2231 shsurf->output = get_default_output(es->compositor);
2232}
2233
2234static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002235surface_clear_next_states(struct shell_surface *shsurf)
2236{
2237 shsurf->next_state.maximized = false;
2238 shsurf->next_state.fullscreen = false;
2239
2240 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2241 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2242 shsurf->state_changed = true;
2243}
2244
2245static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002246set_toplevel(struct shell_surface *shsurf)
2247{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002248 shell_surface_set_parent(shsurf, NULL);
2249 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002250 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002251
2252 /* The layer_link is updated in set_surface_type(),
2253 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002254}
2255
2256static void
2257shell_surface_set_toplevel(struct wl_client *client,
2258 struct wl_resource *resource)
2259{
2260 struct shell_surface *surface = wl_resource_get_user_data(resource);
2261
2262 set_toplevel(surface);
2263}
2264
2265static void
2266set_transient(struct shell_surface *shsurf,
2267 struct weston_surface *parent, int x, int y, uint32_t flags)
2268{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002269 assert(parent != NULL);
2270
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002271 shell_surface_set_parent(shsurf, parent);
2272
2273 surface_clear_next_states(shsurf);
2274
Philip Withnallbecb77e2013-11-25 18:01:30 +00002275 shsurf->transient.x = x;
2276 shsurf->transient.y = y;
2277 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002278
Rafael Antognollied207b42013-12-03 15:35:43 -02002279 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002280 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002281 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002282
2283 /* The layer_link is updated in set_surface_type(),
2284 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002285}
2286
2287static void
2288shell_surface_set_transient(struct wl_client *client,
2289 struct wl_resource *resource,
2290 struct wl_resource *parent_resource,
2291 int x, int y, uint32_t flags)
2292{
2293 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2294 struct weston_surface *parent =
2295 wl_resource_get_user_data(parent_resource);
2296
2297 set_transient(shsurf, parent, x, y, flags);
2298}
2299
2300static void
2301set_fullscreen(struct shell_surface *shsurf,
2302 uint32_t method,
2303 uint32_t framerate,
2304 struct weston_output *output)
2305{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002306 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002307
2308 shsurf->fullscreen_output = shsurf->output;
2309 shsurf->fullscreen.type = method;
2310 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002311
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002312 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002313
2314 shsurf->client->send_configure(shsurf->surface, 0,
2315 shsurf->output->width,
2316 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002317
2318 /* The layer_link is updated in set_surface_type(),
2319 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002320}
2321
2322static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002323weston_view_set_initial_position(struct weston_view *view,
2324 struct desktop_shell *shell);
2325
2326static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002327unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002328{
Philip Withnallf85fe842013-11-25 18:01:37 +00002329 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002330 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2331 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002332 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002333 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002334
Alex Wu4539b082012-03-01 12:57:46 +08002335 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2336 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002337
Alex Wu4539b082012-03-01 12:57:46 +08002338 wl_list_remove(&shsurf->fullscreen.transform.link);
2339 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002340
Jason Ekstranda7af7042013-10-12 22:38:11 -05002341 if (shsurf->fullscreen.black_view)
2342 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2343 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002344
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002345 if (shsurf->saved_position_valid)
2346 weston_view_set_position(shsurf->view,
2347 shsurf->saved_x, shsurf->saved_y);
2348 else
2349 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2350
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002351 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002352 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002354 shsurf->saved_rotation_valid = false;
2355 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002356
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002357 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002358}
2359
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002360static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361shell_surface_set_fullscreen(struct wl_client *client,
2362 struct wl_resource *resource,
2363 uint32_t method,
2364 uint32_t framerate,
2365 struct wl_resource *output_resource)
2366{
2367 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2368 struct weston_output *output;
2369
2370 if (output_resource)
2371 output = wl_resource_get_user_data(output_resource);
2372 else
2373 output = NULL;
2374
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002375 shell_surface_set_parent(shsurf, NULL);
2376
Rafael Antognolli03b16592013-12-03 15:35:42 -02002377 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002378 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002379
2380 shsurf->next_state.fullscreen = true;
2381 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002382}
2383
2384static void
2385set_popup(struct shell_surface *shsurf,
2386 struct weston_surface *parent,
2387 struct weston_seat *seat,
2388 uint32_t serial,
2389 int32_t x,
2390 int32_t y)
2391{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002392 assert(parent != NULL);
2393
Philip Withnallbecb77e2013-11-25 18:01:30 +00002394 shsurf->popup.shseat = get_shell_seat(seat);
2395 shsurf->popup.serial = serial;
2396 shsurf->popup.x = x;
2397 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002398
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002399 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002400}
2401
2402static void
2403shell_surface_set_popup(struct wl_client *client,
2404 struct wl_resource *resource,
2405 struct wl_resource *seat_resource,
2406 uint32_t serial,
2407 struct wl_resource *parent_resource,
2408 int32_t x, int32_t y, uint32_t flags)
2409{
2410 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002411 struct weston_surface *parent =
2412 wl_resource_get_user_data(parent_resource);
2413
2414 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002415
Rafael Antognolli03b16592013-12-03 15:35:42 -02002416 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002418 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002419 wl_resource_get_user_data(seat_resource),
2420 serial, x, y);
2421}
2422
2423static void
2424set_maximized(struct shell_surface *shsurf,
2425 struct weston_output *output)
2426{
2427 struct desktop_shell *shell;
2428 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002429
Philip Withnall352e7ed2013-11-25 18:01:35 +00002430 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002431
2432 shell = shell_surface_get_shell(shsurf);
2433 panel_height = get_output_panel_height(shell, shsurf->output);
2434 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2435
2436 shsurf->client->send_configure(shsurf->surface, edges,
2437 shsurf->output->width,
2438 shsurf->output->height - panel_height);
2439
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002440 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002441}
2442
2443static void
2444unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002445{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002446 /* undo all maximized things here */
2447 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002448
2449 if (shsurf->saved_position_valid)
2450 weston_view_set_position(shsurf->view,
2451 shsurf->saved_x, shsurf->saved_y);
2452 else
2453 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002454
2455 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002456 wl_list_insert(&shsurf->view->geometry.transformation_list,
2457 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002458 shsurf->saved_rotation_valid = false;
2459 }
2460
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002461 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002462}
2463
Philip Withnallbecb77e2013-11-25 18:01:30 +00002464static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002465set_minimized(struct weston_surface *surface, uint32_t is_true)
2466{
2467 struct shell_surface *shsurf;
2468 struct workspace *current_ws;
2469 struct weston_seat *seat;
2470 struct weston_surface *focus;
2471 struct weston_view *view;
2472
2473 view = get_default_view(surface);
2474 if (!view)
2475 return;
2476
2477 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2478
2479 shsurf = get_shell_surface(surface);
2480 current_ws = get_current_workspace(shsurf->shell);
2481
2482 wl_list_remove(&view->layer_link);
2483 /* hide or show, depending on the state */
2484 if (is_true) {
2485 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2486
2487 drop_focus_state(shsurf->shell, current_ws, view->surface);
2488 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2489 if (!seat->keyboard)
2490 continue;
2491 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2492 if (focus == view->surface)
2493 weston_keyboard_set_focus(seat->keyboard, NULL);
2494 }
2495 }
2496 else {
2497 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2498
2499 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2500 if (!seat->keyboard)
2501 continue;
2502 activate(shsurf->shell, view->surface, seat);
2503 }
2504 }
2505
2506 shell_surface_update_child_surface_layers(shsurf);
2507
2508 weston_view_damage_below(view);
2509}
2510
2511static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512shell_surface_set_maximized(struct wl_client *client,
2513 struct wl_resource *resource,
2514 struct wl_resource *output_resource)
2515{
2516 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2517 struct weston_output *output;
2518
2519 if (output_resource)
2520 output = wl_resource_get_user_data(output_resource);
2521 else
2522 output = NULL;
2523
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002524 shell_surface_set_parent(shsurf, NULL);
2525
Rafael Antognolli03b16592013-12-03 15:35:42 -02002526 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002527 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002528
2529 shsurf->next_state.maximized = true;
2530 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002531}
2532
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002533/* This is only ever called from set_surface_type(), so there’s no need to
2534 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002535static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002536reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002537{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002538 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002539 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002540 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002542
Pekka Paalanen98262232011-12-01 10:42:22 +02002543 return 0;
2544}
2545
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002546static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002547set_full_output(struct shell_surface *shsurf)
2548{
2549 shsurf->saved_x = shsurf->view->geometry.x;
2550 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002551 shsurf->saved_width = shsurf->surface->width;
2552 shsurf->saved_height = shsurf->surface->height;
2553 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002554 shsurf->saved_position_valid = true;
2555
2556 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2557 wl_list_remove(&shsurf->rotation.transform.link);
2558 wl_list_init(&shsurf->rotation.transform.link);
2559 weston_view_geometry_dirty(shsurf->view);
2560 shsurf->saved_rotation_valid = true;
2561 }
2562}
2563
2564static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002565set_surface_type(struct shell_surface *shsurf)
2566{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002567 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002569
Philip Withnallbecb77e2013-11-25 18:01:30 +00002570 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002571
Rafael Antognolli03b16592013-12-03 15:35:42 -02002572 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002573 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002574
2575 switch (shsurf->type) {
2576 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002577 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002578 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002579 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002580 weston_view_set_position(shsurf->view,
2581 pev->geometry.x + shsurf->transient.x,
2582 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002583 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002584 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002585
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002586 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002587 weston_view_set_position(shsurf->view, shsurf->transient.x,
2588 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002589 break;
2590
Philip Withnall0f640e22013-11-25 18:01:31 +00002591 case SHELL_SURFACE_POPUP:
2592 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002593 default:
2594 break;
2595 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002596
2597 /* Update the surface’s layer. */
2598 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002599}
2600
Tiago Vignattibe143262012-04-16 17:31:41 +03002601static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002602shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002603{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002604 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002605}
2606
Alex Wu21858432012-04-01 20:13:08 +08002607static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002608black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002609
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002611create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002612 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002613 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002614{
2615 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002617
2618 surface = weston_surface_create(ec);
2619 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002620 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002621 return NULL;
2622 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002623 view = weston_view_create(surface);
2624 if (surface == NULL) {
2625 weston_log("no memory\n");
2626 weston_surface_destroy(surface);
2627 return NULL;
2628 }
Alex Wu4539b082012-03-01 12:57:46 +08002629
Alex Wu21858432012-04-01 20:13:08 +08002630 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002631 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002632 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002633 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002634 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002635 pixman_region32_fini(&surface->input);
2636 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002637
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002638 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002639 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002640
2641 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002642}
2643
Philip Withnalled8f1a92013-11-25 18:01:43 +00002644static void
2645shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2646{
2647 struct weston_output *output = shsurf->fullscreen_output;
2648
Rafael Antognolli03b16592013-12-03 15:35:42 -02002649 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002650
2651 if (!shsurf->fullscreen.black_view)
2652 shsurf->fullscreen.black_view =
2653 create_black_surface(shsurf->surface->compositor,
2654 shsurf->surface,
2655 output->x, output->y,
2656 output->width,
2657 output->height);
2658
2659 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2660 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2661 wl_list_insert(&shsurf->view->layer_link,
2662 &shsurf->fullscreen.black_view->layer_link);
2663 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2664 weston_surface_damage(shsurf->surface);
2665}
2666
Alex Wu4539b082012-03-01 12:57:46 +08002667/* Create black surface and append it to the associated fullscreen surface.
2668 * Handle size dismatch and positioning according to the method. */
2669static void
2670shell_configure_fullscreen(struct shell_surface *shsurf)
2671{
2672 struct weston_output *output = shsurf->fullscreen_output;
2673 struct weston_surface *surface = shsurf->surface;
2674 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002675 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002676 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002677
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002678 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2679 restore_output_mode(output);
2680
Philip Withnalled8f1a92013-11-25 18:01:43 +00002681 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002682
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002684 &surf_width, &surf_height);
2685
Alex Wu4539b082012-03-01 12:57:46 +08002686 switch (shsurf->fullscreen.type) {
2687 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002688 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002690 break;
2691 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002692 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002693 if (output->width == surf_width &&
2694 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 weston_view_set_position(shsurf->view,
2696 output->x - surf_x,
2697 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002698 break;
2699 }
2700
Alex Wu4539b082012-03-01 12:57:46 +08002701 matrix = &shsurf->fullscreen.transform.matrix;
2702 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002703
Scott Moreau1bad5db2012-08-18 01:04:05 -06002704 output_aspect = (float) output->width /
2705 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002706 /* XXX: Use surf_width and surf_height here? */
2707 surface_aspect = (float) surface->width /
2708 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002709 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002710 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002711 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002712 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002713 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002714 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002715
Alex Wu4539b082012-03-01 12:57:46 +08002716 weston_matrix_scale(matrix, scale, scale, 1);
2717 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002718 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002719 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002720 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2721 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002723
Alex Wu4539b082012-03-01 12:57:46 +08002724 break;
2725 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002726 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002727 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002728 surf_width * surface->buffer_viewport.buffer.scale,
2729 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002730 shsurf->fullscreen.framerate};
2731
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002732 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002733 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734 weston_view_set_position(shsurf->view,
2735 output->x - surf_x,
2736 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002737 shsurf->fullscreen.black_view->surface->width = output->width;
2738 shsurf->fullscreen.black_view->surface->height = output->height;
2739 weston_view_set_position(shsurf->fullscreen.black_view,
2740 output->x - surf_x,
2741 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002742 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002743 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002744 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002746 }
Alex Wubd3354b2012-04-17 17:20:49 +08002747 }
Alex Wu4539b082012-03-01 12:57:46 +08002748 break;
2749 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002751 break;
2752 default:
2753 break;
2754 }
2755}
2756
Alex Wu4539b082012-03-01 12:57:46 +08002757static void
2758shell_map_fullscreen(struct shell_surface *shsurf)
2759{
Alex Wubd3354b2012-04-17 17:20:49 +08002760 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002761}
2762
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002763static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002764set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2765{
2766 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002767 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002768 shsurf->transient.x = x;
2769 shsurf->transient.y = y;
2770 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002771
2772 shell_surface_set_parent(shsurf, NULL);
2773
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002774 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002775 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002776}
2777
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002778static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002779
2780static void
2781destroy_shell_seat(struct wl_listener *listener, void *data)
2782{
2783 struct shell_seat *shseat =
2784 container_of(listener,
2785 struct shell_seat, seat_destroy_listener);
2786 struct shell_surface *shsurf, *prev = NULL;
2787
2788 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002789 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002790 shseat->popup_grab.client = NULL;
2791
2792 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2793 shsurf->popup.shseat = NULL;
2794 if (prev) {
2795 wl_list_init(&prev->popup.grab_link);
2796 }
2797 prev = shsurf;
2798 }
2799 wl_list_init(&prev->popup.grab_link);
2800 }
2801
2802 wl_list_remove(&shseat->seat_destroy_listener.link);
2803 free(shseat);
2804}
2805
2806static struct shell_seat *
2807create_shell_seat(struct weston_seat *seat)
2808{
2809 struct shell_seat *shseat;
2810
2811 shseat = calloc(1, sizeof *shseat);
2812 if (!shseat) {
2813 weston_log("no memory to allocate shell seat\n");
2814 return NULL;
2815 }
2816
2817 shseat->seat = seat;
2818 wl_list_init(&shseat->popup_grab.surfaces_list);
2819
2820 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2821 wl_signal_add(&seat->destroy_signal,
2822 &shseat->seat_destroy_listener);
2823
2824 return shseat;
2825}
2826
2827static struct shell_seat *
2828get_shell_seat(struct weston_seat *seat)
2829{
2830 struct wl_listener *listener;
2831
2832 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2833 if (listener == NULL)
2834 return create_shell_seat(seat);
2835
2836 return container_of(listener,
2837 struct shell_seat, seat_destroy_listener);
2838}
2839
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002840static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002841popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002842{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002843 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002844 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002845 struct shell_seat *shseat =
2846 container_of(grab, struct shell_seat, popup_grab.grab);
2847 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002848 wl_fixed_t sx, sy;
2849
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850 view = weston_compositor_pick_view(pointer->seat->compositor,
2851 pointer->x, pointer->y,
2852 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002853
Jason Ekstranda7af7042013-10-12 22:38:11 -05002854 if (view && view->surface->resource &&
2855 wl_resource_get_client(view->surface->resource) == client) {
2856 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002857 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002858 weston_pointer_set_focus(pointer, NULL,
2859 wl_fixed_from_int(0),
2860 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002861 }
2862}
2863
2864static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002865popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2866 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002867{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002868 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002869 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002870 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002871
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002872 weston_pointer_move(pointer, x, y);
2873
Neil Roberts96d790e2013-09-19 17:32:00 +01002874 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002875 weston_view_from_global_fixed(pointer->focus,
2876 pointer->x, pointer->y,
2877 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002878 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002879 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002880}
2881
2882static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002883popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002884 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002885{
2886 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002887 struct shell_seat *shseat =
2888 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002889 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002890 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002891 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002892 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002893
Neil Roberts96d790e2013-09-19 17:32:00 +01002894 resource_list = &grab->pointer->focus_resource_list;
2895 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002896 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002897 wl_resource_for_each(resource, resource_list) {
2898 wl_pointer_send_button(resource, serial,
2899 time, button, state);
2900 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002901 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002902 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002903 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002904 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002905 }
2906
Daniel Stone4dbadb12012-05-30 16:31:51 +01002907 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002908 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002909}
2910
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002911static void
2912popup_grab_cancel(struct weston_pointer_grab *grab)
2913{
2914 popup_grab_end(grab->pointer);
2915}
2916
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002917static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002918 popup_grab_focus,
2919 popup_grab_motion,
2920 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002921 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002922};
2923
2924static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002925shell_surface_send_popup_done(struct shell_surface *shsurf)
2926{
2927 if (shell_surface_is_wl_shell_surface(shsurf))
2928 wl_shell_surface_send_popup_done(shsurf->resource);
2929 else if (shell_surface_is_xdg_popup(shsurf))
2930 xdg_popup_send_popup_done(shsurf->resource,
2931 shsurf->popup.serial);
2932}
2933
2934static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002935popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002936{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002937 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002938 struct shell_seat *shseat =
2939 container_of(grab, struct shell_seat, popup_grab.grab);
2940 struct shell_surface *shsurf;
2941 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002942
2943 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002944 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002945 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002946 shseat->popup_grab.grab.interface = NULL;
2947 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002948 /* Send the popup_done event to all the popups open */
2949 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002950 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002951 shsurf->popup.shseat = NULL;
2952 if (prev) {
2953 wl_list_init(&prev->popup.grab_link);
2954 }
2955 prev = shsurf;
2956 }
2957 wl_list_init(&prev->popup.grab_link);
2958 wl_list_init(&shseat->popup_grab.surfaces_list);
2959 }
2960}
2961
2962static void
2963add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2964{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002965 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002966
2967 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002968 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002969 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002970 /* We must make sure here that this popup was opened after
2971 * a mouse press, and not just by moving around with other
2972 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002973 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002974 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002975
Rob Bradforddfe31052013-06-26 19:49:11 +01002976 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002977 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002978 } else {
2979 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002980 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002981}
2982
2983static void
2984remove_popup_grab(struct shell_surface *shsurf)
2985{
2986 struct shell_seat *shseat = shsurf->popup.shseat;
2987
2988 wl_list_remove(&shsurf->popup.grab_link);
2989 wl_list_init(&shsurf->popup.grab_link);
2990 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002991 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002992 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002993 }
2994}
2995
2996static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002997shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002998{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002999 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003000 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003001
Jason Ekstranda7af7042013-10-12 22:38:11 -05003002 shsurf->surface->output = parent_view->output;
3003 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003004
Jason Ekstranda7af7042013-10-12 22:38:11 -05003005 weston_view_set_transform_parent(shsurf->view, parent_view);
3006 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3007 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003008
Kristian Høgsberge3148752013-05-06 23:19:49 -04003009 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003010 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003011 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003012 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003013 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003014 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003015}
3016
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003017static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003018 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003019 shell_surface_move,
3020 shell_surface_resize,
3021 shell_surface_set_toplevel,
3022 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003023 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003024 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003025 shell_surface_set_maximized,
3026 shell_surface_set_title,
3027 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003028};
3029
3030static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003031destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003032{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003033 struct shell_surface *child, *next;
3034
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003035 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3036
Giulio Camuffo5085a752013-03-25 21:42:45 +01003037 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3038 remove_popup_grab(shsurf);
3039 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003040
Alex Wubd3354b2012-04-17 17:20:49 +08003041 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003042 shell_surface_is_top_fullscreen(shsurf))
3043 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003044
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 if (shsurf->fullscreen.black_view)
3046 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003047
Alex Wubd3354b2012-04-17 17:20:49 +08003048 /* As destroy_resource() use wl_list_for_each_safe(),
3049 * we can always remove the listener.
3050 */
3051 wl_list_remove(&shsurf->surface_destroy_listener.link);
3052 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003053 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003054
Jason Ekstranda7af7042013-10-12 22:38:11 -05003055 weston_view_destroy(shsurf->view);
3056
Philip Withnall648a4dd2013-11-25 18:01:44 +00003057 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003058 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3059 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003060
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003061 wl_list_remove(&shsurf->link);
3062 free(shsurf);
3063}
3064
3065static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003066shell_destroy_shell_surface(struct wl_resource *resource)
3067{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003068 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003069
Bryan Caina46b9462014-04-04 17:41:24 -05003070 if (!wl_list_empty(&shsurf->popup.grab_link))
3071 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003072 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003073}
3074
3075static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003076shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003077{
3078 struct shell_surface *shsurf = container_of(listener,
3079 struct shell_surface,
3080 surface_destroy_listener);
3081
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003082 if (shsurf->resource)
3083 wl_resource_destroy(shsurf->resource);
3084 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003085 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003086}
3087
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003088static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003089fade_out_done(struct weston_view_animation *animation, void *data)
3090{
3091 struct shell_surface *shsurf = data;
3092
3093 weston_surface_destroy(shsurf->surface);
3094}
3095
3096static void
3097handle_resource_destroy(struct wl_listener *listener, void *data)
3098{
3099 struct shell_surface *shsurf =
3100 container_of(listener, struct shell_surface,
3101 resource_destroy_listener);
3102
3103 shsurf->surface->ref_count++;
3104
3105 pixman_region32_fini(&shsurf->surface->pending.input);
3106 pixman_region32_init(&shsurf->surface->pending.input);
3107 pixman_region32_fini(&shsurf->surface->input);
3108 pixman_region32_init(&shsurf->surface->input);
Kristian Høgsbergb033c5e2014-03-20 14:49:07 -07003109 if (weston_surface_is_mapped(shsurf->surface))
3110 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3111 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003112}
3113
3114static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003115shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003116
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003117struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003118get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003119{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003120 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003121 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003122 else
3123 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003124}
3125
Rafael Antognollie2a34552013-12-03 15:35:45 -02003126static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003127create_common_surface(struct shell_client *owner, void *shell,
3128 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003129 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003130{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003131 struct shell_surface *shsurf;
3132
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003133 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003134 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003135 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003136 }
3137
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003138 shsurf = calloc(1, sizeof *shsurf);
3139 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003140 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003141 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003142 }
3143
Jason Ekstranda7af7042013-10-12 22:38:11 -05003144 shsurf->view = weston_view_create(surface);
3145 if (!shsurf->view) {
3146 weston_log("no memory to allocate shell surface\n");
3147 free(shsurf);
3148 return NULL;
3149 }
3150
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003151 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003152 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003153
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003154 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3155 wl_resource_add_destroy_listener(surface->resource,
3156 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003157 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003158
Tiago Vignattibc052c92012-04-19 16:18:18 +03003159 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003160 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003161 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003162 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003163 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003164 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003165 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3166 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003167 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003168 wl_list_init(&shsurf->fullscreen.transform.link);
3169
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003170 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003171 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003172 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003173 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003174
3175 /* init link so its safe to always remove it in destroy_shell_surface */
3176 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003177 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003178
Pekka Paalanen460099f2012-01-20 16:48:25 +02003179 /* empty when not in use */
3180 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003181 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003182
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003183 wl_list_init(&shsurf->workspace_transform.link);
3184
Philip Withnall648a4dd2013-11-25 18:01:44 +00003185 wl_list_init(&shsurf->children_link);
3186 wl_list_init(&shsurf->children_list);
3187 shsurf->parent = NULL;
3188
Pekka Paalanen98262232011-12-01 10:42:22 +02003189 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003190
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003191 shsurf->client = client;
3192
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003193 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003194}
3195
Rafael Antognollie2a34552013-12-03 15:35:45 -02003196static struct shell_surface *
3197create_shell_surface(void *shell, struct weston_surface *surface,
3198 const struct weston_shell_client *client)
3199{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003200 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003201}
3202
Jason Ekstranda7af7042013-10-12 22:38:11 -05003203static struct weston_view *
3204get_primary_view(void *shell, struct shell_surface *shsurf)
3205{
3206 return shsurf->view;
3207}
3208
Tiago Vignattibc052c92012-04-19 16:18:18 +03003209static void
3210shell_get_shell_surface(struct wl_client *client,
3211 struct wl_resource *resource,
3212 uint32_t id,
3213 struct wl_resource *surface_resource)
3214{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003215 struct weston_surface *surface =
3216 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003217 struct shell_client *sc = wl_resource_get_user_data(resource);
3218 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003219 struct shell_surface *shsurf;
3220
3221 if (get_shell_surface(surface)) {
3222 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003223 WL_DISPLAY_ERROR_INVALID_OBJECT,
3224 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003225 return;
3226 }
3227
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003228 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003229 if (!shsurf) {
3230 wl_resource_post_error(surface_resource,
3231 WL_DISPLAY_ERROR_INVALID_OBJECT,
3232 "surface->configure already set");
3233 return;
3234 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003235
Jason Ekstranda85118c2013-06-27 20:17:02 -05003236 shsurf->resource =
3237 wl_resource_create(client,
3238 &wl_shell_surface_interface, 1, id);
3239 wl_resource_set_implementation(shsurf->resource,
3240 &shell_surface_implementation,
3241 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003242}
3243
Rafael Antognollie2a34552013-12-03 15:35:45 -02003244static bool
3245shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3246{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003247 /* A shell surface without a resource is created from xwayland
3248 * and is considered a wl_shell surface for now. */
3249
3250 return shsurf->resource == NULL ||
3251 wl_resource_instance_of(shsurf->resource,
3252 &wl_shell_surface_interface,
3253 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003254}
3255
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003256static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003257 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003258};
3259
Rafael Antognollie2a34552013-12-03 15:35:45 -02003260/****************************
3261 * xdg-shell implementation */
3262
3263static void
3264xdg_surface_destroy(struct wl_client *client,
3265 struct wl_resource *resource)
3266{
3267 wl_resource_destroy(resource);
3268}
3269
3270static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003271xdg_surface_set_transient_for(struct wl_client *client,
3272 struct wl_resource *resource,
3273 struct wl_resource *parent_resource)
3274{
3275 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3276 struct weston_surface *parent;
3277
3278 if (parent_resource)
3279 parent = wl_resource_get_user_data(parent_resource);
3280 else
3281 parent = NULL;
3282
3283 shell_surface_set_parent(shsurf, parent);
3284}
3285
3286static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003287xdg_surface_set_margin(struct wl_client *client,
3288 struct wl_resource *resource,
3289 int32_t left,
3290 int32_t right,
3291 int32_t top,
3292 int32_t bottom)
3293{
3294 /* Do nothing, Weston doesn't try to constrain or place
3295 * surfaces in any special manner... */
3296}
3297
3298static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003299xdg_surface_set_app_id(struct wl_client *client,
3300 struct wl_resource *resource,
3301 const char *app_id)
3302{
3303 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3304
3305 free(shsurf->class);
3306 shsurf->class = strdup(app_id);
3307}
3308
3309static void
3310xdg_surface_set_title(struct wl_client *client,
3311 struct wl_resource *resource, const char *title)
3312{
3313 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3314
3315 set_title(shsurf, title);
3316}
3317
3318static void
3319xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3320 struct wl_resource *seat_resource, uint32_t serial)
3321{
3322 common_surface_move(resource, seat_resource, serial);
3323}
3324
3325static void
3326xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3327 struct wl_resource *seat_resource, uint32_t serial,
3328 uint32_t edges)
3329{
3330 common_surface_resize(resource, seat_resource, serial, edges);
3331}
3332
3333static void
3334xdg_surface_set_output(struct wl_client *client,
3335 struct wl_resource *resource,
3336 struct wl_resource *output_resource)
3337{
3338 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3339 struct weston_output *output;
3340
3341 if (output_resource)
3342 output = wl_resource_get_user_data(output_resource);
3343 else
3344 output = NULL;
3345
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003346 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003347}
3348
3349static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003350xdg_surface_change_state(struct shell_surface *shsurf,
3351 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003352{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003353 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003354 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003355
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003356 switch (state) {
3357 case XDG_SURFACE_STATE_MAXIMIZED:
3358 shsurf->requested_state.maximized = value;
3359 if (value)
3360 set_maximized(shsurf, NULL);
3361 break;
3362 case XDG_SURFACE_STATE_FULLSCREEN:
3363 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003364
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003365 if (value)
3366 set_fullscreen(shsurf,
3367 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3368 0, shsurf->recommended_output);
3369 break;
3370 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003371}
3372
3373static void
3374xdg_surface_request_change_state(struct wl_client *client,
3375 struct wl_resource *resource,
3376 uint32_t state,
3377 uint32_t value,
3378 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003379{
3380 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003381
3382 /* The client can't know what the current state is, so we need
3383 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003384
3385 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3386 return;
3387
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003388 switch (state) {
3389 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003390 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003391 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003392 default:
3393 /* send error? ignore? send change state with value 0? */
3394 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003395 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003396
3397 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003398}
3399
3400static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003401xdg_surface_ack_change_state(struct wl_client *client,
3402 struct wl_resource *resource,
3403 uint32_t state,
3404 uint32_t value,
3405 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003406{
3407 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3408
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003409 if (shsurf->state_requested) {
3410 shsurf->next_state = shsurf->requested_state;
3411 shsurf->state_changed = true;
3412 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003413 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003414}
3415
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003416static void
3417xdg_surface_set_minimized(struct wl_client *client,
3418 struct wl_resource *resource)
3419{
3420 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3421
3422 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3423 return;
3424
3425 /* apply compositor's own minimization logic (hide) */
3426 set_minimized(shsurf->surface, 1);
3427}
3428
Rafael Antognollie2a34552013-12-03 15:35:45 -02003429static const struct xdg_surface_interface xdg_surface_implementation = {
3430 xdg_surface_destroy,
3431 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003432 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003433 xdg_surface_set_title,
3434 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003435 xdg_surface_move,
3436 xdg_surface_resize,
3437 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003438 xdg_surface_request_change_state,
3439 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003440 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003441};
3442
3443static void
3444xdg_send_configure(struct weston_surface *surface,
3445 uint32_t edges, int32_t width, int32_t height)
3446{
3447 struct shell_surface *shsurf = get_shell_surface(surface);
3448
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003449 assert(shsurf);
3450
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003451 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003452}
3453
3454static const struct weston_shell_client xdg_client = {
3455 xdg_send_configure
3456};
3457
3458static void
3459xdg_use_unstable_version(struct wl_client *client,
3460 struct wl_resource *resource,
3461 int32_t version)
3462{
3463 if (version > 1) {
3464 wl_resource_post_error(resource,
3465 1,
3466 "xdg-shell:: version not implemented yet.");
3467 return;
3468 }
3469}
3470
3471static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003472create_xdg_surface(struct shell_client *owner, void *shell,
3473 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003474 const struct weston_shell_client *client)
3475{
3476 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003477
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003478 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003479 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003480
3481 return shsurf;
3482}
3483
3484static void
3485xdg_get_xdg_surface(struct wl_client *client,
3486 struct wl_resource *resource,
3487 uint32_t id,
3488 struct wl_resource *surface_resource)
3489{
3490 struct weston_surface *surface =
3491 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003492 struct shell_client *sc = wl_resource_get_user_data(resource);
3493 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003494 struct shell_surface *shsurf;
3495
3496 if (get_shell_surface(surface)) {
3497 wl_resource_post_error(surface_resource,
3498 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003499 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003500 return;
3501 }
3502
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003503 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003504 if (!shsurf) {
3505 wl_resource_post_error(surface_resource,
3506 WL_DISPLAY_ERROR_INVALID_OBJECT,
3507 "surface->configure already set");
3508 return;
3509 }
3510
3511 shsurf->resource =
3512 wl_resource_create(client,
3513 &xdg_surface_interface, 1, id);
3514 wl_resource_set_implementation(shsurf->resource,
3515 &xdg_surface_implementation,
3516 shsurf, shell_destroy_shell_surface);
3517}
3518
3519static bool
3520shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3521{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003522 return shsurf->resource &&
3523 wl_resource_instance_of(shsurf->resource,
3524 &xdg_surface_interface,
3525 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003526}
3527
3528/* xdg-popup implementation */
3529
3530static void
3531xdg_popup_destroy(struct wl_client *client,
3532 struct wl_resource *resource)
3533{
3534 wl_resource_destroy(resource);
3535}
3536
Rafael Antognollie2a34552013-12-03 15:35:45 -02003537static const struct xdg_popup_interface xdg_popup_implementation = {
3538 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003539};
3540
3541static void
3542xdg_popup_send_configure(struct weston_surface *surface,
3543 uint32_t edges, int32_t width, int32_t height)
3544{
3545}
3546
3547static const struct weston_shell_client xdg_popup_client = {
3548 xdg_popup_send_configure
3549};
3550
3551static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003552create_xdg_popup(struct shell_client *owner, void *shell,
3553 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003554 const struct weston_shell_client *client,
3555 struct weston_surface *parent,
3556 struct shell_seat *seat,
3557 uint32_t serial,
3558 int32_t x, int32_t y)
3559{
3560 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003561
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003562 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003563 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003564 shsurf->popup.shseat = seat;
3565 shsurf->popup.serial = serial;
3566 shsurf->popup.x = x;
3567 shsurf->popup.y = y;
3568 shell_surface_set_parent(shsurf, parent);
3569
3570 return shsurf;
3571}
3572
3573static void
3574xdg_get_xdg_popup(struct wl_client *client,
3575 struct wl_resource *resource,
3576 uint32_t id,
3577 struct wl_resource *surface_resource,
3578 struct wl_resource *parent_resource,
3579 struct wl_resource *seat_resource,
3580 uint32_t serial,
3581 int32_t x, int32_t y, uint32_t flags)
3582{
3583 struct weston_surface *surface =
3584 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003585 struct shell_client *sc = wl_resource_get_user_data(resource);
3586 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003587 struct shell_surface *shsurf;
3588 struct weston_surface *parent;
3589 struct shell_seat *seat;
3590
3591 if (get_shell_surface(surface)) {
3592 wl_resource_post_error(surface_resource,
3593 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003594 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003595 return;
3596 }
3597
3598 if (!parent_resource) {
3599 wl_resource_post_error(surface_resource,
3600 WL_DISPLAY_ERROR_INVALID_OBJECT,
3601 "xdg_shell::get_xdg_popup requires a parent shell surface");
3602 }
3603
3604 parent = wl_resource_get_user_data(parent_resource);
3605 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3606
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003607 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003608 parent, seat, serial, x, y);
3609 if (!shsurf) {
3610 wl_resource_post_error(surface_resource,
3611 WL_DISPLAY_ERROR_INVALID_OBJECT,
3612 "surface->configure already set");
3613 return;
3614 }
3615
3616 shsurf->resource =
3617 wl_resource_create(client,
3618 &xdg_popup_interface, 1, id);
3619 wl_resource_set_implementation(shsurf->resource,
3620 &xdg_popup_implementation,
3621 shsurf, shell_destroy_shell_surface);
3622}
3623
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003624static void
3625xdg_pong(struct wl_client *client,
3626 struct wl_resource *resource, uint32_t serial)
3627{
3628 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003629
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003630 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003631}
3632
Rafael Antognollie2a34552013-12-03 15:35:45 -02003633static bool
3634shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3635{
3636 return wl_resource_instance_of(shsurf->resource,
3637 &xdg_popup_interface,
3638 &xdg_popup_implementation);
3639}
3640
3641static const struct xdg_shell_interface xdg_implementation = {
3642 xdg_use_unstable_version,
3643 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003644 xdg_get_xdg_popup,
3645 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003646};
3647
3648static int
3649xdg_shell_unversioned_dispatch(const void *implementation,
3650 void *_target, uint32_t opcode,
3651 const struct wl_message *message,
3652 union wl_argument *args)
3653{
3654 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003655 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003656
3657 if (opcode != 0) {
3658 wl_resource_post_error(resource,
3659 WL_DISPLAY_ERROR_INVALID_OBJECT,
3660 "must call use_unstable_version first");
3661 return 0;
3662 }
3663
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003664#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003665
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003666 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3667 "shell implementation doesn't match protocol version");
3668
Rafael Antognollie2a34552013-12-03 15:35:45 -02003669 if (args[0].i != XDG_SERVER_VERSION) {
3670 wl_resource_post_error(resource,
3671 WL_DISPLAY_ERROR_INVALID_OBJECT,
3672 "incompatible version, server is %d "
3673 "client wants %d",
3674 XDG_SERVER_VERSION, args[0].i);
3675 return 0;
3676 }
3677
3678 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003679 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003680
3681 return 1;
3682}
3683
3684/* end of xdg-shell implementation */
3685/***********************************/
3686
Kristian Høgsberg07937562011-04-12 17:25:42 -04003687static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003688shell_fade(struct desktop_shell *shell, enum fade_type type);
3689
3690static int
3691screensaver_timeout(void *data)
3692{
3693 struct desktop_shell *shell = data;
3694
3695 shell_fade(shell, FADE_OUT);
3696
3697 return 1;
3698}
3699
3700static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003701handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003702{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003703 struct desktop_shell *shell =
3704 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003705
Pekka Paalanen18027e52011-12-02 16:31:49 +02003706 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003707
3708 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003709 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003710}
3711
3712static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003713launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003714{
3715 if (shell->screensaver.binding)
3716 return;
3717
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003718 if (!shell->screensaver.path) {
3719 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003720 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003721 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003722
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003723 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003724 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003725 return;
3726 }
3727
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003728 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003729 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003730 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003731 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003732}
3733
3734static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003735terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003736{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003737 if (shell->screensaver.process.pid == 0)
3738 return;
3739
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003740 /* Disarm the screensaver timer, otherwise it may fire when the
3741 * compositor is not in the idle state. In that case, the screen will
3742 * be locked, but the wake_signal won't fire on user input, making the
3743 * system unresponsive. */
3744 wl_event_source_timer_update(shell->screensaver.timer, 0);
3745
Pekka Paalanen18027e52011-12-02 16:31:49 +02003746 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003747}
3748
3749static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003750configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003751{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003752 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003753
Jason Ekstranda7af7042013-10-12 22:38:11 -05003754 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3755 if (v->output == ev->output && v != ev) {
3756 weston_view_unmap(v);
3757 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003758 }
3759 }
3760
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003761 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003762
Jason Ekstranda7af7042013-10-12 22:38:11 -05003763 if (wl_list_empty(&ev->layer_link)) {
3764 wl_list_insert(&layer->view_list, &ev->layer_link);
3765 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003766 }
3767}
3768
3769static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003770background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003771{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003772 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003773 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003774
Jason Ekstranda7af7042013-10-12 22:38:11 -05003775 view = container_of(es->views.next, struct weston_view, surface_link);
3776
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003777 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003778}
3779
3780static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003781desktop_shell_set_background(struct wl_client *client,
3782 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003783 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003784 struct wl_resource *surface_resource)
3785{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003786 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003787 struct weston_surface *surface =
3788 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003789 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003790
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003791 if (surface->configure) {
3792 wl_resource_post_error(surface_resource,
3793 WL_DISPLAY_ERROR_INVALID_OBJECT,
3794 "surface role already assigned");
3795 return;
3796 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003797
Jason Ekstranda7af7042013-10-12 22:38:11 -05003798 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3799 weston_view_destroy(view);
3800 view = weston_view_create(surface);
3801
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003802 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003803 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003804 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003805 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003806 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003807 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003808 surface->output->width,
3809 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003810}
3811
3812static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003813panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003814{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003815 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003816 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003817
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818 view = container_of(es->views.next, struct weston_view, surface_link);
3819
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003820 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003821}
3822
3823static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003824desktop_shell_set_panel(struct wl_client *client,
3825 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003826 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003827 struct wl_resource *surface_resource)
3828{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003829 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003830 struct weston_surface *surface =
3831 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003832 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003833
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003834 if (surface->configure) {
3835 wl_resource_post_error(surface_resource,
3836 WL_DISPLAY_ERROR_INVALID_OBJECT,
3837 "surface role already assigned");
3838 return;
3839 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003840
Jason Ekstranda7af7042013-10-12 22:38:11 -05003841 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3842 weston_view_destroy(view);
3843 view = weston_view_create(surface);
3844
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003845 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003846 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003847 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003848 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003849 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003850 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003851 surface->output->width,
3852 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003853}
3854
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003855static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003856lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003857{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003858 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003859 struct weston_view *view;
3860
3861 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003862
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003863 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003864 return;
3865
Jason Ekstranda7af7042013-10-12 22:38:11 -05003866 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003867
3868 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003869 wl_list_insert(&shell->lock_layer.view_list,
3870 &view->layer_link);
3871 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003872 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003873 }
3874}
3875
3876static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003877handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003878{
Tiago Vignattibe143262012-04-16 17:31:41 +03003879 struct desktop_shell *shell =
3880 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003881
Martin Minarik6d118362012-06-07 18:01:59 +02003882 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003883 shell->lock_surface = NULL;
3884}
3885
3886static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003887desktop_shell_set_lock_surface(struct wl_client *client,
3888 struct wl_resource *resource,
3889 struct wl_resource *surface_resource)
3890{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003891 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003892 struct weston_surface *surface =
3893 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003894
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003895 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003896
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003897 if (!shell->locked)
3898 return;
3899
Pekka Paalanen98262232011-12-01 10:42:22 +02003900 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003901
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003902 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003903 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003904 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003905
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003906 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003907 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003908 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003909}
3910
3911static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003912resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003913{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003914 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003915
Pekka Paalanen77346a62011-11-30 16:26:35 +02003916 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003917
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003918 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003919 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003920 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003921 &shell->input_panel_layer.link);
3922 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003923 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003924 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003925 wl_list_insert(&shell->compositor->cursor_layer.link,
3926 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003927 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003928 wl_list_insert(&shell->fullscreen_layer.link,
3929 &shell->panel_layer.link);
3930 wl_list_insert(&shell->panel_layer.link,
3931 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003932
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003933 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003934
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003935 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003936 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003937 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003938}
3939
3940static void
3941desktop_shell_unlock(struct wl_client *client,
3942 struct wl_resource *resource)
3943{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003944 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003945
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003946 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003947
3948 if (shell->locked)
3949 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003950}
3951
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003952static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003953desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003954 struct wl_resource *resource,
3955 struct wl_resource *surface_resource)
3956{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003957 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003958
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003959 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003960 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003961}
3962
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003963static void
3964desktop_shell_desktop_ready(struct wl_client *client,
3965 struct wl_resource *resource)
3966{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003967 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003968
3969 shell_fade_startup(shell);
3970}
3971
Kristian Høgsberg75840622011-09-06 13:48:16 -04003972static const struct desktop_shell_interface desktop_shell_implementation = {
3973 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003974 desktop_shell_set_panel,
3975 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003976 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003977 desktop_shell_set_grab_surface,
3978 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003979};
3980
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003981static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003982get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003983{
3984 struct shell_surface *shsurf;
3985
3986 shsurf = get_shell_surface(surface);
3987 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003988 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003989 return shsurf->type;
3990}
3991
Kristian Høgsberg75840622011-09-06 13:48:16 -04003992static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003993move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003994{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003995 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003996 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003997 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003998
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003999 if (seat->pointer->focus == NULL)
4000 return;
4001
4002 focus = seat->pointer->focus->surface;
4003
Pekka Paalanen01388e22013-04-25 13:57:44 +03004004 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004005 if (surface == NULL)
4006 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004007
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004008 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004009 if (shsurf == NULL || shsurf->state.fullscreen ||
4010 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004011 return;
4012
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004013 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004014}
4015
4016static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004017maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4018{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004019 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004020 struct weston_surface *surface;
4021 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004022 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004023
4024 surface = weston_surface_get_main_surface(focus);
4025 if (surface == NULL)
4026 return;
4027
4028 shsurf = get_shell_surface(surface);
4029 if (shsurf == NULL)
4030 return;
4031
4032 if (!shell_surface_is_xdg_surface(shsurf))
4033 return;
4034
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004035 serial = wl_display_next_serial(seat->compositor->wl_display);
4036 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4037 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004038}
4039
4040static void
4041fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4042{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004043 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004044 struct weston_surface *surface;
4045 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004046 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004047
4048 surface = weston_surface_get_main_surface(focus);
4049 if (surface == NULL)
4050 return;
4051
4052 shsurf = get_shell_surface(surface);
4053 if (shsurf == NULL)
4054 return;
4055
4056 if (!shell_surface_is_xdg_surface(shsurf))
4057 return;
4058
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004059 serial = wl_display_next_serial(seat->compositor->wl_display);
4060 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4061 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004062}
4063
4064static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004065touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4066{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004067 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004068 struct weston_surface *surface;
4069 struct shell_surface *shsurf;
4070
4071 surface = weston_surface_get_main_surface(focus);
4072 if (surface == NULL)
4073 return;
4074
4075 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004076 if (shsurf == NULL || shsurf->state.fullscreen ||
4077 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004078 return;
4079
4080 surface_touch_move(shsurf, (struct weston_seat *) seat);
4081}
4082
4083static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004084resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004085{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004086 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004087 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004088 uint32_t edges = 0;
4089 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004090 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004091
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004092 if (seat->pointer->focus == NULL)
4093 return;
4094
4095 focus = seat->pointer->focus->surface;
4096
Pekka Paalanen01388e22013-04-25 13:57:44 +03004097 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004098 if (surface == NULL)
4099 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004100
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004101 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004102 if (shsurf == NULL || shsurf->state.fullscreen ||
4103 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004104 return;
4105
Jason Ekstranda7af7042013-10-12 22:38:11 -05004106 weston_view_from_global(shsurf->view,
4107 wl_fixed_to_int(seat->pointer->grab_x),
4108 wl_fixed_to_int(seat->pointer->grab_y),
4109 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004110
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004111 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004112 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004113 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004114 edges |= 0;
4115 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004116 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004117
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004118 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004119 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004120 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004121 edges |= 0;
4122 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004123 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004124
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004125 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004126}
4127
4128static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004129surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004130 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004131{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004132 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004133 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004134 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004135 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004136
Pekka Paalanen01388e22013-04-25 13:57:44 +03004137 /* XXX: broken for windows containing sub-surfaces */
4138 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004139 if (surface == NULL)
4140 return;
4141
4142 shsurf = get_shell_surface(surface);
4143 if (!shsurf)
4144 return;
4145
Jason Ekstranda7af7042013-10-12 22:38:11 -05004146 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004147
Jason Ekstranda7af7042013-10-12 22:38:11 -05004148 if (shsurf->view->alpha > 1.0)
4149 shsurf->view->alpha = 1.0;
4150 if (shsurf->view->alpha < step)
4151 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004152
Jason Ekstranda7af7042013-10-12 22:38:11 -05004153 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004154 weston_surface_damage(surface);
4155}
4156
4157static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004158do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004159 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004160{
Daniel Stone37816df2012-05-16 18:45:18 +01004161 struct weston_seat *ws = (struct weston_seat *) seat;
4162 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004163 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004164 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004165
4166 wl_list_for_each(output, &compositor->output_list, link) {
4167 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004168 wl_fixed_to_double(seat->pointer->x),
4169 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004170 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004171 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004172 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004173 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004174 increment = -output->zoom.increment;
4175 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004176 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004177 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004178 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004179 else
4180 increment = 0;
4181
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004182 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004183
Scott Moreaue6603982012-06-11 13:07:51 -06004184 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004185 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004186 else if (output->zoom.level > output->zoom.max_level)
4187 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004188 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004189 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004190 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004191
Scott Moreaue6603982012-06-11 13:07:51 -06004192 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004193
Jason Ekstranda7af7042013-10-12 22:38:11 -05004194 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004195 }
4196 }
4197}
4198
Scott Moreauccbf29d2012-02-22 14:21:41 -07004199static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004200zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004201 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004202{
4203 do_zoom(seat, time, 0, axis, value);
4204}
4205
4206static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004207zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004208 void *data)
4209{
4210 do_zoom(seat, time, key, 0, 0);
4211}
4212
4213static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004214terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004215 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004216{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004217 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004218
Daniel Stone325fc2d2012-05-30 16:31:58 +01004219 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004220}
4221
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004222static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004223rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4224 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004225{
4226 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004227 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004228 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004229 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004230 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004231
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004232 weston_pointer_move(pointer, x, y);
4233
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004234 if (!shsurf)
4235 return;
4236
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004237 cx = 0.5f * shsurf->surface->width;
4238 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004239
Daniel Stone37816df2012-05-16 18:45:18 +01004240 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4241 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004242 r = sqrtf(dx * dx + dy * dy);
4243
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004244 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004246
4247 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004248 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004249 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004250
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004251 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004252 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004253
4254 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004255 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004256 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004257 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004258 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004259
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004260 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004261 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004262 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004263 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004264 wl_list_init(&shsurf->rotation.transform.link);
4265 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004266 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004267 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004268
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004269 /* We need to adjust the position of the surface
4270 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004271 cposx = shsurf->view->geometry.x + cx;
4272 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004273 dposx = rotate->center.x - cposx;
4274 dposy = rotate->center.y - cposy;
4275 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004276 weston_view_set_position(shsurf->view,
4277 shsurf->view->geometry.x + dposx,
4278 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004279 }
4280
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004281 /* Repaint implies weston_surface_update_transform(), which
4282 * lazily applies the damage due to rotation update.
4283 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004284 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004285}
4286
4287static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004288rotate_grab_button(struct weston_pointer_grab *grab,
4289 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004290{
4291 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004292 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004293 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004294 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004295 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004296
Daniel Stone4dbadb12012-05-30 16:31:51 +01004297 if (pointer->button_count == 0 &&
4298 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004299 if (shsurf)
4300 weston_matrix_multiply(&shsurf->rotation.rotation,
4301 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004302 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004303 free(rotate);
4304 }
4305}
4306
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004307static void
4308rotate_grab_cancel(struct weston_pointer_grab *grab)
4309{
4310 struct rotate_grab *rotate =
4311 container_of(grab, struct rotate_grab, base.grab);
4312
4313 shell_grab_end(&rotate->base);
4314 free(rotate);
4315}
4316
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004317static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004318 noop_grab_focus,
4319 rotate_grab_motion,
4320 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004321 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004322};
4323
4324static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004325surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004326{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004327 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004328 float dx, dy;
4329 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004330
Pekka Paalanen460099f2012-01-20 16:48:25 +02004331 rotate = malloc(sizeof *rotate);
4332 if (!rotate)
4333 return;
4334
Jason Ekstranda7af7042013-10-12 22:38:11 -05004335 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004336 surface->surface->width * 0.5f,
4337 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004338 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004339
Daniel Stone37816df2012-05-16 18:45:18 +01004340 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4341 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004342 r = sqrtf(dx * dx + dy * dy);
4343 if (r > 20.0f) {
4344 struct weston_matrix inverse;
4345
4346 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004347 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004348 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004349
4350 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004351 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004352 } else {
4353 weston_matrix_init(&surface->rotation.rotation);
4354 weston_matrix_init(&rotate->rotation);
4355 }
4356
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004357 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4358 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004359}
4360
4361static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004362rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004363 void *data)
4364{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004365 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004366 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004367 struct shell_surface *surface;
4368
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004369 if (seat->pointer->focus == NULL)
4370 return;
4371
4372 focus = seat->pointer->focus->surface;
4373
Pekka Paalanen01388e22013-04-25 13:57:44 +03004374 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004375 if (base_surface == NULL)
4376 return;
4377
4378 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004379 if (surface == NULL || surface->state.fullscreen ||
4380 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004381 return;
4382
4383 surface_rotate(surface, seat);
4384}
4385
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004386/* Move all fullscreen layers down to the current workspace in a non-reversible
4387 * manner. This should be used when implementing shell-wide overlays, such as
4388 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004389void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004390lower_fullscreen_layer(struct desktop_shell *shell)
4391{
4392 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004394
4395 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 wl_list_for_each_reverse_safe(view, prev,
4397 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004398 layer_link) {
4399 wl_list_remove(&view->layer_link);
4400 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4401 weston_view_damage_below(view);
4402 weston_surface_damage(view->surface);
4403 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004404}
4405
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004406void
Tiago Vignattibe143262012-04-16 17:31:41 +03004407activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004408 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004409{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004410 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004411 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004412 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004413 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004414 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004415
Pekka Paalanen01388e22013-04-25 13:57:44 +03004416 main_surface = weston_surface_get_main_surface(es);
4417
Daniel Stone37816df2012-05-16 18:45:18 +01004418 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004419
Jonas Ådahl8538b222012-08-29 22:13:03 +02004420 state = ensure_focus_state(shell, seat);
4421 if (state == NULL)
4422 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004423
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004424 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004425 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004426
Rafael Antognolli03b16592013-12-03 15:35:42 -02004427 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004428 assert(shsurf);
4429
Rafael Antognolli03b16592013-12-03 15:35:42 -02004430 if (shsurf->state.fullscreen)
4431 shell_configure_fullscreen(shsurf);
4432 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004433 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004434
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004435 /* Update the surface’s layer. This brings it to the top of the stacking
4436 * order as appropriate. */
4437 shell_surface_update_layer(shsurf);
4438
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004439 if (shell->focus_animation_type != ANIMATION_NONE) {
4440 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004441 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004442 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004443}
4444
Alex Wu21858432012-04-01 20:13:08 +08004445/* no-op func for checking black surface */
4446static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004447black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004448{
4449}
4450
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004451static bool
Alex Wu21858432012-04-01 20:13:08 +08004452is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4453{
4454 if (es->configure == black_surface_configure) {
4455 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004456 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004457 return true;
4458 }
4459 return false;
4460}
4461
Kristian Høgsberg75840622011-09-06 13:48:16 -04004462static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004463activate_binding(struct weston_seat *seat,
4464 struct desktop_shell *shell,
4465 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004466{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004467 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004468
Alex Wu9c35e6b2012-03-05 14:13:13 +08004469 if (!focus)
4470 return;
4471
Pekka Paalanen01388e22013-04-25 13:57:44 +03004472 if (is_black_surface(focus, &main_surface))
4473 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004474
Pekka Paalanen01388e22013-04-25 13:57:44 +03004475 main_surface = weston_surface_get_main_surface(focus);
4476 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004477 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004478
Neil Robertsa28c6932013-10-03 16:43:04 +01004479 activate(shell, focus, seat);
4480}
4481
4482static void
4483click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4484 void *data)
4485{
4486 if (seat->pointer->grab != &seat->pointer->default_grab)
4487 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004488 if (seat->pointer->focus == NULL)
4489 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004490
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004491 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004492}
4493
4494static void
4495touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4496{
4497 if (seat->touch->grab != &seat->touch->default_grab)
4498 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004499 if (seat->touch->focus == NULL)
4500 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004501
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004502 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004503}
4504
4505static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004506unfocus_all_seats(struct desktop_shell *shell)
4507{
4508 struct weston_seat *seat, *next;
4509
4510 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4511 if (seat->keyboard == NULL)
4512 continue;
4513
4514 weston_keyboard_set_focus(seat->keyboard, NULL);
4515 }
4516}
4517
4518static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004519lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004520{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004521 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004522
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004523 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004524 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004525 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004526 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004527
4528 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004529
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004530 /* Hide all surfaces by removing the fullscreen, panel and
4531 * toplevel layers. This way nothing else can show or receive
4532 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004533
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004534 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004535 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004536 if (shell->showing_input_panels)
4537 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004538 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004539 wl_list_insert(&shell->compositor->cursor_layer.link,
4540 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004541
Pekka Paalanen77346a62011-11-30 16:26:35 +02004542 launch_screensaver(shell);
4543
Neil Robertsb4a91702014-04-09 16:33:32 +01004544 /* Remove the keyboard focus on all seats. This will be
4545 * restored to the workspace's saved state via
4546 * restore_focus_state when the compositor is unlocked */
4547 unfocus_all_seats(shell);
4548
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004549 /* TODO: disable bindings that should not work while locked. */
4550
4551 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004552}
4553
4554static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004555unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004556{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004557 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004558 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004559 return;
4560 }
4561
4562 /* If desktop-shell client has gone away, unlock immediately. */
4563 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004564 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004565 return;
4566 }
4567
4568 if (shell->prepare_event_sent)
4569 return;
4570
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004571 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004572 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004573}
4574
4575static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004576shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004577{
4578 struct desktop_shell *shell = data;
4579
4580 shell->fade.animation = NULL;
4581
4582 switch (shell->fade.type) {
4583 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004584 weston_surface_destroy(shell->fade.view->surface);
4585 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004586 break;
4587 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004588 lock(shell);
4589 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004590 default:
4591 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004592 }
4593}
4594
Jason Ekstranda7af7042013-10-12 22:38:11 -05004595static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004596shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004597{
4598 struct weston_compositor *compositor = shell->compositor;
4599 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004600 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004601
4602 surface = weston_surface_create(compositor);
4603 if (!surface)
4604 return NULL;
4605
Jason Ekstranda7af7042013-10-12 22:38:11 -05004606 view = weston_view_create(surface);
4607 if (!view) {
4608 weston_surface_destroy(surface);
4609 return NULL;
4610 }
4611
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004612 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004613 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004614 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004615 wl_list_insert(&compositor->fade_layer.view_list,
4616 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004617 pixman_region32_init(&surface->input);
4618
Jason Ekstranda7af7042013-10-12 22:38:11 -05004619 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004620}
4621
4622static void
4623shell_fade(struct desktop_shell *shell, enum fade_type type)
4624{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004625 float tint;
4626
4627 switch (type) {
4628 case FADE_IN:
4629 tint = 0.0;
4630 break;
4631 case FADE_OUT:
4632 tint = 1.0;
4633 break;
4634 default:
4635 weston_log("shell: invalid fade type\n");
4636 return;
4637 }
4638
4639 shell->fade.type = type;
4640
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641 if (shell->fade.view == NULL) {
4642 shell->fade.view = shell_fade_create_surface(shell);
4643 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004644 return;
4645
Jason Ekstranda7af7042013-10-12 22:38:11 -05004646 shell->fade.view->alpha = 1.0 - tint;
4647 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004648 }
4649
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004650 if (shell->fade.view->output == NULL) {
4651 /* If the black view gets a NULL output, we lost the
4652 * last output and we'll just cancel the fade. This
4653 * happens when you close the last window under the
4654 * X11 or Wayland backends. */
4655 shell->locked = false;
4656 weston_surface_destroy(shell->fade.view->surface);
4657 shell->fade.view = NULL;
4658 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004659 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004660 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004661 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004662 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004663 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004664 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004665 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004666}
4667
4668static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004669do_shell_fade_startup(void *data)
4670{
4671 struct desktop_shell *shell = data;
4672
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004673 if (shell->startup_animation_type == ANIMATION_FADE)
4674 shell_fade(shell, FADE_IN);
4675 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004676 weston_surface_destroy(shell->fade.view->surface);
4677 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004678 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004679}
4680
4681static void
4682shell_fade_startup(struct desktop_shell *shell)
4683{
4684 struct wl_event_loop *loop;
4685
4686 if (!shell->fade.startup_timer)
4687 return;
4688
4689 wl_event_source_remove(shell->fade.startup_timer);
4690 shell->fade.startup_timer = NULL;
4691
4692 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4693 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4694}
4695
4696static int
4697fade_startup_timeout(void *data)
4698{
4699 struct desktop_shell *shell = data;
4700
4701 shell_fade_startup(shell);
4702 return 0;
4703}
4704
4705static void
4706shell_fade_init(struct desktop_shell *shell)
4707{
4708 /* Make compositor output all black, and wait for the desktop-shell
4709 * client to signal it is ready, then fade in. The timer triggers a
4710 * fade-in, in case the desktop-shell client takes too long.
4711 */
4712
4713 struct wl_event_loop *loop;
4714
Jason Ekstranda7af7042013-10-12 22:38:11 -05004715 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004716 weston_log("%s: warning: fade surface already exists\n",
4717 __func__);
4718 return;
4719 }
4720
Jason Ekstranda7af7042013-10-12 22:38:11 -05004721 shell->fade.view = shell_fade_create_surface(shell);
4722 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004723 return;
4724
Jason Ekstranda7af7042013-10-12 22:38:11 -05004725 weston_view_update_transform(shell->fade.view);
4726 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004727
4728 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4729 shell->fade.startup_timer =
4730 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4731 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4732}
4733
4734static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004735idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004736{
4737 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004738 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004739 struct weston_seat *seat;
4740
4741 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4742 if (seat->pointer)
4743 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004744
4745 shell_fade(shell, FADE_OUT);
4746 /* lock() is called from shell_fade_done() */
4747}
4748
4749static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004750wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004751{
4752 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004753 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004754
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004755 unlock(shell);
4756}
4757
4758static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004759center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004760{
Giulio Camuffob8366642013-04-25 13:57:46 +03004761 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004762 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004763
Jason Ekstranda7af7042013-10-12 22:38:11 -05004764 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004765
4766 x = output->x + (output->width - width) / 2 - surf_x / 2;
4767 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004768
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004769 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004770}
4771
4772static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004773weston_view_set_initial_position(struct weston_view *view,
4774 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004775{
4776 struct weston_compositor *compositor = shell->compositor;
4777 int ix = 0, iy = 0;
4778 int range_x, range_y;
4779 int dx, dy, x, y, panel_height;
4780 struct weston_output *output, *target_output = NULL;
4781 struct weston_seat *seat;
4782
4783 /* As a heuristic place the new window on the same output as the
4784 * pointer. Falling back to the output containing 0, 0.
4785 *
4786 * TODO: Do something clever for touch too?
4787 */
4788 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004789 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004790 ix = wl_fixed_to_int(seat->pointer->x);
4791 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004792 break;
4793 }
4794 }
4795
4796 wl_list_for_each(output, &compositor->output_list, link) {
4797 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4798 target_output = output;
4799 break;
4800 }
4801 }
4802
4803 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004804 weston_view_set_position(view, 10 + random() % 400,
4805 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004806 return;
4807 }
4808
4809 /* Valid range within output where the surface will still be onscreen.
4810 * If this is negative it means that the surface is bigger than
4811 * output.
4812 */
4813 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004814 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004815 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004816 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004817
Rob Bradford4cb88c72012-08-13 15:18:44 +01004818 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004819 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004820 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004821 dx = 0;
4822
4823 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004824 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004825 else
4826 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004827
4828 x = target_output->x + dx;
4829 y = target_output->y + dy;
4830
Jason Ekstranda7af7042013-10-12 22:38:11 -05004831 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004832}
4833
4834static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004836 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004837{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004838 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004839 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004840 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004841 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004842
Pekka Paalanen77346a62011-11-30 16:26:35 +02004843 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004844 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004845 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004846 if (shsurf->state.fullscreen) {
4847 center_on_output(shsurf->view, shsurf->fullscreen_output);
4848 shell_map_fullscreen(shsurf);
4849 } else if (shsurf->state.maximized) {
4850 /* use surface configure to set the geometry */
4851 panel_height = get_output_panel_height(shell, shsurf->output);
4852 surface_subsurfaces_boundingbox(shsurf->surface,
4853 &surf_x, &surf_y, NULL, NULL);
4854 weston_view_set_position(shsurf->view,
4855 shsurf->output->x - surf_x,
4856 shsurf->output->y +
4857 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004858 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004859 weston_view_set_initial_position(shsurf->view, shell);
4860 }
Juan Zhao96879df2012-02-07 08:45:41 +08004861 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004862 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004863 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004864 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004865 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004866 weston_view_set_position(shsurf->view,
4867 shsurf->view->geometry.x + sx,
4868 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004869 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004870 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004871 default:
4872 ;
4873 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004874
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004875 /* Surface stacking order, see also activate(). */
4876 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004877
Jason Ekstranda7af7042013-10-12 22:38:11 -05004878 if (shsurf->type != SHELL_SURFACE_NONE) {
4879 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004880 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004881 shsurf->surface->output = shsurf->output;
4882 shsurf->view->output = shsurf->output;
4883 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004884 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004885
Jason Ekstranda7af7042013-10-12 22:38:11 -05004886 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004887 /* XXX: xwayland's using the same fields for transient type */
4888 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004889 if (shsurf->transient.flags ==
4890 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4891 break;
4892 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004893 if (shsurf->state.relative &&
4894 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4895 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004896 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004897 break;
4898 wl_list_for_each(seat, &compositor->seat_list, link)
4899 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004900 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004901 case SHELL_SURFACE_POPUP:
4902 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004903 default:
4904 break;
4905 }
4906
Rafael Antognolli03b16592013-12-03 15:35:42 -02004907 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4908 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004909 {
4910 switch (shell->win_animation_type) {
4911 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004912 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004913 break;
4914 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004915 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004916 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004917 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004918 default:
4919 break;
4920 }
4921 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004922}
4923
4924static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004925configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004926 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004927{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004928 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004929 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004930 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004931
Pekka Paalanen77346a62011-11-30 16:26:35 +02004932 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004933
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004934 assert(shsurf);
4935
Rafael Antognolli03b16592013-12-03 15:35:42 -02004936 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004937 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004938 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004939 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004940 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4941 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004942 mx = shsurf->output->x - surf_x;
4943 my = shsurf->output->y +
4944 get_output_panel_height(shell,shsurf->output) - surf_y;
4945 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004946 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004947 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004948 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004949
Alex Wu4539b082012-03-01 12:57:46 +08004950 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004951 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004952 wl_list_for_each(view, &surface->views, surface_link)
4953 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004954
Rafael Antognolli03b16592013-12-03 15:35:42 -02004955 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004956 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004957 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004958}
4959
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004960static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004961shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004962{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004963 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004964 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004965 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004966
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004967 assert(shsurf);
4968
4969 shell = shsurf->shell;
4970
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004971 if (!weston_surface_is_mapped(es) &&
4972 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004973 remove_popup_grab(shsurf);
4974 }
4975
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004976 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004977 return;
4978
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004979 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004980 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004981 type_changed = 1;
4982 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004983
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004984 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004985 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004986 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004987 shsurf->last_width != es->width ||
4988 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004989 float from_x, from_y;
4990 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004991
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004992 if (shsurf->resize_edges) {
4993 sx = 0;
4994 sy = 0;
4995 }
4996
4997 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4998 sx = shsurf->last_width - es->width;
4999 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5000 sy = shsurf->last_height - es->height;
5001
5002 shsurf->last_width = es->width;
5003 shsurf->last_height = es->height;
5004
Jason Ekstranda7af7042013-10-12 22:38:11 -05005005 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5006 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005007 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005008 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005009 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005010 }
5011}
5012
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005013static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005014
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005015static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005016desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005017{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005018 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005019 struct desktop_shell *shell =
5020 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005021
5022 shell->child.process.pid = 0;
5023 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005024
5025 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5026 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005027 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005028 shell->child.deathstamp = time;
5029 shell->child.deathcount = 0;
5030 }
5031
5032 shell->child.deathcount++;
5033 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005034 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005035 return;
5036 }
5037
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005038 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005039 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005040 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005041}
5042
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005043static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005044desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5045{
5046 struct desktop_shell *shell;
5047
5048 shell = container_of(listener, struct desktop_shell,
5049 child.client_destroy_listener);
5050
5051 shell->child.client = NULL;
5052}
5053
5054static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005055launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005056{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005057 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005058
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005059 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005060 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005061 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005062 desktop_shell_sigchld);
5063
5064 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005065 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005066
5067 shell->child.client_destroy_listener.notify =
5068 desktop_shell_client_destroy;
5069 wl_client_add_destroy_listener(shell->child.client,
5070 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005071}
5072
5073static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005074handle_shell_client_destroy(struct wl_listener *listener, void *data)
5075{
5076 struct shell_client *sc =
5077 container_of(listener, struct shell_client, destroy_listener);
5078
5079 if (sc->ping_timer)
5080 wl_event_source_remove(sc->ping_timer);
5081 free(sc);
5082}
5083
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005084static struct shell_client *
5085shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5086 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005087{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005088 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005089
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005090 sc = zalloc(sizeof *sc);
5091 if (sc == NULL) {
5092 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005093 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005094 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005095
5096 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005097 if (sc->resource == NULL) {
5098 free(sc);
5099 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005100 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005101 }
5102
5103 sc->client = client;
5104 sc->shell = shell;
5105 sc->destroy_listener.notify = handle_shell_client_destroy;
5106 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5107
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005108 return sc;
5109}
5110
5111static void
5112bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5113{
5114 struct desktop_shell *shell = data;
5115 struct shell_client *sc;
5116
5117 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5118 if (sc)
5119 wl_resource_set_implementation(sc->resource,
5120 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005121 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005122}
5123
5124static void
5125bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5126{
5127 struct desktop_shell *shell = data;
5128 struct shell_client *sc;
5129
5130 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5131 if (sc)
5132 wl_resource_set_dispatcher(sc->resource,
5133 xdg_shell_unversioned_dispatch,
5134 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005135}
5136
5137static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005138unbind_desktop_shell(struct wl_resource *resource)
5139{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005140 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005141
5142 if (shell->locked)
5143 resume_desktop(shell);
5144
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005145 shell->child.desktop_shell = NULL;
5146 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005147}
5148
5149static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005150bind_desktop_shell(struct wl_client *client,
5151 void *data, uint32_t version, uint32_t id)
5152{
Tiago Vignattibe143262012-04-16 17:31:41 +03005153 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005154 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005155
Jason Ekstranda85118c2013-06-27 20:17:02 -05005156 resource = wl_resource_create(client, &desktop_shell_interface,
5157 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005158
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005159 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005160 wl_resource_set_implementation(resource,
5161 &desktop_shell_implementation,
5162 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005163 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005164
5165 if (version < 2)
5166 shell_fade_startup(shell);
5167
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005168 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005169 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005170
5171 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5172 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005173 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005174}
5175
Pekka Paalanen6e168112011-11-24 11:34:05 +02005176static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005177screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005178{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005179 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005180 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005181
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005182 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005183 return;
5184
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005185 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005186 if (!shell->locked)
5187 return;
5188
Jason Ekstranda7af7042013-10-12 22:38:11 -05005189 view = container_of(surface->views.next, struct weston_view, surface_link);
5190 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005191
Jason Ekstranda7af7042013-10-12 22:38:11 -05005192 if (wl_list_empty(&view->layer_link)) {
5193 wl_list_insert(shell->lock_layer.view_list.prev,
5194 &view->layer_link);
5195 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005196 wl_event_source_timer_update(shell->screensaver.timer,
5197 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005198 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005199 }
5200}
5201
5202static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005203screensaver_set_surface(struct wl_client *client,
5204 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005205 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005206 struct wl_resource *output_resource)
5207{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005208 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005209 struct weston_surface *surface =
5210 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005211 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005212 struct weston_view *view, *next;
5213
5214 /* Make sure we only have one view */
5215 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5216 weston_view_destroy(view);
5217 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005218
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005219 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005220 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005221 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005222}
5223
5224static const struct screensaver_interface screensaver_implementation = {
5225 screensaver_set_surface
5226};
5227
5228static void
5229unbind_screensaver(struct wl_resource *resource)
5230{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005231 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005232
Pekka Paalanen77346a62011-11-30 16:26:35 +02005233 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005234}
5235
5236static void
5237bind_screensaver(struct wl_client *client,
5238 void *data, uint32_t version, uint32_t id)
5239{
Tiago Vignattibe143262012-04-16 17:31:41 +03005240 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005241 struct wl_resource *resource;
5242
Jason Ekstranda85118c2013-06-27 20:17:02 -05005243 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005244
Pekka Paalanen77346a62011-11-30 16:26:35 +02005245 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005246 wl_resource_set_implementation(resource,
5247 &screensaver_implementation,
5248 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005249 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005250 return;
5251 }
5252
5253 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5254 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005255 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005256}
5257
Kristian Høgsberg07045392012-02-19 18:52:44 -05005258struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005259 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005260 struct weston_surface *current;
5261 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005262 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005263 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005264};
5265
5266static void
5267switcher_next(struct switcher *switcher)
5268{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005269 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005270 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005271 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005272 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005273
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005274 /* temporary re-display minimized surfaces */
5275 struct weston_view *tmp;
5276 struct weston_view **minimized;
5277 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5278 wl_list_remove(&view->layer_link);
5279 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5280 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5281 *minimized = view;
5282 }
5283
Jason Ekstranda7af7042013-10-12 22:38:11 -05005284 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005285 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005286 if (shsurf &&
5287 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5288 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005289 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005290 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005291 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005292 next = view->surface;
5293 prev = view->surface;
5294 view->alpha = 0.25;
5295 weston_view_geometry_dirty(view);
5296 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005297 }
Alex Wu1659daa2012-04-01 20:13:09 +08005298
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299 if (is_black_surface(view->surface, NULL)) {
5300 view->alpha = 0.25;
5301 weston_view_geometry_dirty(view);
5302 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005303 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005304 }
5305
5306 if (next == NULL)
5307 next = first;
5308
Alex Wu07b26062012-03-12 16:06:01 +08005309 if (next == NULL)
5310 return;
5311
Kristian Høgsberg07045392012-02-19 18:52:44 -05005312 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005313 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005314
5315 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005316 wl_list_for_each(view, &next->views, surface_link)
5317 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005318
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005319 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005320 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005321 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005322}
5323
5324static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005325switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005326{
5327 struct switcher *switcher =
5328 container_of(listener, struct switcher, listener);
5329
5330 switcher_next(switcher);
5331}
5332
5333static void
Daniel Stone351eb612012-05-31 15:27:47 -04005334switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005335{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005336 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005337 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005338 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005339
Jason Ekstranda7af7042013-10-12 22:38:11 -05005340 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005341 if (is_focus_view(view))
5342 continue;
5343
Jason Ekstranda7af7042013-10-12 22:38:11 -05005344 view->alpha = 1.0;
5345 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005346 }
5347
Alex Wu07b26062012-03-12 16:06:01 +08005348 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005349 activate(switcher->shell, switcher->current,
5350 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005351 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005352 weston_keyboard_end_grab(keyboard);
5353 if (keyboard->input_method_resource)
5354 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005355
5356 /* re-hide surfaces that were temporary shown during the switch */
5357 struct weston_view **minimized;
5358 wl_array_for_each(minimized, &switcher->minimized_array) {
5359 /* with the exception of the current selected */
5360 if ((*minimized)->surface != switcher->current) {
5361 wl_list_remove(&(*minimized)->layer_link);
5362 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5363 weston_view_damage_below(*minimized);
5364 }
5365 }
5366 wl_array_release(&switcher->minimized_array);
5367
Kristian Høgsberg07045392012-02-19 18:52:44 -05005368 free(switcher);
5369}
5370
5371static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005372switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005373 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005374{
5375 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005376 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005377
Daniel Stonec9785ea2012-05-30 16:31:52 +01005378 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005379 switcher_next(switcher);
5380}
5381
5382static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005383switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005384 uint32_t mods_depressed, uint32_t mods_latched,
5385 uint32_t mods_locked, uint32_t group)
5386{
5387 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005388 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005389
Daniel Stone351eb612012-05-31 15:27:47 -04005390 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5391 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005392}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005393
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005394static void
5395switcher_cancel(struct weston_keyboard_grab *grab)
5396{
5397 struct switcher *switcher = container_of(grab, struct switcher, grab);
5398
5399 switcher_destroy(switcher);
5400}
5401
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005402static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005403 switcher_key,
5404 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005405 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005406};
5407
5408static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005409switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005410 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005411{
Tiago Vignattibe143262012-04-16 17:31:41 +03005412 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005413 struct switcher *switcher;
5414
5415 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005416 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005417 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005418 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005419 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005420 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005421
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005422 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005423 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005424 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005425 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5426 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005427 switcher_next(switcher);
5428}
5429
Pekka Paalanen3c647232011-12-22 13:43:43 +02005430static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005431backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005432 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005433{
5434 struct weston_compositor *compositor = data;
5435 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005436 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005437
5438 /* TODO: we're limiting to simple use cases, where we assume just
5439 * control on the primary display. We'd have to extend later if we
5440 * ever get support for setting backlights on random desktop LCD
5441 * panels though */
5442 output = get_default_output(compositor);
5443 if (!output)
5444 return;
5445
5446 if (!output->set_backlight)
5447 return;
5448
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005449 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5450 backlight_new = output->backlight_current - 25;
5451 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5452 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005453
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005454 if (backlight_new < 5)
5455 backlight_new = 5;
5456 if (backlight_new > 255)
5457 backlight_new = 255;
5458
5459 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005460 output->set_backlight(output, output->backlight_current);
5461}
5462
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005463struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005464 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005465 struct weston_seat *seat;
5466 uint32_t key[2];
5467 int key_released[2];
5468};
5469
5470static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005471debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005472 uint32_t key, uint32_t state)
5473{
5474 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005475 struct weston_compositor *ec = db->seat->compositor;
5476 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005477 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005478 uint32_t serial;
5479 int send = 0, terminate = 0;
5480 int check_binding = 1;
5481 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005482 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005483
5484 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5485 /* Do not run bindings on key releases */
5486 check_binding = 0;
5487
5488 for (i = 0; i < 2; i++)
5489 if (key == db->key[i])
5490 db->key_released[i] = 1;
5491
5492 if (db->key_released[0] && db->key_released[1]) {
5493 /* All key releases been swalled so end the grab */
5494 terminate = 1;
5495 } else if (key != db->key[0] && key != db->key[1]) {
5496 /* Should not swallow release of other keys */
5497 send = 1;
5498 }
5499 } else if (key == db->key[0] && !db->key_released[0]) {
5500 /* Do not check bindings for the first press of the binding
5501 * key. This allows it to be used as a debug shortcut.
5502 * We still need to swallow this event. */
5503 check_binding = 0;
5504 } else if (db->key[1]) {
5505 /* If we already ran a binding don't process another one since
5506 * we can't keep track of all the binding keys that were
5507 * pressed in order to swallow the release events. */
5508 send = 1;
5509 check_binding = 0;
5510 }
5511
5512 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005513 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5514 key, state)) {
5515 /* We ran a binding so swallow the press and keep the
5516 * grab to swallow the released too. */
5517 send = 0;
5518 terminate = 0;
5519 db->key[1] = key;
5520 } else {
5521 /* Terminate the grab since the key pressed is not a
5522 * debug binding key. */
5523 send = 1;
5524 terminate = 1;
5525 }
5526 }
5527
5528 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005529 serial = wl_display_next_serial(display);
5530 resource_list = &grab->keyboard->focus_resource_list;
5531 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005532 wl_keyboard_send_key(resource, serial, time, key, state);
5533 }
5534 }
5535
5536 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005537 weston_keyboard_end_grab(grab->keyboard);
5538 if (grab->keyboard->input_method_resource)
5539 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005540 free(db);
5541 }
5542}
5543
5544static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005545debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005546 uint32_t mods_depressed, uint32_t mods_latched,
5547 uint32_t mods_locked, uint32_t group)
5548{
5549 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005550 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005551
Neil Roberts96d790e2013-09-19 17:32:00 +01005552 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005553
Neil Roberts96d790e2013-09-19 17:32:00 +01005554 wl_resource_for_each(resource, resource_list) {
5555 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5556 mods_latched, mods_locked, group);
5557 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005558}
5559
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005560static void
5561debug_binding_cancel(struct weston_keyboard_grab *grab)
5562{
5563 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5564
5565 weston_keyboard_end_grab(grab->keyboard);
5566 free(db);
5567}
5568
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005569struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005570 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005571 debug_binding_modifiers,
5572 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005573};
5574
5575static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005576debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005577{
5578 struct debug_binding_grab *grab;
5579
5580 grab = calloc(1, sizeof *grab);
5581 if (!grab)
5582 return;
5583
5584 grab->seat = (struct weston_seat *) seat;
5585 grab->key[0] = key;
5586 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005587 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005588}
5589
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005590static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005591force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005592 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005593{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005594 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005595 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005596 struct desktop_shell *shell = data;
5597 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005598 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005599
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005600 focus_surface = seat->keyboard->focus;
5601 if (!focus_surface)
5602 return;
5603
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005604 wl_signal_emit(&compositor->kill_signal, focus_surface);
5605
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005606 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005607 wl_client_get_credentials(client, &pid, NULL, NULL);
5608
5609 /* Skip clients that we launched ourselves (the credentials of
5610 * the socketpair is ours) */
5611 if (pid == getpid())
5612 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005613
Daniel Stone325fc2d2012-05-30 16:31:58 +01005614 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005615}
5616
5617static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005618workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005619 uint32_t key, void *data)
5620{
5621 struct desktop_shell *shell = data;
5622 unsigned int new_index = shell->workspaces.current;
5623
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005624 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005625 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005626 if (new_index != 0)
5627 new_index--;
5628
5629 change_workspace(shell, new_index);
5630}
5631
5632static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005633workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005634 uint32_t key, void *data)
5635{
5636 struct desktop_shell *shell = data;
5637 unsigned int new_index = shell->workspaces.current;
5638
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005639 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005640 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005641 if (new_index < shell->workspaces.num - 1)
5642 new_index++;
5643
5644 change_workspace(shell, new_index);
5645}
5646
5647static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005648workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005649 uint32_t key, void *data)
5650{
5651 struct desktop_shell *shell = data;
5652 unsigned int new_index;
5653
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005654 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005655 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005656 new_index = key - KEY_F1;
5657 if (new_index >= shell->workspaces.num)
5658 new_index = shell->workspaces.num - 1;
5659
5660 change_workspace(shell, new_index);
5661}
5662
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005663static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005664workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005665 uint32_t key, void *data)
5666{
5667 struct desktop_shell *shell = data;
5668 unsigned int new_index = shell->workspaces.current;
5669
5670 if (shell->locked)
5671 return;
5672
5673 if (new_index != 0)
5674 new_index--;
5675
5676 take_surface_to_workspace_by_seat(shell, seat, new_index);
5677}
5678
5679static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005680workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005681 uint32_t key, void *data)
5682{
5683 struct desktop_shell *shell = data;
5684 unsigned int new_index = shell->workspaces.current;
5685
5686 if (shell->locked)
5687 return;
5688
5689 if (new_index < shell->workspaces.num - 1)
5690 new_index++;
5691
5692 take_surface_to_workspace_by_seat(shell, seat, new_index);
5693}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005694
5695static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005696shell_reposition_view_on_output_destroy(struct weston_view *view)
5697{
5698 struct weston_output *output, *first_output;
5699 struct weston_compositor *ec = view->surface->compositor;
5700 struct shell_surface *shsurf;
5701 float x, y;
5702 int visible;
5703
5704 x = view->geometry.x;
5705 y = view->geometry.y;
5706
5707 /* At this point the destroyed output is not in the list anymore.
5708 * If the view is still visible somewhere, we leave where it is,
5709 * otherwise, move it to the first output. */
5710 visible = 0;
5711 wl_list_for_each(output, &ec->output_list, link) {
5712 if (pixman_region32_contains_point(&output->region,
5713 x, y, NULL)) {
5714 visible = 1;
5715 break;
5716 }
5717 }
5718
5719 if (!visible) {
5720 first_output = container_of(ec->output_list.next,
5721 struct weston_output, link);
5722
5723 x = first_output->x + first_output->width / 4;
5724 y = first_output->y + first_output->height / 4;
5725 }
5726
5727 weston_view_set_position(view, x, y);
5728
5729 shsurf = get_shell_surface(view->surface);
5730
5731 if (shsurf) {
5732 shsurf->saved_position_valid = false;
5733 shsurf->next_state.maximized = false;
5734 shsurf->next_state.fullscreen = false;
5735 shsurf->state_changed = true;
5736 }
5737}
5738
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005739void
5740shell_for_each_layer(struct desktop_shell *shell,
5741 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005742{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005743 struct workspace **ws;
5744
5745 func(shell, &shell->fullscreen_layer, data);
5746 func(shell, &shell->panel_layer, data);
5747 func(shell, &shell->background_layer, data);
5748 func(shell, &shell->lock_layer, data);
5749 func(shell, &shell->input_panel_layer, data);
5750
5751 wl_array_for_each(ws, &shell->workspaces.array)
5752 func(shell, &(*ws)->layer, data);
5753}
5754
5755static void
5756shell_output_destroy_move_layer(struct desktop_shell *shell,
5757 struct weston_layer *layer,
5758 void *data)
5759{
5760 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005761 struct weston_view *view;
5762
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005763 wl_list_for_each(view, &layer->view_list, layer_link) {
5764 if (view->output != output)
5765 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005766
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005767 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005768 }
5769}
5770
5771static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005772handle_output_destroy(struct wl_listener *listener, void *data)
5773{
5774 struct shell_output *output_listener =
5775 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005776 struct weston_output *output = output_listener->output;
5777 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005778
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005779 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005780
Xiong Zhang6b481422013-10-23 13:58:32 +08005781 wl_list_remove(&output_listener->destroy_listener.link);
5782 wl_list_remove(&output_listener->link);
5783 free(output_listener);
5784}
5785
5786static void
5787create_shell_output(struct desktop_shell *shell,
5788 struct weston_output *output)
5789{
5790 struct shell_output *shell_output;
5791
5792 shell_output = zalloc(sizeof *shell_output);
5793 if (shell_output == NULL)
5794 return;
5795
5796 shell_output->output = output;
5797 shell_output->shell = shell;
5798 shell_output->destroy_listener.notify = handle_output_destroy;
5799 wl_signal_add(&output->destroy_signal,
5800 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005801 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005802}
5803
5804static void
5805handle_output_create(struct wl_listener *listener, void *data)
5806{
5807 struct desktop_shell *shell =
5808 container_of(listener, struct desktop_shell, output_create_listener);
5809 struct weston_output *output = (struct weston_output *)data;
5810
5811 create_shell_output(shell, output);
5812}
5813
5814static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005815handle_output_move_layer(struct desktop_shell *shell,
5816 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005817{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005818 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005819 struct weston_view *view;
5820 float x, y;
5821
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005822 wl_list_for_each(view, &layer->view_list, layer_link) {
5823 if (view->output != output)
5824 continue;
5825
5826 x = view->geometry.x + output->move_x;
5827 y = view->geometry.y + output->move_y;
5828 weston_view_set_position(view, x, y);
5829 }
5830}
5831
5832static void
5833handle_output_move(struct wl_listener *listener, void *data)
5834{
5835 struct desktop_shell *shell;
5836
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005837 shell = container_of(listener, struct desktop_shell,
5838 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005839
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005840 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005841}
5842
5843static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005844setup_output_destroy_handler(struct weston_compositor *ec,
5845 struct desktop_shell *shell)
5846{
5847 struct weston_output *output;
5848
5849 wl_list_init(&shell->output_list);
5850 wl_list_for_each(output, &ec->output_list, link)
5851 create_shell_output(shell, output);
5852
5853 shell->output_create_listener.notify = handle_output_create;
5854 wl_signal_add(&ec->output_created_signal,
5855 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005856
5857 shell->output_move_listener.notify = handle_output_move;
5858 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005859}
5860
5861static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005862shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005863{
Tiago Vignattibe143262012-04-16 17:31:41 +03005864 struct desktop_shell *shell =
5865 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005866 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005867 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005868
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005869 /* Force state to unlocked so we don't try to fade */
5870 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005871 if (shell->child.client)
5872 wl_client_destroy(shell->child.client);
5873
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005874 wl_list_remove(&shell->idle_listener.link);
5875 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005876
5877 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005878
Xiong Zhang6b481422013-10-23 13:58:32 +08005879 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5880 wl_list_remove(&shell_output->destroy_listener.link);
5881 wl_list_remove(&shell_output->link);
5882 free(shell_output);
5883 }
5884
5885 wl_list_remove(&shell->output_create_listener.link);
5886
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005887 wl_array_for_each(ws, &shell->workspaces.array)
5888 workspace_destroy(*ws);
5889 wl_array_release(&shell->workspaces.array);
5890
Pekka Paalanen3c647232011-12-22 13:43:43 +02005891 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005892 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005893 free(shell);
5894}
5895
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005896static void
5897shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5898{
5899 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005900 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005901
5902 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005903 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5904 MODIFIER_CTRL | MODIFIER_ALT,
5905 terminate_binding, ec);
5906 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5907 click_to_activate_binding,
5908 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005909 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5910 click_to_activate_binding,
5911 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005912 weston_compositor_add_touch_binding(ec, 0,
5913 touch_to_activate_binding,
5914 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005915 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5916 MODIFIER_SUPER | MODIFIER_ALT,
5917 surface_opacity_binding, NULL);
5918 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5919 MODIFIER_SUPER, zoom_axis_binding,
5920 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005921
5922 /* configurable bindings */
5923 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005924 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5925 zoom_key_binding, NULL);
5926 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5927 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005928 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5929 maximize_binding, NULL);
5930 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5931 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005932 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5933 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005934 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005935 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5936 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005937 weston_compositor_add_button_binding(ec, BTN_LEFT,
5938 mod | MODIFIER_SHIFT,
5939 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005940
5941 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5942 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5943 rotate_binding, NULL);
5944
Daniel Stone325fc2d2012-05-30 16:31:58 +01005945 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5946 shell);
5947 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5948 ec);
5949 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5950 backlight_binding, ec);
5951 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5952 ec);
5953 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5954 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005955 weston_compositor_add_key_binding(ec, KEY_K, mod,
5956 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005957 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5958 workspace_up_binding, shell);
5959 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5960 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005961 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5962 workspace_move_surface_up_binding,
5963 shell);
5964 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5965 workspace_move_surface_down_binding,
5966 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005967
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005968 if (shell->exposay_modifier)
5969 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5970 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005971
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005972 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5973 if (shell->workspaces.num > 1) {
5974 num_workspace_bindings = shell->workspaces.num;
5975 if (num_workspace_bindings > 6)
5976 num_workspace_bindings = 6;
5977 for (i = 0; i < num_workspace_bindings; i++)
5978 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5979 workspace_f_binding,
5980 shell);
5981 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005982
5983 /* Debug bindings */
5984 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5985 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005986}
5987
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005988WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005989module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005990 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005991{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005992 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005993 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005994 struct workspace **pws;
5995 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005996 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005997
Peter Huttererf3d62272013-08-08 11:57:05 +10005998 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005999 if (shell == NULL)
6000 return -1;
6001
Kristian Høgsberg75840622011-09-06 13:48:16 -04006002 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006003
6004 shell->destroy_listener.notify = shell_destroy;
6005 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006006 shell->idle_listener.notify = idle_handler;
6007 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6008 shell->wake_listener.notify = wake_handler;
6009 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006010
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006011 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006012 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006013 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006014 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006015 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006016 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006017 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006018 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006019 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006020 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006021
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006022 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6023 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006024 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6025 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006026 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006027
6028 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006029 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006030
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006031 if (input_panel_setup(shell) < 0)
6032 return -1;
6033
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006034 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006035
Daniel Stonedf8133b2013-11-19 11:37:14 +01006036 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6037 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6038
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006039 for (i = 0; i < shell->workspaces.num; i++) {
6040 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6041 if (pws == NULL)
6042 return -1;
6043
6044 *pws = workspace_create();
6045 if (*pws == NULL)
6046 return -1;
6047 }
6048 activate_workspace(shell, 0);
6049
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006050 weston_layer_init(&shell->minimized_layer, NULL);
6051
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006052 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006053 wl_list_init(&shell->workspaces.animation.link);
6054 shell->workspaces.animation.frame = animate_workspace_change_frame;
6055
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006056 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006057 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006058 return -1;
6059
Rafael Antognollie2a34552013-12-03 15:35:45 -02006060 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6061 shell, bind_xdg_shell) == NULL)
6062 return -1;
6063
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006064 if (wl_global_create(ec->wl_display,
6065 &desktop_shell_interface, 2,
6066 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006067 return -1;
6068
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006069 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6070 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006071 return -1;
6072
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006073 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6074 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006075 return -1;
6076
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006077 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006078
Xiong Zhang6b481422013-10-23 13:58:32 +08006079 setup_output_destroy_handler(ec, shell);
6080
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006081 loop = wl_display_get_event_loop(ec->wl_display);
6082 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006083
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006084 shell->screensaver.timer =
6085 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6086
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006087 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006088 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006089 create_keyboard_focus_listener(seat);
6090 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006091
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006092 screenshooter_create(ec);
6093
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006094 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006095
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006096 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006097
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006098 return 0;
6099}