blob: 690a3be3a7959072a1e7fefa7ed2cd92f829beeb [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
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700367 if (touch->seat->pointer)
368 popup_grab_end(touch->seat->pointer);
369
Rusty Lynch1084da52013-08-15 09:10:08 -0700370 grab->grab.interface = interface;
371 grab->shsurf = shsurf;
372 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
373 wl_signal_add(&shsurf->destroy_signal,
374 &grab->shsurf_destroy_listener);
375
376 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700377 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700378
379 weston_touch_start_grab(touch, &grab->grab);
380 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500381 weston_touch_set_focus(touch->seat,
382 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700383}
384
385static void
386shell_touch_grab_end(struct shell_touch_grab *grab)
387{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700388 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700389 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700390 grab->shsurf->grabbed = 0;
391 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700392
393 weston_touch_end_grab(grab->touch);
394}
395
396static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800398 struct weston_output *output);
399
Daniel Stone496ca172012-05-30 16:31:42 +0100400static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300401get_modifier(char *modifier)
402{
403 if (!modifier)
404 return MODIFIER_SUPER;
405
406 if (!strcmp("ctrl", modifier))
407 return MODIFIER_CTRL;
408 else if (!strcmp("alt", modifier))
409 return MODIFIER_ALT;
410 else if (!strcmp("super", modifier))
411 return MODIFIER_SUPER;
412 else
413 return MODIFIER_SUPER;
414}
415
Juan Zhaoe10d2792012-04-25 19:09:52 +0800416static enum animation_type
417get_animation_type(char *animation)
418{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800419 if (!animation)
420 return ANIMATION_NONE;
421
Juan Zhaoe10d2792012-04-25 19:09:52 +0800422 if (!strcmp("zoom", animation))
423 return ANIMATION_ZOOM;
424 else if (!strcmp("fade", animation))
425 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100426 else if (!strcmp("dim-layer", animation))
427 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800428 else
429 return ANIMATION_NONE;
430}
431
Alex Wu4539b082012-03-01 12:57:46 +0800432static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400433shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200434{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400435 struct weston_config_section *section;
436 int duration;
437 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200438
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400439 section = weston_config_get_section(shell->compositor->config,
440 "screensaver", NULL, NULL);
441 weston_config_section_get_string(section,
442 "path", &shell->screensaver.path, NULL);
443 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200444 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400445
446 section = weston_config_get_section(shell->compositor->config,
447 "shell", NULL, NULL);
448 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100449 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100450 shell->client = s;
451 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400452 "binding-modifier", &s, "super");
453 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200454 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800455
456 weston_config_section_get_string(section,
457 "exposay-modifier", &s, "none");
458 if (strcmp(s, "none") == 0)
459 shell->exposay_modifier = 0;
460 else
461 shell->exposay_modifier = get_modifier(s);
462 free(s);
463
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400464 weston_config_section_get_string(section, "animation", &s, "none");
465 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200466 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700467 weston_config_section_get_string(section,
468 "startup-animation", &s, "fade");
469 shell->startup_animation_type = get_animation_type(s);
470 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700471 if (shell->startup_animation_type == ANIMATION_ZOOM)
472 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100473 weston_config_section_get_string(section, "focus-animation", &s, "none");
474 shell->focus_animation_type = get_animation_type(s);
475 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400476 weston_config_section_get_uint(section, "num-workspaces",
477 &shell->workspaces.num,
478 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200479}
480
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800481struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100482get_default_output(struct weston_compositor *compositor)
483{
484 return container_of(compositor->output_list.next,
485 struct weston_output, link);
486}
487
488
489/* no-op func for checking focus surface */
490static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600491focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100492{
493}
494
495static struct focus_surface *
496get_focus_surface(struct weston_surface *surface)
497{
498 if (surface->configure == focus_surface_configure)
499 return surface->configure_private;
500 else
501 return NULL;
502}
503
504static bool
505is_focus_surface (struct weston_surface *es)
506{
507 return (es->configure == focus_surface_configure);
508}
509
510static bool
511is_focus_view (struct weston_view *view)
512{
513 return is_focus_surface (view->surface);
514}
515
516static struct focus_surface *
517create_focus_surface(struct weston_compositor *ec,
518 struct weston_output *output)
519{
520 struct focus_surface *fsurf = NULL;
521 struct weston_surface *surface = NULL;
522
523 fsurf = malloc(sizeof *fsurf);
524 if (!fsurf)
525 return NULL;
526
527 fsurf->surface = weston_surface_create(ec);
528 surface = fsurf->surface;
529 if (surface == NULL) {
530 free(fsurf);
531 return NULL;
532 }
533
534 surface->configure = focus_surface_configure;
535 surface->output = output;
536 surface->configure_private = fsurf;
537
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800538 fsurf->view = weston_view_create(surface);
539 if (fsurf->view == NULL) {
540 weston_surface_destroy(surface);
541 free(fsurf);
542 return NULL;
543 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100544 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600546 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600547 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100548 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
549 pixman_region32_fini(&surface->opaque);
550 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
551 output->width, output->height);
552 pixman_region32_fini(&surface->input);
553 pixman_region32_init(&surface->input);
554
555 wl_list_init(&fsurf->workspace_transform.link);
556
557 return fsurf;
558}
559
560static void
561focus_surface_destroy(struct focus_surface *fsurf)
562{
563 weston_surface_destroy(fsurf->surface);
564 free(fsurf);
565}
566
567static void
568focus_animation_done(struct weston_view_animation *animation, void *data)
569{
570 struct workspace *ws = data;
571
572 ws->focus_animation = NULL;
573}
574
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200575static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200576focus_state_destroy(struct focus_state *state)
577{
578 wl_list_remove(&state->seat_destroy_listener.link);
579 wl_list_remove(&state->surface_destroy_listener.link);
580 free(state);
581}
582
583static void
584focus_state_seat_destroy(struct wl_listener *listener, void *data)
585{
586 struct focus_state *state = container_of(listener,
587 struct focus_state,
588 seat_destroy_listener);
589
590 wl_list_remove(&state->link);
591 focus_state_destroy(state);
592}
593
594static void
595focus_state_surface_destroy(struct wl_listener *listener, void *data)
596{
597 struct focus_state *state = container_of(listener,
598 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400599 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400600 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500601 struct weston_surface *main_surface, *next;
602 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200603
Pekka Paalanen01388e22013-04-25 13:57:44 +0300604 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
605
Kristian Høgsberge3778222012-07-31 17:29:30 -0400606 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500607 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
608 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100610 if (is_focus_view(view))
611 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400612
Jason Ekstranda7af7042013-10-12 22:38:11 -0500613 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400614 break;
615 }
616
Pekka Paalanen01388e22013-04-25 13:57:44 +0300617 /* if the focus was a sub-surface, activate its main surface */
618 if (main_surface != state->keyboard_focus)
619 next = main_surface;
620
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100621 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400622 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100623 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400624 activate(shell, next, state->seat);
625 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100626 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
627 if (state->ws->focus_animation)
628 weston_view_animation_destroy(state->ws->focus_animation);
629
630 state->ws->focus_animation = weston_fade_run(
631 state->ws->fsurf_front->view,
632 state->ws->fsurf_front->view->alpha, 0.0, 300,
633 focus_animation_done, state->ws);
634 }
635
Kristian Høgsberge3778222012-07-31 17:29:30 -0400636 wl_list_remove(&state->link);
637 focus_state_destroy(state);
638 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200639}
640
641static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400642focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200643{
Jonas Ådahl04769742012-06-13 00:01:24 +0200644 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200645
646 state = malloc(sizeof *state);
647 if (state == NULL)
648 return NULL;
649
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100650 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400651 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200652 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400653 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200654
655 state->seat_destroy_listener.notify = focus_state_seat_destroy;
656 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400657 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200658 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400659 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200660
661 return state;
662}
663
Jonas Ådahl8538b222012-08-29 22:13:03 +0200664static struct focus_state *
665ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
666{
667 struct workspace *ws = get_current_workspace(shell);
668 struct focus_state *state;
669
670 wl_list_for_each(state, &ws->focus_list, link)
671 if (state->seat == seat)
672 break;
673
674 if (&state->link == &ws->focus_list)
675 state = focus_state_create(seat, ws);
676
677 return state;
678}
679
Jonas Ådahl04769742012-06-13 00:01:24 +0200680static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800681focus_state_set_focus(struct focus_state *state,
682 struct weston_surface *surface)
683{
684 if (state->keyboard_focus) {
685 wl_list_remove(&state->surface_destroy_listener.link);
686 wl_list_init(&state->surface_destroy_listener.link);
687 }
688
689 state->keyboard_focus = surface;
690 if (surface)
691 wl_signal_add(&surface->destroy_signal,
692 &state->surface_destroy_listener);
693}
694
695static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400696restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200697{
698 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400699 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100700 struct wl_list pending_seat_list;
701 struct weston_seat *seat, *next_seat;
702
703 /* Temporarily steal the list of seats so that we can keep
704 * track of the seats we've already processed */
705 wl_list_init(&pending_seat_list);
706 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
707 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200708
709 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100710 wl_list_remove(&state->seat->link);
711 wl_list_insert(&shell->compositor->seat_list,
712 &state->seat->link);
713
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800714 if (state->seat->keyboard == NULL)
715 continue;
716
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400717 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200718
Kristian Høgsberge3148752013-05-06 23:19:49 -0400719 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200720 }
Neil Roberts4237f502014-04-09 16:33:31 +0100721
722 /* For any remaining seats that we don't have a focus state
723 * for we'll reset the keyboard focus to NULL */
724 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
725 wl_list_insert(&shell->compositor->seat_list, &seat->link);
726
727 if (state->seat->keyboard == NULL)
728 continue;
729
730 weston_keyboard_set_focus(seat->keyboard, NULL);
731 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200732}
733
734static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200735replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
736 struct weston_seat *seat)
737{
738 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200739
740 wl_list_for_each(state, &ws->focus_list, link) {
741 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800742 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200743 return;
744 }
745 }
746}
747
748static void
749drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
750 struct weston_surface *surface)
751{
752 struct focus_state *state;
753
754 wl_list_for_each(state, &ws->focus_list, link)
755 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800756 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200757}
758
759static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100760animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
761 struct weston_view *from, struct weston_view *to)
762{
763 struct weston_output *output;
764 bool focus_surface_created = false;
765
766 /* FIXME: Only support dim animation using two layers */
767 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
768 return;
769
770 output = get_default_output(shell->compositor);
771 if (ws->fsurf_front == NULL && (from || to)) {
772 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800773 if (ws->fsurf_front == NULL)
774 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100775 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800776
777 ws->fsurf_back = create_focus_surface(shell->compositor, output);
778 if (ws->fsurf_back == NULL) {
779 focus_surface_destroy(ws->fsurf_front);
780 return;
781 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100782 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800783
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100784 focus_surface_created = true;
785 } else {
786 wl_list_remove(&ws->fsurf_front->view->layer_link);
787 wl_list_remove(&ws->fsurf_back->view->layer_link);
788 }
789
790 if (ws->focus_animation) {
791 weston_view_animation_destroy(ws->focus_animation);
792 ws->focus_animation = NULL;
793 }
794
795 if (to)
796 wl_list_insert(&to->layer_link,
797 &ws->fsurf_front->view->layer_link);
798 else if (from)
799 wl_list_insert(&ws->layer.view_list,
800 &ws->fsurf_front->view->layer_link);
801
802 if (focus_surface_created) {
803 ws->focus_animation = weston_fade_run(
804 ws->fsurf_front->view,
805 ws->fsurf_front->view->alpha, 0.6, 300,
806 focus_animation_done, ws);
807 } else if (from) {
808 wl_list_insert(&from->layer_link,
809 &ws->fsurf_back->view->layer_link);
810 ws->focus_animation = weston_stable_fade_run(
811 ws->fsurf_front->view, 0.0,
812 ws->fsurf_back->view, 0.6,
813 focus_animation_done, ws);
814 } else if (to) {
815 wl_list_insert(&ws->layer.view_list,
816 &ws->fsurf_back->view->layer_link);
817 ws->focus_animation = weston_stable_fade_run(
818 ws->fsurf_front->view, 0.0,
819 ws->fsurf_back->view, 0.6,
820 focus_animation_done, ws);
821 }
822}
823
824static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200825workspace_destroy(struct workspace *ws)
826{
Jonas Ådahl04769742012-06-13 00:01:24 +0200827 struct focus_state *state, *next;
828
829 wl_list_for_each_safe(state, next, &ws->focus_list, link)
830 focus_state_destroy(state);
831
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 if (ws->fsurf_front)
833 focus_surface_destroy(ws->fsurf_front);
834 if (ws->fsurf_back)
835 focus_surface_destroy(ws->fsurf_back);
836
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200837 free(ws);
838}
839
Jonas Ådahl04769742012-06-13 00:01:24 +0200840static void
841seat_destroyed(struct wl_listener *listener, void *data)
842{
843 struct weston_seat *seat = data;
844 struct focus_state *state, *next;
845 struct workspace *ws = container_of(listener,
846 struct workspace,
847 seat_destroyed_listener);
848
849 wl_list_for_each_safe(state, next, &ws->focus_list, link)
850 if (state->seat == seat)
851 wl_list_remove(&state->link);
852}
853
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200854static struct workspace *
855workspace_create(void)
856{
857 struct workspace *ws = malloc(sizeof *ws);
858 if (ws == NULL)
859 return NULL;
860
861 weston_layer_init(&ws->layer, NULL);
862
Jonas Ådahl04769742012-06-13 00:01:24 +0200863 wl_list_init(&ws->focus_list);
864 wl_list_init(&ws->seat_destroyed_listener.link);
865 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 ws->fsurf_front = NULL;
867 ws->fsurf_back = NULL;
868 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200869
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200870 return ws;
871}
872
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200873static int
874workspace_is_empty(struct workspace *ws)
875{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200877}
878
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200879static struct workspace *
880get_workspace(struct desktop_shell *shell, unsigned int index)
881{
882 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200883 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200884 pws += index;
885 return *pws;
886}
887
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800888struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200889get_current_workspace(struct desktop_shell *shell)
890{
891 return get_workspace(shell, shell->workspaces.current);
892}
893
894static void
895activate_workspace(struct desktop_shell *shell, unsigned int index)
896{
897 struct workspace *ws;
898
899 ws = get_workspace(shell, index);
900 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
901
902 shell->workspaces.current = index;
903}
904
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905static unsigned int
906get_output_height(struct weston_output *output)
907{
908 return abs(output->region.extents.y1 - output->region.extents.y2);
909}
910
911static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100912view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200913{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200914 struct weston_transform *transform;
915
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100916 if (is_focus_view(view)) {
917 struct focus_surface *fsurf = get_focus_surface(view->surface);
918 transform = &fsurf->workspace_transform;
919 } else {
920 struct shell_surface *shsurf = get_shell_surface(view->surface);
921 transform = &shsurf->workspace_transform;
922 }
923
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100926 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100928 weston_matrix_init(&transform->matrix);
929 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932}
933
934static void
935workspace_translate_out(struct workspace *ws, double fraction)
936{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200938 unsigned int height;
939 double d;
940
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
942 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200943 d = height * fraction;
944
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100945 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200946 }
947}
948
949static void
950workspace_translate_in(struct workspace *ws, double fraction)
951{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953 unsigned int height;
954 double d;
955
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
957 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958
959 if (fraction > 0)
960 d = -(height - height * fraction);
961 else
962 d = height + height * fraction;
963
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965 }
966}
967
968static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200969broadcast_current_workspace_state(struct desktop_shell *shell)
970{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700971 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200972
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700973 wl_resource_for_each(resource, &shell->workspaces.client_list)
974 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200975 shell->workspaces.current,
976 shell->workspaces.num);
977}
978
979static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980reverse_workspace_change_animation(struct desktop_shell *shell,
981 unsigned int index,
982 struct workspace *from,
983 struct workspace *to)
984{
985 shell->workspaces.current = index;
986
987 shell->workspaces.anim_to = to;
988 shell->workspaces.anim_from = from;
989 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
990 shell->workspaces.anim_timestamp = 0;
991
Scott Moreau4272e632012-08-13 09:58:41 -0600992 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993}
994
995static void
996workspace_deactivate_transforms(struct workspace *ws)
997{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001002 if (is_focus_view(view)) {
1003 struct focus_surface *fsurf = get_focus_surface(view->surface);
1004 transform = &fsurf->workspace_transform;
1005 } else {
1006 struct shell_surface *shsurf = get_shell_surface(view->surface);
1007 transform = &shsurf->workspace_transform;
1008 }
1009
1010 if (!wl_list_empty(&transform->link)) {
1011 wl_list_remove(&transform->link);
1012 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001013 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015 }
1016}
1017
1018static void
1019finish_workspace_change_animation(struct desktop_shell *shell,
1020 struct workspace *from,
1021 struct workspace *to)
1022{
Scott Moreau4272e632012-08-13 09:58:41 -06001023 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024
1025 wl_list_remove(&shell->workspaces.animation.link);
1026 workspace_deactivate_transforms(from);
1027 workspace_deactivate_transforms(to);
1028 shell->workspaces.anim_to = NULL;
1029
1030 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1031}
1032
1033static void
1034animate_workspace_change_frame(struct weston_animation *animation,
1035 struct weston_output *output, uint32_t msecs)
1036{
1037 struct desktop_shell *shell =
1038 container_of(animation, struct desktop_shell,
1039 workspaces.animation);
1040 struct workspace *from = shell->workspaces.anim_from;
1041 struct workspace *to = shell->workspaces.anim_to;
1042 uint32_t t;
1043 double x, y;
1044
1045 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1046 finish_workspace_change_animation(shell, from, to);
1047 return;
1048 }
1049
1050 if (shell->workspaces.anim_timestamp == 0) {
1051 if (shell->workspaces.anim_current == 0.0)
1052 shell->workspaces.anim_timestamp = msecs;
1053 else
1054 shell->workspaces.anim_timestamp =
1055 msecs -
1056 /* Invers of movement function 'y' below. */
1057 (asin(1.0 - shell->workspaces.anim_current) *
1058 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1059 M_2_PI);
1060 }
1061
1062 t = msecs - shell->workspaces.anim_timestamp;
1063
1064 /*
1065 * x = [0, π/2]
1066 * y(x) = sin(x)
1067 */
1068 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1069 y = sin(x);
1070
1071 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001072 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001073
1074 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1075 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1076 shell->workspaces.anim_current = y;
1077
Scott Moreau4272e632012-08-13 09:58:41 -06001078 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001079 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001080 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001081 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001082}
1083
1084static void
1085animate_workspace_change(struct desktop_shell *shell,
1086 unsigned int index,
1087 struct workspace *from,
1088 struct workspace *to)
1089{
1090 struct weston_output *output;
1091
1092 int dir;
1093
1094 if (index > shell->workspaces.current)
1095 dir = -1;
1096 else
1097 dir = 1;
1098
1099 shell->workspaces.current = index;
1100
1101 shell->workspaces.anim_dir = dir;
1102 shell->workspaces.anim_from = from;
1103 shell->workspaces.anim_to = to;
1104 shell->workspaces.anim_current = 0.0;
1105 shell->workspaces.anim_timestamp = 0;
1106
1107 output = container_of(shell->compositor->output_list.next,
1108 struct weston_output, link);
1109 wl_list_insert(&output->animation_list,
1110 &shell->workspaces.animation.link);
1111
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001112 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001113
1114 workspace_translate_in(to, 0);
1115
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001116 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001117
Scott Moreau4272e632012-08-13 09:58:41 -06001118 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119}
1120
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001121static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001122update_workspace(struct desktop_shell *shell, unsigned int index,
1123 struct workspace *from, struct workspace *to)
1124{
1125 shell->workspaces.current = index;
1126 wl_list_insert(&from->layer.link, &to->layer.link);
1127 wl_list_remove(&from->layer.link);
1128}
1129
1130static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001131change_workspace(struct desktop_shell *shell, unsigned int index)
1132{
1133 struct workspace *from;
1134 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001135 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001136
1137 if (index == shell->workspaces.current)
1138 return;
1139
1140 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001142 return;
1143
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001144 from = get_current_workspace(shell);
1145 to = get_workspace(shell, index);
1146
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 if (shell->workspaces.anim_from == to &&
1148 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001149 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001151 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 return;
1153 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001154
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155 if (shell->workspaces.anim_to != NULL)
1156 finish_workspace_change_animation(shell,
1157 shell->workspaces.anim_from,
1158 shell->workspaces.anim_to);
1159
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001161
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001162 if (shell->focus_animation_type != ANIMATION_NONE) {
1163 wl_list_for_each(state, &from->focus_list, link)
1164 if (state->keyboard_focus)
1165 animate_focus_change(shell, from,
1166 get_default_view(state->keyboard_focus), NULL);
1167
1168 wl_list_for_each(state, &to->focus_list, link)
1169 if (state->keyboard_focus)
1170 animate_focus_change(shell, to,
1171 NULL, get_default_view(state->keyboard_focus));
1172 }
1173
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001174 if (workspace_is_empty(to) && workspace_is_empty(from))
1175 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176 else
1177 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001178
1179 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001180}
1181
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001182static bool
1183workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1184{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001186 struct wl_list *e;
1187
1188 if (wl_list_empty(list))
1189 return false;
1190
1191 e = list->next;
1192
1193 if (e->next != list)
1194 return false;
1195
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197}
1198
1199static void
Philip Withnall659163d2013-11-25 18:01:36 +00001200move_surface_to_workspace(struct desktop_shell *shell,
1201 struct shell_surface *shsurf,
1202 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001203{
1204 struct workspace *from;
1205 struct workspace *to;
1206 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001207 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001208 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001209
1210 if (workspace == shell->workspaces.current)
1211 return;
1212
Philip Withnall659163d2013-11-25 18:01:36 +00001213 view = get_default_view(shsurf->surface);
1214 if (!view)
1215 return;
1216
1217 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1218
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001219 if (workspace >= shell->workspaces.num)
1220 workspace = shell->workspaces.num - 1;
1221
Jonas Ådahle9d22502012-08-29 22:13:01 +02001222 from = get_current_workspace(shell);
1223 to = get_workspace(shell, workspace);
1224
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 wl_list_remove(&view->layer_link);
1226 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001227
Philip Withnall648a4dd2013-11-25 18:01:44 +00001228 shell_surface_update_child_surface_layers(shsurf);
1229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001231 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1232 if (!seat->keyboard)
1233 continue;
1234
1235 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001237 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001238 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001241}
1242
1243static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001245 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001246 unsigned int index)
1247{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001248 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001250 struct shell_surface *shsurf;
1251 struct workspace *from;
1252 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001253 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001254
Pekka Paalanen01388e22013-04-25 13:57:44 +03001255 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 view = get_default_view(surface);
1257 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001258 index == shell->workspaces.current ||
1259 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001260 return;
1261
1262 from = get_current_workspace(shell);
1263 to = get_workspace(shell, index);
1264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 wl_list_remove(&view->layer_link);
1266 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267
Philip Withnall659163d2013-11-25 18:01:36 +00001268 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001269 if (shsurf != NULL)
1270 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001271
Jonas Ådahle9d22502012-08-29 22:13:01 +02001272 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001273 drop_focus_state(shell, from, surface);
1274
1275 if (shell->workspaces.anim_from == to &&
1276 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001277 wl_list_remove(&to->layer.link);
1278 wl_list_insert(from->layer.link.prev, &to->layer.link);
1279
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001281 broadcast_current_workspace_state(shell);
1282
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283 return;
1284 }
1285
1286 if (shell->workspaces.anim_to != NULL)
1287 finish_workspace_change_animation(shell,
1288 shell->workspaces.anim_from,
1289 shell->workspaces.anim_to);
1290
1291 if (workspace_is_empty(from) &&
1292 workspace_has_only(to, surface))
1293 update_workspace(shell, index, from, to);
1294 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001295 if (shsurf != NULL &&
1296 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297 wl_list_insert(&shell->workspaces.anim_sticky_list,
1298 &shsurf->workspace_transform.link);
1299
1300 animate_workspace_change(shell, index, from, to);
1301 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302
1303 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001304
1305 state = ensure_focus_state(shell, seat);
1306 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001307 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001308}
1309
1310static void
1311workspace_manager_move_surface(struct wl_client *client,
1312 struct wl_resource *resource,
1313 struct wl_resource *surface_resource,
1314 uint32_t workspace)
1315{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001316 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001318 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001319 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001320 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001321
Pekka Paalanen01388e22013-04-25 13:57:44 +03001322 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001323 shell_surface = get_shell_surface(main_surface);
1324 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001326
1327 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001328}
1329
1330static const struct workspace_manager_interface workspace_manager_implementation = {
1331 workspace_manager_move_surface,
1332};
1333
1334static void
1335unbind_resource(struct wl_resource *resource)
1336{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001337 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001338}
1339
1340static void
1341bind_workspace_manager(struct wl_client *client,
1342 void *data, uint32_t version, uint32_t id)
1343{
1344 struct desktop_shell *shell = data;
1345 struct wl_resource *resource;
1346
Jason Ekstranda85118c2013-06-27 20:17:02 -05001347 resource = wl_resource_create(client,
1348 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001349
1350 if (resource == NULL) {
1351 weston_log("couldn't add workspace manager object");
1352 return;
1353 }
1354
Jason Ekstranda85118c2013-06-27 20:17:02 -05001355 wl_resource_set_implementation(resource,
1356 &workspace_manager_implementation,
1357 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001358 wl_list_insert(&shell->workspaces.client_list,
1359 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001360
1361 workspace_manager_send_state(resource,
1362 shell->workspaces.current,
1363 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001364}
1365
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001366static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001367touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1368 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1369{
1370}
1371
1372static void
1373touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1374{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001375 struct weston_touch_move_grab *move =
1376 (struct weston_touch_move_grab *) container_of(
1377 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001378
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001379 if (touch_id == 0)
1380 move->active = 0;
1381
Jonas Ådahl9484b692013-12-02 22:05:03 +01001382 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001383 shell_touch_grab_end(&move->base);
1384 free(move);
1385 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001386}
1387
1388static void
1389touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1390 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1391{
1392 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1393 struct shell_surface *shsurf = move->base.shsurf;
1394 struct weston_surface *es;
1395 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1396 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1397
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001398 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001399 return;
1400
1401 es = shsurf->surface;
1402
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001403 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001404
1405 weston_compositor_schedule_repaint(es->compositor);
1406}
1407
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001408static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001409touch_move_grab_frame(struct weston_touch_grab *grab)
1410{
1411}
1412
1413static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001414touch_move_grab_cancel(struct weston_touch_grab *grab)
1415{
1416 struct weston_touch_move_grab *move =
1417 (struct weston_touch_move_grab *) container_of(
1418 grab, struct shell_touch_grab, grab);
1419
1420 shell_touch_grab_end(&move->base);
1421 free(move);
1422}
1423
Rusty Lynch1084da52013-08-15 09:10:08 -07001424static const struct weston_touch_grab_interface touch_move_grab_interface = {
1425 touch_move_grab_down,
1426 touch_move_grab_up,
1427 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001428 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001429 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001430};
1431
1432static int
1433surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1434{
1435 struct weston_touch_move_grab *move;
1436
1437 if (!shsurf)
1438 return -1;
1439
Rafael Antognolli03b16592013-12-03 15:35:42 -02001440 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001441 return 0;
1442
1443 move = malloc(sizeof *move);
1444 if (!move)
1445 return -1;
1446
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001447 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001449 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001451 seat->touch->grab_y;
1452
1453 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1454 seat->touch);
1455
1456 return 0;
1457}
1458
1459static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001460noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001461{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001462}
1463
1464static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001465move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1466 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001467{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001468 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001469 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001470 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001471 int dx, dy;
1472
1473 weston_pointer_move(pointer, x, y);
1474 dx = wl_fixed_to_int(pointer->x + move->dx);
1475 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001476
1477 if (!shsurf)
1478 return;
1479
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001480 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001481
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001483}
1484
1485static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001486move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001487 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001488{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001489 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1490 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001491 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001492 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001493
Daniel Stone4dbadb12012-05-30 16:31:51 +01001494 if (pointer->button_count == 0 &&
1495 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001496 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001497 free(grab);
1498 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001499}
1500
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001501static void
1502move_grab_cancel(struct weston_pointer_grab *grab)
1503{
1504 struct shell_grab *shell_grab =
1505 container_of(grab, struct shell_grab, grab);
1506
1507 shell_grab_end(shell_grab);
1508 free(grab);
1509}
1510
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001511static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001512 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513 move_grab_motion,
1514 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001515 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001516};
1517
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001518static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001519surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001520{
1521 struct weston_move_grab *move;
1522
1523 if (!shsurf)
1524 return -1;
1525
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001526 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001527 return 0;
1528
1529 move = malloc(sizeof *move);
1530 if (!move)
1531 return -1;
1532
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001534 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001536 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001537
1538 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001539 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540
1541 return 0;
1542}
1543
1544static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001545common_surface_move(struct wl_resource *resource,
1546 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001547{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001548 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001549 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001550 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001551
Kristian Høgsbergd0b40ed2014-04-29 14:47:46 -07001552 if (shsurf->grabbed)
1553 return;
1554
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001555 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001556 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001557 seat->pointer->button_count > 0 &&
1558 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001560 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001561 (surface_move(shsurf, seat) < 0))
1562 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001563 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001564 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001565 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001567 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001568 (surface_touch_move(shsurf, seat) < 0))
1569 wl_resource_post_no_memory(resource);
1570 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571}
1572
Rafael Antognollie2a34552013-12-03 15:35:45 -02001573static void
1574shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1575 struct wl_resource *seat_resource, uint32_t serial)
1576{
1577 common_surface_move(resource, seat_resource, serial);
1578}
1579
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580struct weston_resize_grab {
1581 struct shell_grab base;
1582 uint32_t edges;
1583 int32_t width, height;
1584};
1585
1586static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001587resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1588 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001589{
1590 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001591 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592 struct shell_surface *shsurf = resize->base.shsurf;
1593 int32_t width, height;
1594 wl_fixed_t from_x, from_y;
1595 wl_fixed_t to_x, to_y;
1596
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001597 weston_pointer_move(pointer, x, y);
1598
Ander Conselvan de Oliveira9c376b52014-04-29 17:54:03 +03001599 if (!shsurf || !shsurf->resource)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001600 return;
1601
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602 weston_view_from_global_fixed(shsurf->view,
1603 pointer->grab_x, pointer->grab_y,
1604 &from_x, &from_y);
1605 weston_view_from_global_fixed(shsurf->view,
1606 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001607
1608 width = resize->width;
1609 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1610 width += wl_fixed_to_int(from_x - to_x);
1611 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1612 width += wl_fixed_to_int(to_x - from_x);
1613 }
1614
1615 height = resize->height;
1616 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1617 height += wl_fixed_to_int(from_y - to_y);
1618 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1619 height += wl_fixed_to_int(to_y - from_y);
1620 }
1621
1622 shsurf->client->send_configure(shsurf->surface,
1623 resize->edges, width, height);
1624}
1625
1626static void
1627send_configure(struct weston_surface *surface,
1628 uint32_t edges, int32_t width, int32_t height)
1629{
1630 struct shell_surface *shsurf = get_shell_surface(surface);
1631
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001632 assert(shsurf);
1633
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001634 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001635 edges, width, height);
1636}
1637
1638static const struct weston_shell_client shell_client = {
1639 send_configure
1640};
1641
1642static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001643resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644 uint32_t time, uint32_t button, uint32_t state_w)
1645{
1646 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001647 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001648 enum wl_pointer_button_state state = state_w;
1649
1650 if (pointer->button_count == 0 &&
1651 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1652 shell_grab_end(&resize->base);
1653 free(grab);
1654 }
1655}
1656
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001657static void
1658resize_grab_cancel(struct weston_pointer_grab *grab)
1659{
1660 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1661
1662 shell_grab_end(&resize->base);
1663 free(grab);
1664}
1665
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001666static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001667 noop_grab_focus,
1668 resize_grab_motion,
1669 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001670 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671};
1672
Giulio Camuffob8366642013-04-25 13:57:46 +03001673/*
1674 * Returns the bounding box of a surface and all its sub-surfaces,
1675 * in the surface coordinates system. */
1676static void
1677surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1678 int32_t *y, int32_t *w, int32_t *h) {
1679 pixman_region32_t region;
1680 pixman_box32_t *box;
1681 struct weston_subsurface *subsurface;
1682
1683 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001684 surface->width,
1685 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001686
1687 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1688 pixman_region32_union_rect(&region, &region,
1689 subsurface->position.x,
1690 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691 subsurface->surface->width,
1692 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001693 }
1694
1695 box = pixman_region32_extents(&region);
1696 if (x)
1697 *x = box->x1;
1698 if (y)
1699 *y = box->y1;
1700 if (w)
1701 *w = box->x2 - box->x1;
1702 if (h)
1703 *h = box->y2 - box->y1;
1704
1705 pixman_region32_fini(&region);
1706}
1707
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708static int
1709surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001710 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711{
1712 struct weston_resize_grab *resize;
1713
Rafael Antognolli03b16592013-12-03 15:35:42 -02001714 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715 return 0;
1716
1717 if (edges == 0 || edges > 15 ||
1718 (edges & 3) == 3 || (edges & 12) == 12)
1719 return 0;
1720
1721 resize = malloc(sizeof *resize);
1722 if (!resize)
1723 return -1;
1724
1725 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001726 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1727 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001729 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001731 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732
1733 return 0;
1734}
1735
1736static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001737common_surface_resize(struct wl_resource *resource,
1738 struct wl_resource *seat_resource, uint32_t serial,
1739 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001741 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001742 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001743 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744
Rafael Antognolli03b16592013-12-03 15:35:42 -02001745 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001746 return;
1747
Kristian Høgsberg7b83ae42014-04-29 14:50:25 -07001748 if (shsurf->grabbed)
1749 return;
1750
Kristian Høgsberge3148752013-05-06 23:19:49 -04001751 if (seat->pointer->button_count == 0 ||
1752 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001753 seat->pointer->focus == NULL)
1754 return;
1755
1756 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1757 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001758 return;
1759
Kristian Høgsberge3148752013-05-06 23:19:49 -04001760 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001761 wl_resource_post_no_memory(resource);
1762}
1763
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001765shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1766 struct wl_resource *seat_resource, uint32_t serial,
1767 uint32_t edges)
1768{
1769 common_surface_resize(resource, seat_resource, serial, edges);
1770}
1771
1772static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001773busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001774{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001775 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001776 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001778 wl_fixed_t sx, sy;
1779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 view = weston_compositor_pick_view(pointer->seat->compositor,
1781 pointer->x, pointer->y,
1782 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001783
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001784 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1785 shell_grab_end(grab);
1786 free(grab);
1787 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001788}
1789
1790static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001791busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1792 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001793{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001794 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001795}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001796
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001797static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001798busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001799 uint32_t time, uint32_t button, uint32_t state)
1800{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001801 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001802 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001803 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001804
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001805 if (shsurf && button == BTN_LEFT && state) {
1806 activate(shsurf->shell, shsurf->surface, seat);
1807 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001808 } else if (shsurf && button == BTN_RIGHT && state) {
1809 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001810 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001811 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812}
1813
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001814static void
1815busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1816{
1817 struct shell_grab *grab = (struct shell_grab *) base;
1818
1819 shell_grab_end(grab);
1820 free(grab);
1821}
1822
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001823static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001824 busy_cursor_grab_focus,
1825 busy_cursor_grab_motion,
1826 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001827 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001828};
1829
1830static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001831set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832{
1833 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001835 if (pointer->grab->interface == &busy_cursor_grab_interface)
1836 return;
1837
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001838 grab = malloc(sizeof *grab);
1839 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001840 return;
1841
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001842 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1843 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001844 /* Mark the shsurf as ungrabbed so that button binding is able
1845 * to move it. */
1846 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001847}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001848
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001850end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001851{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001852 struct shell_grab *grab;
1853 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001854
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001855 wl_list_for_each(seat, &compositor->seat_list, link) {
1856 if (seat->pointer == NULL)
1857 continue;
1858
1859 grab = (struct shell_grab *) seat->pointer->grab;
1860 if (grab->grab.interface == &busy_cursor_grab_interface &&
1861 wl_resource_get_client(grab->shsurf->resource) == client) {
1862 shell_grab_end(grab);
1863 free(grab);
1864 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001865 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001866}
1867
Scott Moreau9521d5e2012-04-19 13:06:17 -06001868static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001869handle_shell_client_destroy(struct wl_listener *listener, void *data);
1870
1871static int
1872xdg_ping_timeout_handler(void *data)
1873{
1874 struct shell_client *sc = data;
1875 struct weston_seat *seat;
1876 struct shell_surface *shsurf;
1877
1878 /* Client is not responding */
1879 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001880 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1881 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1882 continue;
1883 if (seat->pointer->focus->surface->resource == NULL)
1884 continue;
1885
1886 shsurf = get_shell_surface(seat->pointer->focus->surface);
1887 if (shsurf &&
1888 wl_resource_get_client(shsurf->resource) == sc->client)
1889 set_busy_cursor(shsurf, seat->pointer);
1890 }
1891
1892 return 1;
1893}
1894
1895static void
1896handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1897{
1898 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001899 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001900 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001901 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001902
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001903 if (sc->unresponsive) {
1904 xdg_ping_timeout_handler(sc);
1905 return;
1906 }
1907
1908 sc->ping_serial = serial;
1909 loop = wl_display_get_event_loop(compositor->wl_display);
1910 if (sc->ping_timer == NULL)
1911 sc->ping_timer =
1912 wl_event_loop_add_timer(loop,
1913 xdg_ping_timeout_handler, sc);
1914 if (sc->ping_timer == NULL)
1915 return;
1916
1917 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1918
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001919 if (shell_surface_is_xdg_surface(shsurf) ||
1920 shell_surface_is_xdg_popup(shsurf))
1921 xdg_shell_send_ping(sc->resource, serial);
1922 else if (shell_surface_is_wl_shell_surface(shsurf))
1923 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001924}
1925
Scott Moreauff1db4a2012-04-17 19:06:18 -06001926static void
1927ping_handler(struct weston_surface *surface, uint32_t serial)
1928{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001929 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001930
1931 if (!shsurf)
1932 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001933 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001934 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001935 if (shsurf->surface == shsurf->shell->grab_surface)
1936 return;
1937
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001938 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001939}
1940
1941static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001942handle_pointer_focus(struct wl_listener *listener, void *data)
1943{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001944 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001946 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001947 uint32_t serial;
1948
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001950 return;
1951
Jason Ekstranda7af7042013-10-12 22:38:11 -05001952 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001953 serial = wl_display_next_serial(compositor->wl_display);
1954 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001955}
1956
1957static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001958create_pointer_focus_listener(struct weston_seat *seat)
1959{
1960 struct wl_listener *listener;
1961
Kristian Høgsberge3148752013-05-06 23:19:49 -04001962 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001963 return;
1964
1965 listener = malloc(sizeof *listener);
1966 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001967 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001968}
1969
1970static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001971shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1972{
1973 if (--shsurf->focus_count == 0)
1974 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001975 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001976}
1977
1978static void
1979shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1980{
1981 if (shsurf->focus_count++ == 0)
1982 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001983 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001984}
1985
1986static void
1987handle_keyboard_focus(struct wl_listener *listener, void *data)
1988{
1989 struct weston_keyboard *keyboard = data;
1990 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1991
1992 if (seat->focused_surface) {
1993 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1994 if (shsurf)
1995 shell_surface_lose_keyboard_focus(shsurf);
1996 }
1997
1998 seat->focused_surface = keyboard->focus;
1999
2000 if (seat->focused_surface) {
2001 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2002 if (shsurf)
2003 shell_surface_gain_keyboard_focus(shsurf);
2004 }
2005}
2006
2007static void
2008create_keyboard_focus_listener(struct weston_seat *seat)
2009{
2010 struct wl_listener *listener;
2011
2012 if (!seat->keyboard)
2013 return;
2014
2015 listener = malloc(sizeof *listener);
2016 listener->notify = handle_keyboard_focus;
2017 wl_signal_add(&seat->keyboard->focus_signal, listener);
2018}
2019
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002020static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002021shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002022{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002023 if (sc->ping_serial != serial)
2024 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002025
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002026 sc->unresponsive = 0;
2027 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002028
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002029 if (sc->ping_timer) {
2030 wl_event_source_remove(sc->ping_timer);
2031 sc->ping_timer = NULL;
2032 }
2033
2034}
2035
2036static void
2037shell_surface_pong(struct wl_client *client,
2038 struct wl_resource *resource, uint32_t serial)
2039{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002040 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2041 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002042
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002043 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002044}
2045
2046static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002047set_title(struct shell_surface *shsurf, const char *title)
2048{
2049 free(shsurf->title);
2050 shsurf->title = strdup(title);
2051}
2052
2053static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002054shell_surface_set_title(struct wl_client *client,
2055 struct wl_resource *resource, const char *title)
2056{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002057 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002058
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002059 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002060}
2061
2062static void
2063shell_surface_set_class(struct wl_client *client,
2064 struct wl_resource *resource, const char *class)
2065{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002066 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002067
2068 free(shsurf->class);
2069 shsurf->class = strdup(class);
2070}
2071
Alex Wu4539b082012-03-01 12:57:46 +08002072static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002073restore_output_mode(struct weston_output *output)
2074{
Hardening57388e42013-09-18 23:56:36 +02002075 if (output->current_mode != output->original_mode ||
2076 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002077 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002078 output->original_mode,
2079 output->original_scale,
2080 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002081}
2082
2083static void
2084restore_all_output_modes(struct weston_compositor *compositor)
2085{
2086 struct weston_output *output;
2087
2088 wl_list_for_each(output, &compositor->output_list, link)
2089 restore_output_mode(output);
2090}
2091
Philip Withnallbecb77e2013-11-25 18:01:30 +00002092static int
2093get_output_panel_height(struct desktop_shell *shell,
2094 struct weston_output *output)
2095{
2096 struct weston_view *view;
2097 int panel_height = 0;
2098
2099 if (!output)
2100 return 0;
2101
2102 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2103 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002104 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002105 break;
2106 }
2107 }
2108
2109 return panel_height;
2110}
2111
Philip Withnall07926d92013-11-25 18:01:40 +00002112/* The surface will be inserted into the list immediately after the link
2113 * returned by this function (i.e. will be stacked immediately above the
2114 * returned link). */
2115static struct wl_list *
2116shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2117{
2118 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002119 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002120
2121 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002122 case SHELL_SURFACE_POPUP:
2123 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002124 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002125 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002126 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002127 /* Move the surface to its parent layer so
2128 * that surfaces which are transient for
2129 * fullscreen surfaces don't get hidden by the
2130 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002131
2132 /* TODO: Handle a parent with multiple views */
2133 parent = get_default_view(shsurf->parent);
2134 if (parent)
2135 return parent->layer_link.prev;
2136 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002137 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002138
2139 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002140 return &shsurf->shell->fullscreen_layer.view_list;
2141
Philip Withnall07926d92013-11-25 18:01:40 +00002142 case SHELL_SURFACE_NONE:
2143 default:
2144 /* Go to the fallback, below. */
2145 break;
2146 }
2147
2148 /* Move the surface to a normal workspace layer so that surfaces
2149 * which were previously fullscreen or transient are no longer
2150 * rendered on top. */
2151 ws = get_current_workspace(shsurf->shell);
2152 return &ws->layer.view_list;
2153}
2154
Philip Withnall648a4dd2013-11-25 18:01:44 +00002155static void
2156shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2157{
2158 struct shell_surface *child;
2159
2160 /* Move the child layers to the same workspace as shsurf. They will be
2161 * stacked above shsurf. */
2162 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2163 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002164 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002165 weston_view_geometry_dirty(child->view);
2166 wl_list_remove(&child->view->layer_link);
2167 wl_list_insert(shsurf->view->layer_link.prev,
2168 &child->view->layer_link);
2169 weston_view_geometry_dirty(child->view);
2170 weston_surface_damage(child->surface);
2171
2172 /* Recurse. We don’t expect this to recurse very far (if
2173 * at all) because that would imply we have transient
2174 * (or popup) children of transient surfaces, which
2175 * would be unusual. */
2176 shell_surface_update_child_surface_layers(child);
2177 }
2178 }
2179}
2180
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002181/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002182 * geometry to ensure the changes are redrawn.
2183 *
2184 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2185 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002186static void
2187shell_surface_update_layer(struct shell_surface *shsurf)
2188{
2189 struct wl_list *new_layer_link;
2190
2191 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2192
2193 if (new_layer_link == &shsurf->view->layer_link)
2194 return;
2195
2196 weston_view_geometry_dirty(shsurf->view);
2197 wl_list_remove(&shsurf->view->layer_link);
2198 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2199 weston_view_geometry_dirty(shsurf->view);
2200 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002201
2202 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002203}
2204
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002205static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002206shell_surface_set_parent(struct shell_surface *shsurf,
2207 struct weston_surface *parent)
2208{
2209 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002210
2211 wl_list_remove(&shsurf->children_link);
2212 wl_list_init(&shsurf->children_link);
2213
2214 /* Insert into the parent surface’s child list. */
2215 if (parent != NULL) {
2216 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2217 if (parent_shsurf != NULL)
2218 wl_list_insert(&parent_shsurf->children_list,
2219 &shsurf->children_link);
2220 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002221}
2222
2223static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002224shell_surface_set_output(struct shell_surface *shsurf,
2225 struct weston_output *output)
2226{
2227 struct weston_surface *es = shsurf->surface;
2228
2229 /* get the default output, if the client set it as NULL
2230 check whether the ouput is available */
2231 if (output)
2232 shsurf->output = output;
2233 else if (es->output)
2234 shsurf->output = es->output;
2235 else
2236 shsurf->output = get_default_output(es->compositor);
2237}
2238
2239static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002240surface_clear_next_states(struct shell_surface *shsurf)
2241{
2242 shsurf->next_state.maximized = false;
2243 shsurf->next_state.fullscreen = false;
2244
2245 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2246 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2247 shsurf->state_changed = true;
2248}
2249
2250static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002251set_toplevel(struct shell_surface *shsurf)
2252{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002253 shell_surface_set_parent(shsurf, NULL);
2254 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002255 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002256
2257 /* The layer_link is updated in set_surface_type(),
2258 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002259}
2260
2261static void
2262shell_surface_set_toplevel(struct wl_client *client,
2263 struct wl_resource *resource)
2264{
2265 struct shell_surface *surface = wl_resource_get_user_data(resource);
2266
2267 set_toplevel(surface);
2268}
2269
2270static void
2271set_transient(struct shell_surface *shsurf,
2272 struct weston_surface *parent, int x, int y, uint32_t flags)
2273{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002274 assert(parent != NULL);
2275
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002276 shell_surface_set_parent(shsurf, parent);
2277
2278 surface_clear_next_states(shsurf);
2279
Philip Withnallbecb77e2013-11-25 18:01:30 +00002280 shsurf->transient.x = x;
2281 shsurf->transient.y = y;
2282 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002283
Rafael Antognollied207b42013-12-03 15:35:43 -02002284 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002285 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002286 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002287
2288 /* The layer_link is updated in set_surface_type(),
2289 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002290}
2291
2292static void
2293shell_surface_set_transient(struct wl_client *client,
2294 struct wl_resource *resource,
2295 struct wl_resource *parent_resource,
2296 int x, int y, uint32_t flags)
2297{
2298 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2299 struct weston_surface *parent =
2300 wl_resource_get_user_data(parent_resource);
2301
2302 set_transient(shsurf, parent, x, y, flags);
2303}
2304
2305static void
2306set_fullscreen(struct shell_surface *shsurf,
2307 uint32_t method,
2308 uint32_t framerate,
2309 struct weston_output *output)
2310{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002311 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002312
2313 shsurf->fullscreen_output = shsurf->output;
2314 shsurf->fullscreen.type = method;
2315 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002316
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002317 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002318
2319 shsurf->client->send_configure(shsurf->surface, 0,
2320 shsurf->output->width,
2321 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002322
2323 /* The layer_link is updated in set_surface_type(),
2324 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002325}
2326
2327static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002328weston_view_set_initial_position(struct weston_view *view,
2329 struct desktop_shell *shell);
2330
2331static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002332unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002333{
Philip Withnallf85fe842013-11-25 18:01:37 +00002334 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002335 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2336 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002337 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002338 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002339
Alex Wu4539b082012-03-01 12:57:46 +08002340 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2341 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002342
Alex Wu4539b082012-03-01 12:57:46 +08002343 wl_list_remove(&shsurf->fullscreen.transform.link);
2344 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002345
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346 if (shsurf->fullscreen.black_view)
2347 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2348 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002349
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002350 if (shsurf->saved_position_valid)
2351 weston_view_set_position(shsurf->view,
2352 shsurf->saved_x, shsurf->saved_y);
2353 else
2354 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2355
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002356 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002357 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002358 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002359 shsurf->saved_rotation_valid = false;
2360 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002361
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002362 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002363}
2364
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002365static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366shell_surface_set_fullscreen(struct wl_client *client,
2367 struct wl_resource *resource,
2368 uint32_t method,
2369 uint32_t framerate,
2370 struct wl_resource *output_resource)
2371{
2372 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2373 struct weston_output *output;
2374
2375 if (output_resource)
2376 output = wl_resource_get_user_data(output_resource);
2377 else
2378 output = NULL;
2379
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002380 shell_surface_set_parent(shsurf, NULL);
2381
Rafael Antognolli03b16592013-12-03 15:35:42 -02002382 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002383 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002384
2385 shsurf->next_state.fullscreen = true;
2386 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002387}
2388
2389static void
2390set_popup(struct shell_surface *shsurf,
2391 struct weston_surface *parent,
2392 struct weston_seat *seat,
2393 uint32_t serial,
2394 int32_t x,
2395 int32_t y)
2396{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002397 assert(parent != NULL);
2398
Philip Withnallbecb77e2013-11-25 18:01:30 +00002399 shsurf->popup.shseat = get_shell_seat(seat);
2400 shsurf->popup.serial = serial;
2401 shsurf->popup.x = x;
2402 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002403
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002404 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002405}
2406
2407static void
2408shell_surface_set_popup(struct wl_client *client,
2409 struct wl_resource *resource,
2410 struct wl_resource *seat_resource,
2411 uint32_t serial,
2412 struct wl_resource *parent_resource,
2413 int32_t x, int32_t y, uint32_t flags)
2414{
2415 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002416 struct weston_surface *parent =
2417 wl_resource_get_user_data(parent_resource);
2418
2419 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002420
Rafael Antognolli03b16592013-12-03 15:35:42 -02002421 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002422 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002423 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002424 wl_resource_get_user_data(seat_resource),
2425 serial, x, y);
2426}
2427
2428static void
2429set_maximized(struct shell_surface *shsurf,
2430 struct weston_output *output)
2431{
2432 struct desktop_shell *shell;
2433 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002434
Philip Withnall352e7ed2013-11-25 18:01:35 +00002435 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002436
2437 shell = shell_surface_get_shell(shsurf);
2438 panel_height = get_output_panel_height(shell, shsurf->output);
2439 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2440
2441 shsurf->client->send_configure(shsurf->surface, edges,
2442 shsurf->output->width,
2443 shsurf->output->height - panel_height);
2444
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002445 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002446}
2447
2448static void
2449unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002450{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002451 /* undo all maximized things here */
2452 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002453
2454 if (shsurf->saved_position_valid)
2455 weston_view_set_position(shsurf->view,
2456 shsurf->saved_x, shsurf->saved_y);
2457 else
2458 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002459
2460 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002461 wl_list_insert(&shsurf->view->geometry.transformation_list,
2462 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002463 shsurf->saved_rotation_valid = false;
2464 }
2465
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002466 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002467}
2468
Philip Withnallbecb77e2013-11-25 18:01:30 +00002469static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002470set_minimized(struct weston_surface *surface, uint32_t is_true)
2471{
2472 struct shell_surface *shsurf;
2473 struct workspace *current_ws;
2474 struct weston_seat *seat;
2475 struct weston_surface *focus;
2476 struct weston_view *view;
2477
2478 view = get_default_view(surface);
2479 if (!view)
2480 return;
2481
2482 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2483
2484 shsurf = get_shell_surface(surface);
2485 current_ws = get_current_workspace(shsurf->shell);
2486
2487 wl_list_remove(&view->layer_link);
2488 /* hide or show, depending on the state */
2489 if (is_true) {
2490 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2491
2492 drop_focus_state(shsurf->shell, current_ws, view->surface);
2493 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2494 if (!seat->keyboard)
2495 continue;
2496 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2497 if (focus == view->surface)
2498 weston_keyboard_set_focus(seat->keyboard, NULL);
2499 }
2500 }
2501 else {
2502 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2503
2504 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2505 if (!seat->keyboard)
2506 continue;
2507 activate(shsurf->shell, view->surface, seat);
2508 }
2509 }
2510
2511 shell_surface_update_child_surface_layers(shsurf);
2512
2513 weston_view_damage_below(view);
2514}
2515
2516static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002517shell_surface_set_maximized(struct wl_client *client,
2518 struct wl_resource *resource,
2519 struct wl_resource *output_resource)
2520{
2521 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2522 struct weston_output *output;
2523
2524 if (output_resource)
2525 output = wl_resource_get_user_data(output_resource);
2526 else
2527 output = NULL;
2528
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002529 shell_surface_set_parent(shsurf, NULL);
2530
Rafael Antognolli03b16592013-12-03 15:35:42 -02002531 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002532 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002533
2534 shsurf->next_state.maximized = true;
2535 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002536}
2537
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002538/* This is only ever called from set_surface_type(), so there’s no need to
2539 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002540static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002542{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002543 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002544 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002545 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002546 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002547
Pekka Paalanen98262232011-12-01 10:42:22 +02002548 return 0;
2549}
2550
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002551static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002552set_full_output(struct shell_surface *shsurf)
2553{
2554 shsurf->saved_x = shsurf->view->geometry.x;
2555 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002556 shsurf->saved_width = shsurf->surface->width;
2557 shsurf->saved_height = shsurf->surface->height;
2558 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002559 shsurf->saved_position_valid = true;
2560
2561 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2562 wl_list_remove(&shsurf->rotation.transform.link);
2563 wl_list_init(&shsurf->rotation.transform.link);
2564 weston_view_geometry_dirty(shsurf->view);
2565 shsurf->saved_rotation_valid = true;
2566 }
2567}
2568
2569static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002570set_surface_type(struct shell_surface *shsurf)
2571{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002572 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002574
Philip Withnallbecb77e2013-11-25 18:01:30 +00002575 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002576
Rafael Antognolli03b16592013-12-03 15:35:42 -02002577 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002578 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002579
2580 switch (shsurf->type) {
2581 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002582 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002583 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002584 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002585 weston_view_set_position(shsurf->view,
2586 pev->geometry.x + shsurf->transient.x,
2587 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002588 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002589 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002590
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002591 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 weston_view_set_position(shsurf->view, shsurf->transient.x,
2593 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002594 break;
2595
Philip Withnall0f640e22013-11-25 18:01:31 +00002596 case SHELL_SURFACE_POPUP:
2597 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002598 default:
2599 break;
2600 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002601
2602 /* Update the surface’s layer. */
2603 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002604}
2605
Tiago Vignattibe143262012-04-16 17:31:41 +03002606static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002607shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002608{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002609 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002610}
2611
Alex Wu21858432012-04-01 20:13:08 +08002612static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002613black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002614
Jason Ekstranda7af7042013-10-12 22:38:11 -05002615static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002616create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002617 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002618 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002619{
2620 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002621 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002622
2623 surface = weston_surface_create(ec);
2624 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002625 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002626 return NULL;
2627 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 view = weston_view_create(surface);
2629 if (surface == NULL) {
2630 weston_log("no memory\n");
2631 weston_surface_destroy(surface);
2632 return NULL;
2633 }
Alex Wu4539b082012-03-01 12:57:46 +08002634
Alex Wu21858432012-04-01 20:13:08 +08002635 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002636 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002637 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002638 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002639 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002640 pixman_region32_fini(&surface->input);
2641 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002642
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002643 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002644 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645
2646 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002647}
2648
Philip Withnalled8f1a92013-11-25 18:01:43 +00002649static void
2650shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2651{
2652 struct weston_output *output = shsurf->fullscreen_output;
2653
Rafael Antognolli03b16592013-12-03 15:35:42 -02002654 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002655
2656 if (!shsurf->fullscreen.black_view)
2657 shsurf->fullscreen.black_view =
2658 create_black_surface(shsurf->surface->compositor,
2659 shsurf->surface,
2660 output->x, output->y,
2661 output->width,
2662 output->height);
2663
2664 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2665 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2666 wl_list_insert(&shsurf->view->layer_link,
2667 &shsurf->fullscreen.black_view->layer_link);
2668 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2669 weston_surface_damage(shsurf->surface);
2670}
2671
Alex Wu4539b082012-03-01 12:57:46 +08002672/* Create black surface and append it to the associated fullscreen surface.
2673 * Handle size dismatch and positioning according to the method. */
2674static void
2675shell_configure_fullscreen(struct shell_surface *shsurf)
2676{
2677 struct weston_output *output = shsurf->fullscreen_output;
2678 struct weston_surface *surface = shsurf->surface;
2679 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002680 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002681 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002682
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002683 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2684 restore_output_mode(output);
2685
Philip Withnalled8f1a92013-11-25 18:01:43 +00002686 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002687
Jason Ekstranda7af7042013-10-12 22:38:11 -05002688 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002689 &surf_width, &surf_height);
2690
Alex Wu4539b082012-03-01 12:57:46 +08002691 switch (shsurf->fullscreen.type) {
2692 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002693 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002695 break;
2696 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002697 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002698 if (output->width == surf_width &&
2699 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002700 weston_view_set_position(shsurf->view,
2701 output->x - surf_x,
2702 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002703 break;
2704 }
2705
Alex Wu4539b082012-03-01 12:57:46 +08002706 matrix = &shsurf->fullscreen.transform.matrix;
2707 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002708
Scott Moreau1bad5db2012-08-18 01:04:05 -06002709 output_aspect = (float) output->width /
2710 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 /* XXX: Use surf_width and surf_height here? */
2712 surface_aspect = (float) surface->width /
2713 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002714 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002715 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002716 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002717 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002718 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002719 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002720
Alex Wu4539b082012-03-01 12:57:46 +08002721 weston_matrix_scale(matrix, scale, scale, 1);
2722 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002723 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002724 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002725 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2726 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002728
Alex Wu4539b082012-03-01 12:57:46 +08002729 break;
2730 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002731 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002732 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002733 surf_width * surface->buffer_viewport.buffer.scale,
2734 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002735 shsurf->fullscreen.framerate};
2736
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002737 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002738 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 weston_view_set_position(shsurf->view,
2740 output->x - surf_x,
2741 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002742 shsurf->fullscreen.black_view->surface->width = output->width;
2743 shsurf->fullscreen.black_view->surface->height = output->height;
2744 weston_view_set_position(shsurf->fullscreen.black_view,
2745 output->x - surf_x,
2746 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002747 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002748 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002749 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002751 }
Alex Wubd3354b2012-04-17 17:20:49 +08002752 }
Alex Wu4539b082012-03-01 12:57:46 +08002753 break;
2754 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002755 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002756 break;
2757 default:
2758 break;
2759 }
2760}
2761
Alex Wu4539b082012-03-01 12:57:46 +08002762static void
2763shell_map_fullscreen(struct shell_surface *shsurf)
2764{
Alex Wubd3354b2012-04-17 17:20:49 +08002765 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002766}
2767
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002768static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002769set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2770{
2771 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002772 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002773 shsurf->transient.x = x;
2774 shsurf->transient.y = y;
2775 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002776
2777 shell_surface_set_parent(shsurf, NULL);
2778
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002779 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002780 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002781}
2782
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002783static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002784
2785static void
2786destroy_shell_seat(struct wl_listener *listener, void *data)
2787{
2788 struct shell_seat *shseat =
2789 container_of(listener,
2790 struct shell_seat, seat_destroy_listener);
2791 struct shell_surface *shsurf, *prev = NULL;
2792
2793 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002794 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002795 shseat->popup_grab.client = NULL;
2796
2797 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2798 shsurf->popup.shseat = NULL;
2799 if (prev) {
2800 wl_list_init(&prev->popup.grab_link);
2801 }
2802 prev = shsurf;
2803 }
2804 wl_list_init(&prev->popup.grab_link);
2805 }
2806
2807 wl_list_remove(&shseat->seat_destroy_listener.link);
2808 free(shseat);
2809}
2810
2811static struct shell_seat *
2812create_shell_seat(struct weston_seat *seat)
2813{
2814 struct shell_seat *shseat;
2815
2816 shseat = calloc(1, sizeof *shseat);
2817 if (!shseat) {
2818 weston_log("no memory to allocate shell seat\n");
2819 return NULL;
2820 }
2821
2822 shseat->seat = seat;
2823 wl_list_init(&shseat->popup_grab.surfaces_list);
2824
2825 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2826 wl_signal_add(&seat->destroy_signal,
2827 &shseat->seat_destroy_listener);
2828
2829 return shseat;
2830}
2831
2832static struct shell_seat *
2833get_shell_seat(struct weston_seat *seat)
2834{
2835 struct wl_listener *listener;
2836
2837 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2838 if (listener == NULL)
2839 return create_shell_seat(seat);
2840
2841 return container_of(listener,
2842 struct shell_seat, seat_destroy_listener);
2843}
2844
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002845static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002846popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002847{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002848 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002849 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002850 struct shell_seat *shseat =
2851 container_of(grab, struct shell_seat, popup_grab.grab);
2852 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002853 wl_fixed_t sx, sy;
2854
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 view = weston_compositor_pick_view(pointer->seat->compositor,
2856 pointer->x, pointer->y,
2857 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002858
Jason Ekstranda7af7042013-10-12 22:38:11 -05002859 if (view && view->surface->resource &&
2860 wl_resource_get_client(view->surface->resource) == client) {
2861 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002862 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002863 weston_pointer_set_focus(pointer, NULL,
2864 wl_fixed_from_int(0),
2865 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002866 }
2867}
2868
2869static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002870popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2871 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002872{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002873 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002874 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002875 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002876
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002877 weston_pointer_move(pointer, x, y);
2878
Neil Roberts96d790e2013-09-19 17:32:00 +01002879 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002880 weston_view_from_global_fixed(pointer->focus,
2881 pointer->x, pointer->y,
2882 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002883 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002884 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002885}
2886
2887static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002888popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002889 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002890{
2891 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002892 struct shell_seat *shseat =
2893 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002894 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002895 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002896 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002897 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002898
Neil Roberts96d790e2013-09-19 17:32:00 +01002899 resource_list = &grab->pointer->focus_resource_list;
2900 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002901 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002902 wl_resource_for_each(resource, resource_list) {
2903 wl_pointer_send_button(resource, serial,
2904 time, button, state);
2905 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002906 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002907 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002908 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002909 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002910 }
2911
Daniel Stone4dbadb12012-05-30 16:31:51 +01002912 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002913 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002914}
2915
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002916static void
2917popup_grab_cancel(struct weston_pointer_grab *grab)
2918{
2919 popup_grab_end(grab->pointer);
2920}
2921
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002922static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002923 popup_grab_focus,
2924 popup_grab_motion,
2925 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002926 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002927};
2928
2929static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002930shell_surface_send_popup_done(struct shell_surface *shsurf)
2931{
2932 if (shell_surface_is_wl_shell_surface(shsurf))
2933 wl_shell_surface_send_popup_done(shsurf->resource);
2934 else if (shell_surface_is_xdg_popup(shsurf))
2935 xdg_popup_send_popup_done(shsurf->resource,
2936 shsurf->popup.serial);
2937}
2938
2939static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002940popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002941{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002942 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002943 struct shell_seat *shseat =
2944 container_of(grab, struct shell_seat, popup_grab.grab);
2945 struct shell_surface *shsurf;
2946 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002947
2948 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002949 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002950 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002951 shseat->popup_grab.grab.interface = NULL;
2952 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002953 /* Send the popup_done event to all the popups open */
2954 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002955 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002956 shsurf->popup.shseat = NULL;
2957 if (prev) {
2958 wl_list_init(&prev->popup.grab_link);
2959 }
2960 prev = shsurf;
2961 }
2962 wl_list_init(&prev->popup.grab_link);
2963 wl_list_init(&shseat->popup_grab.surfaces_list);
2964 }
2965}
2966
2967static void
2968add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2969{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002970 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002971
2972 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002973 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002974 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002975 /* We must make sure here that this popup was opened after
2976 * a mouse press, and not just by moving around with other
2977 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002978 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002979 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002980
Rob Bradforddfe31052013-06-26 19:49:11 +01002981 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002982 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002983 } else {
2984 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002985 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002986}
2987
2988static void
2989remove_popup_grab(struct shell_surface *shsurf)
2990{
2991 struct shell_seat *shseat = shsurf->popup.shseat;
2992
2993 wl_list_remove(&shsurf->popup.grab_link);
2994 wl_list_init(&shsurf->popup.grab_link);
2995 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002996 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002997 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002998 }
2999}
3000
3001static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003002shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003003{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003004 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003005 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 shsurf->surface->output = parent_view->output;
3008 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003009
Jason Ekstranda7af7042013-10-12 22:38:11 -05003010 weston_view_set_transform_parent(shsurf->view, parent_view);
3011 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3012 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003013
Kristian Høgsberge3148752013-05-06 23:19:49 -04003014 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003015 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003016 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003017 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003018 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003019 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003020}
3021
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003022static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003023 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003024 shell_surface_move,
3025 shell_surface_resize,
3026 shell_surface_set_toplevel,
3027 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003028 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003029 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003030 shell_surface_set_maximized,
3031 shell_surface_set_title,
3032 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003033};
3034
3035static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003036destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003037{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003038 struct shell_surface *child, *next;
3039
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003040 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3041
Giulio Camuffo5085a752013-03-25 21:42:45 +01003042 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3043 remove_popup_grab(shsurf);
3044 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003045
Alex Wubd3354b2012-04-17 17:20:49 +08003046 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003047 shell_surface_is_top_fullscreen(shsurf))
3048 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003049
Jason Ekstranda7af7042013-10-12 22:38:11 -05003050 if (shsurf->fullscreen.black_view)
3051 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003052
Alex Wubd3354b2012-04-17 17:20:49 +08003053 /* As destroy_resource() use wl_list_for_each_safe(),
3054 * we can always remove the listener.
3055 */
3056 wl_list_remove(&shsurf->surface_destroy_listener.link);
3057 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003058 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003059
Jason Ekstranda7af7042013-10-12 22:38:11 -05003060 weston_view_destroy(shsurf->view);
3061
Philip Withnall648a4dd2013-11-25 18:01:44 +00003062 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003063 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3064 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003065
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003066 wl_list_remove(&shsurf->link);
3067 free(shsurf);
3068}
3069
3070static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003071shell_destroy_shell_surface(struct wl_resource *resource)
3072{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003073 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003074
Bryan Caina46b9462014-04-04 17:41:24 -05003075 if (!wl_list_empty(&shsurf->popup.grab_link))
3076 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003077 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003078}
3079
3080static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003081shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003082{
3083 struct shell_surface *shsurf = container_of(listener,
3084 struct shell_surface,
3085 surface_destroy_listener);
3086
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 if (shsurf->resource)
3088 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003089
3090 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003091}
3092
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003093static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003094fade_out_done(struct weston_view_animation *animation, void *data)
3095{
3096 struct shell_surface *shsurf = data;
3097
3098 weston_surface_destroy(shsurf->surface);
3099}
3100
3101static void
3102handle_resource_destroy(struct wl_listener *listener, void *data)
3103{
3104 struct shell_surface *shsurf =
3105 container_of(listener, struct shell_surface,
3106 resource_destroy_listener);
3107
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003108 if (!weston_surface_is_mapped(shsurf->surface))
3109 return;
3110
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003111 shsurf->surface->ref_count++;
3112
3113 pixman_region32_fini(&shsurf->surface->pending.input);
3114 pixman_region32_init(&shsurf->surface->pending.input);
3115 pixman_region32_fini(&shsurf->surface->input);
3116 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003117 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3118 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003119}
3120
3121static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003122shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003123
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003124struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003125get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003126{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003127 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003128 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003129 else
3130 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003131}
3132
Rafael Antognollie2a34552013-12-03 15:35:45 -02003133static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003134create_common_surface(struct shell_client *owner, void *shell,
3135 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003136 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003137{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003138 struct shell_surface *shsurf;
3139
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003140 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003141 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003142 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003143 }
3144
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003145 shsurf = calloc(1, sizeof *shsurf);
3146 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003147 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003148 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003149 }
3150
Jason Ekstranda7af7042013-10-12 22:38:11 -05003151 shsurf->view = weston_view_create(surface);
3152 if (!shsurf->view) {
3153 weston_log("no memory to allocate shell surface\n");
3154 free(shsurf);
3155 return NULL;
3156 }
3157
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003158 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003159 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003160
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003161 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3162 wl_resource_add_destroy_listener(surface->resource,
3163 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003164 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003165
Tiago Vignattibc052c92012-04-19 16:18:18 +03003166 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003167 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003168 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003169 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003170 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003171 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003172 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3173 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003174 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003175 wl_list_init(&shsurf->fullscreen.transform.link);
3176
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003177 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003178 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003179 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003180 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003181
3182 /* init link so its safe to always remove it in destroy_shell_surface */
3183 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003184 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003185
Pekka Paalanen460099f2012-01-20 16:48:25 +02003186 /* empty when not in use */
3187 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003188 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003189
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003190 wl_list_init(&shsurf->workspace_transform.link);
3191
Philip Withnall648a4dd2013-11-25 18:01:44 +00003192 wl_list_init(&shsurf->children_link);
3193 wl_list_init(&shsurf->children_list);
3194 shsurf->parent = NULL;
3195
Pekka Paalanen98262232011-12-01 10:42:22 +02003196 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003197
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003198 shsurf->client = client;
3199
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003200 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003201}
3202
Rafael Antognollie2a34552013-12-03 15:35:45 -02003203static struct shell_surface *
3204create_shell_surface(void *shell, struct weston_surface *surface,
3205 const struct weston_shell_client *client)
3206{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003207 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003208}
3209
Jason Ekstranda7af7042013-10-12 22:38:11 -05003210static struct weston_view *
3211get_primary_view(void *shell, struct shell_surface *shsurf)
3212{
3213 return shsurf->view;
3214}
3215
Tiago Vignattibc052c92012-04-19 16:18:18 +03003216static void
3217shell_get_shell_surface(struct wl_client *client,
3218 struct wl_resource *resource,
3219 uint32_t id,
3220 struct wl_resource *surface_resource)
3221{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003222 struct weston_surface *surface =
3223 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003224 struct shell_client *sc = wl_resource_get_user_data(resource);
3225 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003226 struct shell_surface *shsurf;
3227
3228 if (get_shell_surface(surface)) {
3229 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003230 WL_DISPLAY_ERROR_INVALID_OBJECT,
3231 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003232 return;
3233 }
3234
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003235 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003236 if (!shsurf) {
3237 wl_resource_post_error(surface_resource,
3238 WL_DISPLAY_ERROR_INVALID_OBJECT,
3239 "surface->configure already set");
3240 return;
3241 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003242
Jason Ekstranda85118c2013-06-27 20:17:02 -05003243 shsurf->resource =
3244 wl_resource_create(client,
3245 &wl_shell_surface_interface, 1, id);
3246 wl_resource_set_implementation(shsurf->resource,
3247 &shell_surface_implementation,
3248 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003249}
3250
Rafael Antognollie2a34552013-12-03 15:35:45 -02003251static bool
3252shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3253{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003254 /* A shell surface without a resource is created from xwayland
3255 * and is considered a wl_shell surface for now. */
3256
3257 return shsurf->resource == NULL ||
3258 wl_resource_instance_of(shsurf->resource,
3259 &wl_shell_surface_interface,
3260 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003261}
3262
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003263static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003264 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003265};
3266
Rafael Antognollie2a34552013-12-03 15:35:45 -02003267/****************************
3268 * xdg-shell implementation */
3269
3270static void
3271xdg_surface_destroy(struct wl_client *client,
3272 struct wl_resource *resource)
3273{
3274 wl_resource_destroy(resource);
3275}
3276
3277static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003278xdg_surface_set_transient_for(struct wl_client *client,
3279 struct wl_resource *resource,
3280 struct wl_resource *parent_resource)
3281{
3282 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3283 struct weston_surface *parent;
3284
3285 if (parent_resource)
3286 parent = wl_resource_get_user_data(parent_resource);
3287 else
3288 parent = NULL;
3289
3290 shell_surface_set_parent(shsurf, parent);
3291}
3292
3293static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003294xdg_surface_set_margin(struct wl_client *client,
3295 struct wl_resource *resource,
3296 int32_t left,
3297 int32_t right,
3298 int32_t top,
3299 int32_t bottom)
3300{
3301 /* Do nothing, Weston doesn't try to constrain or place
3302 * surfaces in any special manner... */
3303}
3304
3305static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003306xdg_surface_set_app_id(struct wl_client *client,
3307 struct wl_resource *resource,
3308 const char *app_id)
3309{
3310 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3311
3312 free(shsurf->class);
3313 shsurf->class = strdup(app_id);
3314}
3315
3316static void
3317xdg_surface_set_title(struct wl_client *client,
3318 struct wl_resource *resource, const char *title)
3319{
3320 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3321
3322 set_title(shsurf, title);
3323}
3324
3325static void
3326xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3327 struct wl_resource *seat_resource, uint32_t serial)
3328{
3329 common_surface_move(resource, seat_resource, serial);
3330}
3331
3332static void
3333xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3334 struct wl_resource *seat_resource, uint32_t serial,
3335 uint32_t edges)
3336{
3337 common_surface_resize(resource, seat_resource, serial, edges);
3338}
3339
3340static void
3341xdg_surface_set_output(struct wl_client *client,
3342 struct wl_resource *resource,
3343 struct wl_resource *output_resource)
3344{
3345 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3346 struct weston_output *output;
3347
3348 if (output_resource)
3349 output = wl_resource_get_user_data(output_resource);
3350 else
3351 output = NULL;
3352
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003353 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003354}
3355
3356static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003357xdg_surface_change_state(struct shell_surface *shsurf,
3358 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003359{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003360 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003361 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003362
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003363 switch (state) {
3364 case XDG_SURFACE_STATE_MAXIMIZED:
3365 shsurf->requested_state.maximized = value;
3366 if (value)
3367 set_maximized(shsurf, NULL);
3368 break;
3369 case XDG_SURFACE_STATE_FULLSCREEN:
3370 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003371
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003372 if (value)
3373 set_fullscreen(shsurf,
3374 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3375 0, shsurf->recommended_output);
3376 break;
3377 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003378}
3379
3380static void
3381xdg_surface_request_change_state(struct wl_client *client,
3382 struct wl_resource *resource,
3383 uint32_t state,
3384 uint32_t value,
3385 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003386{
3387 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003388
3389 /* The client can't know what the current state is, so we need
3390 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003391
3392 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3393 return;
3394
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003395 switch (state) {
3396 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003397 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003398 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003399 default:
3400 /* send error? ignore? send change state with value 0? */
3401 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003402 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003403
3404 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003405}
3406
3407static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003408xdg_surface_ack_change_state(struct wl_client *client,
3409 struct wl_resource *resource,
3410 uint32_t state,
3411 uint32_t value,
3412 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003413{
3414 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3415
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003416 if (shsurf->state_requested) {
3417 shsurf->next_state = shsurf->requested_state;
3418 shsurf->state_changed = true;
3419 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003420 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003421}
3422
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003423static void
3424xdg_surface_set_minimized(struct wl_client *client,
3425 struct wl_resource *resource)
3426{
3427 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3428
3429 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3430 return;
3431
3432 /* apply compositor's own minimization logic (hide) */
3433 set_minimized(shsurf->surface, 1);
3434}
3435
Rafael Antognollie2a34552013-12-03 15:35:45 -02003436static const struct xdg_surface_interface xdg_surface_implementation = {
3437 xdg_surface_destroy,
3438 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003439 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003440 xdg_surface_set_title,
3441 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003442 xdg_surface_move,
3443 xdg_surface_resize,
3444 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003445 xdg_surface_request_change_state,
3446 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003447 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003448};
3449
3450static void
3451xdg_send_configure(struct weston_surface *surface,
3452 uint32_t edges, int32_t width, int32_t height)
3453{
3454 struct shell_surface *shsurf = get_shell_surface(surface);
3455
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003456 assert(shsurf);
3457
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003458 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003459}
3460
3461static const struct weston_shell_client xdg_client = {
3462 xdg_send_configure
3463};
3464
3465static void
3466xdg_use_unstable_version(struct wl_client *client,
3467 struct wl_resource *resource,
3468 int32_t version)
3469{
3470 if (version > 1) {
3471 wl_resource_post_error(resource,
3472 1,
3473 "xdg-shell:: version not implemented yet.");
3474 return;
3475 }
3476}
3477
3478static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003479create_xdg_surface(struct shell_client *owner, void *shell,
3480 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003481 const struct weston_shell_client *client)
3482{
3483 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003484
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003485 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003486 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003487
3488 return shsurf;
3489}
3490
3491static void
3492xdg_get_xdg_surface(struct wl_client *client,
3493 struct wl_resource *resource,
3494 uint32_t id,
3495 struct wl_resource *surface_resource)
3496{
3497 struct weston_surface *surface =
3498 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003499 struct shell_client *sc = wl_resource_get_user_data(resource);
3500 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003501 struct shell_surface *shsurf;
3502
3503 if (get_shell_surface(surface)) {
3504 wl_resource_post_error(surface_resource,
3505 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003506 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003507 return;
3508 }
3509
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003510 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003511 if (!shsurf) {
3512 wl_resource_post_error(surface_resource,
3513 WL_DISPLAY_ERROR_INVALID_OBJECT,
3514 "surface->configure already set");
3515 return;
3516 }
3517
3518 shsurf->resource =
3519 wl_resource_create(client,
3520 &xdg_surface_interface, 1, id);
3521 wl_resource_set_implementation(shsurf->resource,
3522 &xdg_surface_implementation,
3523 shsurf, shell_destroy_shell_surface);
3524}
3525
3526static bool
3527shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3528{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003529 return shsurf->resource &&
3530 wl_resource_instance_of(shsurf->resource,
3531 &xdg_surface_interface,
3532 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003533}
3534
3535/* xdg-popup implementation */
3536
3537static void
3538xdg_popup_destroy(struct wl_client *client,
3539 struct wl_resource *resource)
3540{
3541 wl_resource_destroy(resource);
3542}
3543
Rafael Antognollie2a34552013-12-03 15:35:45 -02003544static const struct xdg_popup_interface xdg_popup_implementation = {
3545 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003546};
3547
3548static void
3549xdg_popup_send_configure(struct weston_surface *surface,
3550 uint32_t edges, int32_t width, int32_t height)
3551{
3552}
3553
3554static const struct weston_shell_client xdg_popup_client = {
3555 xdg_popup_send_configure
3556};
3557
3558static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003559create_xdg_popup(struct shell_client *owner, void *shell,
3560 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003561 const struct weston_shell_client *client,
3562 struct weston_surface *parent,
3563 struct shell_seat *seat,
3564 uint32_t serial,
3565 int32_t x, int32_t y)
3566{
3567 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003568
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003569 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003570 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003571 shsurf->popup.shseat = seat;
3572 shsurf->popup.serial = serial;
3573 shsurf->popup.x = x;
3574 shsurf->popup.y = y;
3575 shell_surface_set_parent(shsurf, parent);
3576
3577 return shsurf;
3578}
3579
3580static void
3581xdg_get_xdg_popup(struct wl_client *client,
3582 struct wl_resource *resource,
3583 uint32_t id,
3584 struct wl_resource *surface_resource,
3585 struct wl_resource *parent_resource,
3586 struct wl_resource *seat_resource,
3587 uint32_t serial,
3588 int32_t x, int32_t y, uint32_t flags)
3589{
3590 struct weston_surface *surface =
3591 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003592 struct shell_client *sc = wl_resource_get_user_data(resource);
3593 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003594 struct shell_surface *shsurf;
3595 struct weston_surface *parent;
3596 struct shell_seat *seat;
3597
3598 if (get_shell_surface(surface)) {
3599 wl_resource_post_error(surface_resource,
3600 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003601 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003602 return;
3603 }
3604
3605 if (!parent_resource) {
3606 wl_resource_post_error(surface_resource,
3607 WL_DISPLAY_ERROR_INVALID_OBJECT,
3608 "xdg_shell::get_xdg_popup requires a parent shell surface");
3609 }
3610
3611 parent = wl_resource_get_user_data(parent_resource);
3612 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3613
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003614 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003615 parent, seat, serial, x, y);
3616 if (!shsurf) {
3617 wl_resource_post_error(surface_resource,
3618 WL_DISPLAY_ERROR_INVALID_OBJECT,
3619 "surface->configure already set");
3620 return;
3621 }
3622
3623 shsurf->resource =
3624 wl_resource_create(client,
3625 &xdg_popup_interface, 1, id);
3626 wl_resource_set_implementation(shsurf->resource,
3627 &xdg_popup_implementation,
3628 shsurf, shell_destroy_shell_surface);
3629}
3630
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003631static void
3632xdg_pong(struct wl_client *client,
3633 struct wl_resource *resource, uint32_t serial)
3634{
3635 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003636
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003637 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003638}
3639
Rafael Antognollie2a34552013-12-03 15:35:45 -02003640static bool
3641shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3642{
3643 return wl_resource_instance_of(shsurf->resource,
3644 &xdg_popup_interface,
3645 &xdg_popup_implementation);
3646}
3647
3648static const struct xdg_shell_interface xdg_implementation = {
3649 xdg_use_unstable_version,
3650 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003651 xdg_get_xdg_popup,
3652 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003653};
3654
3655static int
3656xdg_shell_unversioned_dispatch(const void *implementation,
3657 void *_target, uint32_t opcode,
3658 const struct wl_message *message,
3659 union wl_argument *args)
3660{
3661 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003662 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003663
3664 if (opcode != 0) {
3665 wl_resource_post_error(resource,
3666 WL_DISPLAY_ERROR_INVALID_OBJECT,
3667 "must call use_unstable_version first");
3668 return 0;
3669 }
3670
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003671#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003672
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003673 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3674 "shell implementation doesn't match protocol version");
3675
Rafael Antognollie2a34552013-12-03 15:35:45 -02003676 if (args[0].i != XDG_SERVER_VERSION) {
3677 wl_resource_post_error(resource,
3678 WL_DISPLAY_ERROR_INVALID_OBJECT,
3679 "incompatible version, server is %d "
3680 "client wants %d",
3681 XDG_SERVER_VERSION, args[0].i);
3682 return 0;
3683 }
3684
3685 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003686 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003687
3688 return 1;
3689}
3690
3691/* end of xdg-shell implementation */
3692/***********************************/
3693
Kristian Høgsberg07937562011-04-12 17:25:42 -04003694static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003695shell_fade(struct desktop_shell *shell, enum fade_type type);
3696
3697static int
3698screensaver_timeout(void *data)
3699{
3700 struct desktop_shell *shell = data;
3701
3702 shell_fade(shell, FADE_OUT);
3703
3704 return 1;
3705}
3706
3707static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003708handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003709{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003710 struct desktop_shell *shell =
3711 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003712
Pekka Paalanen18027e52011-12-02 16:31:49 +02003713 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003714
3715 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003716 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003717}
3718
3719static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003720launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003721{
3722 if (shell->screensaver.binding)
3723 return;
3724
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003725 if (!shell->screensaver.path) {
3726 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003727 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003728 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003729
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003730 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003731 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003732 return;
3733 }
3734
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003735 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003736 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003737 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003738 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003739}
3740
3741static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003742terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003743{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003744 if (shell->screensaver.process.pid == 0)
3745 return;
3746
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003747 /* Disarm the screensaver timer, otherwise it may fire when the
3748 * compositor is not in the idle state. In that case, the screen will
3749 * be locked, but the wake_signal won't fire on user input, making the
3750 * system unresponsive. */
3751 wl_event_source_timer_update(shell->screensaver.timer, 0);
3752
Pekka Paalanen18027e52011-12-02 16:31:49 +02003753 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003754}
3755
3756static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003757configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003758{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003759 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003760
Jason Ekstranda7af7042013-10-12 22:38:11 -05003761 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3762 if (v->output == ev->output && v != ev) {
3763 weston_view_unmap(v);
3764 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003765 }
3766 }
3767
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003768 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003769
Jason Ekstranda7af7042013-10-12 22:38:11 -05003770 if (wl_list_empty(&ev->layer_link)) {
3771 wl_list_insert(&layer->view_list, &ev->layer_link);
3772 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003773 }
3774}
3775
3776static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003777background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003778{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003779 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003780 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003781
Jason Ekstranda7af7042013-10-12 22:38:11 -05003782 view = container_of(es->views.next, struct weston_view, surface_link);
3783
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003784 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003785}
3786
3787static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003788desktop_shell_set_background(struct wl_client *client,
3789 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003790 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003791 struct wl_resource *surface_resource)
3792{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003793 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003794 struct weston_surface *surface =
3795 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003796 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003797
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003798 if (surface->configure) {
3799 wl_resource_post_error(surface_resource,
3800 WL_DISPLAY_ERROR_INVALID_OBJECT,
3801 "surface role already assigned");
3802 return;
3803 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003804
Jason Ekstranda7af7042013-10-12 22:38:11 -05003805 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3806 weston_view_destroy(view);
3807 view = weston_view_create(surface);
3808
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003809 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003810 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003811 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003812 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003813 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003814 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003815 surface->output->width,
3816 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003817}
3818
3819static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003820panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003821{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003822 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003823 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003824
Jason Ekstranda7af7042013-10-12 22:38:11 -05003825 view = container_of(es->views.next, struct weston_view, surface_link);
3826
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003827 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003828}
3829
3830static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003831desktop_shell_set_panel(struct wl_client *client,
3832 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003833 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003834 struct wl_resource *surface_resource)
3835{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003836 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003837 struct weston_surface *surface =
3838 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003839 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003840
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003841 if (surface->configure) {
3842 wl_resource_post_error(surface_resource,
3843 WL_DISPLAY_ERROR_INVALID_OBJECT,
3844 "surface role already assigned");
3845 return;
3846 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003847
Jason Ekstranda7af7042013-10-12 22:38:11 -05003848 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3849 weston_view_destroy(view);
3850 view = weston_view_create(surface);
3851
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003852 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003853 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003854 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003855 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003856 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003857 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003858 surface->output->width,
3859 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003860}
3861
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003862static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003863lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003864{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003865 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003866 struct weston_view *view;
3867
3868 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003869
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003870 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003871 return;
3872
Jason Ekstranda7af7042013-10-12 22:38:11 -05003873 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003874
3875 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003876 wl_list_insert(&shell->lock_layer.view_list,
3877 &view->layer_link);
3878 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003879 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003880 }
3881}
3882
3883static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003884handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003885{
Tiago Vignattibe143262012-04-16 17:31:41 +03003886 struct desktop_shell *shell =
3887 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003888
Martin Minarik6d118362012-06-07 18:01:59 +02003889 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003890 shell->lock_surface = NULL;
3891}
3892
3893static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003894desktop_shell_set_lock_surface(struct wl_client *client,
3895 struct wl_resource *resource,
3896 struct wl_resource *surface_resource)
3897{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003898 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003899 struct weston_surface *surface =
3900 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003901
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003902 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003903
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003904 if (!shell->locked)
3905 return;
3906
Pekka Paalanen98262232011-12-01 10:42:22 +02003907 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003908
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003909 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003910 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003911 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003912
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003913 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003914 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003915 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003916}
3917
3918static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003919resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003920{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003921 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003922
Pekka Paalanen77346a62011-11-30 16:26:35 +02003923 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003924
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003925 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003926 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003927 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003928 &shell->input_panel_layer.link);
3929 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003930 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003931 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003932 wl_list_insert(&shell->compositor->cursor_layer.link,
3933 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003934 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003935 wl_list_insert(&shell->fullscreen_layer.link,
3936 &shell->panel_layer.link);
3937 wl_list_insert(&shell->panel_layer.link,
3938 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003939
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003940 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003941
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003942 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003943 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003944 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003945}
3946
3947static void
3948desktop_shell_unlock(struct wl_client *client,
3949 struct wl_resource *resource)
3950{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003951 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003952
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003953 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003954
3955 if (shell->locked)
3956 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003957}
3958
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003959static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003960desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003961 struct wl_resource *resource,
3962 struct wl_resource *surface_resource)
3963{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003964 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003965
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003966 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003967 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003968}
3969
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003970static void
3971desktop_shell_desktop_ready(struct wl_client *client,
3972 struct wl_resource *resource)
3973{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003974 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003975
3976 shell_fade_startup(shell);
3977}
3978
Kristian Høgsberg75840622011-09-06 13:48:16 -04003979static const struct desktop_shell_interface desktop_shell_implementation = {
3980 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003981 desktop_shell_set_panel,
3982 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003983 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003984 desktop_shell_set_grab_surface,
3985 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003986};
3987
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003988static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003989get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003990{
3991 struct shell_surface *shsurf;
3992
3993 shsurf = get_shell_surface(surface);
3994 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003995 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003996 return shsurf->type;
3997}
3998
Kristian Høgsberg75840622011-09-06 13:48:16 -04003999static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004000move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004001{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004002 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004003 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004004 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004005
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004006 if (seat->pointer->focus == NULL)
4007 return;
4008
4009 focus = seat->pointer->focus->surface;
4010
Pekka Paalanen01388e22013-04-25 13:57:44 +03004011 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004012 if (surface == NULL)
4013 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004014
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004015 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004016 if (shsurf == NULL || shsurf->state.fullscreen ||
4017 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004018 return;
4019
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004020 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004021}
4022
4023static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004024maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4025{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004026 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004027 struct weston_surface *surface;
4028 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004029 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004030
4031 surface = weston_surface_get_main_surface(focus);
4032 if (surface == NULL)
4033 return;
4034
4035 shsurf = get_shell_surface(surface);
4036 if (shsurf == NULL)
4037 return;
4038
4039 if (!shell_surface_is_xdg_surface(shsurf))
4040 return;
4041
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004042 serial = wl_display_next_serial(seat->compositor->wl_display);
4043 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4044 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004045}
4046
4047static void
4048fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4049{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004050 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004051 struct weston_surface *surface;
4052 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004053 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004054
4055 surface = weston_surface_get_main_surface(focus);
4056 if (surface == NULL)
4057 return;
4058
4059 shsurf = get_shell_surface(surface);
4060 if (shsurf == NULL)
4061 return;
4062
4063 if (!shell_surface_is_xdg_surface(shsurf))
4064 return;
4065
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004066 serial = wl_display_next_serial(seat->compositor->wl_display);
4067 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4068 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004069}
4070
4071static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004072touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4073{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004074 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004075 struct weston_surface *surface;
4076 struct shell_surface *shsurf;
4077
4078 surface = weston_surface_get_main_surface(focus);
4079 if (surface == NULL)
4080 return;
4081
4082 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004083 if (shsurf == NULL || shsurf->state.fullscreen ||
4084 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004085 return;
4086
4087 surface_touch_move(shsurf, (struct weston_seat *) seat);
4088}
4089
4090static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004091resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004092{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004093 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004094 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004095 uint32_t edges = 0;
4096 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004097 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004098
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004099 if (seat->pointer->focus == NULL)
4100 return;
4101
4102 focus = seat->pointer->focus->surface;
4103
Pekka Paalanen01388e22013-04-25 13:57:44 +03004104 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004105 if (surface == NULL)
4106 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004107
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004108 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004109 if (shsurf == NULL || shsurf->state.fullscreen ||
4110 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004111 return;
4112
Jason Ekstranda7af7042013-10-12 22:38:11 -05004113 weston_view_from_global(shsurf->view,
4114 wl_fixed_to_int(seat->pointer->grab_x),
4115 wl_fixed_to_int(seat->pointer->grab_y),
4116 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004117
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004118 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004119 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004120 else if (x < 2 * shsurf->surface->width / 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_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004124
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004125 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004126 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004127 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004128 edges |= 0;
4129 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004130 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004131
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004132 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004133}
4134
4135static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004136surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004137 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004138{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004139 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004140 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004141 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004142 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004143
Pekka Paalanen01388e22013-04-25 13:57:44 +03004144 /* XXX: broken for windows containing sub-surfaces */
4145 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004146 if (surface == NULL)
4147 return;
4148
4149 shsurf = get_shell_surface(surface);
4150 if (!shsurf)
4151 return;
4152
Jason Ekstranda7af7042013-10-12 22:38:11 -05004153 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004154
Jason Ekstranda7af7042013-10-12 22:38:11 -05004155 if (shsurf->view->alpha > 1.0)
4156 shsurf->view->alpha = 1.0;
4157 if (shsurf->view->alpha < step)
4158 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004159
Jason Ekstranda7af7042013-10-12 22:38:11 -05004160 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004161 weston_surface_damage(surface);
4162}
4163
4164static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004165do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004166 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004167{
Daniel Stone37816df2012-05-16 18:45:18 +01004168 struct weston_seat *ws = (struct weston_seat *) seat;
4169 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004170 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004171 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004172
4173 wl_list_for_each(output, &compositor->output_list, link) {
4174 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004175 wl_fixed_to_double(seat->pointer->x),
4176 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004177 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004178 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004179 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004180 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004181 increment = -output->zoom.increment;
4182 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004183 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004184 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004185 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004186 else
4187 increment = 0;
4188
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004189 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004190
Scott Moreaue6603982012-06-11 13:07:51 -06004191 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004192 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004193 else if (output->zoom.level > output->zoom.max_level)
4194 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004195 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004196 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004197 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004198
Scott Moreaue6603982012-06-11 13:07:51 -06004199 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004200
Jason Ekstranda7af7042013-10-12 22:38:11 -05004201 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004202 }
4203 }
4204}
4205
Scott Moreauccbf29d2012-02-22 14:21:41 -07004206static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004207zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004208 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004209{
4210 do_zoom(seat, time, 0, axis, value);
4211}
4212
4213static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004214zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004215 void *data)
4216{
4217 do_zoom(seat, time, key, 0, 0);
4218}
4219
4220static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004221terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004222 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004223{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004224 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004225
Daniel Stone325fc2d2012-05-30 16:31:58 +01004226 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004227}
4228
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004229static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004230rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4231 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004232{
4233 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004234 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004235 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004236 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004237 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004238
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004239 weston_pointer_move(pointer, x, y);
4240
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004241 if (!shsurf)
4242 return;
4243
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004244 cx = 0.5f * shsurf->surface->width;
4245 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004246
Daniel Stone37816df2012-05-16 18:45:18 +01004247 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4248 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004249 r = sqrtf(dx * dx + dy * dy);
4250
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004251 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004252 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004253
4254 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004255 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004256 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004257
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004258 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004259 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004260
4261 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004262 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004263 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004264 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004265 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004266
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004267 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004269 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004270 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004271 wl_list_init(&shsurf->rotation.transform.link);
4272 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004273 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004274 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004275
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004276 /* We need to adjust the position of the surface
4277 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004278 cposx = shsurf->view->geometry.x + cx;
4279 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004280 dposx = rotate->center.x - cposx;
4281 dposy = rotate->center.y - cposy;
4282 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004283 weston_view_set_position(shsurf->view,
4284 shsurf->view->geometry.x + dposx,
4285 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004286 }
4287
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004288 /* Repaint implies weston_surface_update_transform(), which
4289 * lazily applies the damage due to rotation update.
4290 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004291 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004292}
4293
4294static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004295rotate_grab_button(struct weston_pointer_grab *grab,
4296 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004297{
4298 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004299 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004300 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004301 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004302 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004303
Daniel Stone4dbadb12012-05-30 16:31:51 +01004304 if (pointer->button_count == 0 &&
4305 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004306 if (shsurf)
4307 weston_matrix_multiply(&shsurf->rotation.rotation,
4308 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004309 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004310 free(rotate);
4311 }
4312}
4313
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004314static void
4315rotate_grab_cancel(struct weston_pointer_grab *grab)
4316{
4317 struct rotate_grab *rotate =
4318 container_of(grab, struct rotate_grab, base.grab);
4319
4320 shell_grab_end(&rotate->base);
4321 free(rotate);
4322}
4323
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004324static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004325 noop_grab_focus,
4326 rotate_grab_motion,
4327 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004328 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004329};
4330
4331static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004332surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004333{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004334 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004335 float dx, dy;
4336 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004337
Pekka Paalanen460099f2012-01-20 16:48:25 +02004338 rotate = malloc(sizeof *rotate);
4339 if (!rotate)
4340 return;
4341
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004343 surface->surface->width * 0.5f,
4344 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004345 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004346
Daniel Stone37816df2012-05-16 18:45:18 +01004347 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4348 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004349 r = sqrtf(dx * dx + dy * dy);
4350 if (r > 20.0f) {
4351 struct weston_matrix inverse;
4352
4353 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004354 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004355 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004356
4357 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004358 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004359 } else {
4360 weston_matrix_init(&surface->rotation.rotation);
4361 weston_matrix_init(&rotate->rotation);
4362 }
4363
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004364 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4365 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004366}
4367
4368static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004369rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004370 void *data)
4371{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004372 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004373 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004374 struct shell_surface *surface;
4375
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004376 if (seat->pointer->focus == NULL)
4377 return;
4378
4379 focus = seat->pointer->focus->surface;
4380
Pekka Paalanen01388e22013-04-25 13:57:44 +03004381 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004382 if (base_surface == NULL)
4383 return;
4384
4385 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004386 if (surface == NULL || surface->state.fullscreen ||
4387 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004388 return;
4389
4390 surface_rotate(surface, seat);
4391}
4392
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004393/* Move all fullscreen layers down to the current workspace in a non-reversible
4394 * manner. This should be used when implementing shell-wide overlays, such as
4395 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004396void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004397lower_fullscreen_layer(struct desktop_shell *shell)
4398{
4399 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004401
4402 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 wl_list_for_each_reverse_safe(view, prev,
4404 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004405 layer_link) {
4406 wl_list_remove(&view->layer_link);
4407 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4408 weston_view_damage_below(view);
4409 weston_surface_damage(view->surface);
4410 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004411}
4412
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004413void
Tiago Vignattibe143262012-04-16 17:31:41 +03004414activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004415 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004416{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004417 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004418 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004419 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004420 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004421 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004422
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004423 lower_fullscreen_layer(shell);
4424
Pekka Paalanen01388e22013-04-25 13:57:44 +03004425 main_surface = weston_surface_get_main_surface(es);
4426
Daniel Stone37816df2012-05-16 18:45:18 +01004427 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004428
Jonas Ådahl8538b222012-08-29 22:13:03 +02004429 state = ensure_focus_state(shell, seat);
4430 if (state == NULL)
4431 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004432
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004433 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004434 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004435
Rafael Antognolli03b16592013-12-03 15:35:42 -02004436 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004437 assert(shsurf);
4438
Rafael Antognolli03b16592013-12-03 15:35:42 -02004439 if (shsurf->state.fullscreen)
4440 shell_configure_fullscreen(shsurf);
4441 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004442 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004443
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004444 /* Update the surface’s layer. This brings it to the top of the stacking
4445 * order as appropriate. */
4446 shell_surface_update_layer(shsurf);
4447
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004448 if (shell->focus_animation_type != ANIMATION_NONE) {
4449 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004450 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004451 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004452}
4453
Alex Wu21858432012-04-01 20:13:08 +08004454/* no-op func for checking black surface */
4455static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004456black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004457{
4458}
4459
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004460static bool
Alex Wu21858432012-04-01 20:13:08 +08004461is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4462{
4463 if (es->configure == black_surface_configure) {
4464 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004465 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004466 return true;
4467 }
4468 return false;
4469}
4470
Kristian Høgsberg75840622011-09-06 13:48:16 -04004471static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004472activate_binding(struct weston_seat *seat,
4473 struct desktop_shell *shell,
4474 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004475{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004476 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004477
Alex Wu9c35e6b2012-03-05 14:13:13 +08004478 if (!focus)
4479 return;
4480
Pekka Paalanen01388e22013-04-25 13:57:44 +03004481 if (is_black_surface(focus, &main_surface))
4482 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004483
Pekka Paalanen01388e22013-04-25 13:57:44 +03004484 main_surface = weston_surface_get_main_surface(focus);
4485 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004486 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004487
Neil Robertsa28c6932013-10-03 16:43:04 +01004488 activate(shell, focus, seat);
4489}
4490
4491static void
4492click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4493 void *data)
4494{
4495 if (seat->pointer->grab != &seat->pointer->default_grab)
4496 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004497 if (seat->pointer->focus == NULL)
4498 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004499
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004500 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004501}
4502
4503static void
4504touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4505{
4506 if (seat->touch->grab != &seat->touch->default_grab)
4507 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004508 if (seat->touch->focus == NULL)
4509 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004510
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004511 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004512}
4513
4514static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004515unfocus_all_seats(struct desktop_shell *shell)
4516{
4517 struct weston_seat *seat, *next;
4518
4519 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4520 if (seat->keyboard == NULL)
4521 continue;
4522
4523 weston_keyboard_set_focus(seat->keyboard, NULL);
4524 }
4525}
4526
4527static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004528lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004529{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004530 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004531
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004532 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004533 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004534 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004535 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004536
4537 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004538
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004539 /* Hide all surfaces by removing the fullscreen, panel and
4540 * toplevel layers. This way nothing else can show or receive
4541 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004542
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004543 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004544 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004545 if (shell->showing_input_panels)
4546 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004547 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004548 wl_list_insert(&shell->compositor->cursor_layer.link,
4549 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004550
Pekka Paalanen77346a62011-11-30 16:26:35 +02004551 launch_screensaver(shell);
4552
Neil Robertsb4a91702014-04-09 16:33:32 +01004553 /* Remove the keyboard focus on all seats. This will be
4554 * restored to the workspace's saved state via
4555 * restore_focus_state when the compositor is unlocked */
4556 unfocus_all_seats(shell);
4557
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004558 /* TODO: disable bindings that should not work while locked. */
4559
4560 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004561}
4562
4563static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004564unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004565{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004566 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004567 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004568 return;
4569 }
4570
4571 /* If desktop-shell client has gone away, unlock immediately. */
4572 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004573 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004574 return;
4575 }
4576
4577 if (shell->prepare_event_sent)
4578 return;
4579
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004580 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004581 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004582}
4583
4584static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004585shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004586{
4587 struct desktop_shell *shell = data;
4588
4589 shell->fade.animation = NULL;
4590
4591 switch (shell->fade.type) {
4592 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004593 weston_surface_destroy(shell->fade.view->surface);
4594 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004595 break;
4596 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004597 lock(shell);
4598 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004599 default:
4600 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004601 }
4602}
4603
Jason Ekstranda7af7042013-10-12 22:38:11 -05004604static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004605shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004606{
4607 struct weston_compositor *compositor = shell->compositor;
4608 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004609 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004610
4611 surface = weston_surface_create(compositor);
4612 if (!surface)
4613 return NULL;
4614
Jason Ekstranda7af7042013-10-12 22:38:11 -05004615 view = weston_view_create(surface);
4616 if (!view) {
4617 weston_surface_destroy(surface);
4618 return NULL;
4619 }
4620
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004621 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004622 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004623 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004624 wl_list_insert(&compositor->fade_layer.view_list,
4625 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004626 pixman_region32_init(&surface->input);
4627
Jason Ekstranda7af7042013-10-12 22:38:11 -05004628 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004629}
4630
4631static void
4632shell_fade(struct desktop_shell *shell, enum fade_type type)
4633{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004634 float tint;
4635
4636 switch (type) {
4637 case FADE_IN:
4638 tint = 0.0;
4639 break;
4640 case FADE_OUT:
4641 tint = 1.0;
4642 break;
4643 default:
4644 weston_log("shell: invalid fade type\n");
4645 return;
4646 }
4647
4648 shell->fade.type = type;
4649
Jason Ekstranda7af7042013-10-12 22:38:11 -05004650 if (shell->fade.view == NULL) {
4651 shell->fade.view = shell_fade_create_surface(shell);
4652 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004653 return;
4654
Jason Ekstranda7af7042013-10-12 22:38:11 -05004655 shell->fade.view->alpha = 1.0 - tint;
4656 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004657 }
4658
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004659 if (shell->fade.view->output == NULL) {
4660 /* If the black view gets a NULL output, we lost the
4661 * last output and we'll just cancel the fade. This
4662 * happens when you close the last window under the
4663 * X11 or Wayland backends. */
4664 shell->locked = false;
4665 weston_surface_destroy(shell->fade.view->surface);
4666 shell->fade.view = NULL;
4667 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004668 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004669 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004670 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004671 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004672 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004673 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004674 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004675}
4676
4677static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004678do_shell_fade_startup(void *data)
4679{
4680 struct desktop_shell *shell = data;
4681
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004682 if (shell->startup_animation_type == ANIMATION_FADE)
4683 shell_fade(shell, FADE_IN);
4684 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004685 weston_surface_destroy(shell->fade.view->surface);
4686 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004687 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004688}
4689
4690static void
4691shell_fade_startup(struct desktop_shell *shell)
4692{
4693 struct wl_event_loop *loop;
4694
4695 if (!shell->fade.startup_timer)
4696 return;
4697
4698 wl_event_source_remove(shell->fade.startup_timer);
4699 shell->fade.startup_timer = NULL;
4700
4701 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4702 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4703}
4704
4705static int
4706fade_startup_timeout(void *data)
4707{
4708 struct desktop_shell *shell = data;
4709
4710 shell_fade_startup(shell);
4711 return 0;
4712}
4713
4714static void
4715shell_fade_init(struct desktop_shell *shell)
4716{
4717 /* Make compositor output all black, and wait for the desktop-shell
4718 * client to signal it is ready, then fade in. The timer triggers a
4719 * fade-in, in case the desktop-shell client takes too long.
4720 */
4721
4722 struct wl_event_loop *loop;
4723
Jason Ekstranda7af7042013-10-12 22:38:11 -05004724 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004725 weston_log("%s: warning: fade surface already exists\n",
4726 __func__);
4727 return;
4728 }
4729
Jason Ekstranda7af7042013-10-12 22:38:11 -05004730 shell->fade.view = shell_fade_create_surface(shell);
4731 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004732 return;
4733
Jason Ekstranda7af7042013-10-12 22:38:11 -05004734 weston_view_update_transform(shell->fade.view);
4735 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004736
4737 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4738 shell->fade.startup_timer =
4739 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4740 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4741}
4742
4743static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004744idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004745{
4746 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004747 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004748 struct weston_seat *seat;
4749
4750 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4751 if (seat->pointer)
4752 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004753
4754 shell_fade(shell, FADE_OUT);
4755 /* lock() is called from shell_fade_done() */
4756}
4757
4758static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004759wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004760{
4761 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004762 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004763
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004764 unlock(shell);
4765}
4766
4767static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004768center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004769{
Giulio Camuffob8366642013-04-25 13:57:46 +03004770 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004771 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004772
Jason Ekstranda7af7042013-10-12 22:38:11 -05004773 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004774
4775 x = output->x + (output->width - width) / 2 - surf_x / 2;
4776 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004777
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004778 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004779}
4780
4781static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004782weston_view_set_initial_position(struct weston_view *view,
4783 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004784{
4785 struct weston_compositor *compositor = shell->compositor;
4786 int ix = 0, iy = 0;
4787 int range_x, range_y;
4788 int dx, dy, x, y, panel_height;
4789 struct weston_output *output, *target_output = NULL;
4790 struct weston_seat *seat;
4791
4792 /* As a heuristic place the new window on the same output as the
4793 * pointer. Falling back to the output containing 0, 0.
4794 *
4795 * TODO: Do something clever for touch too?
4796 */
4797 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004798 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004799 ix = wl_fixed_to_int(seat->pointer->x);
4800 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004801 break;
4802 }
4803 }
4804
4805 wl_list_for_each(output, &compositor->output_list, link) {
4806 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4807 target_output = output;
4808 break;
4809 }
4810 }
4811
4812 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004813 weston_view_set_position(view, 10 + random() % 400,
4814 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004815 return;
4816 }
4817
4818 /* Valid range within output where the surface will still be onscreen.
4819 * If this is negative it means that the surface is bigger than
4820 * output.
4821 */
4822 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004823 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004824 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004825 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004826
Rob Bradford4cb88c72012-08-13 15:18:44 +01004827 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004828 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004829 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004830 dx = 0;
4831
4832 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004833 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004834 else
4835 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004836
4837 x = target_output->x + dx;
4838 y = target_output->y + dy;
4839
Jason Ekstranda7af7042013-10-12 22:38:11 -05004840 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004841}
4842
4843static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004844map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004845 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004846{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004847 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004848 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004849 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004850 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004851
Pekka Paalanen77346a62011-11-30 16:26:35 +02004852 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004853 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004854 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004855 if (shsurf->state.fullscreen) {
4856 center_on_output(shsurf->view, shsurf->fullscreen_output);
4857 shell_map_fullscreen(shsurf);
4858 } else if (shsurf->state.maximized) {
4859 /* use surface configure to set the geometry */
4860 panel_height = get_output_panel_height(shell, shsurf->output);
4861 surface_subsurfaces_boundingbox(shsurf->surface,
4862 &surf_x, &surf_y, NULL, NULL);
4863 weston_view_set_position(shsurf->view,
4864 shsurf->output->x - surf_x,
4865 shsurf->output->y +
4866 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004867 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004868 weston_view_set_initial_position(shsurf->view, shell);
4869 }
Juan Zhao96879df2012-02-07 08:45:41 +08004870 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004871 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004872 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004873 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004874 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004875 weston_view_set_position(shsurf->view,
4876 shsurf->view->geometry.x + sx,
4877 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004878 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004879 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004880 default:
4881 ;
4882 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004883
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004884 /* Surface stacking order, see also activate(). */
4885 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004886
Jason Ekstranda7af7042013-10-12 22:38:11 -05004887 if (shsurf->type != SHELL_SURFACE_NONE) {
4888 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004889 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 shsurf->surface->output = shsurf->output;
4891 shsurf->view->output = shsurf->output;
4892 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004893 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004894
Jason Ekstranda7af7042013-10-12 22:38:11 -05004895 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004896 /* XXX: xwayland's using the same fields for transient type */
4897 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004898 if (shsurf->transient.flags ==
4899 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4900 break;
4901 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004902 if (shsurf->state.relative &&
4903 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4904 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004905 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004906 break;
4907 wl_list_for_each(seat, &compositor->seat_list, link)
4908 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004909 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004910 case SHELL_SURFACE_POPUP:
4911 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004912 default:
4913 break;
4914 }
4915
Rafael Antognolli03b16592013-12-03 15:35:42 -02004916 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4917 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004918 {
4919 switch (shell->win_animation_type) {
4920 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004921 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004922 break;
4923 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004924 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004925 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004926 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004927 default:
4928 break;
4929 }
4930 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004931}
4932
4933static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004934configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004935 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004936{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004937 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004938 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004939 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004940
Pekka Paalanen77346a62011-11-30 16:26:35 +02004941 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004942
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004943 assert(shsurf);
4944
Rafael Antognolli03b16592013-12-03 15:35:42 -02004945 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004946 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004947 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004948 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004949 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4950 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004951 mx = shsurf->output->x - surf_x;
4952 my = shsurf->output->y +
4953 get_output_panel_height(shell,shsurf->output) - surf_y;
4954 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004955 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004956 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004957 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004958
Alex Wu4539b082012-03-01 12:57:46 +08004959 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004960 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004961 wl_list_for_each(view, &surface->views, surface_link)
4962 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004963
Rafael Antognolli03b16592013-12-03 15:35:42 -02004964 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004965 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004966 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004967}
4968
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004969static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004970shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004971{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004972 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004973 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004974 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004975
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004976 assert(shsurf);
4977
4978 shell = shsurf->shell;
4979
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004980 if (!weston_surface_is_mapped(es) &&
4981 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004982 remove_popup_grab(shsurf);
4983 }
4984
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004985 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004986 return;
4987
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004988 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004989 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004990 type_changed = 1;
4991 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004992
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004993 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004994 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004995 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004996 shsurf->last_width != es->width ||
4997 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004998 float from_x, from_y;
4999 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005000
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005001 if (shsurf->resize_edges) {
5002 sx = 0;
5003 sy = 0;
5004 }
5005
5006 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5007 sx = shsurf->last_width - es->width;
5008 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5009 sy = shsurf->last_height - es->height;
5010
5011 shsurf->last_width = es->width;
5012 shsurf->last_height = es->height;
5013
Jason Ekstranda7af7042013-10-12 22:38:11 -05005014 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5015 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005016 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005017 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005018 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005019 }
5020}
5021
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005022static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005023
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005024static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005025desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005026{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005027 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005028 struct desktop_shell *shell =
5029 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005030
5031 shell->child.process.pid = 0;
5032 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005033
5034 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5035 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005036 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005037 shell->child.deathstamp = time;
5038 shell->child.deathcount = 0;
5039 }
5040
5041 shell->child.deathcount++;
5042 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005043 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005044 return;
5045 }
5046
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005047 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005048 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005049 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005050}
5051
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005052static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005053desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5054{
5055 struct desktop_shell *shell;
5056
5057 shell = container_of(listener, struct desktop_shell,
5058 child.client_destroy_listener);
5059
5060 shell->child.client = NULL;
5061}
5062
5063static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005064launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005065{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005066 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005067
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005068 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005069 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005070 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005071 desktop_shell_sigchld);
5072
5073 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005074 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005075
5076 shell->child.client_destroy_listener.notify =
5077 desktop_shell_client_destroy;
5078 wl_client_add_destroy_listener(shell->child.client,
5079 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005080}
5081
5082static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005083handle_shell_client_destroy(struct wl_listener *listener, void *data)
5084{
5085 struct shell_client *sc =
5086 container_of(listener, struct shell_client, destroy_listener);
5087
5088 if (sc->ping_timer)
5089 wl_event_source_remove(sc->ping_timer);
5090 free(sc);
5091}
5092
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005093static struct shell_client *
5094shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5095 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005096{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005097 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005098
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005099 sc = zalloc(sizeof *sc);
5100 if (sc == NULL) {
5101 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005102 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005103 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005104
5105 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005106 if (sc->resource == NULL) {
5107 free(sc);
5108 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005109 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005110 }
5111
5112 sc->client = client;
5113 sc->shell = shell;
5114 sc->destroy_listener.notify = handle_shell_client_destroy;
5115 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5116
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005117 return sc;
5118}
5119
5120static void
5121bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5122{
5123 struct desktop_shell *shell = data;
5124 struct shell_client *sc;
5125
5126 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5127 if (sc)
5128 wl_resource_set_implementation(sc->resource,
5129 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005130 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005131}
5132
5133static void
5134bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5135{
5136 struct desktop_shell *shell = data;
5137 struct shell_client *sc;
5138
5139 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5140 if (sc)
5141 wl_resource_set_dispatcher(sc->resource,
5142 xdg_shell_unversioned_dispatch,
5143 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005144}
5145
5146static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005147unbind_desktop_shell(struct wl_resource *resource)
5148{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005149 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005150
5151 if (shell->locked)
5152 resume_desktop(shell);
5153
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005154 shell->child.desktop_shell = NULL;
5155 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005156}
5157
5158static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005159bind_desktop_shell(struct wl_client *client,
5160 void *data, uint32_t version, uint32_t id)
5161{
Tiago Vignattibe143262012-04-16 17:31:41 +03005162 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005163 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005164
Jason Ekstranda85118c2013-06-27 20:17:02 -05005165 resource = wl_resource_create(client, &desktop_shell_interface,
5166 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005167
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005168 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005169 wl_resource_set_implementation(resource,
5170 &desktop_shell_implementation,
5171 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005172 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005173
5174 if (version < 2)
5175 shell_fade_startup(shell);
5176
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005177 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005178 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005179
5180 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5181 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005182 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005183}
5184
Pekka Paalanen6e168112011-11-24 11:34:05 +02005185static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005186screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005187{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005188 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005189 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005190
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005191 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005192 return;
5193
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005194 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005195 if (!shell->locked)
5196 return;
5197
Jason Ekstranda7af7042013-10-12 22:38:11 -05005198 view = container_of(surface->views.next, struct weston_view, surface_link);
5199 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005200
Jason Ekstranda7af7042013-10-12 22:38:11 -05005201 if (wl_list_empty(&view->layer_link)) {
5202 wl_list_insert(shell->lock_layer.view_list.prev,
5203 &view->layer_link);
5204 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005205 wl_event_source_timer_update(shell->screensaver.timer,
5206 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005207 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005208 }
5209}
5210
5211static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005212screensaver_set_surface(struct wl_client *client,
5213 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005214 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005215 struct wl_resource *output_resource)
5216{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005217 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005218 struct weston_surface *surface =
5219 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005220 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005221 struct weston_view *view, *next;
5222
5223 /* Make sure we only have one view */
5224 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5225 weston_view_destroy(view);
5226 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005227
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005228 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005229 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005230 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005231}
5232
5233static const struct screensaver_interface screensaver_implementation = {
5234 screensaver_set_surface
5235};
5236
5237static void
5238unbind_screensaver(struct wl_resource *resource)
5239{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005240 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005241
Pekka Paalanen77346a62011-11-30 16:26:35 +02005242 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005243}
5244
5245static void
5246bind_screensaver(struct wl_client *client,
5247 void *data, uint32_t version, uint32_t id)
5248{
Tiago Vignattibe143262012-04-16 17:31:41 +03005249 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005250 struct wl_resource *resource;
5251
Jason Ekstranda85118c2013-06-27 20:17:02 -05005252 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005253
Pekka Paalanen77346a62011-11-30 16:26:35 +02005254 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005255 wl_resource_set_implementation(resource,
5256 &screensaver_implementation,
5257 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005258 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005259 return;
5260 }
5261
5262 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5263 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005264 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005265}
5266
Kristian Høgsberg07045392012-02-19 18:52:44 -05005267struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005268 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005269 struct weston_surface *current;
5270 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005271 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005272 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005273};
5274
5275static void
5276switcher_next(struct switcher *switcher)
5277{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005278 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005279 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005280 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005281 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005282
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005283 /* temporary re-display minimized surfaces */
5284 struct weston_view *tmp;
5285 struct weston_view **minimized;
5286 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5287 wl_list_remove(&view->layer_link);
5288 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5289 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5290 *minimized = view;
5291 }
5292
Jason Ekstranda7af7042013-10-12 22:38:11 -05005293 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005294 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005295 if (shsurf &&
5296 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5297 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005298 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005300 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005301 next = view->surface;
5302 prev = view->surface;
5303 view->alpha = 0.25;
5304 weston_view_geometry_dirty(view);
5305 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005306 }
Alex Wu1659daa2012-04-01 20:13:09 +08005307
Jason Ekstranda7af7042013-10-12 22:38:11 -05005308 if (is_black_surface(view->surface, NULL)) {
5309 view->alpha = 0.25;
5310 weston_view_geometry_dirty(view);
5311 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005312 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005313 }
5314
5315 if (next == NULL)
5316 next = first;
5317
Alex Wu07b26062012-03-12 16:06:01 +08005318 if (next == NULL)
5319 return;
5320
Kristian Høgsberg07045392012-02-19 18:52:44 -05005321 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005322 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005323
5324 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005325 wl_list_for_each(view, &next->views, surface_link)
5326 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005327
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005328 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005329 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005330 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005331}
5332
5333static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005334switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005335{
5336 struct switcher *switcher =
5337 container_of(listener, struct switcher, listener);
5338
5339 switcher_next(switcher);
5340}
5341
5342static void
Daniel Stone351eb612012-05-31 15:27:47 -04005343switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005344{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005345 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005346 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005347 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005348
Jason Ekstranda7af7042013-10-12 22:38:11 -05005349 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005350 if (is_focus_view(view))
5351 continue;
5352
Jason Ekstranda7af7042013-10-12 22:38:11 -05005353 view->alpha = 1.0;
5354 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005355 }
5356
Alex Wu07b26062012-03-12 16:06:01 +08005357 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005358 activate(switcher->shell, switcher->current,
5359 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005360 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005361 weston_keyboard_end_grab(keyboard);
5362 if (keyboard->input_method_resource)
5363 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005364
5365 /* re-hide surfaces that were temporary shown during the switch */
5366 struct weston_view **minimized;
5367 wl_array_for_each(minimized, &switcher->minimized_array) {
5368 /* with the exception of the current selected */
5369 if ((*minimized)->surface != switcher->current) {
5370 wl_list_remove(&(*minimized)->layer_link);
5371 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5372 weston_view_damage_below(*minimized);
5373 }
5374 }
5375 wl_array_release(&switcher->minimized_array);
5376
Kristian Høgsberg07045392012-02-19 18:52:44 -05005377 free(switcher);
5378}
5379
5380static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005381switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005382 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005383{
5384 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005385 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005386
Daniel Stonec9785ea2012-05-30 16:31:52 +01005387 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005388 switcher_next(switcher);
5389}
5390
5391static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005392switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005393 uint32_t mods_depressed, uint32_t mods_latched,
5394 uint32_t mods_locked, uint32_t group)
5395{
5396 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005397 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005398
Daniel Stone351eb612012-05-31 15:27:47 -04005399 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5400 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005401}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005402
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005403static void
5404switcher_cancel(struct weston_keyboard_grab *grab)
5405{
5406 struct switcher *switcher = container_of(grab, struct switcher, grab);
5407
5408 switcher_destroy(switcher);
5409}
5410
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005411static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005412 switcher_key,
5413 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005414 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005415};
5416
5417static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005418switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005419 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005420{
Tiago Vignattibe143262012-04-16 17:31:41 +03005421 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005422 struct switcher *switcher;
5423
5424 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005425 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005426 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005427 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005428 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005429 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005430
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005431 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005432 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005433 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005434 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5435 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005436 switcher_next(switcher);
5437}
5438
Pekka Paalanen3c647232011-12-22 13:43:43 +02005439static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005440backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005441 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005442{
5443 struct weston_compositor *compositor = data;
5444 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005445 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005446
5447 /* TODO: we're limiting to simple use cases, where we assume just
5448 * control on the primary display. We'd have to extend later if we
5449 * ever get support for setting backlights on random desktop LCD
5450 * panels though */
5451 output = get_default_output(compositor);
5452 if (!output)
5453 return;
5454
5455 if (!output->set_backlight)
5456 return;
5457
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005458 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5459 backlight_new = output->backlight_current - 25;
5460 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5461 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005462
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005463 if (backlight_new < 5)
5464 backlight_new = 5;
5465 if (backlight_new > 255)
5466 backlight_new = 255;
5467
5468 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005469 output->set_backlight(output, output->backlight_current);
5470}
5471
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005472struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005473 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005474 struct weston_seat *seat;
5475 uint32_t key[2];
5476 int key_released[2];
5477};
5478
5479static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005480debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005481 uint32_t key, uint32_t state)
5482{
5483 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005484 struct weston_compositor *ec = db->seat->compositor;
5485 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005486 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005487 uint32_t serial;
5488 int send = 0, terminate = 0;
5489 int check_binding = 1;
5490 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005491 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005492
5493 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5494 /* Do not run bindings on key releases */
5495 check_binding = 0;
5496
5497 for (i = 0; i < 2; i++)
5498 if (key == db->key[i])
5499 db->key_released[i] = 1;
5500
5501 if (db->key_released[0] && db->key_released[1]) {
5502 /* All key releases been swalled so end the grab */
5503 terminate = 1;
5504 } else if (key != db->key[0] && key != db->key[1]) {
5505 /* Should not swallow release of other keys */
5506 send = 1;
5507 }
5508 } else if (key == db->key[0] && !db->key_released[0]) {
5509 /* Do not check bindings for the first press of the binding
5510 * key. This allows it to be used as a debug shortcut.
5511 * We still need to swallow this event. */
5512 check_binding = 0;
5513 } else if (db->key[1]) {
5514 /* If we already ran a binding don't process another one since
5515 * we can't keep track of all the binding keys that were
5516 * pressed in order to swallow the release events. */
5517 send = 1;
5518 check_binding = 0;
5519 }
5520
5521 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005522 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5523 key, state)) {
5524 /* We ran a binding so swallow the press and keep the
5525 * grab to swallow the released too. */
5526 send = 0;
5527 terminate = 0;
5528 db->key[1] = key;
5529 } else {
5530 /* Terminate the grab since the key pressed is not a
5531 * debug binding key. */
5532 send = 1;
5533 terminate = 1;
5534 }
5535 }
5536
5537 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005538 serial = wl_display_next_serial(display);
5539 resource_list = &grab->keyboard->focus_resource_list;
5540 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005541 wl_keyboard_send_key(resource, serial, time, key, state);
5542 }
5543 }
5544
5545 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005546 weston_keyboard_end_grab(grab->keyboard);
5547 if (grab->keyboard->input_method_resource)
5548 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005549 free(db);
5550 }
5551}
5552
5553static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005554debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005555 uint32_t mods_depressed, uint32_t mods_latched,
5556 uint32_t mods_locked, uint32_t group)
5557{
5558 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005559 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005560
Neil Roberts96d790e2013-09-19 17:32:00 +01005561 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005562
Neil Roberts96d790e2013-09-19 17:32:00 +01005563 wl_resource_for_each(resource, resource_list) {
5564 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5565 mods_latched, mods_locked, group);
5566 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005567}
5568
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005569static void
5570debug_binding_cancel(struct weston_keyboard_grab *grab)
5571{
5572 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5573
5574 weston_keyboard_end_grab(grab->keyboard);
5575 free(db);
5576}
5577
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005578struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005579 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005580 debug_binding_modifiers,
5581 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005582};
5583
5584static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005585debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005586{
5587 struct debug_binding_grab *grab;
5588
5589 grab = calloc(1, sizeof *grab);
5590 if (!grab)
5591 return;
5592
5593 grab->seat = (struct weston_seat *) seat;
5594 grab->key[0] = key;
5595 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005596 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005597}
5598
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005599static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005600force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005601 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005602{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005603 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005604 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005605 struct desktop_shell *shell = data;
5606 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005607 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005608
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005609 focus_surface = seat->keyboard->focus;
5610 if (!focus_surface)
5611 return;
5612
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005613 wl_signal_emit(&compositor->kill_signal, focus_surface);
5614
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005615 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005616 wl_client_get_credentials(client, &pid, NULL, NULL);
5617
5618 /* Skip clients that we launched ourselves (the credentials of
5619 * the socketpair is ours) */
5620 if (pid == getpid())
5621 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005622
Daniel Stone325fc2d2012-05-30 16:31:58 +01005623 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005624}
5625
5626static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005627workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005628 uint32_t key, void *data)
5629{
5630 struct desktop_shell *shell = data;
5631 unsigned int new_index = shell->workspaces.current;
5632
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005633 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005634 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005635 if (new_index != 0)
5636 new_index--;
5637
5638 change_workspace(shell, new_index);
5639}
5640
5641static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005642workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005643 uint32_t key, void *data)
5644{
5645 struct desktop_shell *shell = data;
5646 unsigned int new_index = shell->workspaces.current;
5647
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005648 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005649 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005650 if (new_index < shell->workspaces.num - 1)
5651 new_index++;
5652
5653 change_workspace(shell, new_index);
5654}
5655
5656static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005657workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005658 uint32_t key, void *data)
5659{
5660 struct desktop_shell *shell = data;
5661 unsigned int new_index;
5662
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005663 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005664 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005665 new_index = key - KEY_F1;
5666 if (new_index >= shell->workspaces.num)
5667 new_index = shell->workspaces.num - 1;
5668
5669 change_workspace(shell, new_index);
5670}
5671
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005672static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005673workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005674 uint32_t key, void *data)
5675{
5676 struct desktop_shell *shell = data;
5677 unsigned int new_index = shell->workspaces.current;
5678
5679 if (shell->locked)
5680 return;
5681
5682 if (new_index != 0)
5683 new_index--;
5684
5685 take_surface_to_workspace_by_seat(shell, seat, new_index);
5686}
5687
5688static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005689workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005690 uint32_t key, void *data)
5691{
5692 struct desktop_shell *shell = data;
5693 unsigned int new_index = shell->workspaces.current;
5694
5695 if (shell->locked)
5696 return;
5697
5698 if (new_index < shell->workspaces.num - 1)
5699 new_index++;
5700
5701 take_surface_to_workspace_by_seat(shell, seat, new_index);
5702}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005703
5704static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005705shell_reposition_view_on_output_destroy(struct weston_view *view)
5706{
5707 struct weston_output *output, *first_output;
5708 struct weston_compositor *ec = view->surface->compositor;
5709 struct shell_surface *shsurf;
5710 float x, y;
5711 int visible;
5712
5713 x = view->geometry.x;
5714 y = view->geometry.y;
5715
5716 /* At this point the destroyed output is not in the list anymore.
5717 * If the view is still visible somewhere, we leave where it is,
5718 * otherwise, move it to the first output. */
5719 visible = 0;
5720 wl_list_for_each(output, &ec->output_list, link) {
5721 if (pixman_region32_contains_point(&output->region,
5722 x, y, NULL)) {
5723 visible = 1;
5724 break;
5725 }
5726 }
5727
5728 if (!visible) {
5729 first_output = container_of(ec->output_list.next,
5730 struct weston_output, link);
5731
5732 x = first_output->x + first_output->width / 4;
5733 y = first_output->y + first_output->height / 4;
5734 }
5735
5736 weston_view_set_position(view, x, y);
5737
5738 shsurf = get_shell_surface(view->surface);
5739
5740 if (shsurf) {
5741 shsurf->saved_position_valid = false;
5742 shsurf->next_state.maximized = false;
5743 shsurf->next_state.fullscreen = false;
5744 shsurf->state_changed = true;
5745 }
5746}
5747
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005748void
5749shell_for_each_layer(struct desktop_shell *shell,
5750 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005751{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005752 struct workspace **ws;
5753
5754 func(shell, &shell->fullscreen_layer, data);
5755 func(shell, &shell->panel_layer, data);
5756 func(shell, &shell->background_layer, data);
5757 func(shell, &shell->lock_layer, data);
5758 func(shell, &shell->input_panel_layer, data);
5759
5760 wl_array_for_each(ws, &shell->workspaces.array)
5761 func(shell, &(*ws)->layer, data);
5762}
5763
5764static void
5765shell_output_destroy_move_layer(struct desktop_shell *shell,
5766 struct weston_layer *layer,
5767 void *data)
5768{
5769 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005770 struct weston_view *view;
5771
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005772 wl_list_for_each(view, &layer->view_list, layer_link) {
5773 if (view->output != output)
5774 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005775
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005776 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005777 }
5778}
5779
5780static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005781handle_output_destroy(struct wl_listener *listener, void *data)
5782{
5783 struct shell_output *output_listener =
5784 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005785 struct weston_output *output = output_listener->output;
5786 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005787
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005788 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005789
Xiong Zhang6b481422013-10-23 13:58:32 +08005790 wl_list_remove(&output_listener->destroy_listener.link);
5791 wl_list_remove(&output_listener->link);
5792 free(output_listener);
5793}
5794
5795static void
5796create_shell_output(struct desktop_shell *shell,
5797 struct weston_output *output)
5798{
5799 struct shell_output *shell_output;
5800
5801 shell_output = zalloc(sizeof *shell_output);
5802 if (shell_output == NULL)
5803 return;
5804
5805 shell_output->output = output;
5806 shell_output->shell = shell;
5807 shell_output->destroy_listener.notify = handle_output_destroy;
5808 wl_signal_add(&output->destroy_signal,
5809 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005810 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005811}
5812
5813static void
5814handle_output_create(struct wl_listener *listener, void *data)
5815{
5816 struct desktop_shell *shell =
5817 container_of(listener, struct desktop_shell, output_create_listener);
5818 struct weston_output *output = (struct weston_output *)data;
5819
5820 create_shell_output(shell, output);
5821}
5822
5823static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005824handle_output_move_layer(struct desktop_shell *shell,
5825 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005826{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005827 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005828 struct weston_view *view;
5829 float x, y;
5830
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005831 wl_list_for_each(view, &layer->view_list, layer_link) {
5832 if (view->output != output)
5833 continue;
5834
5835 x = view->geometry.x + output->move_x;
5836 y = view->geometry.y + output->move_y;
5837 weston_view_set_position(view, x, y);
5838 }
5839}
5840
5841static void
5842handle_output_move(struct wl_listener *listener, void *data)
5843{
5844 struct desktop_shell *shell;
5845
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005846 shell = container_of(listener, struct desktop_shell,
5847 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005848
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005849 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005850}
5851
5852static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005853setup_output_destroy_handler(struct weston_compositor *ec,
5854 struct desktop_shell *shell)
5855{
5856 struct weston_output *output;
5857
5858 wl_list_init(&shell->output_list);
5859 wl_list_for_each(output, &ec->output_list, link)
5860 create_shell_output(shell, output);
5861
5862 shell->output_create_listener.notify = handle_output_create;
5863 wl_signal_add(&ec->output_created_signal,
5864 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005865
5866 shell->output_move_listener.notify = handle_output_move;
5867 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005868}
5869
5870static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005871shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005872{
Tiago Vignattibe143262012-04-16 17:31:41 +03005873 struct desktop_shell *shell =
5874 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005875 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005876 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005877
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005878 /* Force state to unlocked so we don't try to fade */
5879 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005880 if (shell->child.client)
5881 wl_client_destroy(shell->child.client);
5882
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005883 wl_list_remove(&shell->idle_listener.link);
5884 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005885
5886 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005887
Xiong Zhang6b481422013-10-23 13:58:32 +08005888 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5889 wl_list_remove(&shell_output->destroy_listener.link);
5890 wl_list_remove(&shell_output->link);
5891 free(shell_output);
5892 }
5893
5894 wl_list_remove(&shell->output_create_listener.link);
5895
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005896 wl_array_for_each(ws, &shell->workspaces.array)
5897 workspace_destroy(*ws);
5898 wl_array_release(&shell->workspaces.array);
5899
Pekka Paalanen3c647232011-12-22 13:43:43 +02005900 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005901 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005902 free(shell);
5903}
5904
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005905static void
5906shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5907{
5908 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005909 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005910
5911 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005912 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5913 MODIFIER_CTRL | MODIFIER_ALT,
5914 terminate_binding, ec);
5915 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5916 click_to_activate_binding,
5917 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005918 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5919 click_to_activate_binding,
5920 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005921 weston_compositor_add_touch_binding(ec, 0,
5922 touch_to_activate_binding,
5923 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005924 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5925 MODIFIER_SUPER | MODIFIER_ALT,
5926 surface_opacity_binding, NULL);
5927 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5928 MODIFIER_SUPER, zoom_axis_binding,
5929 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005930
5931 /* configurable bindings */
5932 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005933 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5934 zoom_key_binding, NULL);
5935 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5936 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005937 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5938 maximize_binding, NULL);
5939 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5940 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005941 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5942 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005943 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005944 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5945 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005946 weston_compositor_add_button_binding(ec, BTN_LEFT,
5947 mod | MODIFIER_SHIFT,
5948 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005949
5950 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5951 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5952 rotate_binding, NULL);
5953
Daniel Stone325fc2d2012-05-30 16:31:58 +01005954 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5955 shell);
5956 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5957 ec);
5958 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5959 backlight_binding, ec);
5960 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5961 ec);
5962 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5963 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005964 weston_compositor_add_key_binding(ec, KEY_K, mod,
5965 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005966 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5967 workspace_up_binding, shell);
5968 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5969 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005970 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5971 workspace_move_surface_up_binding,
5972 shell);
5973 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5974 workspace_move_surface_down_binding,
5975 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005976
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005977 if (shell->exposay_modifier)
5978 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5979 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005980
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005981 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5982 if (shell->workspaces.num > 1) {
5983 num_workspace_bindings = shell->workspaces.num;
5984 if (num_workspace_bindings > 6)
5985 num_workspace_bindings = 6;
5986 for (i = 0; i < num_workspace_bindings; i++)
5987 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5988 workspace_f_binding,
5989 shell);
5990 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005991
5992 /* Debug bindings */
5993 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5994 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005995}
5996
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005997WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005998module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005999 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006000{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006001 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006002 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006003 struct workspace **pws;
6004 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006005 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006006
Peter Huttererf3d62272013-08-08 11:57:05 +10006007 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006008 if (shell == NULL)
6009 return -1;
6010
Kristian Høgsberg75840622011-09-06 13:48:16 -04006011 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006012
6013 shell->destroy_listener.notify = shell_destroy;
6014 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006015 shell->idle_listener.notify = idle_handler;
6016 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6017 shell->wake_listener.notify = wake_handler;
6018 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006019
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006020 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006021 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006022 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006023 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006024 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006025 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006026 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006027 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006028 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006029 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006030
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006031 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6032 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006033 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6034 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006035 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006036
6037 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006038 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006039
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006040 if (input_panel_setup(shell) < 0)
6041 return -1;
6042
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006043 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006044
Daniel Stonedf8133b2013-11-19 11:37:14 +01006045 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6046 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6047
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006048 for (i = 0; i < shell->workspaces.num; i++) {
6049 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6050 if (pws == NULL)
6051 return -1;
6052
6053 *pws = workspace_create();
6054 if (*pws == NULL)
6055 return -1;
6056 }
6057 activate_workspace(shell, 0);
6058
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006059 weston_layer_init(&shell->minimized_layer, NULL);
6060
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006061 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006062 wl_list_init(&shell->workspaces.animation.link);
6063 shell->workspaces.animation.frame = animate_workspace_change_frame;
6064
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006065 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006066 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006067 return -1;
6068
Rafael Antognollie2a34552013-12-03 15:35:45 -02006069 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6070 shell, bind_xdg_shell) == NULL)
6071 return -1;
6072
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006073 if (wl_global_create(ec->wl_display,
6074 &desktop_shell_interface, 2,
6075 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006076 return -1;
6077
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006078 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6079 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006080 return -1;
6081
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006082 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6083 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006084 return -1;
6085
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006086 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006087
Xiong Zhang6b481422013-10-23 13:58:32 +08006088 setup_output_destroy_handler(ec, shell);
6089
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006090 loop = wl_display_get_event_loop(ec->wl_display);
6091 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006092
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006093 shell->screensaver.timer =
6094 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6095
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006096 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006097 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006098 create_keyboard_focus_listener(seat);
6099 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006100
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006101 screenshooter_create(ec);
6102
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006103 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006104
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006105 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006106
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006107 return 0;
6108}