blob: 79bd65c1b6a84062d17b27fc637245ee9f5b230a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500112 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600113 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700115 struct wl_listener resource_destroy_listener;
116
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400117 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800122 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200125 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200160 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
165 struct {
166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500169 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200170 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500172
173 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200174};
175
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400177 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300178 struct shell_surface *shsurf;
179 struct wl_listener shsurf_destroy_listener;
180};
181
Rusty Lynch1084da52013-08-15 09:10:08 -0700182struct shell_touch_grab {
183 struct weston_touch_grab grab;
184 struct shell_surface *shsurf;
185 struct wl_listener shsurf_destroy_listener;
186 struct weston_touch *touch;
187};
188
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300189struct weston_move_grab {
190 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100191 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500192};
193
Rusty Lynch1084da52013-08-15 09:10:08 -0700194struct weston_touch_move_grab {
195 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800196 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700197 wl_fixed_t dx, dy;
198};
199
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300201 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500202 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200204 float x;
205 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206 } center;
207};
208
Giulio Camuffo5085a752013-03-25 21:42:45 +0100209struct shell_seat {
210 struct weston_seat *seat;
211 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500212 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100213
214 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400215 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100216 struct wl_list surfaces_list;
217 struct wl_client *client;
218 int32_t initial_up;
219 } popup_grab;
220};
221
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800222struct shell_client {
223 struct wl_resource *resource;
224 struct wl_client *client;
225 struct desktop_shell *shell;
226 struct wl_listener destroy_listener;
227 struct wl_event_source *ping_timer;
228 uint32_t ping_serial;
229 int unresponsive;
230};
231
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100232void
233set_alpha_if_fullscreen(struct shell_surface *shsurf)
234{
235 if (shsurf && shsurf->state.fullscreen)
236 shsurf->fullscreen.black_view->alpha = 0.25;
237}
238
Alex Wubd3354b2012-04-17 17:20:49 +0800239static struct desktop_shell *
240shell_surface_get_shell(struct shell_surface *shsurf);
241
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500242static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400243surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500244
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300245static void
246shell_fade_startup(struct desktop_shell *shell);
247
Philip Withnallbecb77e2013-11-25 18:01:30 +0000248static struct shell_seat *
249get_shell_seat(struct weston_seat *seat);
250
Philip Withnall648a4dd2013-11-25 18:01:44 +0000251static void
252shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
253
Alex Wubd3354b2012-04-17 17:20:49 +0800254static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200255shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
256
257static bool
258shell_surface_is_xdg_surface(struct shell_surface *shsurf);
259
260static bool
261shell_surface_is_xdg_popup(struct shell_surface *shsurf);
262
263static void
264shell_surface_set_parent(struct shell_surface *shsurf,
265 struct weston_surface *parent);
266
267static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800268shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
269{
270 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800272
273 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100274
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800276 return false;
277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
279 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800280 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800282}
283
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500284static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400285destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286{
287 struct shell_grab *grab;
288
289 grab = container_of(listener, struct shell_grab,
290 shsurf_destroy_listener);
291
292 grab->shsurf = NULL;
293}
294
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800295struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296get_default_view(struct weston_surface *surface)
297{
298 struct shell_surface *shsurf;
299 struct weston_view *view;
300
301 if (!surface || wl_list_empty(&surface->views))
302 return NULL;
303
304 shsurf = get_shell_surface(surface);
305 if (shsurf)
306 return shsurf->view;
307
308 wl_list_for_each(view, &surface->views, surface_link)
309 if (weston_view_is_mapped(view))
310 return view;
311
312 return container_of(surface->views.next, struct weston_view, surface_link);
313}
314
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400316popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400317
318static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300324{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct desktop_shell *shell = shsurf->shell;
326
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400327 popup_grab_end(pointer);
328
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500332 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400333 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300334
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700335 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400336 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400337 if (shell->child.desktop_shell) {
338 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
339 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 weston_pointer_set_focus(pointer,
341 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400342 wl_fixed_from_int(0),
343 wl_fixed_from_int(0));
344 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300345}
346
347static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300348shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700350 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400351 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700352 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800353 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700354 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300355
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700356 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357}
358
359static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700360shell_touch_grab_start(struct shell_touch_grab *grab,
361 const struct weston_touch_grab_interface *interface,
362 struct shell_surface *shsurf,
363 struct weston_touch *touch)
364{
365 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800366
Rusty Lynch1084da52013-08-15 09:10:08 -0700367 grab->grab.interface = interface;
368 grab->shsurf = shsurf;
369 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
370 wl_signal_add(&shsurf->destroy_signal,
371 &grab->shsurf_destroy_listener);
372
373 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700374 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700375
376 weston_touch_start_grab(touch, &grab->grab);
377 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500378 weston_touch_set_focus(touch->seat,
379 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700380}
381
382static void
383shell_touch_grab_end(struct shell_touch_grab *grab)
384{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700385 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700386 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 grab->shsurf->grabbed = 0;
388 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700389
390 weston_touch_end_grab(grab->touch);
391}
392
393static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800395 struct weston_output *output);
396
Daniel Stone496ca172012-05-30 16:31:42 +0100397static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300398get_modifier(char *modifier)
399{
400 if (!modifier)
401 return MODIFIER_SUPER;
402
403 if (!strcmp("ctrl", modifier))
404 return MODIFIER_CTRL;
405 else if (!strcmp("alt", modifier))
406 return MODIFIER_ALT;
407 else if (!strcmp("super", modifier))
408 return MODIFIER_SUPER;
409 else
410 return MODIFIER_SUPER;
411}
412
Juan Zhaoe10d2792012-04-25 19:09:52 +0800413static enum animation_type
414get_animation_type(char *animation)
415{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800416 if (!animation)
417 return ANIMATION_NONE;
418
Juan Zhaoe10d2792012-04-25 19:09:52 +0800419 if (!strcmp("zoom", animation))
420 return ANIMATION_ZOOM;
421 else if (!strcmp("fade", animation))
422 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100423 else if (!strcmp("dim-layer", animation))
424 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800425 else
426 return ANIMATION_NONE;
427}
428
Alex Wu4539b082012-03-01 12:57:46 +0800429static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400430shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200431{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400432 struct weston_config_section *section;
433 int duration;
434 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200435
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400436 section = weston_config_get_section(shell->compositor->config,
437 "screensaver", NULL, NULL);
438 weston_config_section_get_string(section,
439 "path", &shell->screensaver.path, NULL);
440 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200441 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442
443 section = weston_config_get_section(shell->compositor->config,
444 "shell", NULL, NULL);
445 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100446 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100447 shell->client = s;
448 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400449 "binding-modifier", &s, "super");
450 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200451 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800452
453 weston_config_section_get_string(section,
454 "exposay-modifier", &s, "none");
455 if (strcmp(s, "none") == 0)
456 shell->exposay_modifier = 0;
457 else
458 shell->exposay_modifier = get_modifier(s);
459 free(s);
460
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400461 weston_config_section_get_string(section, "animation", &s, "none");
462 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200463 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700464 weston_config_section_get_string(section,
465 "startup-animation", &s, "fade");
466 shell->startup_animation_type = get_animation_type(s);
467 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700468 if (shell->startup_animation_type == ANIMATION_ZOOM)
469 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100470 weston_config_section_get_string(section, "focus-animation", &s, "none");
471 shell->focus_animation_type = get_animation_type(s);
472 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 weston_config_section_get_uint(section, "num-workspaces",
474 &shell->workspaces.num,
475 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200476}
477
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800478struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100479get_default_output(struct weston_compositor *compositor)
480{
481 return container_of(compositor->output_list.next,
482 struct weston_output, link);
483}
484
485
486/* no-op func for checking focus surface */
487static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600488focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489{
490}
491
492static struct focus_surface *
493get_focus_surface(struct weston_surface *surface)
494{
495 if (surface->configure == focus_surface_configure)
496 return surface->configure_private;
497 else
498 return NULL;
499}
500
501static bool
502is_focus_surface (struct weston_surface *es)
503{
504 return (es->configure == focus_surface_configure);
505}
506
507static bool
508is_focus_view (struct weston_view *view)
509{
510 return is_focus_surface (view->surface);
511}
512
513static struct focus_surface *
514create_focus_surface(struct weston_compositor *ec,
515 struct weston_output *output)
516{
517 struct focus_surface *fsurf = NULL;
518 struct weston_surface *surface = NULL;
519
520 fsurf = malloc(sizeof *fsurf);
521 if (!fsurf)
522 return NULL;
523
524 fsurf->surface = weston_surface_create(ec);
525 surface = fsurf->surface;
526 if (surface == NULL) {
527 free(fsurf);
528 return NULL;
529 }
530
531 surface->configure = focus_surface_configure;
532 surface->output = output;
533 surface->configure_private = fsurf;
534
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800535 fsurf->view = weston_view_create(surface);
536 if (fsurf->view == NULL) {
537 weston_surface_destroy(surface);
538 free(fsurf);
539 return NULL;
540 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100541 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100542
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600543 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600544 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
546 pixman_region32_fini(&surface->opaque);
547 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
548 output->width, output->height);
549 pixman_region32_fini(&surface->input);
550 pixman_region32_init(&surface->input);
551
552 wl_list_init(&fsurf->workspace_transform.link);
553
554 return fsurf;
555}
556
557static void
558focus_surface_destroy(struct focus_surface *fsurf)
559{
560 weston_surface_destroy(fsurf->surface);
561 free(fsurf);
562}
563
564static void
565focus_animation_done(struct weston_view_animation *animation, void *data)
566{
567 struct workspace *ws = data;
568
569 ws->focus_animation = NULL;
570}
571
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200572static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200573focus_state_destroy(struct focus_state *state)
574{
575 wl_list_remove(&state->seat_destroy_listener.link);
576 wl_list_remove(&state->surface_destroy_listener.link);
577 free(state);
578}
579
580static void
581focus_state_seat_destroy(struct wl_listener *listener, void *data)
582{
583 struct focus_state *state = container_of(listener,
584 struct focus_state,
585 seat_destroy_listener);
586
587 wl_list_remove(&state->link);
588 focus_state_destroy(state);
589}
590
591static void
592focus_state_surface_destroy(struct wl_listener *listener, void *data)
593{
594 struct focus_state *state = container_of(listener,
595 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400596 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400597 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500598 struct weston_surface *main_surface, *next;
599 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200600
Pekka Paalanen01388e22013-04-25 13:57:44 +0300601 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
602
Kristian Høgsberge3778222012-07-31 17:29:30 -0400603 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
605 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400606 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100607 if (is_focus_view(view))
608 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400609
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400611 break;
612 }
613
Pekka Paalanen01388e22013-04-25 13:57:44 +0300614 /* if the focus was a sub-surface, activate its main surface */
615 if (main_surface != state->keyboard_focus)
616 next = main_surface;
617
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400619 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100620 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 activate(shell, next, state->seat);
622 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100623 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
624 if (state->ws->focus_animation)
625 weston_view_animation_destroy(state->ws->focus_animation);
626
627 state->ws->focus_animation = weston_fade_run(
628 state->ws->fsurf_front->view,
629 state->ws->fsurf_front->view->alpha, 0.0, 300,
630 focus_animation_done, state->ws);
631 }
632
Kristian Høgsberge3778222012-07-31 17:29:30 -0400633 wl_list_remove(&state->link);
634 focus_state_destroy(state);
635 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200636}
637
638static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400639focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200640{
Jonas Ådahl04769742012-06-13 00:01:24 +0200641 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
643 state = malloc(sizeof *state);
644 if (state == NULL)
645 return NULL;
646
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400648 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200649 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400650 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200651
652 state->seat_destroy_listener.notify = focus_state_seat_destroy;
653 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400654 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200655 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400656 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200657
658 return state;
659}
660
Jonas Ådahl8538b222012-08-29 22:13:03 +0200661static struct focus_state *
662ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
663{
664 struct workspace *ws = get_current_workspace(shell);
665 struct focus_state *state;
666
667 wl_list_for_each(state, &ws->focus_list, link)
668 if (state->seat == seat)
669 break;
670
671 if (&state->link == &ws->focus_list)
672 state = focus_state_create(seat, ws);
673
674 return state;
675}
676
Jonas Ådahl04769742012-06-13 00:01:24 +0200677static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800678focus_state_set_focus(struct focus_state *state,
679 struct weston_surface *surface)
680{
681 if (state->keyboard_focus) {
682 wl_list_remove(&state->surface_destroy_listener.link);
683 wl_list_init(&state->surface_destroy_listener.link);
684 }
685
686 state->keyboard_focus = surface;
687 if (surface)
688 wl_signal_add(&surface->destroy_signal,
689 &state->surface_destroy_listener);
690}
691
692static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400693restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200694{
695 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400696 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100697 struct wl_list pending_seat_list;
698 struct weston_seat *seat, *next_seat;
699
700 /* Temporarily steal the list of seats so that we can keep
701 * track of the seats we've already processed */
702 wl_list_init(&pending_seat_list);
703 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
704 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200705
706 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100707 wl_list_remove(&state->seat->link);
708 wl_list_insert(&shell->compositor->seat_list,
709 &state->seat->link);
710
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800711 if (state->seat->keyboard == NULL)
712 continue;
713
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400714 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200715
Kristian Høgsberge3148752013-05-06 23:19:49 -0400716 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200717 }
Neil Roberts4237f502014-04-09 16:33:31 +0100718
719 /* For any remaining seats that we don't have a focus state
720 * for we'll reset the keyboard focus to NULL */
721 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
722 wl_list_insert(&shell->compositor->seat_list, &seat->link);
723
724 if (state->seat->keyboard == NULL)
725 continue;
726
727 weston_keyboard_set_focus(seat->keyboard, NULL);
728 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200729}
730
731static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200732replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
733 struct weston_seat *seat)
734{
735 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200736
737 wl_list_for_each(state, &ws->focus_list, link) {
738 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800739 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200740 return;
741 }
742 }
743}
744
745static void
746drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
747 struct weston_surface *surface)
748{
749 struct focus_state *state;
750
751 wl_list_for_each(state, &ws->focus_list, link)
752 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800753 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200754}
755
756static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100757animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
758 struct weston_view *from, struct weston_view *to)
759{
760 struct weston_output *output;
761 bool focus_surface_created = false;
762
763 /* FIXME: Only support dim animation using two layers */
764 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
765 return;
766
767 output = get_default_output(shell->compositor);
768 if (ws->fsurf_front == NULL && (from || to)) {
769 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800770 if (ws->fsurf_front == NULL)
771 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100772 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800773
774 ws->fsurf_back = create_focus_surface(shell->compositor, output);
775 if (ws->fsurf_back == NULL) {
776 focus_surface_destroy(ws->fsurf_front);
777 return;
778 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100779 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800780
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100781 focus_surface_created = true;
782 } else {
783 wl_list_remove(&ws->fsurf_front->view->layer_link);
784 wl_list_remove(&ws->fsurf_back->view->layer_link);
785 }
786
787 if (ws->focus_animation) {
788 weston_view_animation_destroy(ws->focus_animation);
789 ws->focus_animation = NULL;
790 }
791
792 if (to)
793 wl_list_insert(&to->layer_link,
794 &ws->fsurf_front->view->layer_link);
795 else if (from)
796 wl_list_insert(&ws->layer.view_list,
797 &ws->fsurf_front->view->layer_link);
798
799 if (focus_surface_created) {
800 ws->focus_animation = weston_fade_run(
801 ws->fsurf_front->view,
802 ws->fsurf_front->view->alpha, 0.6, 300,
803 focus_animation_done, ws);
804 } else if (from) {
805 wl_list_insert(&from->layer_link,
806 &ws->fsurf_back->view->layer_link);
807 ws->focus_animation = weston_stable_fade_run(
808 ws->fsurf_front->view, 0.0,
809 ws->fsurf_back->view, 0.6,
810 focus_animation_done, ws);
811 } else if (to) {
812 wl_list_insert(&ws->layer.view_list,
813 &ws->fsurf_back->view->layer_link);
814 ws->focus_animation = weston_stable_fade_run(
815 ws->fsurf_front->view, 0.0,
816 ws->fsurf_back->view, 0.6,
817 focus_animation_done, ws);
818 }
819}
820
821static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200822workspace_destroy(struct workspace *ws)
823{
Jonas Ådahl04769742012-06-13 00:01:24 +0200824 struct focus_state *state, *next;
825
826 wl_list_for_each_safe(state, next, &ws->focus_list, link)
827 focus_state_destroy(state);
828
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100829 if (ws->fsurf_front)
830 focus_surface_destroy(ws->fsurf_front);
831 if (ws->fsurf_back)
832 focus_surface_destroy(ws->fsurf_back);
833
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200834 free(ws);
835}
836
Jonas Ådahl04769742012-06-13 00:01:24 +0200837static void
838seat_destroyed(struct wl_listener *listener, void *data)
839{
840 struct weston_seat *seat = data;
841 struct focus_state *state, *next;
842 struct workspace *ws = container_of(listener,
843 struct workspace,
844 seat_destroyed_listener);
845
846 wl_list_for_each_safe(state, next, &ws->focus_list, link)
847 if (state->seat == seat)
848 wl_list_remove(&state->link);
849}
850
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200851static struct workspace *
852workspace_create(void)
853{
854 struct workspace *ws = malloc(sizeof *ws);
855 if (ws == NULL)
856 return NULL;
857
858 weston_layer_init(&ws->layer, NULL);
859
Jonas Ådahl04769742012-06-13 00:01:24 +0200860 wl_list_init(&ws->focus_list);
861 wl_list_init(&ws->seat_destroyed_listener.link);
862 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 ws->fsurf_front = NULL;
864 ws->fsurf_back = NULL;
865 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200866
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200867 return ws;
868}
869
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200870static int
871workspace_is_empty(struct workspace *ws)
872{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500873 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200874}
875
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200876static struct workspace *
877get_workspace(struct desktop_shell *shell, unsigned int index)
878{
879 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200880 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200881 pws += index;
882 return *pws;
883}
884
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800885struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200886get_current_workspace(struct desktop_shell *shell)
887{
888 return get_workspace(shell, shell->workspaces.current);
889}
890
891static void
892activate_workspace(struct desktop_shell *shell, unsigned int index)
893{
894 struct workspace *ws;
895
896 ws = get_workspace(shell, index);
897 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
898
899 shell->workspaces.current = index;
900}
901
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200902static unsigned int
903get_output_height(struct weston_output *output)
904{
905 return abs(output->region.extents.y1 - output->region.extents.y2);
906}
907
908static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100909view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200910{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200911 struct weston_transform *transform;
912
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100913 if (is_focus_view(view)) {
914 struct focus_surface *fsurf = get_focus_surface(view->surface);
915 transform = &fsurf->workspace_transform;
916 } else {
917 struct shell_surface *shsurf = get_shell_surface(view->surface);
918 transform = &shsurf->workspace_transform;
919 }
920
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100923 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100925 weston_matrix_init(&transform->matrix);
926 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200929}
930
931static void
932workspace_translate_out(struct workspace *ws, double fraction)
933{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935 unsigned int height;
936 double d;
937
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
939 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940 d = height * fraction;
941
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100942 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200943 }
944}
945
946static void
947workspace_translate_in(struct workspace *ws, double fraction)
948{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200950 unsigned int height;
951 double d;
952
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
954 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200955
956 if (fraction > 0)
957 d = -(height - height * fraction);
958 else
959 d = height + height * fraction;
960
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100961 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962 }
963}
964
965static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200966broadcast_current_workspace_state(struct desktop_shell *shell)
967{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700968 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200969
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700970 wl_resource_for_each(resource, &shell->workspaces.client_list)
971 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200972 shell->workspaces.current,
973 shell->workspaces.num);
974}
975
976static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977reverse_workspace_change_animation(struct desktop_shell *shell,
978 unsigned int index,
979 struct workspace *from,
980 struct workspace *to)
981{
982 shell->workspaces.current = index;
983
984 shell->workspaces.anim_to = to;
985 shell->workspaces.anim_from = from;
986 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
987 shell->workspaces.anim_timestamp = 0;
988
Scott Moreau4272e632012-08-13 09:58:41 -0600989 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990}
991
992static void
993workspace_deactivate_transforms(struct workspace *ws)
994{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 if (is_focus_view(view)) {
1000 struct focus_surface *fsurf = get_focus_surface(view->surface);
1001 transform = &fsurf->workspace_transform;
1002 } else {
1003 struct shell_surface *shsurf = get_shell_surface(view->surface);
1004 transform = &shsurf->workspace_transform;
1005 }
1006
1007 if (!wl_list_empty(&transform->link)) {
1008 wl_list_remove(&transform->link);
1009 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001010 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001012 }
1013}
1014
1015static void
1016finish_workspace_change_animation(struct desktop_shell *shell,
1017 struct workspace *from,
1018 struct workspace *to)
1019{
Scott Moreau4272e632012-08-13 09:58:41 -06001020 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001021
1022 wl_list_remove(&shell->workspaces.animation.link);
1023 workspace_deactivate_transforms(from);
1024 workspace_deactivate_transforms(to);
1025 shell->workspaces.anim_to = NULL;
1026
1027 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1028}
1029
1030static void
1031animate_workspace_change_frame(struct weston_animation *animation,
1032 struct weston_output *output, uint32_t msecs)
1033{
1034 struct desktop_shell *shell =
1035 container_of(animation, struct desktop_shell,
1036 workspaces.animation);
1037 struct workspace *from = shell->workspaces.anim_from;
1038 struct workspace *to = shell->workspaces.anim_to;
1039 uint32_t t;
1040 double x, y;
1041
1042 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1043 finish_workspace_change_animation(shell, from, to);
1044 return;
1045 }
1046
1047 if (shell->workspaces.anim_timestamp == 0) {
1048 if (shell->workspaces.anim_current == 0.0)
1049 shell->workspaces.anim_timestamp = msecs;
1050 else
1051 shell->workspaces.anim_timestamp =
1052 msecs -
1053 /* Invers of movement function 'y' below. */
1054 (asin(1.0 - shell->workspaces.anim_current) *
1055 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1056 M_2_PI);
1057 }
1058
1059 t = msecs - shell->workspaces.anim_timestamp;
1060
1061 /*
1062 * x = [0, π/2]
1063 * y(x) = sin(x)
1064 */
1065 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1066 y = sin(x);
1067
1068 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001069 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070
1071 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1072 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1073 shell->workspaces.anim_current = y;
1074
Scott Moreau4272e632012-08-13 09:58:41 -06001075 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001076 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001077 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001079}
1080
1081static void
1082animate_workspace_change(struct desktop_shell *shell,
1083 unsigned int index,
1084 struct workspace *from,
1085 struct workspace *to)
1086{
1087 struct weston_output *output;
1088
1089 int dir;
1090
1091 if (index > shell->workspaces.current)
1092 dir = -1;
1093 else
1094 dir = 1;
1095
1096 shell->workspaces.current = index;
1097
1098 shell->workspaces.anim_dir = dir;
1099 shell->workspaces.anim_from = from;
1100 shell->workspaces.anim_to = to;
1101 shell->workspaces.anim_current = 0.0;
1102 shell->workspaces.anim_timestamp = 0;
1103
1104 output = container_of(shell->compositor->output_list.next,
1105 struct weston_output, link);
1106 wl_list_insert(&output->animation_list,
1107 &shell->workspaces.animation.link);
1108
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001109 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001110
1111 workspace_translate_in(to, 0);
1112
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001113 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001114
Scott Moreau4272e632012-08-13 09:58:41 -06001115 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116}
1117
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001118static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001119update_workspace(struct desktop_shell *shell, unsigned int index,
1120 struct workspace *from, struct workspace *to)
1121{
1122 shell->workspaces.current = index;
1123 wl_list_insert(&from->layer.link, &to->layer.link);
1124 wl_list_remove(&from->layer.link);
1125}
1126
1127static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001128change_workspace(struct desktop_shell *shell, unsigned int index)
1129{
1130 struct workspace *from;
1131 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001132 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001133
1134 if (index == shell->workspaces.current)
1135 return;
1136
1137 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001139 return;
1140
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001141 from = get_current_workspace(shell);
1142 to = get_workspace(shell, index);
1143
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144 if (shell->workspaces.anim_from == to &&
1145 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001146 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001147 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001148 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149 return;
1150 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001151
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 if (shell->workspaces.anim_to != NULL)
1153 finish_workspace_change_animation(shell,
1154 shell->workspaces.anim_from,
1155 shell->workspaces.anim_to);
1156
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001157 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001158
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001159 if (shell->focus_animation_type != ANIMATION_NONE) {
1160 wl_list_for_each(state, &from->focus_list, link)
1161 if (state->keyboard_focus)
1162 animate_focus_change(shell, from,
1163 get_default_view(state->keyboard_focus), NULL);
1164
1165 wl_list_for_each(state, &to->focus_list, link)
1166 if (state->keyboard_focus)
1167 animate_focus_change(shell, to,
1168 NULL, get_default_view(state->keyboard_focus));
1169 }
1170
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171 if (workspace_is_empty(to) && workspace_is_empty(from))
1172 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001173 else
1174 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001175
1176 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001177}
1178
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001179static bool
1180workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1181{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001183 struct wl_list *e;
1184
1185 if (wl_list_empty(list))
1186 return false;
1187
1188 e = list->next;
1189
1190 if (e->next != list)
1191 return false;
1192
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001194}
1195
1196static void
Philip Withnall659163d2013-11-25 18:01:36 +00001197move_surface_to_workspace(struct desktop_shell *shell,
1198 struct shell_surface *shsurf,
1199 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001200{
1201 struct workspace *from;
1202 struct workspace *to;
1203 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001204 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001205 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001206
1207 if (workspace == shell->workspaces.current)
1208 return;
1209
Philip Withnall659163d2013-11-25 18:01:36 +00001210 view = get_default_view(shsurf->surface);
1211 if (!view)
1212 return;
1213
1214 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1215
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001216 if (workspace >= shell->workspaces.num)
1217 workspace = shell->workspaces.num - 1;
1218
Jonas Ådahle9d22502012-08-29 22:13:01 +02001219 from = get_current_workspace(shell);
1220 to = get_workspace(shell, workspace);
1221
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 wl_list_remove(&view->layer_link);
1223 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001224
Philip Withnall648a4dd2013-11-25 18:01:44 +00001225 shell_surface_update_child_surface_layers(shsurf);
1226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001228 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1229 if (!seat->keyboard)
1230 continue;
1231
1232 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001234 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001235 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001236
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001238}
1239
1240static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001242 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001243 unsigned int index)
1244{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001245 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 struct shell_surface *shsurf;
1248 struct workspace *from;
1249 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001250 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251
Pekka Paalanen01388e22013-04-25 13:57:44 +03001252 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 view = get_default_view(surface);
1254 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001255 index == shell->workspaces.current ||
1256 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 return;
1258
1259 from = get_current_workspace(shell);
1260 to = get_workspace(shell, index);
1261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 wl_list_remove(&view->layer_link);
1263 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001264
Philip Withnall659163d2013-11-25 18:01:36 +00001265 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001266 if (shsurf != NULL)
1267 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001268
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270 drop_focus_state(shell, from, surface);
1271
1272 if (shell->workspaces.anim_from == to &&
1273 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001274 wl_list_remove(&to->layer.link);
1275 wl_list_insert(from->layer.link.prev, &to->layer.link);
1276
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001278 broadcast_current_workspace_state(shell);
1279
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 return;
1281 }
1282
1283 if (shell->workspaces.anim_to != NULL)
1284 finish_workspace_change_animation(shell,
1285 shell->workspaces.anim_from,
1286 shell->workspaces.anim_to);
1287
1288 if (workspace_is_empty(from) &&
1289 workspace_has_only(to, surface))
1290 update_workspace(shell, index, from, to);
1291 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001292 if (shsurf != NULL &&
1293 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294 wl_list_insert(&shell->workspaces.anim_sticky_list,
1295 &shsurf->workspace_transform.link);
1296
1297 animate_workspace_change(shell, index, from, to);
1298 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299
1300 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001301
1302 state = ensure_focus_state(shell, seat);
1303 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001304 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001305}
1306
1307static void
1308workspace_manager_move_surface(struct wl_client *client,
1309 struct wl_resource *resource,
1310 struct wl_resource *surface_resource,
1311 uint32_t workspace)
1312{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001313 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001314 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001315 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001316 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001317 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318
Pekka Paalanen01388e22013-04-25 13:57:44 +03001319 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001320 shell_surface = get_shell_surface(main_surface);
1321 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001323
1324 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325}
1326
1327static const struct workspace_manager_interface workspace_manager_implementation = {
1328 workspace_manager_move_surface,
1329};
1330
1331static void
1332unbind_resource(struct wl_resource *resource)
1333{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001334 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001335}
1336
1337static void
1338bind_workspace_manager(struct wl_client *client,
1339 void *data, uint32_t version, uint32_t id)
1340{
1341 struct desktop_shell *shell = data;
1342 struct wl_resource *resource;
1343
Jason Ekstranda85118c2013-06-27 20:17:02 -05001344 resource = wl_resource_create(client,
1345 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001346
1347 if (resource == NULL) {
1348 weston_log("couldn't add workspace manager object");
1349 return;
1350 }
1351
Jason Ekstranda85118c2013-06-27 20:17:02 -05001352 wl_resource_set_implementation(resource,
1353 &workspace_manager_implementation,
1354 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001355 wl_list_insert(&shell->workspaces.client_list,
1356 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001357
1358 workspace_manager_send_state(resource,
1359 shell->workspaces.current,
1360 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001361}
1362
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001363static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001364touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1365 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1366{
1367}
1368
1369static void
1370touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1371{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001372 struct weston_touch_move_grab *move =
1373 (struct weston_touch_move_grab *) container_of(
1374 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001375
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001376 if (touch_id == 0)
1377 move->active = 0;
1378
Jonas Ådahl9484b692013-12-02 22:05:03 +01001379 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001380 shell_touch_grab_end(&move->base);
1381 free(move);
1382 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001383}
1384
1385static void
1386touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1387 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1388{
1389 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1390 struct shell_surface *shsurf = move->base.shsurf;
1391 struct weston_surface *es;
1392 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1393 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1394
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001395 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001396 return;
1397
1398 es = shsurf->surface;
1399
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001400 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001401
1402 weston_compositor_schedule_repaint(es->compositor);
1403}
1404
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001405static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001406touch_move_grab_frame(struct weston_touch_grab *grab)
1407{
1408}
1409
1410static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001411touch_move_grab_cancel(struct weston_touch_grab *grab)
1412{
1413 struct weston_touch_move_grab *move =
1414 (struct weston_touch_move_grab *) container_of(
1415 grab, struct shell_touch_grab, grab);
1416
1417 shell_touch_grab_end(&move->base);
1418 free(move);
1419}
1420
Rusty Lynch1084da52013-08-15 09:10:08 -07001421static const struct weston_touch_grab_interface touch_move_grab_interface = {
1422 touch_move_grab_down,
1423 touch_move_grab_up,
1424 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001425 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001426 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001427};
1428
1429static int
1430surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1431{
1432 struct weston_touch_move_grab *move;
1433
1434 if (!shsurf)
1435 return -1;
1436
Rafael Antognolli03b16592013-12-03 15:35:42 -02001437 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001438 return 0;
1439
1440 move = malloc(sizeof *move);
1441 if (!move)
1442 return -1;
1443
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001444 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001446 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001448 seat->touch->grab_y;
1449
1450 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1451 seat->touch);
1452
1453 return 0;
1454}
1455
1456static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001457noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001458{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001459}
1460
1461static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001462move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1463 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001464{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001465 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001466 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001467 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001468 int dx, dy;
1469
1470 weston_pointer_move(pointer, x, y);
1471 dx = wl_fixed_to_int(pointer->x + move->dx);
1472 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001473
1474 if (!shsurf)
1475 return;
1476
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001477 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001478
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001480}
1481
1482static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001483move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001484 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001485{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001486 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1487 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001488 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001489 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001490
Daniel Stone4dbadb12012-05-30 16:31:51 +01001491 if (pointer->button_count == 0 &&
1492 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001493 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001494 free(grab);
1495 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001496}
1497
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001498static void
1499move_grab_cancel(struct weston_pointer_grab *grab)
1500{
1501 struct shell_grab *shell_grab =
1502 container_of(grab, struct shell_grab, grab);
1503
1504 shell_grab_end(shell_grab);
1505 free(grab);
1506}
1507
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001508static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001509 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001510 move_grab_motion,
1511 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001512 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513};
1514
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001515static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001516surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001517{
1518 struct weston_move_grab *move;
1519
1520 if (!shsurf)
1521 return -1;
1522
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001523 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001524 return 0;
1525
1526 move = malloc(sizeof *move);
1527 if (!move)
1528 return -1;
1529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001531 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001533 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001534
1535 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001536 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001537
1538 return 0;
1539}
1540
1541static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001542common_surface_move(struct wl_resource *resource,
1543 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001544{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001545 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001546 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001547 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548
Kristian Høgsbergd0b40ed2014-04-29 14:47:46 -07001549 if (shsurf->grabbed)
1550 return;
1551
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001552 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001553 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001554 seat->pointer->button_count > 0 &&
1555 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001557 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001558 (surface_move(shsurf, seat) < 0))
1559 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001560 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001561 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001562 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001564 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001565 (surface_touch_move(shsurf, seat) < 0))
1566 wl_resource_post_no_memory(resource);
1567 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568}
1569
Rafael Antognollie2a34552013-12-03 15:35:45 -02001570static void
1571shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1572 struct wl_resource *seat_resource, uint32_t serial)
1573{
1574 common_surface_move(resource, seat_resource, serial);
1575}
1576
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001577struct weston_resize_grab {
1578 struct shell_grab base;
1579 uint32_t edges;
1580 int32_t width, height;
1581};
1582
1583static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001584resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1585 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001586{
1587 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001588 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001589 struct shell_surface *shsurf = resize->base.shsurf;
1590 int32_t width, height;
1591 wl_fixed_t from_x, from_y;
1592 wl_fixed_t to_x, to_y;
1593
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001594 weston_pointer_move(pointer, x, y);
1595
Ander Conselvan de Oliveira9c376b52014-04-29 17:54:03 +03001596 if (!shsurf || !shsurf->resource)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597 return;
1598
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 weston_view_from_global_fixed(shsurf->view,
1600 pointer->grab_x, pointer->grab_y,
1601 &from_x, &from_y);
1602 weston_view_from_global_fixed(shsurf->view,
1603 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604
1605 width = resize->width;
1606 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1607 width += wl_fixed_to_int(from_x - to_x);
1608 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1609 width += wl_fixed_to_int(to_x - from_x);
1610 }
1611
1612 height = resize->height;
1613 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1614 height += wl_fixed_to_int(from_y - to_y);
1615 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1616 height += wl_fixed_to_int(to_y - from_y);
1617 }
1618
1619 shsurf->client->send_configure(shsurf->surface,
1620 resize->edges, width, height);
1621}
1622
1623static void
1624send_configure(struct weston_surface *surface,
1625 uint32_t edges, int32_t width, int32_t height)
1626{
1627 struct shell_surface *shsurf = get_shell_surface(surface);
1628
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001629 assert(shsurf);
1630
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001631 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001632 edges, width, height);
1633}
1634
1635static const struct weston_shell_client shell_client = {
1636 send_configure
1637};
1638
1639static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001640resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641 uint32_t time, uint32_t button, uint32_t state_w)
1642{
1643 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001644 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645 enum wl_pointer_button_state state = state_w;
1646
1647 if (pointer->button_count == 0 &&
1648 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1649 shell_grab_end(&resize->base);
1650 free(grab);
1651 }
1652}
1653
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001654static void
1655resize_grab_cancel(struct weston_pointer_grab *grab)
1656{
1657 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1658
1659 shell_grab_end(&resize->base);
1660 free(grab);
1661}
1662
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001663static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001664 noop_grab_focus,
1665 resize_grab_motion,
1666 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001667 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001668};
1669
Giulio Camuffob8366642013-04-25 13:57:46 +03001670/*
1671 * Returns the bounding box of a surface and all its sub-surfaces,
1672 * in the surface coordinates system. */
1673static void
1674surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1675 int32_t *y, int32_t *w, int32_t *h) {
1676 pixman_region32_t region;
1677 pixman_box32_t *box;
1678 struct weston_subsurface *subsurface;
1679
1680 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 surface->width,
1682 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001683
1684 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1685 pixman_region32_union_rect(&region, &region,
1686 subsurface->position.x,
1687 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 subsurface->surface->width,
1689 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001690 }
1691
1692 box = pixman_region32_extents(&region);
1693 if (x)
1694 *x = box->x1;
1695 if (y)
1696 *y = box->y1;
1697 if (w)
1698 *w = box->x2 - box->x1;
1699 if (h)
1700 *h = box->y2 - box->y1;
1701
1702 pixman_region32_fini(&region);
1703}
1704
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705static int
1706surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001707 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708{
1709 struct weston_resize_grab *resize;
1710
Rafael Antognolli03b16592013-12-03 15:35:42 -02001711 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001712 return 0;
1713
1714 if (edges == 0 || edges > 15 ||
1715 (edges & 3) == 3 || (edges & 12) == 12)
1716 return 0;
1717
1718 resize = malloc(sizeof *resize);
1719 if (!resize)
1720 return -1;
1721
1722 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001723 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1724 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001726 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001728 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001729
1730 return 0;
1731}
1732
1733static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001734common_surface_resize(struct wl_resource *resource,
1735 struct wl_resource *seat_resource, uint32_t serial,
1736 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001737{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001738 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001739 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001740 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741
Rafael Antognolli03b16592013-12-03 15:35:42 -02001742 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743 return;
1744
Kristian Høgsberg7b83ae42014-04-29 14:50:25 -07001745 if (shsurf->grabbed)
1746 return;
1747
Kristian Høgsberge3148752013-05-06 23:19:49 -04001748 if (seat->pointer->button_count == 0 ||
1749 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001750 seat->pointer->focus == NULL)
1751 return;
1752
1753 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1754 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001755 return;
1756
Kristian Høgsberge3148752013-05-06 23:19:49 -04001757 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001758 wl_resource_post_no_memory(resource);
1759}
1760
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001761static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001762shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1763 struct wl_resource *seat_resource, uint32_t serial,
1764 uint32_t edges)
1765{
1766 common_surface_resize(resource, seat_resource, serial, edges);
1767}
1768
1769static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001770busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001771{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001772 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001773 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001775 wl_fixed_t sx, sy;
1776
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 view = weston_compositor_pick_view(pointer->seat->compositor,
1778 pointer->x, pointer->y,
1779 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001780
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001781 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1782 shell_grab_end(grab);
1783 free(grab);
1784 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001785}
1786
1787static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001788busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1789 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001790{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001791 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001793
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001795busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796 uint32_t time, uint32_t button, uint32_t state)
1797{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001798 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001799 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001800 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001801
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001802 if (shsurf && button == BTN_LEFT && state) {
1803 activate(shsurf->shell, shsurf->surface, seat);
1804 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001805 } else if (shsurf && button == BTN_RIGHT && state) {
1806 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001807 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001808 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809}
1810
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001811static void
1812busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1813{
1814 struct shell_grab *grab = (struct shell_grab *) base;
1815
1816 shell_grab_end(grab);
1817 free(grab);
1818}
1819
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001820static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001821 busy_cursor_grab_focus,
1822 busy_cursor_grab_motion,
1823 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001824 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825};
1826
1827static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001828set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829{
1830 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001832 if (pointer->grab->interface == &busy_cursor_grab_interface)
1833 return;
1834
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835 grab = malloc(sizeof *grab);
1836 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001837 return;
1838
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001839 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1840 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001841 /* Mark the shsurf as ungrabbed so that button binding is able
1842 * to move it. */
1843 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001844}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001845
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001847end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001849 struct shell_grab *grab;
1850 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001851
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001852 wl_list_for_each(seat, &compositor->seat_list, link) {
1853 if (seat->pointer == NULL)
1854 continue;
1855
1856 grab = (struct shell_grab *) seat->pointer->grab;
1857 if (grab->grab.interface == &busy_cursor_grab_interface &&
1858 wl_resource_get_client(grab->shsurf->resource) == client) {
1859 shell_grab_end(grab);
1860 free(grab);
1861 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001862 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001863}
1864
Scott Moreau9521d5e2012-04-19 13:06:17 -06001865static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001866handle_shell_client_destroy(struct wl_listener *listener, void *data);
1867
1868static int
1869xdg_ping_timeout_handler(void *data)
1870{
1871 struct shell_client *sc = data;
1872 struct weston_seat *seat;
1873 struct shell_surface *shsurf;
1874
1875 /* Client is not responding */
1876 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001877 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1878 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1879 continue;
1880 if (seat->pointer->focus->surface->resource == NULL)
1881 continue;
1882
1883 shsurf = get_shell_surface(seat->pointer->focus->surface);
1884 if (shsurf &&
1885 wl_resource_get_client(shsurf->resource) == sc->client)
1886 set_busy_cursor(shsurf, seat->pointer);
1887 }
1888
1889 return 1;
1890}
1891
1892static void
1893handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1894{
1895 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001896 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001897 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001898 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001899
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001900 if (sc->unresponsive) {
1901 xdg_ping_timeout_handler(sc);
1902 return;
1903 }
1904
1905 sc->ping_serial = serial;
1906 loop = wl_display_get_event_loop(compositor->wl_display);
1907 if (sc->ping_timer == NULL)
1908 sc->ping_timer =
1909 wl_event_loop_add_timer(loop,
1910 xdg_ping_timeout_handler, sc);
1911 if (sc->ping_timer == NULL)
1912 return;
1913
1914 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1915
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001916 if (shell_surface_is_xdg_surface(shsurf) ||
1917 shell_surface_is_xdg_popup(shsurf))
1918 xdg_shell_send_ping(sc->resource, serial);
1919 else if (shell_surface_is_wl_shell_surface(shsurf))
1920 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001921}
1922
Scott Moreauff1db4a2012-04-17 19:06:18 -06001923static void
1924ping_handler(struct weston_surface *surface, uint32_t serial)
1925{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001926 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001927
1928 if (!shsurf)
1929 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001930 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001931 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001932 if (shsurf->surface == shsurf->shell->grab_surface)
1933 return;
1934
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001935 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001936}
1937
1938static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001939handle_pointer_focus(struct wl_listener *listener, void *data)
1940{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001941 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001942 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001943 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001944 uint32_t serial;
1945
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001947 return;
1948
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001950 serial = wl_display_next_serial(compositor->wl_display);
1951 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001952}
1953
1954static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001955create_pointer_focus_listener(struct weston_seat *seat)
1956{
1957 struct wl_listener *listener;
1958
Kristian Høgsberge3148752013-05-06 23:19:49 -04001959 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001960 return;
1961
1962 listener = malloc(sizeof *listener);
1963 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001964 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001965}
1966
1967static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001968shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1969{
1970 if (--shsurf->focus_count == 0)
1971 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001972 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001973}
1974
1975static void
1976shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1977{
1978 if (shsurf->focus_count++ == 0)
1979 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05001980 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001981}
1982
1983static void
1984handle_keyboard_focus(struct wl_listener *listener, void *data)
1985{
1986 struct weston_keyboard *keyboard = data;
1987 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1988
1989 if (seat->focused_surface) {
1990 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1991 if (shsurf)
1992 shell_surface_lose_keyboard_focus(shsurf);
1993 }
1994
1995 seat->focused_surface = keyboard->focus;
1996
1997 if (seat->focused_surface) {
1998 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1999 if (shsurf)
2000 shell_surface_gain_keyboard_focus(shsurf);
2001 }
2002}
2003
2004static void
2005create_keyboard_focus_listener(struct weston_seat *seat)
2006{
2007 struct wl_listener *listener;
2008
2009 if (!seat->keyboard)
2010 return;
2011
2012 listener = malloc(sizeof *listener);
2013 listener->notify = handle_keyboard_focus;
2014 wl_signal_add(&seat->keyboard->focus_signal, listener);
2015}
2016
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002017static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002018shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002019{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002020 if (sc->ping_serial != serial)
2021 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002022
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002023 sc->unresponsive = 0;
2024 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002025
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002026 if (sc->ping_timer) {
2027 wl_event_source_remove(sc->ping_timer);
2028 sc->ping_timer = NULL;
2029 }
2030
2031}
2032
2033static void
2034shell_surface_pong(struct wl_client *client,
2035 struct wl_resource *resource, uint32_t serial)
2036{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002037 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2038 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002039
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002040 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002041}
2042
2043static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002044set_title(struct shell_surface *shsurf, const char *title)
2045{
2046 free(shsurf->title);
2047 shsurf->title = strdup(title);
2048}
2049
2050static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002051shell_surface_set_title(struct wl_client *client,
2052 struct wl_resource *resource, const char *title)
2053{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002054 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002055
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002056 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002057}
2058
2059static void
2060shell_surface_set_class(struct wl_client *client,
2061 struct wl_resource *resource, const char *class)
2062{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002063 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002064
2065 free(shsurf->class);
2066 shsurf->class = strdup(class);
2067}
2068
Alex Wu4539b082012-03-01 12:57:46 +08002069static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002070restore_output_mode(struct weston_output *output)
2071{
Hardening57388e42013-09-18 23:56:36 +02002072 if (output->current_mode != output->original_mode ||
2073 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002074 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002075 output->original_mode,
2076 output->original_scale,
2077 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002078}
2079
2080static void
2081restore_all_output_modes(struct weston_compositor *compositor)
2082{
2083 struct weston_output *output;
2084
2085 wl_list_for_each(output, &compositor->output_list, link)
2086 restore_output_mode(output);
2087}
2088
Philip Withnallbecb77e2013-11-25 18:01:30 +00002089static int
2090get_output_panel_height(struct desktop_shell *shell,
2091 struct weston_output *output)
2092{
2093 struct weston_view *view;
2094 int panel_height = 0;
2095
2096 if (!output)
2097 return 0;
2098
2099 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2100 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002101 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002102 break;
2103 }
2104 }
2105
2106 return panel_height;
2107}
2108
Philip Withnall07926d92013-11-25 18:01:40 +00002109/* The surface will be inserted into the list immediately after the link
2110 * returned by this function (i.e. will be stacked immediately above the
2111 * returned link). */
2112static struct wl_list *
2113shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2114{
2115 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002116 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002117
2118 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002119 case SHELL_SURFACE_POPUP:
2120 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002121 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002122 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002123 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002124 /* Move the surface to its parent layer so
2125 * that surfaces which are transient for
2126 * fullscreen surfaces don't get hidden by the
2127 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002128
2129 /* TODO: Handle a parent with multiple views */
2130 parent = get_default_view(shsurf->parent);
2131 if (parent)
2132 return parent->layer_link.prev;
2133 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002134 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002135
2136 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002137 return &shsurf->shell->fullscreen_layer.view_list;
2138
Philip Withnall07926d92013-11-25 18:01:40 +00002139 case SHELL_SURFACE_NONE:
2140 default:
2141 /* Go to the fallback, below. */
2142 break;
2143 }
2144
2145 /* Move the surface to a normal workspace layer so that surfaces
2146 * which were previously fullscreen or transient are no longer
2147 * rendered on top. */
2148 ws = get_current_workspace(shsurf->shell);
2149 return &ws->layer.view_list;
2150}
2151
Philip Withnall648a4dd2013-11-25 18:01:44 +00002152static void
2153shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2154{
2155 struct shell_surface *child;
2156
2157 /* Move the child layers to the same workspace as shsurf. They will be
2158 * stacked above shsurf. */
2159 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2160 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002161 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002162 weston_view_geometry_dirty(child->view);
2163 wl_list_remove(&child->view->layer_link);
2164 wl_list_insert(shsurf->view->layer_link.prev,
2165 &child->view->layer_link);
2166 weston_view_geometry_dirty(child->view);
2167 weston_surface_damage(child->surface);
2168
2169 /* Recurse. We don’t expect this to recurse very far (if
2170 * at all) because that would imply we have transient
2171 * (or popup) children of transient surfaces, which
2172 * would be unusual. */
2173 shell_surface_update_child_surface_layers(child);
2174 }
2175 }
2176}
2177
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002178/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002179 * geometry to ensure the changes are redrawn.
2180 *
2181 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2182 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002183static void
2184shell_surface_update_layer(struct shell_surface *shsurf)
2185{
2186 struct wl_list *new_layer_link;
2187
2188 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2189
2190 if (new_layer_link == &shsurf->view->layer_link)
2191 return;
2192
2193 weston_view_geometry_dirty(shsurf->view);
2194 wl_list_remove(&shsurf->view->layer_link);
2195 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2196 weston_view_geometry_dirty(shsurf->view);
2197 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002198
2199 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002200}
2201
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002202static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002203shell_surface_set_parent(struct shell_surface *shsurf,
2204 struct weston_surface *parent)
2205{
2206 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002207
2208 wl_list_remove(&shsurf->children_link);
2209 wl_list_init(&shsurf->children_link);
2210
2211 /* Insert into the parent surface’s child list. */
2212 if (parent != NULL) {
2213 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2214 if (parent_shsurf != NULL)
2215 wl_list_insert(&parent_shsurf->children_list,
2216 &shsurf->children_link);
2217 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002218}
2219
2220static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002221shell_surface_set_output(struct shell_surface *shsurf,
2222 struct weston_output *output)
2223{
2224 struct weston_surface *es = shsurf->surface;
2225
2226 /* get the default output, if the client set it as NULL
2227 check whether the ouput is available */
2228 if (output)
2229 shsurf->output = output;
2230 else if (es->output)
2231 shsurf->output = es->output;
2232 else
2233 shsurf->output = get_default_output(es->compositor);
2234}
2235
2236static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002237surface_clear_next_states(struct shell_surface *shsurf)
2238{
2239 shsurf->next_state.maximized = false;
2240 shsurf->next_state.fullscreen = false;
2241
2242 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2243 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2244 shsurf->state_changed = true;
2245}
2246
2247static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002248set_toplevel(struct shell_surface *shsurf)
2249{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002250 shell_surface_set_parent(shsurf, NULL);
2251 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002252 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002253
2254 /* The layer_link is updated in set_surface_type(),
2255 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002256}
2257
2258static void
2259shell_surface_set_toplevel(struct wl_client *client,
2260 struct wl_resource *resource)
2261{
2262 struct shell_surface *surface = wl_resource_get_user_data(resource);
2263
2264 set_toplevel(surface);
2265}
2266
2267static void
2268set_transient(struct shell_surface *shsurf,
2269 struct weston_surface *parent, int x, int y, uint32_t flags)
2270{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002271 assert(parent != NULL);
2272
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002273 shell_surface_set_parent(shsurf, parent);
2274
2275 surface_clear_next_states(shsurf);
2276
Philip Withnallbecb77e2013-11-25 18:01:30 +00002277 shsurf->transient.x = x;
2278 shsurf->transient.y = y;
2279 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002280
Rafael Antognollied207b42013-12-03 15:35:43 -02002281 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002282 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002283 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002284
2285 /* The layer_link is updated in set_surface_type(),
2286 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287}
2288
2289static void
2290shell_surface_set_transient(struct wl_client *client,
2291 struct wl_resource *resource,
2292 struct wl_resource *parent_resource,
2293 int x, int y, uint32_t flags)
2294{
2295 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2296 struct weston_surface *parent =
2297 wl_resource_get_user_data(parent_resource);
2298
2299 set_transient(shsurf, parent, x, y, flags);
2300}
2301
2302static void
2303set_fullscreen(struct shell_surface *shsurf,
2304 uint32_t method,
2305 uint32_t framerate,
2306 struct weston_output *output)
2307{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002308 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002309
2310 shsurf->fullscreen_output = shsurf->output;
2311 shsurf->fullscreen.type = method;
2312 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002313
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002314 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002315
2316 shsurf->client->send_configure(shsurf->surface, 0,
2317 shsurf->output->width,
2318 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002319
2320 /* The layer_link is updated in set_surface_type(),
2321 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002322}
2323
2324static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002325weston_view_set_initial_position(struct weston_view *view,
2326 struct desktop_shell *shell);
2327
2328static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002329unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002330{
Philip Withnallf85fe842013-11-25 18:01:37 +00002331 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002332 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2333 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002334 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002335 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002336
Alex Wu4539b082012-03-01 12:57:46 +08002337 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2338 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002339
Alex Wu4539b082012-03-01 12:57:46 +08002340 wl_list_remove(&shsurf->fullscreen.transform.link);
2341 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002342
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343 if (shsurf->fullscreen.black_view)
2344 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2345 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002346
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002347 if (shsurf->saved_position_valid)
2348 weston_view_set_position(shsurf->view,
2349 shsurf->saved_x, shsurf->saved_y);
2350 else
2351 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2352
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002353 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002355 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002356 shsurf->saved_rotation_valid = false;
2357 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002358
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002359 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002360}
2361
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002362static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002363shell_surface_set_fullscreen(struct wl_client *client,
2364 struct wl_resource *resource,
2365 uint32_t method,
2366 uint32_t framerate,
2367 struct wl_resource *output_resource)
2368{
2369 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2370 struct weston_output *output;
2371
2372 if (output_resource)
2373 output = wl_resource_get_user_data(output_resource);
2374 else
2375 output = NULL;
2376
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002377 shell_surface_set_parent(shsurf, NULL);
2378
Rafael Antognolli03b16592013-12-03 15:35:42 -02002379 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002380 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002381
2382 shsurf->next_state.fullscreen = true;
2383 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002384}
2385
2386static void
2387set_popup(struct shell_surface *shsurf,
2388 struct weston_surface *parent,
2389 struct weston_seat *seat,
2390 uint32_t serial,
2391 int32_t x,
2392 int32_t y)
2393{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002394 assert(parent != NULL);
2395
Philip Withnallbecb77e2013-11-25 18:01:30 +00002396 shsurf->popup.shseat = get_shell_seat(seat);
2397 shsurf->popup.serial = serial;
2398 shsurf->popup.x = x;
2399 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002400
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002401 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402}
2403
2404static void
2405shell_surface_set_popup(struct wl_client *client,
2406 struct wl_resource *resource,
2407 struct wl_resource *seat_resource,
2408 uint32_t serial,
2409 struct wl_resource *parent_resource,
2410 int32_t x, int32_t y, uint32_t flags)
2411{
2412 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002413 struct weston_surface *parent =
2414 wl_resource_get_user_data(parent_resource);
2415
2416 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417
Rafael Antognolli03b16592013-12-03 15:35:42 -02002418 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002419 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002420 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002421 wl_resource_get_user_data(seat_resource),
2422 serial, x, y);
2423}
2424
2425static void
2426set_maximized(struct shell_surface *shsurf,
2427 struct weston_output *output)
2428{
2429 struct desktop_shell *shell;
2430 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002431
Philip Withnall352e7ed2013-11-25 18:01:35 +00002432 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002433
2434 shell = shell_surface_get_shell(shsurf);
2435 panel_height = get_output_panel_height(shell, shsurf->output);
2436 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2437
2438 shsurf->client->send_configure(shsurf->surface, edges,
2439 shsurf->output->width,
2440 shsurf->output->height - panel_height);
2441
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002442 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002443}
2444
2445static void
2446unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002447{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002448 /* undo all maximized things here */
2449 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002450
2451 if (shsurf->saved_position_valid)
2452 weston_view_set_position(shsurf->view,
2453 shsurf->saved_x, shsurf->saved_y);
2454 else
2455 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002456
2457 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 wl_list_insert(&shsurf->view->geometry.transformation_list,
2459 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002460 shsurf->saved_rotation_valid = false;
2461 }
2462
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002463 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002464}
2465
Philip Withnallbecb77e2013-11-25 18:01:30 +00002466static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002467set_minimized(struct weston_surface *surface, uint32_t is_true)
2468{
2469 struct shell_surface *shsurf;
2470 struct workspace *current_ws;
2471 struct weston_seat *seat;
2472 struct weston_surface *focus;
2473 struct weston_view *view;
2474
2475 view = get_default_view(surface);
2476 if (!view)
2477 return;
2478
2479 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2480
2481 shsurf = get_shell_surface(surface);
2482 current_ws = get_current_workspace(shsurf->shell);
2483
2484 wl_list_remove(&view->layer_link);
2485 /* hide or show, depending on the state */
2486 if (is_true) {
2487 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2488
2489 drop_focus_state(shsurf->shell, current_ws, view->surface);
2490 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2491 if (!seat->keyboard)
2492 continue;
2493 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2494 if (focus == view->surface)
2495 weston_keyboard_set_focus(seat->keyboard, NULL);
2496 }
2497 }
2498 else {
2499 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2500
2501 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2502 if (!seat->keyboard)
2503 continue;
2504 activate(shsurf->shell, view->surface, seat);
2505 }
2506 }
2507
2508 shell_surface_update_child_surface_layers(shsurf);
2509
2510 weston_view_damage_below(view);
2511}
2512
2513static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002514shell_surface_set_maximized(struct wl_client *client,
2515 struct wl_resource *resource,
2516 struct wl_resource *output_resource)
2517{
2518 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2519 struct weston_output *output;
2520
2521 if (output_resource)
2522 output = wl_resource_get_user_data(output_resource);
2523 else
2524 output = NULL;
2525
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002526 shell_surface_set_parent(shsurf, NULL);
2527
Rafael Antognolli03b16592013-12-03 15:35:42 -02002528 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002529 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002530
2531 shsurf->next_state.maximized = true;
2532 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002533}
2534
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002535/* This is only ever called from set_surface_type(), so there’s no need to
2536 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002537static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002538reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002539{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002540 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002541 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002542 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002543 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002544
Pekka Paalanen98262232011-12-01 10:42:22 +02002545 return 0;
2546}
2547
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002548static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002549set_full_output(struct shell_surface *shsurf)
2550{
2551 shsurf->saved_x = shsurf->view->geometry.x;
2552 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002553 shsurf->saved_width = shsurf->surface->width;
2554 shsurf->saved_height = shsurf->surface->height;
2555 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002556 shsurf->saved_position_valid = true;
2557
2558 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2559 wl_list_remove(&shsurf->rotation.transform.link);
2560 wl_list_init(&shsurf->rotation.transform.link);
2561 weston_view_geometry_dirty(shsurf->view);
2562 shsurf->saved_rotation_valid = true;
2563 }
2564}
2565
2566static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002567set_surface_type(struct shell_surface *shsurf)
2568{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002569 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002570 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002571
Philip Withnallbecb77e2013-11-25 18:01:30 +00002572 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002573
Rafael Antognolli03b16592013-12-03 15:35:42 -02002574 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002575 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002576
2577 switch (shsurf->type) {
2578 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002579 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002580 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002581 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002582 weston_view_set_position(shsurf->view,
2583 pev->geometry.x + shsurf->transient.x,
2584 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002585 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002586 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002587
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002588 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002589 weston_view_set_position(shsurf->view, shsurf->transient.x,
2590 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002591 break;
2592
Philip Withnall0f640e22013-11-25 18:01:31 +00002593 case SHELL_SURFACE_POPUP:
2594 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002595 default:
2596 break;
2597 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002598
2599 /* Update the surface’s layer. */
2600 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002601}
2602
Tiago Vignattibe143262012-04-16 17:31:41 +03002603static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002604shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002605{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002606 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002607}
2608
Alex Wu21858432012-04-01 20:13:08 +08002609static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002610black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002611
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002613create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002614 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002615 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002616{
2617 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002618 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002619
2620 surface = weston_surface_create(ec);
2621 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002622 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002623 return NULL;
2624 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 view = weston_view_create(surface);
2626 if (surface == NULL) {
2627 weston_log("no memory\n");
2628 weston_surface_destroy(surface);
2629 return NULL;
2630 }
Alex Wu4539b082012-03-01 12:57:46 +08002631
Alex Wu21858432012-04-01 20:13:08 +08002632 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002633 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002634 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002635 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002636 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002637 pixman_region32_fini(&surface->input);
2638 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002639
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002640 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002641 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642
2643 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002644}
2645
Philip Withnalled8f1a92013-11-25 18:01:43 +00002646static void
2647shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2648{
2649 struct weston_output *output = shsurf->fullscreen_output;
2650
Rafael Antognolli03b16592013-12-03 15:35:42 -02002651 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002652
2653 if (!shsurf->fullscreen.black_view)
2654 shsurf->fullscreen.black_view =
2655 create_black_surface(shsurf->surface->compositor,
2656 shsurf->surface,
2657 output->x, output->y,
2658 output->width,
2659 output->height);
2660
2661 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2662 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2663 wl_list_insert(&shsurf->view->layer_link,
2664 &shsurf->fullscreen.black_view->layer_link);
2665 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2666 weston_surface_damage(shsurf->surface);
2667}
2668
Alex Wu4539b082012-03-01 12:57:46 +08002669/* Create black surface and append it to the associated fullscreen surface.
2670 * Handle size dismatch and positioning according to the method. */
2671static void
2672shell_configure_fullscreen(struct shell_surface *shsurf)
2673{
2674 struct weston_output *output = shsurf->fullscreen_output;
2675 struct weston_surface *surface = shsurf->surface;
2676 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002677 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002678 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002679
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002680 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2681 restore_output_mode(output);
2682
Philip Withnalled8f1a92013-11-25 18:01:43 +00002683 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002684
Jason Ekstranda7af7042013-10-12 22:38:11 -05002685 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002686 &surf_width, &surf_height);
2687
Alex Wu4539b082012-03-01 12:57:46 +08002688 switch (shsurf->fullscreen.type) {
2689 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002690 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002691 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002692 break;
2693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002694 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002695 if (output->width == surf_width &&
2696 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002697 weston_view_set_position(shsurf->view,
2698 output->x - surf_x,
2699 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002700 break;
2701 }
2702
Alex Wu4539b082012-03-01 12:57:46 +08002703 matrix = &shsurf->fullscreen.transform.matrix;
2704 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002705
Scott Moreau1bad5db2012-08-18 01:04:05 -06002706 output_aspect = (float) output->width /
2707 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 /* XXX: Use surf_width and surf_height here? */
2709 surface_aspect = (float) surface->width /
2710 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002711 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002712 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002713 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002714 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002715 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002716 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002717
Alex Wu4539b082012-03-01 12:57:46 +08002718 weston_matrix_scale(matrix, scale, scale, 1);
2719 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002721 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002722 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2723 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002725
Alex Wu4539b082012-03-01 12:57:46 +08002726 break;
2727 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002728 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002729 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002730 surf_width * surface->buffer_viewport.buffer.scale,
2731 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002732 shsurf->fullscreen.framerate};
2733
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002734 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002735 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736 weston_view_set_position(shsurf->view,
2737 output->x - surf_x,
2738 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002739 shsurf->fullscreen.black_view->surface->width = output->width;
2740 shsurf->fullscreen.black_view->surface->height = output->height;
2741 weston_view_set_position(shsurf->fullscreen.black_view,
2742 output->x - surf_x,
2743 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002744 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002745 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002746 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002748 }
Alex Wubd3354b2012-04-17 17:20:49 +08002749 }
Alex Wu4539b082012-03-01 12:57:46 +08002750 break;
2751 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002753 break;
2754 default:
2755 break;
2756 }
2757}
2758
Alex Wu4539b082012-03-01 12:57:46 +08002759static void
2760shell_map_fullscreen(struct shell_surface *shsurf)
2761{
Alex Wubd3354b2012-04-17 17:20:49 +08002762 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002763}
2764
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002765static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002766set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2767{
2768 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002769 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002770 shsurf->transient.x = x;
2771 shsurf->transient.y = y;
2772 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002773
2774 shell_surface_set_parent(shsurf, NULL);
2775
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002776 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002777 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002778}
2779
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002780static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002781
2782static void
2783destroy_shell_seat(struct wl_listener *listener, void *data)
2784{
2785 struct shell_seat *shseat =
2786 container_of(listener,
2787 struct shell_seat, seat_destroy_listener);
2788 struct shell_surface *shsurf, *prev = NULL;
2789
2790 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002791 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002792 shseat->popup_grab.client = NULL;
2793
2794 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2795 shsurf->popup.shseat = NULL;
2796 if (prev) {
2797 wl_list_init(&prev->popup.grab_link);
2798 }
2799 prev = shsurf;
2800 }
2801 wl_list_init(&prev->popup.grab_link);
2802 }
2803
2804 wl_list_remove(&shseat->seat_destroy_listener.link);
2805 free(shseat);
2806}
2807
2808static struct shell_seat *
2809create_shell_seat(struct weston_seat *seat)
2810{
2811 struct shell_seat *shseat;
2812
2813 shseat = calloc(1, sizeof *shseat);
2814 if (!shseat) {
2815 weston_log("no memory to allocate shell seat\n");
2816 return NULL;
2817 }
2818
2819 shseat->seat = seat;
2820 wl_list_init(&shseat->popup_grab.surfaces_list);
2821
2822 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2823 wl_signal_add(&seat->destroy_signal,
2824 &shseat->seat_destroy_listener);
2825
2826 return shseat;
2827}
2828
2829static struct shell_seat *
2830get_shell_seat(struct weston_seat *seat)
2831{
2832 struct wl_listener *listener;
2833
2834 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2835 if (listener == NULL)
2836 return create_shell_seat(seat);
2837
2838 return container_of(listener,
2839 struct shell_seat, seat_destroy_listener);
2840}
2841
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002842static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002843popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002844{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002845 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002846 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002847 struct shell_seat *shseat =
2848 container_of(grab, struct shell_seat, popup_grab.grab);
2849 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002850 wl_fixed_t sx, sy;
2851
Jason Ekstranda7af7042013-10-12 22:38:11 -05002852 view = weston_compositor_pick_view(pointer->seat->compositor,
2853 pointer->x, pointer->y,
2854 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002855
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 if (view && view->surface->resource &&
2857 wl_resource_get_client(view->surface->resource) == client) {
2858 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002859 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002860 weston_pointer_set_focus(pointer, NULL,
2861 wl_fixed_from_int(0),
2862 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863 }
2864}
2865
2866static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002867popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2868 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002869{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002870 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002871 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002872 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002873
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002874 weston_pointer_move(pointer, x, y);
2875
Neil Roberts96d790e2013-09-19 17:32:00 +01002876 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002877 weston_view_from_global_fixed(pointer->focus,
2878 pointer->x, pointer->y,
2879 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002880 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002881 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002882}
2883
2884static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002885popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002886 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002887{
2888 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002889 struct shell_seat *shseat =
2890 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002891 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002892 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002893 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002894 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002895
Neil Roberts96d790e2013-09-19 17:32:00 +01002896 resource_list = &grab->pointer->focus_resource_list;
2897 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002898 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002899 wl_resource_for_each(resource, resource_list) {
2900 wl_pointer_send_button(resource, serial,
2901 time, button, state);
2902 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002903 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002904 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002905 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002906 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002907 }
2908
Daniel Stone4dbadb12012-05-30 16:31:51 +01002909 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002910 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002911}
2912
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002913static void
2914popup_grab_cancel(struct weston_pointer_grab *grab)
2915{
2916 popup_grab_end(grab->pointer);
2917}
2918
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002919static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002920 popup_grab_focus,
2921 popup_grab_motion,
2922 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002923 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002924};
2925
2926static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002927shell_surface_send_popup_done(struct shell_surface *shsurf)
2928{
2929 if (shell_surface_is_wl_shell_surface(shsurf))
2930 wl_shell_surface_send_popup_done(shsurf->resource);
2931 else if (shell_surface_is_xdg_popup(shsurf))
2932 xdg_popup_send_popup_done(shsurf->resource,
2933 shsurf->popup.serial);
2934}
2935
2936static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002937popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002938{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002939 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002940 struct shell_seat *shseat =
2941 container_of(grab, struct shell_seat, popup_grab.grab);
2942 struct shell_surface *shsurf;
2943 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002944
2945 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002946 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002947 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002948 shseat->popup_grab.grab.interface = NULL;
2949 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002950 /* Send the popup_done event to all the popups open */
2951 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002952 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002953 shsurf->popup.shseat = NULL;
2954 if (prev) {
2955 wl_list_init(&prev->popup.grab_link);
2956 }
2957 prev = shsurf;
2958 }
2959 wl_list_init(&prev->popup.grab_link);
2960 wl_list_init(&shseat->popup_grab.surfaces_list);
2961 }
2962}
2963
2964static void
2965add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2966{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002967 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002968
2969 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002970 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002971 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002972 /* We must make sure here that this popup was opened after
2973 * a mouse press, and not just by moving around with other
2974 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002975 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002976 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002977
Rob Bradforddfe31052013-06-26 19:49:11 +01002978 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002979 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002980 } else {
2981 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002982 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002983}
2984
2985static void
2986remove_popup_grab(struct shell_surface *shsurf)
2987{
2988 struct shell_seat *shseat = shsurf->popup.shseat;
2989
2990 wl_list_remove(&shsurf->popup.grab_link);
2991 wl_list_init(&shsurf->popup.grab_link);
2992 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002993 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002994 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002995 }
2996}
2997
2998static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002999shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003000{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003001 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003002 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003003
Jason Ekstranda7af7042013-10-12 22:38:11 -05003004 shsurf->surface->output = parent_view->output;
3005 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 weston_view_set_transform_parent(shsurf->view, parent_view);
3008 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3009 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003010
Kristian Høgsberge3148752013-05-06 23:19:49 -04003011 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003012 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003013 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003014 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003015 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003016 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003017}
3018
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003019static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003020 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003021 shell_surface_move,
3022 shell_surface_resize,
3023 shell_surface_set_toplevel,
3024 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003025 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003026 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003027 shell_surface_set_maximized,
3028 shell_surface_set_title,
3029 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003030};
3031
3032static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003033destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003034{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003035 struct shell_surface *child, *next;
3036
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003037 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3038
Giulio Camuffo5085a752013-03-25 21:42:45 +01003039 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3040 remove_popup_grab(shsurf);
3041 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003042
Alex Wubd3354b2012-04-17 17:20:49 +08003043 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003044 shell_surface_is_top_fullscreen(shsurf))
3045 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003046
Jason Ekstranda7af7042013-10-12 22:38:11 -05003047 if (shsurf->fullscreen.black_view)
3048 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003049
Alex Wubd3354b2012-04-17 17:20:49 +08003050 /* As destroy_resource() use wl_list_for_each_safe(),
3051 * we can always remove the listener.
3052 */
3053 wl_list_remove(&shsurf->surface_destroy_listener.link);
3054 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003055 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003056
Jason Ekstranda7af7042013-10-12 22:38:11 -05003057 weston_view_destroy(shsurf->view);
3058
Philip Withnall648a4dd2013-11-25 18:01:44 +00003059 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003060 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3061 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003062
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003063 wl_list_remove(&shsurf->link);
3064 free(shsurf);
3065}
3066
3067static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003068shell_destroy_shell_surface(struct wl_resource *resource)
3069{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003070 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003071
Bryan Caina46b9462014-04-04 17:41:24 -05003072 if (!wl_list_empty(&shsurf->popup.grab_link))
3073 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003074 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003075}
3076
3077static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003078shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003079{
3080 struct shell_surface *shsurf = container_of(listener,
3081 struct shell_surface,
3082 surface_destroy_listener);
3083
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003084 if (shsurf->resource)
3085 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003086
3087 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003088}
3089
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003090static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003091fade_out_done(struct weston_view_animation *animation, void *data)
3092{
3093 struct shell_surface *shsurf = data;
3094
3095 weston_surface_destroy(shsurf->surface);
3096}
3097
3098static void
3099handle_resource_destroy(struct wl_listener *listener, void *data)
3100{
3101 struct shell_surface *shsurf =
3102 container_of(listener, struct shell_surface,
3103 resource_destroy_listener);
3104
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003105 if (!weston_surface_is_mapped(shsurf->surface))
3106 return;
3107
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003108 shsurf->surface->ref_count++;
3109
3110 pixman_region32_fini(&shsurf->surface->pending.input);
3111 pixman_region32_init(&shsurf->surface->pending.input);
3112 pixman_region32_fini(&shsurf->surface->input);
3113 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003114 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3115 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003116}
3117
3118static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003119shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003120
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003121struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003122get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003123{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003124 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003125 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003126 else
3127 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003128}
3129
Rafael Antognollie2a34552013-12-03 15:35:45 -02003130static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003131create_common_surface(struct shell_client *owner, void *shell,
3132 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003133 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003134{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003135 struct shell_surface *shsurf;
3136
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003137 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003138 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003139 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003140 }
3141
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003142 shsurf = calloc(1, sizeof *shsurf);
3143 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003144 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003145 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003146 }
3147
Jason Ekstranda7af7042013-10-12 22:38:11 -05003148 shsurf->view = weston_view_create(surface);
3149 if (!shsurf->view) {
3150 weston_log("no memory to allocate shell surface\n");
3151 free(shsurf);
3152 return NULL;
3153 }
3154
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003155 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003156 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003157
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003158 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3159 wl_resource_add_destroy_listener(surface->resource,
3160 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003161 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003162
Tiago Vignattibc052c92012-04-19 16:18:18 +03003163 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003164 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003165 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003166 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003167 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003168 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003169 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3170 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003171 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003172 wl_list_init(&shsurf->fullscreen.transform.link);
3173
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003174 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003175 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003176 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003177 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003178
3179 /* init link so its safe to always remove it in destroy_shell_surface */
3180 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003181 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003182
Pekka Paalanen460099f2012-01-20 16:48:25 +02003183 /* empty when not in use */
3184 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003185 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003186
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003187 wl_list_init(&shsurf->workspace_transform.link);
3188
Philip Withnall648a4dd2013-11-25 18:01:44 +00003189 wl_list_init(&shsurf->children_link);
3190 wl_list_init(&shsurf->children_list);
3191 shsurf->parent = NULL;
3192
Pekka Paalanen98262232011-12-01 10:42:22 +02003193 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003194
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003195 shsurf->client = client;
3196
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003197 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003198}
3199
Rafael Antognollie2a34552013-12-03 15:35:45 -02003200static struct shell_surface *
3201create_shell_surface(void *shell, struct weston_surface *surface,
3202 const struct weston_shell_client *client)
3203{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003204 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003205}
3206
Jason Ekstranda7af7042013-10-12 22:38:11 -05003207static struct weston_view *
3208get_primary_view(void *shell, struct shell_surface *shsurf)
3209{
3210 return shsurf->view;
3211}
3212
Tiago Vignattibc052c92012-04-19 16:18:18 +03003213static void
3214shell_get_shell_surface(struct wl_client *client,
3215 struct wl_resource *resource,
3216 uint32_t id,
3217 struct wl_resource *surface_resource)
3218{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003219 struct weston_surface *surface =
3220 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003221 struct shell_client *sc = wl_resource_get_user_data(resource);
3222 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003223 struct shell_surface *shsurf;
3224
3225 if (get_shell_surface(surface)) {
3226 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003227 WL_DISPLAY_ERROR_INVALID_OBJECT,
3228 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003229 return;
3230 }
3231
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003232 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003233 if (!shsurf) {
3234 wl_resource_post_error(surface_resource,
3235 WL_DISPLAY_ERROR_INVALID_OBJECT,
3236 "surface->configure already set");
3237 return;
3238 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003239
Jason Ekstranda85118c2013-06-27 20:17:02 -05003240 shsurf->resource =
3241 wl_resource_create(client,
3242 &wl_shell_surface_interface, 1, id);
3243 wl_resource_set_implementation(shsurf->resource,
3244 &shell_surface_implementation,
3245 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003246}
3247
Rafael Antognollie2a34552013-12-03 15:35:45 -02003248static bool
3249shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3250{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003251 /* A shell surface without a resource is created from xwayland
3252 * and is considered a wl_shell surface for now. */
3253
3254 return shsurf->resource == NULL ||
3255 wl_resource_instance_of(shsurf->resource,
3256 &wl_shell_surface_interface,
3257 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003258}
3259
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003260static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003261 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003262};
3263
Rafael Antognollie2a34552013-12-03 15:35:45 -02003264/****************************
3265 * xdg-shell implementation */
3266
3267static void
3268xdg_surface_destroy(struct wl_client *client,
3269 struct wl_resource *resource)
3270{
3271 wl_resource_destroy(resource);
3272}
3273
3274static void
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003275xdg_surface_set_transient_for(struct wl_client *client,
3276 struct wl_resource *resource,
3277 struct wl_resource *parent_resource)
3278{
3279 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3280 struct weston_surface *parent;
3281
3282 if (parent_resource)
3283 parent = wl_resource_get_user_data(parent_resource);
3284 else
3285 parent = NULL;
3286
3287 shell_surface_set_parent(shsurf, parent);
3288}
3289
3290static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003291xdg_surface_set_margin(struct wl_client *client,
3292 struct wl_resource *resource,
3293 int32_t left,
3294 int32_t right,
3295 int32_t top,
3296 int32_t bottom)
3297{
3298 /* Do nothing, Weston doesn't try to constrain or place
3299 * surfaces in any special manner... */
3300}
3301
3302static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003303xdg_surface_set_app_id(struct wl_client *client,
3304 struct wl_resource *resource,
3305 const char *app_id)
3306{
3307 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3308
3309 free(shsurf->class);
3310 shsurf->class = strdup(app_id);
3311}
3312
3313static void
3314xdg_surface_set_title(struct wl_client *client,
3315 struct wl_resource *resource, const char *title)
3316{
3317 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3318
3319 set_title(shsurf, title);
3320}
3321
3322static void
3323xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3324 struct wl_resource *seat_resource, uint32_t serial)
3325{
3326 common_surface_move(resource, seat_resource, serial);
3327}
3328
3329static void
3330xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3331 struct wl_resource *seat_resource, uint32_t serial,
3332 uint32_t edges)
3333{
3334 common_surface_resize(resource, seat_resource, serial, edges);
3335}
3336
3337static void
3338xdg_surface_set_output(struct wl_client *client,
3339 struct wl_resource *resource,
3340 struct wl_resource *output_resource)
3341{
3342 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3343 struct weston_output *output;
3344
3345 if (output_resource)
3346 output = wl_resource_get_user_data(output_resource);
3347 else
3348 output = NULL;
3349
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003350 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003351}
3352
3353static void
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003354xdg_surface_change_state(struct shell_surface *shsurf,
3355 uint32_t state, uint32_t value, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003356{
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003357 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003358 shsurf->state_requested = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003359
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003360 switch (state) {
3361 case XDG_SURFACE_STATE_MAXIMIZED:
3362 shsurf->requested_state.maximized = value;
3363 if (value)
3364 set_maximized(shsurf, NULL);
3365 break;
3366 case XDG_SURFACE_STATE_FULLSCREEN:
3367 shsurf->requested_state.fullscreen = value;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003368
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003369 if (value)
3370 set_fullscreen(shsurf,
3371 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3372 0, shsurf->recommended_output);
3373 break;
3374 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003375}
3376
3377static void
3378xdg_surface_request_change_state(struct wl_client *client,
3379 struct wl_resource *resource,
3380 uint32_t state,
3381 uint32_t value,
3382 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003383{
3384 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003385
3386 /* The client can't know what the current state is, so we need
3387 to always send a state change in response. */
Rafael Antognollie2a34552013-12-03 15:35:45 -02003388
3389 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3390 return;
3391
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003392 switch (state) {
3393 case XDG_SURFACE_STATE_MAXIMIZED:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003394 case XDG_SURFACE_STATE_FULLSCREEN:
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003395 break;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003396 default:
3397 /* send error? ignore? send change state with value 0? */
3398 return;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003399 }
Kristian Høgsberg283bf372014-02-18 23:28:09 -08003400
3401 xdg_surface_change_state(shsurf, state, value, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003402}
3403
3404static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003405xdg_surface_ack_change_state(struct wl_client *client,
3406 struct wl_resource *resource,
3407 uint32_t state,
3408 uint32_t value,
3409 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003410{
3411 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3412
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003413 if (shsurf->state_requested) {
3414 shsurf->next_state = shsurf->requested_state;
3415 shsurf->state_changed = true;
3416 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003417 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003418}
3419
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003420static void
3421xdg_surface_set_minimized(struct wl_client *client,
3422 struct wl_resource *resource)
3423{
3424 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3425
3426 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3427 return;
3428
3429 /* apply compositor's own minimization logic (hide) */
3430 set_minimized(shsurf->surface, 1);
3431}
3432
Rafael Antognollie2a34552013-12-03 15:35:45 -02003433static const struct xdg_surface_interface xdg_surface_implementation = {
3434 xdg_surface_destroy,
3435 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003436 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003437 xdg_surface_set_title,
3438 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003439 xdg_surface_move,
3440 xdg_surface_resize,
3441 xdg_surface_set_output,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003442 xdg_surface_request_change_state,
3443 xdg_surface_ack_change_state,
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003444 xdg_surface_set_minimized
Rafael Antognollie2a34552013-12-03 15:35:45 -02003445};
3446
3447static void
3448xdg_send_configure(struct weston_surface *surface,
3449 uint32_t edges, int32_t width, int32_t height)
3450{
3451 struct shell_surface *shsurf = get_shell_surface(surface);
3452
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003453 assert(shsurf);
3454
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003455 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003456}
3457
3458static const struct weston_shell_client xdg_client = {
3459 xdg_send_configure
3460};
3461
3462static void
3463xdg_use_unstable_version(struct wl_client *client,
3464 struct wl_resource *resource,
3465 int32_t version)
3466{
3467 if (version > 1) {
3468 wl_resource_post_error(resource,
3469 1,
3470 "xdg-shell:: version not implemented yet.");
3471 return;
3472 }
3473}
3474
3475static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003476create_xdg_surface(struct shell_client *owner, void *shell,
3477 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003478 const struct weston_shell_client *client)
3479{
3480 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003481
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003482 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003483 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003484
3485 return shsurf;
3486}
3487
3488static void
3489xdg_get_xdg_surface(struct wl_client *client,
3490 struct wl_resource *resource,
3491 uint32_t id,
3492 struct wl_resource *surface_resource)
3493{
3494 struct weston_surface *surface =
3495 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003496 struct shell_client *sc = wl_resource_get_user_data(resource);
3497 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003498 struct shell_surface *shsurf;
3499
3500 if (get_shell_surface(surface)) {
3501 wl_resource_post_error(surface_resource,
3502 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003503 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003504 return;
3505 }
3506
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003507 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003508 if (!shsurf) {
3509 wl_resource_post_error(surface_resource,
3510 WL_DISPLAY_ERROR_INVALID_OBJECT,
3511 "surface->configure already set");
3512 return;
3513 }
3514
3515 shsurf->resource =
3516 wl_resource_create(client,
3517 &xdg_surface_interface, 1, id);
3518 wl_resource_set_implementation(shsurf->resource,
3519 &xdg_surface_implementation,
3520 shsurf, shell_destroy_shell_surface);
3521}
3522
3523static bool
3524shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3525{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003526 return shsurf->resource &&
3527 wl_resource_instance_of(shsurf->resource,
3528 &xdg_surface_interface,
3529 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003530}
3531
3532/* xdg-popup implementation */
3533
3534static void
3535xdg_popup_destroy(struct wl_client *client,
3536 struct wl_resource *resource)
3537{
3538 wl_resource_destroy(resource);
3539}
3540
Rafael Antognollie2a34552013-12-03 15:35:45 -02003541static const struct xdg_popup_interface xdg_popup_implementation = {
3542 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003543};
3544
3545static void
3546xdg_popup_send_configure(struct weston_surface *surface,
3547 uint32_t edges, int32_t width, int32_t height)
3548{
3549}
3550
3551static const struct weston_shell_client xdg_popup_client = {
3552 xdg_popup_send_configure
3553};
3554
3555static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003556create_xdg_popup(struct shell_client *owner, void *shell,
3557 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003558 const struct weston_shell_client *client,
3559 struct weston_surface *parent,
3560 struct shell_seat *seat,
3561 uint32_t serial,
3562 int32_t x, int32_t y)
3563{
3564 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003565
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003566 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003567 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003568 shsurf->popup.shseat = seat;
3569 shsurf->popup.serial = serial;
3570 shsurf->popup.x = x;
3571 shsurf->popup.y = y;
3572 shell_surface_set_parent(shsurf, parent);
3573
3574 return shsurf;
3575}
3576
3577static void
3578xdg_get_xdg_popup(struct wl_client *client,
3579 struct wl_resource *resource,
3580 uint32_t id,
3581 struct wl_resource *surface_resource,
3582 struct wl_resource *parent_resource,
3583 struct wl_resource *seat_resource,
3584 uint32_t serial,
3585 int32_t x, int32_t y, uint32_t flags)
3586{
3587 struct weston_surface *surface =
3588 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003589 struct shell_client *sc = wl_resource_get_user_data(resource);
3590 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003591 struct shell_surface *shsurf;
3592 struct weston_surface *parent;
3593 struct shell_seat *seat;
3594
3595 if (get_shell_surface(surface)) {
3596 wl_resource_post_error(surface_resource,
3597 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003598 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003599 return;
3600 }
3601
3602 if (!parent_resource) {
3603 wl_resource_post_error(surface_resource,
3604 WL_DISPLAY_ERROR_INVALID_OBJECT,
3605 "xdg_shell::get_xdg_popup requires a parent shell surface");
3606 }
3607
3608 parent = wl_resource_get_user_data(parent_resource);
3609 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3610
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003611 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003612 parent, seat, serial, x, y);
3613 if (!shsurf) {
3614 wl_resource_post_error(surface_resource,
3615 WL_DISPLAY_ERROR_INVALID_OBJECT,
3616 "surface->configure already set");
3617 return;
3618 }
3619
3620 shsurf->resource =
3621 wl_resource_create(client,
3622 &xdg_popup_interface, 1, id);
3623 wl_resource_set_implementation(shsurf->resource,
3624 &xdg_popup_implementation,
3625 shsurf, shell_destroy_shell_surface);
3626}
3627
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003628static void
3629xdg_pong(struct wl_client *client,
3630 struct wl_resource *resource, uint32_t serial)
3631{
3632 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003633
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003634 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003635}
3636
Rafael Antognollie2a34552013-12-03 15:35:45 -02003637static bool
3638shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3639{
3640 return wl_resource_instance_of(shsurf->resource,
3641 &xdg_popup_interface,
3642 &xdg_popup_implementation);
3643}
3644
3645static const struct xdg_shell_interface xdg_implementation = {
3646 xdg_use_unstable_version,
3647 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003648 xdg_get_xdg_popup,
3649 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003650};
3651
3652static int
3653xdg_shell_unversioned_dispatch(const void *implementation,
3654 void *_target, uint32_t opcode,
3655 const struct wl_message *message,
3656 union wl_argument *args)
3657{
3658 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003659 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003660
3661 if (opcode != 0) {
3662 wl_resource_post_error(resource,
3663 WL_DISPLAY_ERROR_INVALID_OBJECT,
3664 "must call use_unstable_version first");
3665 return 0;
3666 }
3667
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003668#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003669
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003670 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3671 "shell implementation doesn't match protocol version");
3672
Rafael Antognollie2a34552013-12-03 15:35:45 -02003673 if (args[0].i != XDG_SERVER_VERSION) {
3674 wl_resource_post_error(resource,
3675 WL_DISPLAY_ERROR_INVALID_OBJECT,
3676 "incompatible version, server is %d "
3677 "client wants %d",
3678 XDG_SERVER_VERSION, args[0].i);
3679 return 0;
3680 }
3681
3682 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003683 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003684
3685 return 1;
3686}
3687
3688/* end of xdg-shell implementation */
3689/***********************************/
3690
Kristian Høgsberg07937562011-04-12 17:25:42 -04003691static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003692shell_fade(struct desktop_shell *shell, enum fade_type type);
3693
3694static int
3695screensaver_timeout(void *data)
3696{
3697 struct desktop_shell *shell = data;
3698
3699 shell_fade(shell, FADE_OUT);
3700
3701 return 1;
3702}
3703
3704static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003705handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003706{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003707 struct desktop_shell *shell =
3708 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003709
Pekka Paalanen18027e52011-12-02 16:31:49 +02003710 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003711
3712 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003713 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003714}
3715
3716static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003717launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003718{
3719 if (shell->screensaver.binding)
3720 return;
3721
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003722 if (!shell->screensaver.path) {
3723 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003724 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003725 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003726
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003727 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003728 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003729 return;
3730 }
3731
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003732 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003733 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003734 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003735 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003736}
3737
3738static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003739terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003740{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003741 if (shell->screensaver.process.pid == 0)
3742 return;
3743
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003744 /* Disarm the screensaver timer, otherwise it may fire when the
3745 * compositor is not in the idle state. In that case, the screen will
3746 * be locked, but the wake_signal won't fire on user input, making the
3747 * system unresponsive. */
3748 wl_event_source_timer_update(shell->screensaver.timer, 0);
3749
Pekka Paalanen18027e52011-12-02 16:31:49 +02003750 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003751}
3752
3753static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003754configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003755{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003756 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003757
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3759 if (v->output == ev->output && v != ev) {
3760 weston_view_unmap(v);
3761 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003762 }
3763 }
3764
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003765 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003766
Jason Ekstranda7af7042013-10-12 22:38:11 -05003767 if (wl_list_empty(&ev->layer_link)) {
3768 wl_list_insert(&layer->view_list, &ev->layer_link);
3769 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003770 }
3771}
3772
3773static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003774background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003775{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003776 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003777 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003778
Jason Ekstranda7af7042013-10-12 22:38:11 -05003779 view = container_of(es->views.next, struct weston_view, surface_link);
3780
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003781 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003782}
3783
3784static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003785desktop_shell_set_background(struct wl_client *client,
3786 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003787 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003788 struct wl_resource *surface_resource)
3789{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003790 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003791 struct weston_surface *surface =
3792 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003793 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003794
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003795 if (surface->configure) {
3796 wl_resource_post_error(surface_resource,
3797 WL_DISPLAY_ERROR_INVALID_OBJECT,
3798 "surface role already assigned");
3799 return;
3800 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003801
Jason Ekstranda7af7042013-10-12 22:38:11 -05003802 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3803 weston_view_destroy(view);
3804 view = weston_view_create(surface);
3805
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003806 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003807 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003808 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003809 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003810 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003811 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003812 surface->output->width,
3813 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003814}
3815
3816static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003817panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003818{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003819 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003820 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003821
Jason Ekstranda7af7042013-10-12 22:38:11 -05003822 view = container_of(es->views.next, struct weston_view, surface_link);
3823
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003824 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003825}
3826
3827static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003828desktop_shell_set_panel(struct wl_client *client,
3829 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003830 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003831 struct wl_resource *surface_resource)
3832{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003833 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003834 struct weston_surface *surface =
3835 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003836 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003837
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003838 if (surface->configure) {
3839 wl_resource_post_error(surface_resource,
3840 WL_DISPLAY_ERROR_INVALID_OBJECT,
3841 "surface role already assigned");
3842 return;
3843 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003844
Jason Ekstranda7af7042013-10-12 22:38:11 -05003845 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3846 weston_view_destroy(view);
3847 view = weston_view_create(surface);
3848
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003849 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003850 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003851 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003852 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003853 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003854 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003855 surface->output->width,
3856 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003857}
3858
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003859static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003860lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003861{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003862 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003863 struct weston_view *view;
3864
3865 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003866
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003867 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003868 return;
3869
Jason Ekstranda7af7042013-10-12 22:38:11 -05003870 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003871
3872 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003873 wl_list_insert(&shell->lock_layer.view_list,
3874 &view->layer_link);
3875 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003876 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003877 }
3878}
3879
3880static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003881handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003882{
Tiago Vignattibe143262012-04-16 17:31:41 +03003883 struct desktop_shell *shell =
3884 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003885
Martin Minarik6d118362012-06-07 18:01:59 +02003886 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003887 shell->lock_surface = NULL;
3888}
3889
3890static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003891desktop_shell_set_lock_surface(struct wl_client *client,
3892 struct wl_resource *resource,
3893 struct wl_resource *surface_resource)
3894{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003895 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003896 struct weston_surface *surface =
3897 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003898
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003899 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003900
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003901 if (!shell->locked)
3902 return;
3903
Pekka Paalanen98262232011-12-01 10:42:22 +02003904 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003905
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003906 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003907 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003908 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003909
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003910 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003911 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003912 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003913}
3914
3915static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003916resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003917{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003918 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003919
Pekka Paalanen77346a62011-11-30 16:26:35 +02003920 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003921
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003922 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003923 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003924 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003925 &shell->input_panel_layer.link);
3926 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003927 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003928 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003929 wl_list_insert(&shell->compositor->cursor_layer.link,
3930 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003931 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003932 wl_list_insert(&shell->fullscreen_layer.link,
3933 &shell->panel_layer.link);
3934 wl_list_insert(&shell->panel_layer.link,
3935 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003936
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003937 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003938
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003939 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003940 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003941 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003942}
3943
3944static void
3945desktop_shell_unlock(struct wl_client *client,
3946 struct wl_resource *resource)
3947{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003948 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003949
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003950 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003951
3952 if (shell->locked)
3953 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003954}
3955
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003956static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003957desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003958 struct wl_resource *resource,
3959 struct wl_resource *surface_resource)
3960{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003961 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003962
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003963 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003964 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003965}
3966
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003967static void
3968desktop_shell_desktop_ready(struct wl_client *client,
3969 struct wl_resource *resource)
3970{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003971 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003972
3973 shell_fade_startup(shell);
3974}
3975
Kristian Høgsberg75840622011-09-06 13:48:16 -04003976static const struct desktop_shell_interface desktop_shell_implementation = {
3977 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003978 desktop_shell_set_panel,
3979 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003980 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003981 desktop_shell_set_grab_surface,
3982 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003983};
3984
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003985static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003986get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003987{
3988 struct shell_surface *shsurf;
3989
3990 shsurf = get_shell_surface(surface);
3991 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003992 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003993 return shsurf->type;
3994}
3995
Kristian Høgsberg75840622011-09-06 13:48:16 -04003996static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003997move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003998{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003999 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004000 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004001 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004002
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004003 if (seat->pointer->focus == NULL)
4004 return;
4005
4006 focus = seat->pointer->focus->surface;
4007
Pekka Paalanen01388e22013-04-25 13:57:44 +03004008 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004009 if (surface == NULL)
4010 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004011
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004012 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004013 if (shsurf == NULL || shsurf->state.fullscreen ||
4014 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004015 return;
4016
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004017 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004018}
4019
4020static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004021maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4022{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004023 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004024 struct weston_surface *surface;
4025 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004026 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004027
4028 surface = weston_surface_get_main_surface(focus);
4029 if (surface == NULL)
4030 return;
4031
4032 shsurf = get_shell_surface(surface);
4033 if (shsurf == NULL)
4034 return;
4035
4036 if (!shell_surface_is_xdg_surface(shsurf))
4037 return;
4038
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004039 serial = wl_display_next_serial(seat->compositor->wl_display);
4040 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4041 !shsurf->state.maximized, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004042}
4043
4044static void
4045fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4046{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004047 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004048 struct weston_surface *surface;
4049 struct shell_surface *shsurf;
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004050 uint32_t serial;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004051
4052 surface = weston_surface_get_main_surface(focus);
4053 if (surface == NULL)
4054 return;
4055
4056 shsurf = get_shell_surface(surface);
4057 if (shsurf == NULL)
4058 return;
4059
4060 if (!shell_surface_is_xdg_surface(shsurf))
4061 return;
4062
Kristian Høgsberg283bf372014-02-18 23:28:09 -08004063 serial = wl_display_next_serial(seat->compositor->wl_display);
4064 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4065 !shsurf->state.fullscreen, serial);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004066}
4067
4068static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004069touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4070{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004071 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004072 struct weston_surface *surface;
4073 struct shell_surface *shsurf;
4074
4075 surface = weston_surface_get_main_surface(focus);
4076 if (surface == NULL)
4077 return;
4078
4079 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004080 if (shsurf == NULL || shsurf->state.fullscreen ||
4081 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004082 return;
4083
4084 surface_touch_move(shsurf, (struct weston_seat *) seat);
4085}
4086
4087static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004088resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004089{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004090 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004091 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004092 uint32_t edges = 0;
4093 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004094 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004095
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004096 if (seat->pointer->focus == NULL)
4097 return;
4098
4099 focus = seat->pointer->focus->surface;
4100
Pekka Paalanen01388e22013-04-25 13:57:44 +03004101 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004102 if (surface == NULL)
4103 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004104
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004105 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004106 if (shsurf == NULL || shsurf->state.fullscreen ||
4107 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004108 return;
4109
Jason Ekstranda7af7042013-10-12 22:38:11 -05004110 weston_view_from_global(shsurf->view,
4111 wl_fixed_to_int(seat->pointer->grab_x),
4112 wl_fixed_to_int(seat->pointer->grab_y),
4113 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004114
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004115 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004116 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004117 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004118 edges |= 0;
4119 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004120 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004121
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004122 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004123 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004124 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004125 edges |= 0;
4126 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004127 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004128
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004129 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004130}
4131
4132static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004133surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004134 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004135{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004136 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004137 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004138 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004139 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004140
Pekka Paalanen01388e22013-04-25 13:57:44 +03004141 /* XXX: broken for windows containing sub-surfaces */
4142 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004143 if (surface == NULL)
4144 return;
4145
4146 shsurf = get_shell_surface(surface);
4147 if (!shsurf)
4148 return;
4149
Jason Ekstranda7af7042013-10-12 22:38:11 -05004150 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004151
Jason Ekstranda7af7042013-10-12 22:38:11 -05004152 if (shsurf->view->alpha > 1.0)
4153 shsurf->view->alpha = 1.0;
4154 if (shsurf->view->alpha < step)
4155 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004156
Jason Ekstranda7af7042013-10-12 22:38:11 -05004157 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004158 weston_surface_damage(surface);
4159}
4160
4161static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004162do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004163 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004164{
Daniel Stone37816df2012-05-16 18:45:18 +01004165 struct weston_seat *ws = (struct weston_seat *) seat;
4166 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004167 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004168 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004169
4170 wl_list_for_each(output, &compositor->output_list, link) {
4171 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004172 wl_fixed_to_double(seat->pointer->x),
4173 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004174 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004175 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004176 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004177 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004178 increment = -output->zoom.increment;
4179 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004180 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004181 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004182 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004183 else
4184 increment = 0;
4185
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004186 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004187
Scott Moreaue6603982012-06-11 13:07:51 -06004188 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004189 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004190 else if (output->zoom.level > output->zoom.max_level)
4191 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004192 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004193 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004194 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004195
Scott Moreaue6603982012-06-11 13:07:51 -06004196 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004197
Jason Ekstranda7af7042013-10-12 22:38:11 -05004198 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004199 }
4200 }
4201}
4202
Scott Moreauccbf29d2012-02-22 14:21:41 -07004203static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004204zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004205 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004206{
4207 do_zoom(seat, time, 0, axis, value);
4208}
4209
4210static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004211zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004212 void *data)
4213{
4214 do_zoom(seat, time, key, 0, 0);
4215}
4216
4217static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004218terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004219 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004220{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004221 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004222
Daniel Stone325fc2d2012-05-30 16:31:58 +01004223 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004224}
4225
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004226static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004227rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4228 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004229{
4230 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004231 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004232 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004233 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004234 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004235
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004236 weston_pointer_move(pointer, x, y);
4237
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004238 if (!shsurf)
4239 return;
4240
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004241 cx = 0.5f * shsurf->surface->width;
4242 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004243
Daniel Stone37816df2012-05-16 18:45:18 +01004244 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4245 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004246 r = sqrtf(dx * dx + dy * dy);
4247
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004248 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004250
4251 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004252 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004253 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004254
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004255 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004256 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004257
4258 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004259 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004260 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004261 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004262 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004263
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004264 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004266 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004267 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004268 wl_list_init(&shsurf->rotation.transform.link);
4269 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004270 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004271 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004272
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004273 /* We need to adjust the position of the surface
4274 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004275 cposx = shsurf->view->geometry.x + cx;
4276 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004277 dposx = rotate->center.x - cposx;
4278 dposy = rotate->center.y - cposy;
4279 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 weston_view_set_position(shsurf->view,
4281 shsurf->view->geometry.x + dposx,
4282 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004283 }
4284
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004285 /* Repaint implies weston_surface_update_transform(), which
4286 * lazily applies the damage due to rotation update.
4287 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004288 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004289}
4290
4291static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004292rotate_grab_button(struct weston_pointer_grab *grab,
4293 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004294{
4295 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004296 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004297 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004298 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004299 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004300
Daniel Stone4dbadb12012-05-30 16:31:51 +01004301 if (pointer->button_count == 0 &&
4302 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004303 if (shsurf)
4304 weston_matrix_multiply(&shsurf->rotation.rotation,
4305 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004306 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004307 free(rotate);
4308 }
4309}
4310
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004311static void
4312rotate_grab_cancel(struct weston_pointer_grab *grab)
4313{
4314 struct rotate_grab *rotate =
4315 container_of(grab, struct rotate_grab, base.grab);
4316
4317 shell_grab_end(&rotate->base);
4318 free(rotate);
4319}
4320
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004321static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004322 noop_grab_focus,
4323 rotate_grab_motion,
4324 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004325 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004326};
4327
4328static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004329surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004330{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004331 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004332 float dx, dy;
4333 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004334
Pekka Paalanen460099f2012-01-20 16:48:25 +02004335 rotate = malloc(sizeof *rotate);
4336 if (!rotate)
4337 return;
4338
Jason Ekstranda7af7042013-10-12 22:38:11 -05004339 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004340 surface->surface->width * 0.5f,
4341 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004343
Daniel Stone37816df2012-05-16 18:45:18 +01004344 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4345 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004346 r = sqrtf(dx * dx + dy * dy);
4347 if (r > 20.0f) {
4348 struct weston_matrix inverse;
4349
4350 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004351 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004352 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004353
4354 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004355 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004356 } else {
4357 weston_matrix_init(&surface->rotation.rotation);
4358 weston_matrix_init(&rotate->rotation);
4359 }
4360
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004361 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4362 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004363}
4364
4365static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004366rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004367 void *data)
4368{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004369 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004370 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004371 struct shell_surface *surface;
4372
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004373 if (seat->pointer->focus == NULL)
4374 return;
4375
4376 focus = seat->pointer->focus->surface;
4377
Pekka Paalanen01388e22013-04-25 13:57:44 +03004378 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004379 if (base_surface == NULL)
4380 return;
4381
4382 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004383 if (surface == NULL || surface->state.fullscreen ||
4384 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004385 return;
4386
4387 surface_rotate(surface, seat);
4388}
4389
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004390/* Move all fullscreen layers down to the current workspace in a non-reversible
4391 * manner. This should be used when implementing shell-wide overlays, such as
4392 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004393void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004394lower_fullscreen_layer(struct desktop_shell *shell)
4395{
4396 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004397 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004398
4399 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 wl_list_for_each_reverse_safe(view, prev,
4401 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004402 layer_link) {
4403 wl_list_remove(&view->layer_link);
4404 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4405 weston_view_damage_below(view);
4406 weston_surface_damage(view->surface);
4407 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004408}
4409
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004410void
Tiago Vignattibe143262012-04-16 17:31:41 +03004411activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004412 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004413{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004414 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004415 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004416 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004417 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004418 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004419
Pekka Paalanen01388e22013-04-25 13:57:44 +03004420 main_surface = weston_surface_get_main_surface(es);
4421
Daniel Stone37816df2012-05-16 18:45:18 +01004422 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004423
Jonas Ådahl8538b222012-08-29 22:13:03 +02004424 state = ensure_focus_state(shell, seat);
4425 if (state == NULL)
4426 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004427
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004428 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004429 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004430
Rafael Antognolli03b16592013-12-03 15:35:42 -02004431 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004432 assert(shsurf);
4433
Rafael Antognolli03b16592013-12-03 15:35:42 -02004434 if (shsurf->state.fullscreen)
4435 shell_configure_fullscreen(shsurf);
4436 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004437 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004438
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004439 /* Update the surface’s layer. This brings it to the top of the stacking
4440 * order as appropriate. */
4441 shell_surface_update_layer(shsurf);
4442
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004443 if (shell->focus_animation_type != ANIMATION_NONE) {
4444 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004445 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004446 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004447}
4448
Alex Wu21858432012-04-01 20:13:08 +08004449/* no-op func for checking black surface */
4450static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004451black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004452{
4453}
4454
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004455static bool
Alex Wu21858432012-04-01 20:13:08 +08004456is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4457{
4458 if (es->configure == black_surface_configure) {
4459 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004460 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004461 return true;
4462 }
4463 return false;
4464}
4465
Kristian Høgsberg75840622011-09-06 13:48:16 -04004466static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004467activate_binding(struct weston_seat *seat,
4468 struct desktop_shell *shell,
4469 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004470{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004471 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004472
Alex Wu9c35e6b2012-03-05 14:13:13 +08004473 if (!focus)
4474 return;
4475
Pekka Paalanen01388e22013-04-25 13:57:44 +03004476 if (is_black_surface(focus, &main_surface))
4477 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004478
Pekka Paalanen01388e22013-04-25 13:57:44 +03004479 main_surface = weston_surface_get_main_surface(focus);
4480 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004481 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004482
Neil Robertsa28c6932013-10-03 16:43:04 +01004483 activate(shell, focus, seat);
4484}
4485
4486static void
4487click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4488 void *data)
4489{
4490 if (seat->pointer->grab != &seat->pointer->default_grab)
4491 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004492 if (seat->pointer->focus == NULL)
4493 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004494
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004495 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004496}
4497
4498static void
4499touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4500{
4501 if (seat->touch->grab != &seat->touch->default_grab)
4502 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004503 if (seat->touch->focus == NULL)
4504 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004505
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004506 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004507}
4508
4509static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004510unfocus_all_seats(struct desktop_shell *shell)
4511{
4512 struct weston_seat *seat, *next;
4513
4514 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4515 if (seat->keyboard == NULL)
4516 continue;
4517
4518 weston_keyboard_set_focus(seat->keyboard, NULL);
4519 }
4520}
4521
4522static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004523lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004524{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004525 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004526
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004527 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004528 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004529 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004530 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004531
4532 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004533
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004534 /* Hide all surfaces by removing the fullscreen, panel and
4535 * toplevel layers. This way nothing else can show or receive
4536 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004537
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004538 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004539 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004540 if (shell->showing_input_panels)
4541 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004542 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004543 wl_list_insert(&shell->compositor->cursor_layer.link,
4544 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004545
Pekka Paalanen77346a62011-11-30 16:26:35 +02004546 launch_screensaver(shell);
4547
Neil Robertsb4a91702014-04-09 16:33:32 +01004548 /* Remove the keyboard focus on all seats. This will be
4549 * restored to the workspace's saved state via
4550 * restore_focus_state when the compositor is unlocked */
4551 unfocus_all_seats(shell);
4552
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004553 /* TODO: disable bindings that should not work while locked. */
4554
4555 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004556}
4557
4558static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004559unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004560{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004561 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004562 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004563 return;
4564 }
4565
4566 /* If desktop-shell client has gone away, unlock immediately. */
4567 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004568 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004569 return;
4570 }
4571
4572 if (shell->prepare_event_sent)
4573 return;
4574
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004575 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004576 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004577}
4578
4579static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004580shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004581{
4582 struct desktop_shell *shell = data;
4583
4584 shell->fade.animation = NULL;
4585
4586 switch (shell->fade.type) {
4587 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004588 weston_surface_destroy(shell->fade.view->surface);
4589 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004590 break;
4591 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004592 lock(shell);
4593 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004594 default:
4595 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004596 }
4597}
4598
Jason Ekstranda7af7042013-10-12 22:38:11 -05004599static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004600shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004601{
4602 struct weston_compositor *compositor = shell->compositor;
4603 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004604 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004605
4606 surface = weston_surface_create(compositor);
4607 if (!surface)
4608 return NULL;
4609
Jason Ekstranda7af7042013-10-12 22:38:11 -05004610 view = weston_view_create(surface);
4611 if (!view) {
4612 weston_surface_destroy(surface);
4613 return NULL;
4614 }
4615
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004616 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004617 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004618 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004619 wl_list_insert(&compositor->fade_layer.view_list,
4620 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004621 pixman_region32_init(&surface->input);
4622
Jason Ekstranda7af7042013-10-12 22:38:11 -05004623 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004624}
4625
4626static void
4627shell_fade(struct desktop_shell *shell, enum fade_type type)
4628{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004629 float tint;
4630
4631 switch (type) {
4632 case FADE_IN:
4633 tint = 0.0;
4634 break;
4635 case FADE_OUT:
4636 tint = 1.0;
4637 break;
4638 default:
4639 weston_log("shell: invalid fade type\n");
4640 return;
4641 }
4642
4643 shell->fade.type = type;
4644
Jason Ekstranda7af7042013-10-12 22:38:11 -05004645 if (shell->fade.view == NULL) {
4646 shell->fade.view = shell_fade_create_surface(shell);
4647 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004648 return;
4649
Jason Ekstranda7af7042013-10-12 22:38:11 -05004650 shell->fade.view->alpha = 1.0 - tint;
4651 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004652 }
4653
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004654 if (shell->fade.view->output == NULL) {
4655 /* If the black view gets a NULL output, we lost the
4656 * last output and we'll just cancel the fade. This
4657 * happens when you close the last window under the
4658 * X11 or Wayland backends. */
4659 shell->locked = false;
4660 weston_surface_destroy(shell->fade.view->surface);
4661 shell->fade.view = NULL;
4662 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004663 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004664 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004665 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004666 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004667 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004668 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004669 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004670}
4671
4672static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004673do_shell_fade_startup(void *data)
4674{
4675 struct desktop_shell *shell = data;
4676
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004677 if (shell->startup_animation_type == ANIMATION_FADE)
4678 shell_fade(shell, FADE_IN);
4679 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004680 weston_surface_destroy(shell->fade.view->surface);
4681 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004682 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004683}
4684
4685static void
4686shell_fade_startup(struct desktop_shell *shell)
4687{
4688 struct wl_event_loop *loop;
4689
4690 if (!shell->fade.startup_timer)
4691 return;
4692
4693 wl_event_source_remove(shell->fade.startup_timer);
4694 shell->fade.startup_timer = NULL;
4695
4696 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4697 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4698}
4699
4700static int
4701fade_startup_timeout(void *data)
4702{
4703 struct desktop_shell *shell = data;
4704
4705 shell_fade_startup(shell);
4706 return 0;
4707}
4708
4709static void
4710shell_fade_init(struct desktop_shell *shell)
4711{
4712 /* Make compositor output all black, and wait for the desktop-shell
4713 * client to signal it is ready, then fade in. The timer triggers a
4714 * fade-in, in case the desktop-shell client takes too long.
4715 */
4716
4717 struct wl_event_loop *loop;
4718
Jason Ekstranda7af7042013-10-12 22:38:11 -05004719 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004720 weston_log("%s: warning: fade surface already exists\n",
4721 __func__);
4722 return;
4723 }
4724
Jason Ekstranda7af7042013-10-12 22:38:11 -05004725 shell->fade.view = shell_fade_create_surface(shell);
4726 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004727 return;
4728
Jason Ekstranda7af7042013-10-12 22:38:11 -05004729 weston_view_update_transform(shell->fade.view);
4730 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004731
4732 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4733 shell->fade.startup_timer =
4734 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4735 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4736}
4737
4738static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004739idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004740{
4741 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004742 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004743 struct weston_seat *seat;
4744
4745 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4746 if (seat->pointer)
4747 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004748
4749 shell_fade(shell, FADE_OUT);
4750 /* lock() is called from shell_fade_done() */
4751}
4752
4753static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004754wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004755{
4756 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004757 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004758
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004759 unlock(shell);
4760}
4761
4762static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004764{
Giulio Camuffob8366642013-04-25 13:57:46 +03004765 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004766 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004767
Jason Ekstranda7af7042013-10-12 22:38:11 -05004768 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004769
4770 x = output->x + (output->width - width) / 2 - surf_x / 2;
4771 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004772
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004773 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004774}
4775
4776static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004777weston_view_set_initial_position(struct weston_view *view,
4778 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004779{
4780 struct weston_compositor *compositor = shell->compositor;
4781 int ix = 0, iy = 0;
4782 int range_x, range_y;
4783 int dx, dy, x, y, panel_height;
4784 struct weston_output *output, *target_output = NULL;
4785 struct weston_seat *seat;
4786
4787 /* As a heuristic place the new window on the same output as the
4788 * pointer. Falling back to the output containing 0, 0.
4789 *
4790 * TODO: Do something clever for touch too?
4791 */
4792 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004793 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004794 ix = wl_fixed_to_int(seat->pointer->x);
4795 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004796 break;
4797 }
4798 }
4799
4800 wl_list_for_each(output, &compositor->output_list, link) {
4801 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4802 target_output = output;
4803 break;
4804 }
4805 }
4806
4807 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004808 weston_view_set_position(view, 10 + random() % 400,
4809 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004810 return;
4811 }
4812
4813 /* Valid range within output where the surface will still be onscreen.
4814 * If this is negative it means that the surface is bigger than
4815 * output.
4816 */
4817 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004818 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004819 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004820 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004821
Rob Bradford4cb88c72012-08-13 15:18:44 +01004822 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004823 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004824 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004825 dx = 0;
4826
4827 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004828 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004829 else
4830 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004831
4832 x = target_output->x + dx;
4833 y = target_output->y + dy;
4834
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004836}
4837
4838static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004839map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004840 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004841{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004842 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004843 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004844 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004845 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004846
Pekka Paalanen77346a62011-11-30 16:26:35 +02004847 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004848 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004849 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004850 if (shsurf->state.fullscreen) {
4851 center_on_output(shsurf->view, shsurf->fullscreen_output);
4852 shell_map_fullscreen(shsurf);
4853 } else if (shsurf->state.maximized) {
4854 /* use surface configure to set the geometry */
4855 panel_height = get_output_panel_height(shell, shsurf->output);
4856 surface_subsurfaces_boundingbox(shsurf->surface,
4857 &surf_x, &surf_y, NULL, NULL);
4858 weston_view_set_position(shsurf->view,
4859 shsurf->output->x - surf_x,
4860 shsurf->output->y +
4861 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004862 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004863 weston_view_set_initial_position(shsurf->view, shell);
4864 }
Juan Zhao96879df2012-02-07 08:45:41 +08004865 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004866 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004867 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004868 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004869 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004870 weston_view_set_position(shsurf->view,
4871 shsurf->view->geometry.x + sx,
4872 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004873 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004874 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004875 default:
4876 ;
4877 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004878
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004879 /* Surface stacking order, see also activate(). */
4880 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004881
Jason Ekstranda7af7042013-10-12 22:38:11 -05004882 if (shsurf->type != SHELL_SURFACE_NONE) {
4883 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004884 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 shsurf->surface->output = shsurf->output;
4886 shsurf->view->output = shsurf->output;
4887 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004888 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004889
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004891 /* XXX: xwayland's using the same fields for transient type */
4892 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004893 if (shsurf->transient.flags ==
4894 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4895 break;
4896 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004897 if (shsurf->state.relative &&
4898 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4899 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004900 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004901 break;
4902 wl_list_for_each(seat, &compositor->seat_list, link)
4903 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004904 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004905 case SHELL_SURFACE_POPUP:
4906 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004907 default:
4908 break;
4909 }
4910
Rafael Antognolli03b16592013-12-03 15:35:42 -02004911 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4912 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004913 {
4914 switch (shell->win_animation_type) {
4915 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004916 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004917 break;
4918 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004919 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004920 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004921 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004922 default:
4923 break;
4924 }
4925 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004926}
4927
4928static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004929configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004930 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004931{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004932 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004933 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004934 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004935
Pekka Paalanen77346a62011-11-30 16:26:35 +02004936 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004937
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004938 assert(shsurf);
4939
Rafael Antognolli03b16592013-12-03 15:35:42 -02004940 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004941 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004942 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004943 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004944 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4945 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004946 mx = shsurf->output->x - surf_x;
4947 my = shsurf->output->y +
4948 get_output_panel_height(shell,shsurf->output) - surf_y;
4949 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004950 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004951 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004952 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004953
Alex Wu4539b082012-03-01 12:57:46 +08004954 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004955 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004956 wl_list_for_each(view, &surface->views, surface_link)
4957 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004958
Rafael Antognolli03b16592013-12-03 15:35:42 -02004959 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004960 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004961 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004962}
4963
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004964static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004965shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004966{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004967 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004968 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004969 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004970
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004971 assert(shsurf);
4972
4973 shell = shsurf->shell;
4974
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004975 if (!weston_surface_is_mapped(es) &&
4976 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004977 remove_popup_grab(shsurf);
4978 }
4979
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004980 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004981 return;
4982
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004983 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004984 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004985 type_changed = 1;
4986 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004987
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004988 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004989 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004990 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004991 shsurf->last_width != es->width ||
4992 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004993 float from_x, from_y;
4994 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004995
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004996 if (shsurf->resize_edges) {
4997 sx = 0;
4998 sy = 0;
4999 }
5000
5001 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5002 sx = shsurf->last_width - es->width;
5003 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5004 sy = shsurf->last_height - es->height;
5005
5006 shsurf->last_width = es->width;
5007 shsurf->last_height = es->height;
5008
Jason Ekstranda7af7042013-10-12 22:38:11 -05005009 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5010 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005011 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005012 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005013 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005014 }
5015}
5016
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005017static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005018
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005019static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005020desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005021{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005022 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005023 struct desktop_shell *shell =
5024 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005025
5026 shell->child.process.pid = 0;
5027 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005028
5029 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5030 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005031 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005032 shell->child.deathstamp = time;
5033 shell->child.deathcount = 0;
5034 }
5035
5036 shell->child.deathcount++;
5037 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005038 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005039 return;
5040 }
5041
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005042 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005043 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005044 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005045}
5046
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005047static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005048desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5049{
5050 struct desktop_shell *shell;
5051
5052 shell = container_of(listener, struct desktop_shell,
5053 child.client_destroy_listener);
5054
5055 shell->child.client = NULL;
5056}
5057
5058static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005059launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005060{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005061 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005062
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005063 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005064 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005065 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005066 desktop_shell_sigchld);
5067
5068 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005069 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005070
5071 shell->child.client_destroy_listener.notify =
5072 desktop_shell_client_destroy;
5073 wl_client_add_destroy_listener(shell->child.client,
5074 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005075}
5076
5077static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005078handle_shell_client_destroy(struct wl_listener *listener, void *data)
5079{
5080 struct shell_client *sc =
5081 container_of(listener, struct shell_client, destroy_listener);
5082
5083 if (sc->ping_timer)
5084 wl_event_source_remove(sc->ping_timer);
5085 free(sc);
5086}
5087
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005088static struct shell_client *
5089shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5090 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005091{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005092 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005093
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005094 sc = zalloc(sizeof *sc);
5095 if (sc == NULL) {
5096 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005097 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005098 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005099
5100 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005101 if (sc->resource == NULL) {
5102 free(sc);
5103 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005104 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005105 }
5106
5107 sc->client = client;
5108 sc->shell = shell;
5109 sc->destroy_listener.notify = handle_shell_client_destroy;
5110 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5111
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005112 return sc;
5113}
5114
5115static void
5116bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5117{
5118 struct desktop_shell *shell = data;
5119 struct shell_client *sc;
5120
5121 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5122 if (sc)
5123 wl_resource_set_implementation(sc->resource,
5124 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005125 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005126}
5127
5128static void
5129bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5130{
5131 struct desktop_shell *shell = data;
5132 struct shell_client *sc;
5133
5134 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5135 if (sc)
5136 wl_resource_set_dispatcher(sc->resource,
5137 xdg_shell_unversioned_dispatch,
5138 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005139}
5140
5141static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005142unbind_desktop_shell(struct wl_resource *resource)
5143{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005144 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005145
5146 if (shell->locked)
5147 resume_desktop(shell);
5148
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005149 shell->child.desktop_shell = NULL;
5150 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005151}
5152
5153static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005154bind_desktop_shell(struct wl_client *client,
5155 void *data, uint32_t version, uint32_t id)
5156{
Tiago Vignattibe143262012-04-16 17:31:41 +03005157 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005158 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005159
Jason Ekstranda85118c2013-06-27 20:17:02 -05005160 resource = wl_resource_create(client, &desktop_shell_interface,
5161 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005162
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005163 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005164 wl_resource_set_implementation(resource,
5165 &desktop_shell_implementation,
5166 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005167 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005168
5169 if (version < 2)
5170 shell_fade_startup(shell);
5171
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005172 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005173 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005174
5175 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5176 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005177 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005178}
5179
Pekka Paalanen6e168112011-11-24 11:34:05 +02005180static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005181screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005182{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005183 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005184 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005185
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005186 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005187 return;
5188
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005189 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005190 if (!shell->locked)
5191 return;
5192
Jason Ekstranda7af7042013-10-12 22:38:11 -05005193 view = container_of(surface->views.next, struct weston_view, surface_link);
5194 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005195
Jason Ekstranda7af7042013-10-12 22:38:11 -05005196 if (wl_list_empty(&view->layer_link)) {
5197 wl_list_insert(shell->lock_layer.view_list.prev,
5198 &view->layer_link);
5199 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005200 wl_event_source_timer_update(shell->screensaver.timer,
5201 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005202 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005203 }
5204}
5205
5206static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005207screensaver_set_surface(struct wl_client *client,
5208 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005209 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005210 struct wl_resource *output_resource)
5211{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005212 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005213 struct weston_surface *surface =
5214 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005215 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005216 struct weston_view *view, *next;
5217
5218 /* Make sure we only have one view */
5219 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5220 weston_view_destroy(view);
5221 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005222
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005223 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005224 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005225 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005226}
5227
5228static const struct screensaver_interface screensaver_implementation = {
5229 screensaver_set_surface
5230};
5231
5232static void
5233unbind_screensaver(struct wl_resource *resource)
5234{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005235 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005236
Pekka Paalanen77346a62011-11-30 16:26:35 +02005237 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005238}
5239
5240static void
5241bind_screensaver(struct wl_client *client,
5242 void *data, uint32_t version, uint32_t id)
5243{
Tiago Vignattibe143262012-04-16 17:31:41 +03005244 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005245 struct wl_resource *resource;
5246
Jason Ekstranda85118c2013-06-27 20:17:02 -05005247 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005248
Pekka Paalanen77346a62011-11-30 16:26:35 +02005249 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005250 wl_resource_set_implementation(resource,
5251 &screensaver_implementation,
5252 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005253 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005254 return;
5255 }
5256
5257 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5258 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005259 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005260}
5261
Kristian Høgsberg07045392012-02-19 18:52:44 -05005262struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005263 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005264 struct weston_surface *current;
5265 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005266 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005267 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005268};
5269
5270static void
5271switcher_next(struct switcher *switcher)
5272{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005273 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005274 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005275 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005276 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005278 /* temporary re-display minimized surfaces */
5279 struct weston_view *tmp;
5280 struct weston_view **minimized;
5281 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5282 wl_list_remove(&view->layer_link);
5283 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5284 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5285 *minimized = view;
5286 }
5287
Jason Ekstranda7af7042013-10-12 22:38:11 -05005288 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005289 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005290 if (shsurf &&
5291 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5292 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005293 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005294 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005295 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005296 next = view->surface;
5297 prev = view->surface;
5298 view->alpha = 0.25;
5299 weston_view_geometry_dirty(view);
5300 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005301 }
Alex Wu1659daa2012-04-01 20:13:09 +08005302
Jason Ekstranda7af7042013-10-12 22:38:11 -05005303 if (is_black_surface(view->surface, NULL)) {
5304 view->alpha = 0.25;
5305 weston_view_geometry_dirty(view);
5306 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005307 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005308 }
5309
5310 if (next == NULL)
5311 next = first;
5312
Alex Wu07b26062012-03-12 16:06:01 +08005313 if (next == NULL)
5314 return;
5315
Kristian Høgsberg07045392012-02-19 18:52:44 -05005316 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005317 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005318
5319 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005320 wl_list_for_each(view, &next->views, surface_link)
5321 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005322
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005323 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005324 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005325 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005326}
5327
5328static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005329switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005330{
5331 struct switcher *switcher =
5332 container_of(listener, struct switcher, listener);
5333
5334 switcher_next(switcher);
5335}
5336
5337static void
Daniel Stone351eb612012-05-31 15:27:47 -04005338switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005339{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005340 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005341 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005342 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005343
Jason Ekstranda7af7042013-10-12 22:38:11 -05005344 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005345 if (is_focus_view(view))
5346 continue;
5347
Jason Ekstranda7af7042013-10-12 22:38:11 -05005348 view->alpha = 1.0;
5349 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005350 }
5351
Alex Wu07b26062012-03-12 16:06:01 +08005352 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005353 activate(switcher->shell, switcher->current,
5354 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005355 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005356 weston_keyboard_end_grab(keyboard);
5357 if (keyboard->input_method_resource)
5358 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005359
5360 /* re-hide surfaces that were temporary shown during the switch */
5361 struct weston_view **minimized;
5362 wl_array_for_each(minimized, &switcher->minimized_array) {
5363 /* with the exception of the current selected */
5364 if ((*minimized)->surface != switcher->current) {
5365 wl_list_remove(&(*minimized)->layer_link);
5366 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5367 weston_view_damage_below(*minimized);
5368 }
5369 }
5370 wl_array_release(&switcher->minimized_array);
5371
Kristian Høgsberg07045392012-02-19 18:52:44 -05005372 free(switcher);
5373}
5374
5375static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005376switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005377 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005378{
5379 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005380 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005381
Daniel Stonec9785ea2012-05-30 16:31:52 +01005382 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005383 switcher_next(switcher);
5384}
5385
5386static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005387switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005388 uint32_t mods_depressed, uint32_t mods_latched,
5389 uint32_t mods_locked, uint32_t group)
5390{
5391 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005392 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005393
Daniel Stone351eb612012-05-31 15:27:47 -04005394 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5395 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005396}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005397
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005398static void
5399switcher_cancel(struct weston_keyboard_grab *grab)
5400{
5401 struct switcher *switcher = container_of(grab, struct switcher, grab);
5402
5403 switcher_destroy(switcher);
5404}
5405
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005406static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005407 switcher_key,
5408 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005409 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005410};
5411
5412static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005413switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005414 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005415{
Tiago Vignattibe143262012-04-16 17:31:41 +03005416 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005417 struct switcher *switcher;
5418
5419 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005420 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005421 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005422 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005423 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005424 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005425
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005426 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005427 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005428 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005429 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5430 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005431 switcher_next(switcher);
5432}
5433
Pekka Paalanen3c647232011-12-22 13:43:43 +02005434static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005435backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005436 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005437{
5438 struct weston_compositor *compositor = data;
5439 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005440 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005441
5442 /* TODO: we're limiting to simple use cases, where we assume just
5443 * control on the primary display. We'd have to extend later if we
5444 * ever get support for setting backlights on random desktop LCD
5445 * panels though */
5446 output = get_default_output(compositor);
5447 if (!output)
5448 return;
5449
5450 if (!output->set_backlight)
5451 return;
5452
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005453 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5454 backlight_new = output->backlight_current - 25;
5455 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5456 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005457
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005458 if (backlight_new < 5)
5459 backlight_new = 5;
5460 if (backlight_new > 255)
5461 backlight_new = 255;
5462
5463 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005464 output->set_backlight(output, output->backlight_current);
5465}
5466
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005467struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005468 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005469 struct weston_seat *seat;
5470 uint32_t key[2];
5471 int key_released[2];
5472};
5473
5474static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005475debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005476 uint32_t key, uint32_t state)
5477{
5478 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005479 struct weston_compositor *ec = db->seat->compositor;
5480 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005481 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005482 uint32_t serial;
5483 int send = 0, terminate = 0;
5484 int check_binding = 1;
5485 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005486 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005487
5488 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5489 /* Do not run bindings on key releases */
5490 check_binding = 0;
5491
5492 for (i = 0; i < 2; i++)
5493 if (key == db->key[i])
5494 db->key_released[i] = 1;
5495
5496 if (db->key_released[0] && db->key_released[1]) {
5497 /* All key releases been swalled so end the grab */
5498 terminate = 1;
5499 } else if (key != db->key[0] && key != db->key[1]) {
5500 /* Should not swallow release of other keys */
5501 send = 1;
5502 }
5503 } else if (key == db->key[0] && !db->key_released[0]) {
5504 /* Do not check bindings for the first press of the binding
5505 * key. This allows it to be used as a debug shortcut.
5506 * We still need to swallow this event. */
5507 check_binding = 0;
5508 } else if (db->key[1]) {
5509 /* If we already ran a binding don't process another one since
5510 * we can't keep track of all the binding keys that were
5511 * pressed in order to swallow the release events. */
5512 send = 1;
5513 check_binding = 0;
5514 }
5515
5516 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005517 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5518 key, state)) {
5519 /* We ran a binding so swallow the press and keep the
5520 * grab to swallow the released too. */
5521 send = 0;
5522 terminate = 0;
5523 db->key[1] = key;
5524 } else {
5525 /* Terminate the grab since the key pressed is not a
5526 * debug binding key. */
5527 send = 1;
5528 terminate = 1;
5529 }
5530 }
5531
5532 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005533 serial = wl_display_next_serial(display);
5534 resource_list = &grab->keyboard->focus_resource_list;
5535 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005536 wl_keyboard_send_key(resource, serial, time, key, state);
5537 }
5538 }
5539
5540 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005541 weston_keyboard_end_grab(grab->keyboard);
5542 if (grab->keyboard->input_method_resource)
5543 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005544 free(db);
5545 }
5546}
5547
5548static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005549debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005550 uint32_t mods_depressed, uint32_t mods_latched,
5551 uint32_t mods_locked, uint32_t group)
5552{
5553 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005554 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005555
Neil Roberts96d790e2013-09-19 17:32:00 +01005556 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005557
Neil Roberts96d790e2013-09-19 17:32:00 +01005558 wl_resource_for_each(resource, resource_list) {
5559 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5560 mods_latched, mods_locked, group);
5561 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005562}
5563
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005564static void
5565debug_binding_cancel(struct weston_keyboard_grab *grab)
5566{
5567 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5568
5569 weston_keyboard_end_grab(grab->keyboard);
5570 free(db);
5571}
5572
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005573struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005574 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005575 debug_binding_modifiers,
5576 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005577};
5578
5579static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005580debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005581{
5582 struct debug_binding_grab *grab;
5583
5584 grab = calloc(1, sizeof *grab);
5585 if (!grab)
5586 return;
5587
5588 grab->seat = (struct weston_seat *) seat;
5589 grab->key[0] = key;
5590 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005591 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005592}
5593
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005594static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005595force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005596 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005597{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005598 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005599 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005600 struct desktop_shell *shell = data;
5601 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005602 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005603
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005604 focus_surface = seat->keyboard->focus;
5605 if (!focus_surface)
5606 return;
5607
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005608 wl_signal_emit(&compositor->kill_signal, focus_surface);
5609
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005610 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005611 wl_client_get_credentials(client, &pid, NULL, NULL);
5612
5613 /* Skip clients that we launched ourselves (the credentials of
5614 * the socketpair is ours) */
5615 if (pid == getpid())
5616 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005617
Daniel Stone325fc2d2012-05-30 16:31:58 +01005618 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005619}
5620
5621static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005622workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005623 uint32_t key, void *data)
5624{
5625 struct desktop_shell *shell = data;
5626 unsigned int new_index = shell->workspaces.current;
5627
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005628 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005629 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005630 if (new_index != 0)
5631 new_index--;
5632
5633 change_workspace(shell, new_index);
5634}
5635
5636static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005637workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005638 uint32_t key, void *data)
5639{
5640 struct desktop_shell *shell = data;
5641 unsigned int new_index = shell->workspaces.current;
5642
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005643 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005644 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005645 if (new_index < shell->workspaces.num - 1)
5646 new_index++;
5647
5648 change_workspace(shell, new_index);
5649}
5650
5651static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005652workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005653 uint32_t key, void *data)
5654{
5655 struct desktop_shell *shell = data;
5656 unsigned int new_index;
5657
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005658 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005659 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005660 new_index = key - KEY_F1;
5661 if (new_index >= shell->workspaces.num)
5662 new_index = shell->workspaces.num - 1;
5663
5664 change_workspace(shell, new_index);
5665}
5666
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005667static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005668workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005669 uint32_t key, void *data)
5670{
5671 struct desktop_shell *shell = data;
5672 unsigned int new_index = shell->workspaces.current;
5673
5674 if (shell->locked)
5675 return;
5676
5677 if (new_index != 0)
5678 new_index--;
5679
5680 take_surface_to_workspace_by_seat(shell, seat, new_index);
5681}
5682
5683static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005684workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005685 uint32_t key, void *data)
5686{
5687 struct desktop_shell *shell = data;
5688 unsigned int new_index = shell->workspaces.current;
5689
5690 if (shell->locked)
5691 return;
5692
5693 if (new_index < shell->workspaces.num - 1)
5694 new_index++;
5695
5696 take_surface_to_workspace_by_seat(shell, seat, new_index);
5697}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005698
5699static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005700shell_reposition_view_on_output_destroy(struct weston_view *view)
5701{
5702 struct weston_output *output, *first_output;
5703 struct weston_compositor *ec = view->surface->compositor;
5704 struct shell_surface *shsurf;
5705 float x, y;
5706 int visible;
5707
5708 x = view->geometry.x;
5709 y = view->geometry.y;
5710
5711 /* At this point the destroyed output is not in the list anymore.
5712 * If the view is still visible somewhere, we leave where it is,
5713 * otherwise, move it to the first output. */
5714 visible = 0;
5715 wl_list_for_each(output, &ec->output_list, link) {
5716 if (pixman_region32_contains_point(&output->region,
5717 x, y, NULL)) {
5718 visible = 1;
5719 break;
5720 }
5721 }
5722
5723 if (!visible) {
5724 first_output = container_of(ec->output_list.next,
5725 struct weston_output, link);
5726
5727 x = first_output->x + first_output->width / 4;
5728 y = first_output->y + first_output->height / 4;
5729 }
5730
5731 weston_view_set_position(view, x, y);
5732
5733 shsurf = get_shell_surface(view->surface);
5734
5735 if (shsurf) {
5736 shsurf->saved_position_valid = false;
5737 shsurf->next_state.maximized = false;
5738 shsurf->next_state.fullscreen = false;
5739 shsurf->state_changed = true;
5740 }
5741}
5742
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005743void
5744shell_for_each_layer(struct desktop_shell *shell,
5745 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005746{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005747 struct workspace **ws;
5748
5749 func(shell, &shell->fullscreen_layer, data);
5750 func(shell, &shell->panel_layer, data);
5751 func(shell, &shell->background_layer, data);
5752 func(shell, &shell->lock_layer, data);
5753 func(shell, &shell->input_panel_layer, data);
5754
5755 wl_array_for_each(ws, &shell->workspaces.array)
5756 func(shell, &(*ws)->layer, data);
5757}
5758
5759static void
5760shell_output_destroy_move_layer(struct desktop_shell *shell,
5761 struct weston_layer *layer,
5762 void *data)
5763{
5764 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005765 struct weston_view *view;
5766
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005767 wl_list_for_each(view, &layer->view_list, layer_link) {
5768 if (view->output != output)
5769 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005770
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005771 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005772 }
5773}
5774
5775static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005776handle_output_destroy(struct wl_listener *listener, void *data)
5777{
5778 struct shell_output *output_listener =
5779 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005780 struct weston_output *output = output_listener->output;
5781 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005782
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005783 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005784
Xiong Zhang6b481422013-10-23 13:58:32 +08005785 wl_list_remove(&output_listener->destroy_listener.link);
5786 wl_list_remove(&output_listener->link);
5787 free(output_listener);
5788}
5789
5790static void
5791create_shell_output(struct desktop_shell *shell,
5792 struct weston_output *output)
5793{
5794 struct shell_output *shell_output;
5795
5796 shell_output = zalloc(sizeof *shell_output);
5797 if (shell_output == NULL)
5798 return;
5799
5800 shell_output->output = output;
5801 shell_output->shell = shell;
5802 shell_output->destroy_listener.notify = handle_output_destroy;
5803 wl_signal_add(&output->destroy_signal,
5804 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005805 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005806}
5807
5808static void
5809handle_output_create(struct wl_listener *listener, void *data)
5810{
5811 struct desktop_shell *shell =
5812 container_of(listener, struct desktop_shell, output_create_listener);
5813 struct weston_output *output = (struct weston_output *)data;
5814
5815 create_shell_output(shell, output);
5816}
5817
5818static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005819handle_output_move_layer(struct desktop_shell *shell,
5820 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005821{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005822 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005823 struct weston_view *view;
5824 float x, y;
5825
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005826 wl_list_for_each(view, &layer->view_list, layer_link) {
5827 if (view->output != output)
5828 continue;
5829
5830 x = view->geometry.x + output->move_x;
5831 y = view->geometry.y + output->move_y;
5832 weston_view_set_position(view, x, y);
5833 }
5834}
5835
5836static void
5837handle_output_move(struct wl_listener *listener, void *data)
5838{
5839 struct desktop_shell *shell;
5840
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005841 shell = container_of(listener, struct desktop_shell,
5842 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005843
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005844 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005845}
5846
5847static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005848setup_output_destroy_handler(struct weston_compositor *ec,
5849 struct desktop_shell *shell)
5850{
5851 struct weston_output *output;
5852
5853 wl_list_init(&shell->output_list);
5854 wl_list_for_each(output, &ec->output_list, link)
5855 create_shell_output(shell, output);
5856
5857 shell->output_create_listener.notify = handle_output_create;
5858 wl_signal_add(&ec->output_created_signal,
5859 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005860
5861 shell->output_move_listener.notify = handle_output_move;
5862 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005863}
5864
5865static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005866shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005867{
Tiago Vignattibe143262012-04-16 17:31:41 +03005868 struct desktop_shell *shell =
5869 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005870 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005871 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005872
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005873 /* Force state to unlocked so we don't try to fade */
5874 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005875 if (shell->child.client)
5876 wl_client_destroy(shell->child.client);
5877
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005878 wl_list_remove(&shell->idle_listener.link);
5879 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005880
5881 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005882
Xiong Zhang6b481422013-10-23 13:58:32 +08005883 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5884 wl_list_remove(&shell_output->destroy_listener.link);
5885 wl_list_remove(&shell_output->link);
5886 free(shell_output);
5887 }
5888
5889 wl_list_remove(&shell->output_create_listener.link);
5890
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005891 wl_array_for_each(ws, &shell->workspaces.array)
5892 workspace_destroy(*ws);
5893 wl_array_release(&shell->workspaces.array);
5894
Pekka Paalanen3c647232011-12-22 13:43:43 +02005895 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005896 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005897 free(shell);
5898}
5899
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005900static void
5901shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5902{
5903 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005904 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005905
5906 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005907 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5908 MODIFIER_CTRL | MODIFIER_ALT,
5909 terminate_binding, ec);
5910 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5911 click_to_activate_binding,
5912 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005913 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5914 click_to_activate_binding,
5915 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005916 weston_compositor_add_touch_binding(ec, 0,
5917 touch_to_activate_binding,
5918 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005919 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5920 MODIFIER_SUPER | MODIFIER_ALT,
5921 surface_opacity_binding, NULL);
5922 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5923 MODIFIER_SUPER, zoom_axis_binding,
5924 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005925
5926 /* configurable bindings */
5927 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005928 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5929 zoom_key_binding, NULL);
5930 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5931 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005932 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5933 maximize_binding, NULL);
5934 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5935 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005936 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5937 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005938 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005939 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5940 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005941 weston_compositor_add_button_binding(ec, BTN_LEFT,
5942 mod | MODIFIER_SHIFT,
5943 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005944
5945 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5946 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5947 rotate_binding, NULL);
5948
Daniel Stone325fc2d2012-05-30 16:31:58 +01005949 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5950 shell);
5951 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5952 ec);
5953 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5954 backlight_binding, ec);
5955 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5956 ec);
5957 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5958 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005959 weston_compositor_add_key_binding(ec, KEY_K, mod,
5960 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005961 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5962 workspace_up_binding, shell);
5963 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5964 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005965 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5966 workspace_move_surface_up_binding,
5967 shell);
5968 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5969 workspace_move_surface_down_binding,
5970 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005971
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005972 if (shell->exposay_modifier)
5973 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5974 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005975
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005976 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5977 if (shell->workspaces.num > 1) {
5978 num_workspace_bindings = shell->workspaces.num;
5979 if (num_workspace_bindings > 6)
5980 num_workspace_bindings = 6;
5981 for (i = 0; i < num_workspace_bindings; i++)
5982 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5983 workspace_f_binding,
5984 shell);
5985 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005986
5987 /* Debug bindings */
5988 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5989 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005990}
5991
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005992WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005993module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005994 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005995{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005996 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005997 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005998 struct workspace **pws;
5999 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006000 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006001
Peter Huttererf3d62272013-08-08 11:57:05 +10006002 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006003 if (shell == NULL)
6004 return -1;
6005
Kristian Høgsberg75840622011-09-06 13:48:16 -04006006 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006007
6008 shell->destroy_listener.notify = shell_destroy;
6009 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006010 shell->idle_listener.notify = idle_handler;
6011 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6012 shell->wake_listener.notify = wake_handler;
6013 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006014
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006015 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006016 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006017 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006018 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006019 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006020 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006021 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006022 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006023 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006024 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006025
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006026 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6027 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006028 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6029 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006030 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006031
6032 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006033 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006034
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006035 if (input_panel_setup(shell) < 0)
6036 return -1;
6037
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006038 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006039
Daniel Stonedf8133b2013-11-19 11:37:14 +01006040 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6041 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6042
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006043 for (i = 0; i < shell->workspaces.num; i++) {
6044 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6045 if (pws == NULL)
6046 return -1;
6047
6048 *pws = workspace_create();
6049 if (*pws == NULL)
6050 return -1;
6051 }
6052 activate_workspace(shell, 0);
6053
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006054 weston_layer_init(&shell->minimized_layer, NULL);
6055
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006056 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006057 wl_list_init(&shell->workspaces.animation.link);
6058 shell->workspaces.animation.frame = animate_workspace_change_frame;
6059
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006060 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006061 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006062 return -1;
6063
Rafael Antognollie2a34552013-12-03 15:35:45 -02006064 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6065 shell, bind_xdg_shell) == NULL)
6066 return -1;
6067
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006068 if (wl_global_create(ec->wl_display,
6069 &desktop_shell_interface, 2,
6070 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006071 return -1;
6072
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006073 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6074 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006075 return -1;
6076
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006077 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6078 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006079 return -1;
6080
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006081 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006082
Xiong Zhang6b481422013-10-23 13:58:32 +08006083 setup_output_destroy_handler(ec, shell);
6084
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006085 loop = wl_display_get_event_loop(ec->wl_display);
6086 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006087
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006088 shell->screensaver.timer =
6089 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6090
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006091 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006092 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05006093 create_keyboard_focus_listener(seat);
6094 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006095
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006096 screenshooter_create(ec);
6097
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006098 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006099
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006100 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006101
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006102 return 0;
6103}