blob: e4c02959d0165b04d8e6b3e01b5f5eecab9ef440 [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
Pekka Paalanen01388e22013-04-25 13:57:44 +03004423 main_surface = weston_surface_get_main_surface(es);
4424
Daniel Stone37816df2012-05-16 18:45:18 +01004425 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004426
Jonas Ådahl8538b222012-08-29 22:13:03 +02004427 state = ensure_focus_state(shell, seat);
4428 if (state == NULL)
4429 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004430
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004431 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004432 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004433
Rafael Antognolli03b16592013-12-03 15:35:42 -02004434 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004435 assert(shsurf);
4436
Rafael Antognolli03b16592013-12-03 15:35:42 -02004437 if (shsurf->state.fullscreen)
4438 shell_configure_fullscreen(shsurf);
4439 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004440 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004441
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004442 /* Update the surface’s layer. This brings it to the top of the stacking
4443 * order as appropriate. */
4444 shell_surface_update_layer(shsurf);
4445
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004446 if (shell->focus_animation_type != ANIMATION_NONE) {
4447 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004448 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004449 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004450}
4451
Alex Wu21858432012-04-01 20:13:08 +08004452/* no-op func for checking black surface */
4453static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004454black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004455{
4456}
4457
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004458static bool
Alex Wu21858432012-04-01 20:13:08 +08004459is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4460{
4461 if (es->configure == black_surface_configure) {
4462 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004463 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004464 return true;
4465 }
4466 return false;
4467}
4468
Kristian Høgsberg75840622011-09-06 13:48:16 -04004469static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004470activate_binding(struct weston_seat *seat,
4471 struct desktop_shell *shell,
4472 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004473{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004474 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004475
Alex Wu9c35e6b2012-03-05 14:13:13 +08004476 if (!focus)
4477 return;
4478
Pekka Paalanen01388e22013-04-25 13:57:44 +03004479 if (is_black_surface(focus, &main_surface))
4480 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004481
Pekka Paalanen01388e22013-04-25 13:57:44 +03004482 main_surface = weston_surface_get_main_surface(focus);
4483 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004484 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004485
Neil Robertsa28c6932013-10-03 16:43:04 +01004486 activate(shell, focus, seat);
4487}
4488
4489static void
4490click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4491 void *data)
4492{
4493 if (seat->pointer->grab != &seat->pointer->default_grab)
4494 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004495 if (seat->pointer->focus == NULL)
4496 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004497
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004498 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004499}
4500
4501static void
4502touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4503{
4504 if (seat->touch->grab != &seat->touch->default_grab)
4505 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004506 if (seat->touch->focus == NULL)
4507 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004508
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004509 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004510}
4511
4512static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004513unfocus_all_seats(struct desktop_shell *shell)
4514{
4515 struct weston_seat *seat, *next;
4516
4517 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4518 if (seat->keyboard == NULL)
4519 continue;
4520
4521 weston_keyboard_set_focus(seat->keyboard, NULL);
4522 }
4523}
4524
4525static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004526lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004527{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004528 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004529
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004530 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004531 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004532 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004533 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004534
4535 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004536
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004537 /* Hide all surfaces by removing the fullscreen, panel and
4538 * toplevel layers. This way nothing else can show or receive
4539 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004540
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004541 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004542 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004543 if (shell->showing_input_panels)
4544 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004545 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004546 wl_list_insert(&shell->compositor->cursor_layer.link,
4547 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004548
Pekka Paalanen77346a62011-11-30 16:26:35 +02004549 launch_screensaver(shell);
4550
Neil Robertsb4a91702014-04-09 16:33:32 +01004551 /* Remove the keyboard focus on all seats. This will be
4552 * restored to the workspace's saved state via
4553 * restore_focus_state when the compositor is unlocked */
4554 unfocus_all_seats(shell);
4555
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004556 /* TODO: disable bindings that should not work while locked. */
4557
4558 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004559}
4560
4561static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004562unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004563{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004564 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004565 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004566 return;
4567 }
4568
4569 /* If desktop-shell client has gone away, unlock immediately. */
4570 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004571 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004572 return;
4573 }
4574
4575 if (shell->prepare_event_sent)
4576 return;
4577
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004578 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004579 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004580}
4581
4582static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004583shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004584{
4585 struct desktop_shell *shell = data;
4586
4587 shell->fade.animation = NULL;
4588
4589 switch (shell->fade.type) {
4590 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004591 weston_surface_destroy(shell->fade.view->surface);
4592 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004593 break;
4594 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004595 lock(shell);
4596 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004597 default:
4598 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004599 }
4600}
4601
Jason Ekstranda7af7042013-10-12 22:38:11 -05004602static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004603shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004604{
4605 struct weston_compositor *compositor = shell->compositor;
4606 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004607 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004608
4609 surface = weston_surface_create(compositor);
4610 if (!surface)
4611 return NULL;
4612
Jason Ekstranda7af7042013-10-12 22:38:11 -05004613 view = weston_view_create(surface);
4614 if (!view) {
4615 weston_surface_destroy(surface);
4616 return NULL;
4617 }
4618
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004619 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004620 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004621 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004622 wl_list_insert(&compositor->fade_layer.view_list,
4623 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004624 pixman_region32_init(&surface->input);
4625
Jason Ekstranda7af7042013-10-12 22:38:11 -05004626 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004627}
4628
4629static void
4630shell_fade(struct desktop_shell *shell, enum fade_type type)
4631{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004632 float tint;
4633
4634 switch (type) {
4635 case FADE_IN:
4636 tint = 0.0;
4637 break;
4638 case FADE_OUT:
4639 tint = 1.0;
4640 break;
4641 default:
4642 weston_log("shell: invalid fade type\n");
4643 return;
4644 }
4645
4646 shell->fade.type = type;
4647
Jason Ekstranda7af7042013-10-12 22:38:11 -05004648 if (shell->fade.view == NULL) {
4649 shell->fade.view = shell_fade_create_surface(shell);
4650 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004651 return;
4652
Jason Ekstranda7af7042013-10-12 22:38:11 -05004653 shell->fade.view->alpha = 1.0 - tint;
4654 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004655 }
4656
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004657 if (shell->fade.view->output == NULL) {
4658 /* If the black view gets a NULL output, we lost the
4659 * last output and we'll just cancel the fade. This
4660 * happens when you close the last window under the
4661 * X11 or Wayland backends. */
4662 shell->locked = false;
4663 weston_surface_destroy(shell->fade.view->surface);
4664 shell->fade.view = NULL;
4665 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004666 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004667 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004668 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004669 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004670 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004671 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004672 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004673}
4674
4675static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004676do_shell_fade_startup(void *data)
4677{
4678 struct desktop_shell *shell = data;
4679
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004680 if (shell->startup_animation_type == ANIMATION_FADE)
4681 shell_fade(shell, FADE_IN);
4682 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004683 weston_surface_destroy(shell->fade.view->surface);
4684 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004685 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004686}
4687
4688static void
4689shell_fade_startup(struct desktop_shell *shell)
4690{
4691 struct wl_event_loop *loop;
4692
4693 if (!shell->fade.startup_timer)
4694 return;
4695
4696 wl_event_source_remove(shell->fade.startup_timer);
4697 shell->fade.startup_timer = NULL;
4698
4699 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4700 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4701}
4702
4703static int
4704fade_startup_timeout(void *data)
4705{
4706 struct desktop_shell *shell = data;
4707
4708 shell_fade_startup(shell);
4709 return 0;
4710}
4711
4712static void
4713shell_fade_init(struct desktop_shell *shell)
4714{
4715 /* Make compositor output all black, and wait for the desktop-shell
4716 * client to signal it is ready, then fade in. The timer triggers a
4717 * fade-in, in case the desktop-shell client takes too long.
4718 */
4719
4720 struct wl_event_loop *loop;
4721
Jason Ekstranda7af7042013-10-12 22:38:11 -05004722 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004723 weston_log("%s: warning: fade surface already exists\n",
4724 __func__);
4725 return;
4726 }
4727
Jason Ekstranda7af7042013-10-12 22:38:11 -05004728 shell->fade.view = shell_fade_create_surface(shell);
4729 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004730 return;
4731
Jason Ekstranda7af7042013-10-12 22:38:11 -05004732 weston_view_update_transform(shell->fade.view);
4733 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004734
4735 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4736 shell->fade.startup_timer =
4737 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4738 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4739}
4740
4741static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004742idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004743{
4744 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004745 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004746 struct weston_seat *seat;
4747
4748 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4749 if (seat->pointer)
4750 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004751
4752 shell_fade(shell, FADE_OUT);
4753 /* lock() is called from shell_fade_done() */
4754}
4755
4756static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004757wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004758{
4759 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004760 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004761
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004762 unlock(shell);
4763}
4764
4765static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004767{
Giulio Camuffob8366642013-04-25 13:57:46 +03004768 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004769 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004770
Jason Ekstranda7af7042013-10-12 22:38:11 -05004771 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004772
4773 x = output->x + (output->width - width) / 2 - surf_x / 2;
4774 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004775
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004776 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004777}
4778
4779static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004780weston_view_set_initial_position(struct weston_view *view,
4781 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004782{
4783 struct weston_compositor *compositor = shell->compositor;
4784 int ix = 0, iy = 0;
4785 int range_x, range_y;
4786 int dx, dy, x, y, panel_height;
4787 struct weston_output *output, *target_output = NULL;
4788 struct weston_seat *seat;
4789
4790 /* As a heuristic place the new window on the same output as the
4791 * pointer. Falling back to the output containing 0, 0.
4792 *
4793 * TODO: Do something clever for touch too?
4794 */
4795 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004796 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004797 ix = wl_fixed_to_int(seat->pointer->x);
4798 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004799 break;
4800 }
4801 }
4802
4803 wl_list_for_each(output, &compositor->output_list, link) {
4804 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4805 target_output = output;
4806 break;
4807 }
4808 }
4809
4810 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004811 weston_view_set_position(view, 10 + random() % 400,
4812 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004813 return;
4814 }
4815
4816 /* Valid range within output where the surface will still be onscreen.
4817 * If this is negative it means that the surface is bigger than
4818 * output.
4819 */
4820 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004821 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004822 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004823 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004824
Rob Bradford4cb88c72012-08-13 15:18:44 +01004825 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004826 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004827 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004828 dx = 0;
4829
4830 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004831 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004832 else
4833 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004834
4835 x = target_output->x + dx;
4836 y = target_output->y + dy;
4837
Jason Ekstranda7af7042013-10-12 22:38:11 -05004838 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004839}
4840
4841static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004842map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004843 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004844{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004845 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004846 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004847 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004848 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004849
Pekka Paalanen77346a62011-11-30 16:26:35 +02004850 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004851 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004852 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004853 if (shsurf->state.fullscreen) {
4854 center_on_output(shsurf->view, shsurf->fullscreen_output);
4855 shell_map_fullscreen(shsurf);
4856 } else if (shsurf->state.maximized) {
4857 /* use surface configure to set the geometry */
4858 panel_height = get_output_panel_height(shell, shsurf->output);
4859 surface_subsurfaces_boundingbox(shsurf->surface,
4860 &surf_x, &surf_y, NULL, NULL);
4861 weston_view_set_position(shsurf->view,
4862 shsurf->output->x - surf_x,
4863 shsurf->output->y +
4864 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004865 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004866 weston_view_set_initial_position(shsurf->view, shell);
4867 }
Juan Zhao96879df2012-02-07 08:45:41 +08004868 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004869 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004870 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004871 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004872 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004873 weston_view_set_position(shsurf->view,
4874 shsurf->view->geometry.x + sx,
4875 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004876 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004877 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004878 default:
4879 ;
4880 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004881
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004882 /* Surface stacking order, see also activate(). */
4883 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004884
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 if (shsurf->type != SHELL_SURFACE_NONE) {
4886 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004887 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004888 shsurf->surface->output = shsurf->output;
4889 shsurf->view->output = shsurf->output;
4890 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004891 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004892
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004894 /* XXX: xwayland's using the same fields for transient type */
4895 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004896 if (shsurf->transient.flags ==
4897 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4898 break;
4899 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004900 if (shsurf->state.relative &&
4901 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4902 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004903 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004904 break;
4905 wl_list_for_each(seat, &compositor->seat_list, link)
4906 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004907 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004908 case SHELL_SURFACE_POPUP:
4909 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004910 default:
4911 break;
4912 }
4913
Rafael Antognolli03b16592013-12-03 15:35:42 -02004914 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4915 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004916 {
4917 switch (shell->win_animation_type) {
4918 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004919 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004920 break;
4921 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004922 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004923 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004924 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004925 default:
4926 break;
4927 }
4928 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004929}
4930
4931static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004932configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004933 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004934{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004935 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004936 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004937 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004938
Pekka Paalanen77346a62011-11-30 16:26:35 +02004939 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004940
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004941 assert(shsurf);
4942
Rafael Antognolli03b16592013-12-03 15:35:42 -02004943 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004944 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004945 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004946 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004947 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4948 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004949 mx = shsurf->output->x - surf_x;
4950 my = shsurf->output->y +
4951 get_output_panel_height(shell,shsurf->output) - surf_y;
4952 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004953 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004954 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004955 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004956
Alex Wu4539b082012-03-01 12:57:46 +08004957 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004958 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004959 wl_list_for_each(view, &surface->views, surface_link)
4960 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004961
Rafael Antognolli03b16592013-12-03 15:35:42 -02004962 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004963 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004964 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004965}
4966
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004967static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004968shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004969{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004970 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004971 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004972 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004973
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004974 assert(shsurf);
4975
4976 shell = shsurf->shell;
4977
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004978 if (!weston_surface_is_mapped(es) &&
4979 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004980 remove_popup_grab(shsurf);
4981 }
4982
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004983 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004984 return;
4985
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004986 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004987 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004988 type_changed = 1;
4989 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004990
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004991 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004992 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004993 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004994 shsurf->last_width != es->width ||
4995 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004996 float from_x, from_y;
4997 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004998
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004999 if (shsurf->resize_edges) {
5000 sx = 0;
5001 sy = 0;
5002 }
5003
5004 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5005 sx = shsurf->last_width - es->width;
5006 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5007 sy = shsurf->last_height - es->height;
5008
5009 shsurf->last_width = es->width;
5010 shsurf->last_height = es->height;
5011
Jason Ekstranda7af7042013-10-12 22:38:11 -05005012 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5013 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005014 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005015 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005016 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005017 }
5018}
5019
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005020static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005021
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005022static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005023desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005024{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005025 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005026 struct desktop_shell *shell =
5027 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005028
5029 shell->child.process.pid = 0;
5030 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005031
5032 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5033 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005034 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005035 shell->child.deathstamp = time;
5036 shell->child.deathcount = 0;
5037 }
5038
5039 shell->child.deathcount++;
5040 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005041 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005042 return;
5043 }
5044
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005045 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005046 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005047 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005048}
5049
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005050static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005051desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5052{
5053 struct desktop_shell *shell;
5054
5055 shell = container_of(listener, struct desktop_shell,
5056 child.client_destroy_listener);
5057
5058 shell->child.client = NULL;
5059}
5060
5061static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005062launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005063{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005064 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005065
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005066 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005067 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005068 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005069 desktop_shell_sigchld);
5070
5071 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005072 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005073
5074 shell->child.client_destroy_listener.notify =
5075 desktop_shell_client_destroy;
5076 wl_client_add_destroy_listener(shell->child.client,
5077 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005078}
5079
5080static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005081handle_shell_client_destroy(struct wl_listener *listener, void *data)
5082{
5083 struct shell_client *sc =
5084 container_of(listener, struct shell_client, destroy_listener);
5085
5086 if (sc->ping_timer)
5087 wl_event_source_remove(sc->ping_timer);
5088 free(sc);
5089}
5090
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005091static struct shell_client *
5092shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5093 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005094{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005095 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005096
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005097 sc = zalloc(sizeof *sc);
5098 if (sc == NULL) {
5099 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005100 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005101 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005102
5103 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005104 if (sc->resource == NULL) {
5105 free(sc);
5106 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005107 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005108 }
5109
5110 sc->client = client;
5111 sc->shell = shell;
5112 sc->destroy_listener.notify = handle_shell_client_destroy;
5113 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5114
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005115 return sc;
5116}
5117
5118static void
5119bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5120{
5121 struct desktop_shell *shell = data;
5122 struct shell_client *sc;
5123
5124 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5125 if (sc)
5126 wl_resource_set_implementation(sc->resource,
5127 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005128 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005129}
5130
5131static void
5132bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5133{
5134 struct desktop_shell *shell = data;
5135 struct shell_client *sc;
5136
5137 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5138 if (sc)
5139 wl_resource_set_dispatcher(sc->resource,
5140 xdg_shell_unversioned_dispatch,
5141 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005142}
5143
5144static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005145unbind_desktop_shell(struct wl_resource *resource)
5146{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005147 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005148
5149 if (shell->locked)
5150 resume_desktop(shell);
5151
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005152 shell->child.desktop_shell = NULL;
5153 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005154}
5155
5156static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005157bind_desktop_shell(struct wl_client *client,
5158 void *data, uint32_t version, uint32_t id)
5159{
Tiago Vignattibe143262012-04-16 17:31:41 +03005160 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005161 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005162
Jason Ekstranda85118c2013-06-27 20:17:02 -05005163 resource = wl_resource_create(client, &desktop_shell_interface,
5164 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005165
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005166 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005167 wl_resource_set_implementation(resource,
5168 &desktop_shell_implementation,
5169 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005170 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005171
5172 if (version < 2)
5173 shell_fade_startup(shell);
5174
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005175 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005176 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005177
5178 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5179 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005180 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005181}
5182
Pekka Paalanen6e168112011-11-24 11:34:05 +02005183static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005184screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005185{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005186 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005187 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005188
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005189 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005190 return;
5191
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005192 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005193 if (!shell->locked)
5194 return;
5195
Jason Ekstranda7af7042013-10-12 22:38:11 -05005196 view = container_of(surface->views.next, struct weston_view, surface_link);
5197 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005198
Jason Ekstranda7af7042013-10-12 22:38:11 -05005199 if (wl_list_empty(&view->layer_link)) {
5200 wl_list_insert(shell->lock_layer.view_list.prev,
5201 &view->layer_link);
5202 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005203 wl_event_source_timer_update(shell->screensaver.timer,
5204 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005205 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005206 }
5207}
5208
5209static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005210screensaver_set_surface(struct wl_client *client,
5211 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005212 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005213 struct wl_resource *output_resource)
5214{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005215 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005216 struct weston_surface *surface =
5217 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005218 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005219 struct weston_view *view, *next;
5220
5221 /* Make sure we only have one view */
5222 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5223 weston_view_destroy(view);
5224 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005225
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005226 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005227 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005228 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005229}
5230
5231static const struct screensaver_interface screensaver_implementation = {
5232 screensaver_set_surface
5233};
5234
5235static void
5236unbind_screensaver(struct wl_resource *resource)
5237{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005238 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005239
Pekka Paalanen77346a62011-11-30 16:26:35 +02005240 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005241}
5242
5243static void
5244bind_screensaver(struct wl_client *client,
5245 void *data, uint32_t version, uint32_t id)
5246{
Tiago Vignattibe143262012-04-16 17:31:41 +03005247 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005248 struct wl_resource *resource;
5249
Jason Ekstranda85118c2013-06-27 20:17:02 -05005250 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005251
Pekka Paalanen77346a62011-11-30 16:26:35 +02005252 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005253 wl_resource_set_implementation(resource,
5254 &screensaver_implementation,
5255 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005256 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005257 return;
5258 }
5259
5260 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5261 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005262 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005263}
5264
Kristian Høgsberg07045392012-02-19 18:52:44 -05005265struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005266 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005267 struct weston_surface *current;
5268 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005269 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005270 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005271};
5272
5273static void
5274switcher_next(struct switcher *switcher)
5275{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005276 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005278 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005279 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005280
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005281 /* temporary re-display minimized surfaces */
5282 struct weston_view *tmp;
5283 struct weston_view **minimized;
5284 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5285 wl_list_remove(&view->layer_link);
5286 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5287 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5288 *minimized = view;
5289 }
5290
Jason Ekstranda7af7042013-10-12 22:38:11 -05005291 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005292 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005293 if (shsurf &&
5294 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5295 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005296 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005297 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005298 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005299 next = view->surface;
5300 prev = view->surface;
5301 view->alpha = 0.25;
5302 weston_view_geometry_dirty(view);
5303 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005304 }
Alex Wu1659daa2012-04-01 20:13:09 +08005305
Jason Ekstranda7af7042013-10-12 22:38:11 -05005306 if (is_black_surface(view->surface, NULL)) {
5307 view->alpha = 0.25;
5308 weston_view_geometry_dirty(view);
5309 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005310 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005311 }
5312
5313 if (next == NULL)
5314 next = first;
5315
Alex Wu07b26062012-03-12 16:06:01 +08005316 if (next == NULL)
5317 return;
5318
Kristian Høgsberg07045392012-02-19 18:52:44 -05005319 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005320 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005321
5322 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005323 wl_list_for_each(view, &next->views, surface_link)
5324 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005325
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005326 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005327 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005328 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005329}
5330
5331static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005332switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005333{
5334 struct switcher *switcher =
5335 container_of(listener, struct switcher, listener);
5336
5337 switcher_next(switcher);
5338}
5339
5340static void
Daniel Stone351eb612012-05-31 15:27:47 -04005341switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005342{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005343 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005344 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005345 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005346
Jason Ekstranda7af7042013-10-12 22:38:11 -05005347 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005348 if (is_focus_view(view))
5349 continue;
5350
Jason Ekstranda7af7042013-10-12 22:38:11 -05005351 view->alpha = 1.0;
5352 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005353 }
5354
Alex Wu07b26062012-03-12 16:06:01 +08005355 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005356 activate(switcher->shell, switcher->current,
5357 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005358 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005359 weston_keyboard_end_grab(keyboard);
5360 if (keyboard->input_method_resource)
5361 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005362
5363 /* re-hide surfaces that were temporary shown during the switch */
5364 struct weston_view **minimized;
5365 wl_array_for_each(minimized, &switcher->minimized_array) {
5366 /* with the exception of the current selected */
5367 if ((*minimized)->surface != switcher->current) {
5368 wl_list_remove(&(*minimized)->layer_link);
5369 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5370 weston_view_damage_below(*minimized);
5371 }
5372 }
5373 wl_array_release(&switcher->minimized_array);
5374
Kristian Høgsberg07045392012-02-19 18:52:44 -05005375 free(switcher);
5376}
5377
5378static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005379switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005380 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005381{
5382 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005383 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005384
Daniel Stonec9785ea2012-05-30 16:31:52 +01005385 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005386 switcher_next(switcher);
5387}
5388
5389static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005390switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005391 uint32_t mods_depressed, uint32_t mods_latched,
5392 uint32_t mods_locked, uint32_t group)
5393{
5394 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005395 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005396
Daniel Stone351eb612012-05-31 15:27:47 -04005397 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5398 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005399}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005400
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005401static void
5402switcher_cancel(struct weston_keyboard_grab *grab)
5403{
5404 struct switcher *switcher = container_of(grab, struct switcher, grab);
5405
5406 switcher_destroy(switcher);
5407}
5408
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005409static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005410 switcher_key,
5411 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005412 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005413};
5414
5415static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005416switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005417 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005418{
Tiago Vignattibe143262012-04-16 17:31:41 +03005419 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005420 struct switcher *switcher;
5421
5422 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005423 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005424 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005425 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005426 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005427 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005428
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005429 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005430 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005431 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005432 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5433 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005434 switcher_next(switcher);
5435}
5436
Pekka Paalanen3c647232011-12-22 13:43:43 +02005437static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005438backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005439 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005440{
5441 struct weston_compositor *compositor = data;
5442 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005443 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005444
5445 /* TODO: we're limiting to simple use cases, where we assume just
5446 * control on the primary display. We'd have to extend later if we
5447 * ever get support for setting backlights on random desktop LCD
5448 * panels though */
5449 output = get_default_output(compositor);
5450 if (!output)
5451 return;
5452
5453 if (!output->set_backlight)
5454 return;
5455
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005456 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5457 backlight_new = output->backlight_current - 25;
5458 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5459 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005460
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005461 if (backlight_new < 5)
5462 backlight_new = 5;
5463 if (backlight_new > 255)
5464 backlight_new = 255;
5465
5466 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005467 output->set_backlight(output, output->backlight_current);
5468}
5469
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005470struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005471 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005472 struct weston_seat *seat;
5473 uint32_t key[2];
5474 int key_released[2];
5475};
5476
5477static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005478debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005479 uint32_t key, uint32_t state)
5480{
5481 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005482 struct weston_compositor *ec = db->seat->compositor;
5483 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005484 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005485 uint32_t serial;
5486 int send = 0, terminate = 0;
5487 int check_binding = 1;
5488 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005489 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005490
5491 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5492 /* Do not run bindings on key releases */
5493 check_binding = 0;
5494
5495 for (i = 0; i < 2; i++)
5496 if (key == db->key[i])
5497 db->key_released[i] = 1;
5498
5499 if (db->key_released[0] && db->key_released[1]) {
5500 /* All key releases been swalled so end the grab */
5501 terminate = 1;
5502 } else if (key != db->key[0] && key != db->key[1]) {
5503 /* Should not swallow release of other keys */
5504 send = 1;
5505 }
5506 } else if (key == db->key[0] && !db->key_released[0]) {
5507 /* Do not check bindings for the first press of the binding
5508 * key. This allows it to be used as a debug shortcut.
5509 * We still need to swallow this event. */
5510 check_binding = 0;
5511 } else if (db->key[1]) {
5512 /* If we already ran a binding don't process another one since
5513 * we can't keep track of all the binding keys that were
5514 * pressed in order to swallow the release events. */
5515 send = 1;
5516 check_binding = 0;
5517 }
5518
5519 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005520 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5521 key, state)) {
5522 /* We ran a binding so swallow the press and keep the
5523 * grab to swallow the released too. */
5524 send = 0;
5525 terminate = 0;
5526 db->key[1] = key;
5527 } else {
5528 /* Terminate the grab since the key pressed is not a
5529 * debug binding key. */
5530 send = 1;
5531 terminate = 1;
5532 }
5533 }
5534
5535 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005536 serial = wl_display_next_serial(display);
5537 resource_list = &grab->keyboard->focus_resource_list;
5538 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005539 wl_keyboard_send_key(resource, serial, time, key, state);
5540 }
5541 }
5542
5543 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005544 weston_keyboard_end_grab(grab->keyboard);
5545 if (grab->keyboard->input_method_resource)
5546 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005547 free(db);
5548 }
5549}
5550
5551static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005552debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005553 uint32_t mods_depressed, uint32_t mods_latched,
5554 uint32_t mods_locked, uint32_t group)
5555{
5556 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005557 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005558
Neil Roberts96d790e2013-09-19 17:32:00 +01005559 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005560
Neil Roberts96d790e2013-09-19 17:32:00 +01005561 wl_resource_for_each(resource, resource_list) {
5562 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5563 mods_latched, mods_locked, group);
5564 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005565}
5566
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005567static void
5568debug_binding_cancel(struct weston_keyboard_grab *grab)
5569{
5570 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5571
5572 weston_keyboard_end_grab(grab->keyboard);
5573 free(db);
5574}
5575
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005576struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005577 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005578 debug_binding_modifiers,
5579 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005580};
5581
5582static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005583debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005584{
5585 struct debug_binding_grab *grab;
5586
5587 grab = calloc(1, sizeof *grab);
5588 if (!grab)
5589 return;
5590
5591 grab->seat = (struct weston_seat *) seat;
5592 grab->key[0] = key;
5593 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005594 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005595}
5596
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005597static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005598force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005599 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005600{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005601 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005602 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005603 struct desktop_shell *shell = data;
5604 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005605 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005606
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005607 focus_surface = seat->keyboard->focus;
5608 if (!focus_surface)
5609 return;
5610
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005611 wl_signal_emit(&compositor->kill_signal, focus_surface);
5612
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005613 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005614 wl_client_get_credentials(client, &pid, NULL, NULL);
5615
5616 /* Skip clients that we launched ourselves (the credentials of
5617 * the socketpair is ours) */
5618 if (pid == getpid())
5619 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005620
Daniel Stone325fc2d2012-05-30 16:31:58 +01005621 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005622}
5623
5624static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005625workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005626 uint32_t key, void *data)
5627{
5628 struct desktop_shell *shell = data;
5629 unsigned int new_index = shell->workspaces.current;
5630
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005631 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005632 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005633 if (new_index != 0)
5634 new_index--;
5635
5636 change_workspace(shell, new_index);
5637}
5638
5639static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005640workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005641 uint32_t key, void *data)
5642{
5643 struct desktop_shell *shell = data;
5644 unsigned int new_index = shell->workspaces.current;
5645
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005646 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005647 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005648 if (new_index < shell->workspaces.num - 1)
5649 new_index++;
5650
5651 change_workspace(shell, new_index);
5652}
5653
5654static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005655workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005656 uint32_t key, void *data)
5657{
5658 struct desktop_shell *shell = data;
5659 unsigned int new_index;
5660
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005661 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005662 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005663 new_index = key - KEY_F1;
5664 if (new_index >= shell->workspaces.num)
5665 new_index = shell->workspaces.num - 1;
5666
5667 change_workspace(shell, new_index);
5668}
5669
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005670static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005671workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005672 uint32_t key, void *data)
5673{
5674 struct desktop_shell *shell = data;
5675 unsigned int new_index = shell->workspaces.current;
5676
5677 if (shell->locked)
5678 return;
5679
5680 if (new_index != 0)
5681 new_index--;
5682
5683 take_surface_to_workspace_by_seat(shell, seat, new_index);
5684}
5685
5686static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005687workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005688 uint32_t key, void *data)
5689{
5690 struct desktop_shell *shell = data;
5691 unsigned int new_index = shell->workspaces.current;
5692
5693 if (shell->locked)
5694 return;
5695
5696 if (new_index < shell->workspaces.num - 1)
5697 new_index++;
5698
5699 take_surface_to_workspace_by_seat(shell, seat, new_index);
5700}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005701
5702static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005703shell_reposition_view_on_output_destroy(struct weston_view *view)
5704{
5705 struct weston_output *output, *first_output;
5706 struct weston_compositor *ec = view->surface->compositor;
5707 struct shell_surface *shsurf;
5708 float x, y;
5709 int visible;
5710
5711 x = view->geometry.x;
5712 y = view->geometry.y;
5713
5714 /* At this point the destroyed output is not in the list anymore.
5715 * If the view is still visible somewhere, we leave where it is,
5716 * otherwise, move it to the first output. */
5717 visible = 0;
5718 wl_list_for_each(output, &ec->output_list, link) {
5719 if (pixman_region32_contains_point(&output->region,
5720 x, y, NULL)) {
5721 visible = 1;
5722 break;
5723 }
5724 }
5725
5726 if (!visible) {
5727 first_output = container_of(ec->output_list.next,
5728 struct weston_output, link);
5729
5730 x = first_output->x + first_output->width / 4;
5731 y = first_output->y + first_output->height / 4;
5732 }
5733
5734 weston_view_set_position(view, x, y);
5735
5736 shsurf = get_shell_surface(view->surface);
5737
5738 if (shsurf) {
5739 shsurf->saved_position_valid = false;
5740 shsurf->next_state.maximized = false;
5741 shsurf->next_state.fullscreen = false;
5742 shsurf->state_changed = true;
5743 }
5744}
5745
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005746void
5747shell_for_each_layer(struct desktop_shell *shell,
5748 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005749{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005750 struct workspace **ws;
5751
5752 func(shell, &shell->fullscreen_layer, data);
5753 func(shell, &shell->panel_layer, data);
5754 func(shell, &shell->background_layer, data);
5755 func(shell, &shell->lock_layer, data);
5756 func(shell, &shell->input_panel_layer, data);
5757
5758 wl_array_for_each(ws, &shell->workspaces.array)
5759 func(shell, &(*ws)->layer, data);
5760}
5761
5762static void
5763shell_output_destroy_move_layer(struct desktop_shell *shell,
5764 struct weston_layer *layer,
5765 void *data)
5766{
5767 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005768 struct weston_view *view;
5769
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005770 wl_list_for_each(view, &layer->view_list, layer_link) {
5771 if (view->output != output)
5772 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005773
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005774 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005775 }
5776}
5777
5778static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005779handle_output_destroy(struct wl_listener *listener, void *data)
5780{
5781 struct shell_output *output_listener =
5782 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005783 struct weston_output *output = output_listener->output;
5784 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005785
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005786 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005787
Xiong Zhang6b481422013-10-23 13:58:32 +08005788 wl_list_remove(&output_listener->destroy_listener.link);
5789 wl_list_remove(&output_listener->link);
5790 free(output_listener);
5791}
5792
5793static void
5794create_shell_output(struct desktop_shell *shell,
5795 struct weston_output *output)
5796{
5797 struct shell_output *shell_output;
5798
5799 shell_output = zalloc(sizeof *shell_output);
5800 if (shell_output == NULL)
5801 return;
5802
5803 shell_output->output = output;
5804 shell_output->shell = shell;
5805 shell_output->destroy_listener.notify = handle_output_destroy;
5806 wl_signal_add(&output->destroy_signal,
5807 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005808 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005809}
5810
5811static void
5812handle_output_create(struct wl_listener *listener, void *data)
5813{
5814 struct desktop_shell *shell =
5815 container_of(listener, struct desktop_shell, output_create_listener);
5816 struct weston_output *output = (struct weston_output *)data;
5817
5818 create_shell_output(shell, output);
5819}
5820
5821static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005822handle_output_move_layer(struct desktop_shell *shell,
5823 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005824{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005825 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005826 struct weston_view *view;
5827 float x, y;
5828
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005829 wl_list_for_each(view, &layer->view_list, layer_link) {
5830 if (view->output != output)
5831 continue;
5832
5833 x = view->geometry.x + output->move_x;
5834 y = view->geometry.y + output->move_y;
5835 weston_view_set_position(view, x, y);
5836 }
5837}
5838
5839static void
5840handle_output_move(struct wl_listener *listener, void *data)
5841{
5842 struct desktop_shell *shell;
5843
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005844 shell = container_of(listener, struct desktop_shell,
5845 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005846
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005847 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005848}
5849
5850static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005851setup_output_destroy_handler(struct weston_compositor *ec,
5852 struct desktop_shell *shell)
5853{
5854 struct weston_output *output;
5855
5856 wl_list_init(&shell->output_list);
5857 wl_list_for_each(output, &ec->output_list, link)
5858 create_shell_output(shell, output);
5859
5860 shell->output_create_listener.notify = handle_output_create;
5861 wl_signal_add(&ec->output_created_signal,
5862 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005863
5864 shell->output_move_listener.notify = handle_output_move;
5865 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005866}
5867
5868static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005869shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005870{
Tiago Vignattibe143262012-04-16 17:31:41 +03005871 struct desktop_shell *shell =
5872 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005873 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005874 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005875
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005876 /* Force state to unlocked so we don't try to fade */
5877 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005878 if (shell->child.client)
5879 wl_client_destroy(shell->child.client);
5880
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005881 wl_list_remove(&shell->idle_listener.link);
5882 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005883
5884 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005885
Xiong Zhang6b481422013-10-23 13:58:32 +08005886 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5887 wl_list_remove(&shell_output->destroy_listener.link);
5888 wl_list_remove(&shell_output->link);
5889 free(shell_output);
5890 }
5891
5892 wl_list_remove(&shell->output_create_listener.link);
5893
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005894 wl_array_for_each(ws, &shell->workspaces.array)
5895 workspace_destroy(*ws);
5896 wl_array_release(&shell->workspaces.array);
5897
Pekka Paalanen3c647232011-12-22 13:43:43 +02005898 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005899 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005900 free(shell);
5901}
5902
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005903static void
5904shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5905{
5906 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005907 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005908
5909 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005910 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5911 MODIFIER_CTRL | MODIFIER_ALT,
5912 terminate_binding, ec);
5913 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5914 click_to_activate_binding,
5915 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005916 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5917 click_to_activate_binding,
5918 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005919 weston_compositor_add_touch_binding(ec, 0,
5920 touch_to_activate_binding,
5921 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005922 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5923 MODIFIER_SUPER | MODIFIER_ALT,
5924 surface_opacity_binding, NULL);
5925 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5926 MODIFIER_SUPER, zoom_axis_binding,
5927 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005928
5929 /* configurable bindings */
5930 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005931 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5932 zoom_key_binding, NULL);
5933 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5934 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005935 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5936 maximize_binding, NULL);
5937 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5938 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005939 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5940 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005941 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005942 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5943 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005944 weston_compositor_add_button_binding(ec, BTN_LEFT,
5945 mod | MODIFIER_SHIFT,
5946 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005947
5948 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5949 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5950 rotate_binding, NULL);
5951
Daniel Stone325fc2d2012-05-30 16:31:58 +01005952 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5953 shell);
5954 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5955 ec);
5956 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5957 backlight_binding, ec);
5958 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5959 ec);
5960 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5961 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005962 weston_compositor_add_key_binding(ec, KEY_K, mod,
5963 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005964 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5965 workspace_up_binding, shell);
5966 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5967 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005968 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5969 workspace_move_surface_up_binding,
5970 shell);
5971 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5972 workspace_move_surface_down_binding,
5973 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005974
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005975 if (shell->exposay_modifier)
5976 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5977 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005978
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005979 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5980 if (shell->workspaces.num > 1) {
5981 num_workspace_bindings = shell->workspaces.num;
5982 if (num_workspace_bindings > 6)
5983 num_workspace_bindings = 6;
5984 for (i = 0; i < num_workspace_bindings; i++)
5985 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5986 workspace_f_binding,
5987 shell);
5988 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005989
5990 /* Debug bindings */
5991 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5992 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005993}
5994
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005995WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005996module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005997 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005998{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005999 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006000 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006001 struct workspace **pws;
6002 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006003 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006004
Peter Huttererf3d62272013-08-08 11:57:05 +10006005 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006006 if (shell == NULL)
6007 return -1;
6008
Kristian Høgsberg75840622011-09-06 13:48:16 -04006009 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006010
6011 shell->destroy_listener.notify = shell_destroy;
6012 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006013 shell->idle_listener.notify = idle_handler;
6014 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6015 shell->wake_listener.notify = wake_handler;
6016 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006017
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006018 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006019 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006020 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006021 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006022 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006023 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006024 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006025 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006026 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006027 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006028
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006029 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6030 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006031 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6032 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006033 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006034
6035 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006036 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006037
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006038 if (input_panel_setup(shell) < 0)
6039 return -1;
6040
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006041 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006042
Daniel Stonedf8133b2013-11-19 11:37:14 +01006043 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6044 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6045
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006046 for (i = 0; i < shell->workspaces.num; i++) {
6047 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6048 if (pws == NULL)
6049 return -1;
6050
6051 *pws = workspace_create();
6052 if (*pws == NULL)
6053 return -1;
6054 }
6055 activate_workspace(shell, 0);
6056
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006057 weston_layer_init(&shell->minimized_layer, NULL);
6058
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006059 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006060 wl_list_init(&shell->workspaces.animation.link);
6061 shell->workspaces.animation.frame = animate_workspace_change_frame;
6062
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006063 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006064 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006065 return -1;
6066
Rafael Antognollie2a34552013-12-03 15:35:45 -02006067 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6068 shell, bind_xdg_shell) == NULL)
6069 return -1;
6070
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006071 if (wl_global_create(ec->wl_display,
6072 &desktop_shell_interface, 2,
6073 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006074 return -1;
6075
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006076 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6077 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006078 return -1;
6079
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006080 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6081 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006082 return -1;
6083
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006084 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006085
Xiong Zhang6b481422013-10-23 13:58:32 +08006086 setup_output_destroy_handler(ec, shell);
6087
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006088 loop = wl_display_get_event_loop(ec->wl_display);
6089 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006090
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006091 shell->screensaver.timer =
6092 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6093
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006094 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006095 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006096 create_keyboard_focus_listener(seat);
6097 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006098
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006099 screenshooter_create(ec);
6100
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006101 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006102
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006103 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006104
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006105 return 0;
6106}