blob: 591d83210326edeb9a5e23acda51f5b4cdc0032d [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;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400161
162 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200163
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700164 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200165 bool maximized;
166 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200167 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100168 bool lowered;
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
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700173 struct {
174 int left, right, top, bottom;
175 } margin;
176
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500177 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200178};
179
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300180struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400181 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300182 struct shell_surface *shsurf;
183 struct wl_listener shsurf_destroy_listener;
184};
185
Rusty Lynch1084da52013-08-15 09:10:08 -0700186struct shell_touch_grab {
187 struct weston_touch_grab grab;
188 struct shell_surface *shsurf;
189 struct wl_listener shsurf_destroy_listener;
190 struct weston_touch *touch;
191};
192
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300193struct weston_move_grab {
194 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100195 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700196 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500197};
198
Rusty Lynch1084da52013-08-15 09:10:08 -0700199struct weston_touch_move_grab {
200 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800201 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700202 wl_fixed_t dx, dy;
203};
204
Pekka Paalanen460099f2012-01-20 16:48:25 +0200205struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300206 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500207 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200208 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200209 float x;
210 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211 } center;
212};
213
Giulio Camuffo5085a752013-03-25 21:42:45 +0100214struct shell_seat {
215 struct weston_seat *seat;
216 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500217 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100218
Jason Ekstrand024177c2014-04-21 19:42:58 -0500219 struct wl_listener caps_changed_listener;
220 struct wl_listener pointer_focus_listener;
221 struct wl_listener keyboard_focus_listener;
222
Giulio Camuffo5085a752013-03-25 21:42:45 +0100223 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400224 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100225 struct wl_list surfaces_list;
226 struct wl_client *client;
227 int32_t initial_up;
228 } popup_grab;
229};
230
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800231struct shell_client {
232 struct wl_resource *resource;
233 struct wl_client *client;
234 struct desktop_shell *shell;
235 struct wl_listener destroy_listener;
236 struct wl_event_source *ping_timer;
237 uint32_t ping_serial;
238 int unresponsive;
239};
240
Alex Wubd3354b2012-04-17 17:20:49 +0800241static struct desktop_shell *
242shell_surface_get_shell(struct shell_surface *shsurf);
243
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500244static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400245surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500246
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300247static void
248shell_fade_startup(struct desktop_shell *shell);
249
Philip Withnallbecb77e2013-11-25 18:01:30 +0000250static struct shell_seat *
251get_shell_seat(struct weston_seat *seat);
252
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700253static int
254get_output_panel_height(struct desktop_shell *shell,
255 struct weston_output *output);
256
Philip Withnall648a4dd2013-11-25 18:01:44 +0000257static void
258shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
259
Alex Wubd3354b2012-04-17 17:20:49 +0800260static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200261shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
262
263static bool
264shell_surface_is_xdg_surface(struct shell_surface *shsurf);
265
266static bool
267shell_surface_is_xdg_popup(struct shell_surface *shsurf);
268
269static void
270shell_surface_set_parent(struct shell_surface *shsurf,
271 struct weston_surface *parent);
272
273static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800274shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
275{
276 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800278
279 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100280
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800282 return false;
283
Jason Ekstranda7af7042013-10-12 22:38:11 -0500284 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
285 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800286 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500287 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800288}
289
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500290static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400291destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300292{
293 struct shell_grab *grab;
294
295 grab = container_of(listener, struct shell_grab,
296 shsurf_destroy_listener);
297
298 grab->shsurf = NULL;
299}
300
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800301struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500302get_default_view(struct weston_surface *surface)
303{
304 struct shell_surface *shsurf;
305 struct weston_view *view;
306
307 if (!surface || wl_list_empty(&surface->views))
308 return NULL;
309
310 shsurf = get_shell_surface(surface);
311 if (shsurf)
312 return shsurf->view;
313
314 wl_list_for_each(view, &surface->views, surface_link)
315 if (weston_view_is_mapped(view))
316 return view;
317
318 return container_of(surface->views.next, struct weston_view, surface_link);
319}
320
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300321static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400322popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400323
324static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400326 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300327 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300329 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300330{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300331 struct desktop_shell *shell = shsurf->shell;
332
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400333 popup_grab_end(pointer);
334
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300335 grab->grab.interface = interface;
336 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400337 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500338 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400339 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300340
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700341 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400342 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400343 if (shell->child.desktop_shell) {
344 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
345 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500346 weston_pointer_set_focus(pointer,
347 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400348 wl_fixed_from_int(0),
349 wl_fixed_from_int(0));
350 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300351}
352
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700353static int
354get_output_panel_height(struct desktop_shell *shell,
355 struct weston_output *output)
356{
357 struct weston_view *view;
358 int panel_height = 0;
359
360 if (!output)
361 return 0;
362
363 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
364 if (view->surface->output == output) {
365 panel_height = view->surface->height;
366 break;
367 }
368 }
369
370 return panel_height;
371}
372
373static void
374send_configure_for_surface(struct shell_surface *shsurf)
375{
376 int32_t width, height;
377 struct surface_state *state;
378
379 if (shsurf->state_requested)
380 state = &shsurf->requested_state;
381 else if (shsurf->state_changed)
382 state = &shsurf->next_state;
383 else
384 state = &shsurf->state;
385
386 if (state->fullscreen) {
387 width = shsurf->output->width;
388 height = shsurf->output->height;
389 } else if (state->maximized) {
390 struct desktop_shell *shell;
391 uint32_t panel_height = 0;
392
393 shell = shell_surface_get_shell(shsurf);
394 panel_height = get_output_panel_height(shell, shsurf->output);
395
396 width = shsurf->output->width;
397 height = shsurf->output->height - panel_height;
398 } else {
399 width = 0;
400 height = 0;
401 }
402
403 shsurf->client->send_configure(shsurf->surface, width, height);
404}
405
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300406static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400407shell_surface_state_changed(struct shell_surface *shsurf)
408{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700409 if (shell_surface_is_xdg_surface(shsurf))
410 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400411}
412
413static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300414shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300415{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700416 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400417 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700418 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400419
420 if (grab->shsurf->resize_edges) {
421 grab->shsurf->resize_edges = 0;
422 shell_surface_state_changed(grab->shsurf);
423 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700424 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300425
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700426 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300427}
428
429static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700430shell_touch_grab_start(struct shell_touch_grab *grab,
431 const struct weston_touch_grab_interface *interface,
432 struct shell_surface *shsurf,
433 struct weston_touch *touch)
434{
435 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800436
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700437 if (touch->seat->pointer)
438 popup_grab_end(touch->seat->pointer);
439
Rusty Lynch1084da52013-08-15 09:10:08 -0700440 grab->grab.interface = interface;
441 grab->shsurf = shsurf;
442 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
443 wl_signal_add(&shsurf->destroy_signal,
444 &grab->shsurf_destroy_listener);
445
446 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700447 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700448
449 weston_touch_start_grab(touch, &grab->grab);
450 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500451 weston_touch_set_focus(touch->seat,
452 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700453}
454
455static void
456shell_touch_grab_end(struct shell_touch_grab *grab)
457{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700458 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700459 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700460 grab->shsurf->grabbed = 0;
461 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700462
463 weston_touch_end_grab(grab->touch);
464}
465
466static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500467center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800468 struct weston_output *output);
469
Daniel Stone496ca172012-05-30 16:31:42 +0100470static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300471get_modifier(char *modifier)
472{
473 if (!modifier)
474 return MODIFIER_SUPER;
475
476 if (!strcmp("ctrl", modifier))
477 return MODIFIER_CTRL;
478 else if (!strcmp("alt", modifier))
479 return MODIFIER_ALT;
480 else if (!strcmp("super", modifier))
481 return MODIFIER_SUPER;
482 else
483 return MODIFIER_SUPER;
484}
485
Juan Zhaoe10d2792012-04-25 19:09:52 +0800486static enum animation_type
487get_animation_type(char *animation)
488{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800489 if (!animation)
490 return ANIMATION_NONE;
491
Juan Zhaoe10d2792012-04-25 19:09:52 +0800492 if (!strcmp("zoom", animation))
493 return ANIMATION_ZOOM;
494 else if (!strcmp("fade", animation))
495 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100496 else if (!strcmp("dim-layer", animation))
497 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800498 else
499 return ANIMATION_NONE;
500}
501
Alex Wu4539b082012-03-01 12:57:46 +0800502static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400503shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200504{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400505 struct weston_config_section *section;
506 int duration;
507 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200508
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400509 section = weston_config_get_section(shell->compositor->config,
510 "screensaver", NULL, NULL);
511 weston_config_section_get_string(section,
512 "path", &shell->screensaver.path, NULL);
513 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200514 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400515
516 section = weston_config_get_section(shell->compositor->config,
517 "shell", NULL, NULL);
518 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100519 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100520 shell->client = s;
521 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400522 "binding-modifier", &s, "super");
523 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200524 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800525
526 weston_config_section_get_string(section,
527 "exposay-modifier", &s, "none");
528 if (strcmp(s, "none") == 0)
529 shell->exposay_modifier = 0;
530 else
531 shell->exposay_modifier = get_modifier(s);
532 free(s);
533
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400534 weston_config_section_get_string(section, "animation", &s, "none");
535 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200536 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700537 weston_config_section_get_string(section,
538 "startup-animation", &s, "fade");
539 shell->startup_animation_type = get_animation_type(s);
540 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700541 if (shell->startup_animation_type == ANIMATION_ZOOM)
542 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100543 weston_config_section_get_string(section, "focus-animation", &s, "none");
544 shell->focus_animation_type = get_animation_type(s);
545 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400546 weston_config_section_get_uint(section, "num-workspaces",
547 &shell->workspaces.num,
548 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200549}
550
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800551struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100552get_default_output(struct weston_compositor *compositor)
553{
554 return container_of(compositor->output_list.next,
555 struct weston_output, link);
556}
557
558
559/* no-op func for checking focus surface */
560static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600561focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100562{
563}
564
565static struct focus_surface *
566get_focus_surface(struct weston_surface *surface)
567{
568 if (surface->configure == focus_surface_configure)
569 return surface->configure_private;
570 else
571 return NULL;
572}
573
574static bool
575is_focus_surface (struct weston_surface *es)
576{
577 return (es->configure == focus_surface_configure);
578}
579
580static bool
581is_focus_view (struct weston_view *view)
582{
583 return is_focus_surface (view->surface);
584}
585
586static struct focus_surface *
587create_focus_surface(struct weston_compositor *ec,
588 struct weston_output *output)
589{
590 struct focus_surface *fsurf = NULL;
591 struct weston_surface *surface = NULL;
592
593 fsurf = malloc(sizeof *fsurf);
594 if (!fsurf)
595 return NULL;
596
597 fsurf->surface = weston_surface_create(ec);
598 surface = fsurf->surface;
599 if (surface == NULL) {
600 free(fsurf);
601 return NULL;
602 }
603
604 surface->configure = focus_surface_configure;
605 surface->output = output;
606 surface->configure_private = fsurf;
607
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800608 fsurf->view = weston_view_create(surface);
609 if (fsurf->view == NULL) {
610 weston_surface_destroy(surface);
611 free(fsurf);
612 return NULL;
613 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100614 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100615
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600616 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600617 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100618 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
619 pixman_region32_fini(&surface->opaque);
620 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
621 output->width, output->height);
622 pixman_region32_fini(&surface->input);
623 pixman_region32_init(&surface->input);
624
625 wl_list_init(&fsurf->workspace_transform.link);
626
627 return fsurf;
628}
629
630static void
631focus_surface_destroy(struct focus_surface *fsurf)
632{
633 weston_surface_destroy(fsurf->surface);
634 free(fsurf);
635}
636
637static void
638focus_animation_done(struct weston_view_animation *animation, void *data)
639{
640 struct workspace *ws = data;
641
642 ws->focus_animation = NULL;
643}
644
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200645static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200646focus_state_destroy(struct focus_state *state)
647{
648 wl_list_remove(&state->seat_destroy_listener.link);
649 wl_list_remove(&state->surface_destroy_listener.link);
650 free(state);
651}
652
653static void
654focus_state_seat_destroy(struct wl_listener *listener, void *data)
655{
656 struct focus_state *state = container_of(listener,
657 struct focus_state,
658 seat_destroy_listener);
659
660 wl_list_remove(&state->link);
661 focus_state_destroy(state);
662}
663
664static void
665focus_state_surface_destroy(struct wl_listener *listener, void *data)
666{
667 struct focus_state *state = container_of(listener,
668 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400669 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400670 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500671 struct weston_surface *main_surface, *next;
672 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200673
Pekka Paalanen01388e22013-04-25 13:57:44 +0300674 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
675
Kristian Høgsberge3778222012-07-31 17:29:30 -0400676 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500677 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
678 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400679 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100680 if (is_focus_view(view))
681 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400682
Jason Ekstranda7af7042013-10-12 22:38:11 -0500683 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400684 break;
685 }
686
Pekka Paalanen01388e22013-04-25 13:57:44 +0300687 /* if the focus was a sub-surface, activate its main surface */
688 if (main_surface != state->keyboard_focus)
689 next = main_surface;
690
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100691 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400692 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100693 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100694 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400695 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
697 if (state->ws->focus_animation)
698 weston_view_animation_destroy(state->ws->focus_animation);
699
700 state->ws->focus_animation = weston_fade_run(
701 state->ws->fsurf_front->view,
702 state->ws->fsurf_front->view->alpha, 0.0, 300,
703 focus_animation_done, state->ws);
704 }
705
Kristian Høgsberge3778222012-07-31 17:29:30 -0400706 wl_list_remove(&state->link);
707 focus_state_destroy(state);
708 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200709}
710
711static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400712focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200713{
Jonas Ådahl04769742012-06-13 00:01:24 +0200714 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200715
716 state = malloc(sizeof *state);
717 if (state == NULL)
718 return NULL;
719
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100720 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400721 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200722 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400723 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200724
725 state->seat_destroy_listener.notify = focus_state_seat_destroy;
726 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400727 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200728 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400729 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200730
731 return state;
732}
733
Jonas Ådahl8538b222012-08-29 22:13:03 +0200734static struct focus_state *
735ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
736{
737 struct workspace *ws = get_current_workspace(shell);
738 struct focus_state *state;
739
740 wl_list_for_each(state, &ws->focus_list, link)
741 if (state->seat == seat)
742 break;
743
744 if (&state->link == &ws->focus_list)
745 state = focus_state_create(seat, ws);
746
747 return state;
748}
749
Jonas Ådahl04769742012-06-13 00:01:24 +0200750static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800751focus_state_set_focus(struct focus_state *state,
752 struct weston_surface *surface)
753{
754 if (state->keyboard_focus) {
755 wl_list_remove(&state->surface_destroy_listener.link);
756 wl_list_init(&state->surface_destroy_listener.link);
757 }
758
759 state->keyboard_focus = surface;
760 if (surface)
761 wl_signal_add(&surface->destroy_signal,
762 &state->surface_destroy_listener);
763}
764
765static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400766restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200767{
768 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400769 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100770 struct wl_list pending_seat_list;
771 struct weston_seat *seat, *next_seat;
772
773 /* Temporarily steal the list of seats so that we can keep
774 * track of the seats we've already processed */
775 wl_list_init(&pending_seat_list);
776 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
777 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200778
779 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100780 wl_list_remove(&state->seat->link);
781 wl_list_insert(&shell->compositor->seat_list,
782 &state->seat->link);
783
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800784 if (state->seat->keyboard == NULL)
785 continue;
786
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400787 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200788
Kristian Høgsberge3148752013-05-06 23:19:49 -0400789 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200790 }
Neil Roberts4237f502014-04-09 16:33:31 +0100791
792 /* For any remaining seats that we don't have a focus state
793 * for we'll reset the keyboard focus to NULL */
794 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
795 wl_list_insert(&shell->compositor->seat_list, &seat->link);
796
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300797 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100798 continue;
799
800 weston_keyboard_set_focus(seat->keyboard, NULL);
801 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200802}
803
804static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200805replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
806 struct weston_seat *seat)
807{
808 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200809
810 wl_list_for_each(state, &ws->focus_list, link) {
811 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800812 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200813 return;
814 }
815 }
816}
817
818static void
819drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
820 struct weston_surface *surface)
821{
822 struct focus_state *state;
823
824 wl_list_for_each(state, &ws->focus_list, link)
825 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800826 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200827}
828
829static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100830animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
831 struct weston_view *from, struct weston_view *to)
832{
833 struct weston_output *output;
834 bool focus_surface_created = false;
835
836 /* FIXME: Only support dim animation using two layers */
837 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
838 return;
839
840 output = get_default_output(shell->compositor);
841 if (ws->fsurf_front == NULL && (from || to)) {
842 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800843 if (ws->fsurf_front == NULL)
844 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100845 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800846
847 ws->fsurf_back = create_focus_surface(shell->compositor, output);
848 if (ws->fsurf_back == NULL) {
849 focus_surface_destroy(ws->fsurf_front);
850 return;
851 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100852 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800853
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854 focus_surface_created = true;
855 } else {
856 wl_list_remove(&ws->fsurf_front->view->layer_link);
857 wl_list_remove(&ws->fsurf_back->view->layer_link);
858 }
859
860 if (ws->focus_animation) {
861 weston_view_animation_destroy(ws->focus_animation);
862 ws->focus_animation = NULL;
863 }
864
865 if (to)
866 wl_list_insert(&to->layer_link,
867 &ws->fsurf_front->view->layer_link);
868 else if (from)
869 wl_list_insert(&ws->layer.view_list,
870 &ws->fsurf_front->view->layer_link);
871
872 if (focus_surface_created) {
873 ws->focus_animation = weston_fade_run(
874 ws->fsurf_front->view,
875 ws->fsurf_front->view->alpha, 0.6, 300,
876 focus_animation_done, ws);
877 } else if (from) {
878 wl_list_insert(&from->layer_link,
879 &ws->fsurf_back->view->layer_link);
880 ws->focus_animation = weston_stable_fade_run(
881 ws->fsurf_front->view, 0.0,
882 ws->fsurf_back->view, 0.6,
883 focus_animation_done, ws);
884 } else if (to) {
885 wl_list_insert(&ws->layer.view_list,
886 &ws->fsurf_back->view->layer_link);
887 ws->focus_animation = weston_stable_fade_run(
888 ws->fsurf_front->view, 0.0,
889 ws->fsurf_back->view, 0.6,
890 focus_animation_done, ws);
891 }
892}
893
894static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200895workspace_destroy(struct workspace *ws)
896{
Jonas Ådahl04769742012-06-13 00:01:24 +0200897 struct focus_state *state, *next;
898
899 wl_list_for_each_safe(state, next, &ws->focus_list, link)
900 focus_state_destroy(state);
901
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100902 if (ws->fsurf_front)
903 focus_surface_destroy(ws->fsurf_front);
904 if (ws->fsurf_back)
905 focus_surface_destroy(ws->fsurf_back);
906
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907 free(ws);
908}
909
Jonas Ådahl04769742012-06-13 00:01:24 +0200910static void
911seat_destroyed(struct wl_listener *listener, void *data)
912{
913 struct weston_seat *seat = data;
914 struct focus_state *state, *next;
915 struct workspace *ws = container_of(listener,
916 struct workspace,
917 seat_destroyed_listener);
918
919 wl_list_for_each_safe(state, next, &ws->focus_list, link)
920 if (state->seat == seat)
921 wl_list_remove(&state->link);
922}
923
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200924static struct workspace *
925workspace_create(void)
926{
927 struct workspace *ws = malloc(sizeof *ws);
928 if (ws == NULL)
929 return NULL;
930
931 weston_layer_init(&ws->layer, NULL);
932
Jonas Ådahl04769742012-06-13 00:01:24 +0200933 wl_list_init(&ws->focus_list);
934 wl_list_init(&ws->seat_destroyed_listener.link);
935 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100936 ws->fsurf_front = NULL;
937 ws->fsurf_back = NULL;
938 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200939
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940 return ws;
941}
942
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200943static int
944workspace_is_empty(struct workspace *ws)
945{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200947}
948
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200949static struct workspace *
950get_workspace(struct desktop_shell *shell, unsigned int index)
951{
952 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200953 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200954 pws += index;
955 return *pws;
956}
957
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800958struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200959get_current_workspace(struct desktop_shell *shell)
960{
961 return get_workspace(shell, shell->workspaces.current);
962}
963
964static void
965activate_workspace(struct desktop_shell *shell, unsigned int index)
966{
967 struct workspace *ws;
968
969 ws = get_workspace(shell, index);
970 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
971
972 shell->workspaces.current = index;
973}
974
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975static unsigned int
976get_output_height(struct weston_output *output)
977{
978 return abs(output->region.extents.y1 - output->region.extents.y2);
979}
980
981static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100982view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984 struct weston_transform *transform;
985
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100986 if (is_focus_view(view)) {
987 struct focus_surface *fsurf = get_focus_surface(view->surface);
988 transform = &fsurf->workspace_transform;
989 } else {
990 struct shell_surface *shsurf = get_shell_surface(view->surface);
991 transform = &shsurf->workspace_transform;
992 }
993
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998 weston_matrix_init(&transform->matrix);
999 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002}
1003
1004static void
1005workspace_translate_out(struct workspace *ws, double fraction)
1006{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008 unsigned int height;
1009 double d;
1010
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1012 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013 d = height * fraction;
1014
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 }
1017}
1018
1019static void
1020workspace_translate_in(struct workspace *ws, double fraction)
1021{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001023 unsigned int height;
1024 double d;
1025
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1027 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028
1029 if (fraction > 0)
1030 d = -(height - height * fraction);
1031 else
1032 d = height + height * fraction;
1033
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001034 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035 }
1036}
1037
1038static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001039broadcast_current_workspace_state(struct desktop_shell *shell)
1040{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001041 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001042
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001043 wl_resource_for_each(resource, &shell->workspaces.client_list)
1044 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001045 shell->workspaces.current,
1046 shell->workspaces.num);
1047}
1048
1049static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050reverse_workspace_change_animation(struct desktop_shell *shell,
1051 unsigned int index,
1052 struct workspace *from,
1053 struct workspace *to)
1054{
1055 shell->workspaces.current = index;
1056
1057 shell->workspaces.anim_to = to;
1058 shell->workspaces.anim_from = from;
1059 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1060 shell->workspaces.anim_timestamp = 0;
1061
Scott Moreau4272e632012-08-13 09:58:41 -06001062 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063}
1064
1065static void
1066workspace_deactivate_transforms(struct workspace *ws)
1067{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001069 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001072 if (is_focus_view(view)) {
1073 struct focus_surface *fsurf = get_focus_surface(view->surface);
1074 transform = &fsurf->workspace_transform;
1075 } else {
1076 struct shell_surface *shsurf = get_shell_surface(view->surface);
1077 transform = &shsurf->workspace_transform;
1078 }
1079
1080 if (!wl_list_empty(&transform->link)) {
1081 wl_list_remove(&transform->link);
1082 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001083 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001085 }
1086}
1087
1088static void
1089finish_workspace_change_animation(struct desktop_shell *shell,
1090 struct workspace *from,
1091 struct workspace *to)
1092{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001093 struct weston_view *view;
1094
Scott Moreau4272e632012-08-13 09:58:41 -06001095 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001096
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001097 /* Views that extend past the bottom of the output are still
1098 * visible after the workspace animation ends but before its layer
1099 * is hidden. In that case, we need to damage below those views so
1100 * that the screen is properly repainted. */
1101 wl_list_for_each(view, &from->layer.view_list, layer_link)
1102 weston_view_damage_below(view);
1103
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001104 wl_list_remove(&shell->workspaces.animation.link);
1105 workspace_deactivate_transforms(from);
1106 workspace_deactivate_transforms(to);
1107 shell->workspaces.anim_to = NULL;
1108
1109 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1110}
1111
1112static void
1113animate_workspace_change_frame(struct weston_animation *animation,
1114 struct weston_output *output, uint32_t msecs)
1115{
1116 struct desktop_shell *shell =
1117 container_of(animation, struct desktop_shell,
1118 workspaces.animation);
1119 struct workspace *from = shell->workspaces.anim_from;
1120 struct workspace *to = shell->workspaces.anim_to;
1121 uint32_t t;
1122 double x, y;
1123
1124 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1125 finish_workspace_change_animation(shell, from, to);
1126 return;
1127 }
1128
1129 if (shell->workspaces.anim_timestamp == 0) {
1130 if (shell->workspaces.anim_current == 0.0)
1131 shell->workspaces.anim_timestamp = msecs;
1132 else
1133 shell->workspaces.anim_timestamp =
1134 msecs -
1135 /* Invers of movement function 'y' below. */
1136 (asin(1.0 - shell->workspaces.anim_current) *
1137 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1138 M_2_PI);
1139 }
1140
1141 t = msecs - shell->workspaces.anim_timestamp;
1142
1143 /*
1144 * x = [0, π/2]
1145 * y(x) = sin(x)
1146 */
1147 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1148 y = sin(x);
1149
1150 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001151 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152
1153 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1154 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1155 shell->workspaces.anim_current = y;
1156
Scott Moreau4272e632012-08-13 09:58:41 -06001157 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001158 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001159 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161}
1162
1163static void
1164animate_workspace_change(struct desktop_shell *shell,
1165 unsigned int index,
1166 struct workspace *from,
1167 struct workspace *to)
1168{
1169 struct weston_output *output;
1170
1171 int dir;
1172
1173 if (index > shell->workspaces.current)
1174 dir = -1;
1175 else
1176 dir = 1;
1177
1178 shell->workspaces.current = index;
1179
1180 shell->workspaces.anim_dir = dir;
1181 shell->workspaces.anim_from = from;
1182 shell->workspaces.anim_to = to;
1183 shell->workspaces.anim_current = 0.0;
1184 shell->workspaces.anim_timestamp = 0;
1185
1186 output = container_of(shell->compositor->output_list.next,
1187 struct weston_output, link);
1188 wl_list_insert(&output->animation_list,
1189 &shell->workspaces.animation.link);
1190
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001191 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192
1193 workspace_translate_in(to, 0);
1194
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001195 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001196
Scott Moreau4272e632012-08-13 09:58:41 -06001197 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001198}
1199
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001200static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001201update_workspace(struct desktop_shell *shell, unsigned int index,
1202 struct workspace *from, struct workspace *to)
1203{
1204 shell->workspaces.current = index;
1205 wl_list_insert(&from->layer.link, &to->layer.link);
1206 wl_list_remove(&from->layer.link);
1207}
1208
1209static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001210change_workspace(struct desktop_shell *shell, unsigned int index)
1211{
1212 struct workspace *from;
1213 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001214 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001215
1216 if (index == shell->workspaces.current)
1217 return;
1218
1219 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001221 return;
1222
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001223 from = get_current_workspace(shell);
1224 to = get_workspace(shell, index);
1225
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001226 if (shell->workspaces.anim_from == to &&
1227 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001229 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001230 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001231 return;
1232 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001233
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001234 if (shell->workspaces.anim_to != NULL)
1235 finish_workspace_change_animation(shell,
1236 shell->workspaces.anim_from,
1237 shell->workspaces.anim_to);
1238
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001239 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001240
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001241 if (shell->focus_animation_type != ANIMATION_NONE) {
1242 wl_list_for_each(state, &from->focus_list, link)
1243 if (state->keyboard_focus)
1244 animate_focus_change(shell, from,
1245 get_default_view(state->keyboard_focus), NULL);
1246
1247 wl_list_for_each(state, &to->focus_list, link)
1248 if (state->keyboard_focus)
1249 animate_focus_change(shell, to,
1250 NULL, get_default_view(state->keyboard_focus));
1251 }
1252
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001253 if (workspace_is_empty(to) && workspace_is_empty(from))
1254 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001255 else
1256 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001257
1258 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001259}
1260
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001261static bool
1262workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1263{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265 struct wl_list *e;
1266
1267 if (wl_list_empty(list))
1268 return false;
1269
1270 e = list->next;
1271
1272 if (e->next != list)
1273 return false;
1274
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276}
1277
1278static void
Philip Withnall659163d2013-11-25 18:01:36 +00001279move_surface_to_workspace(struct desktop_shell *shell,
1280 struct shell_surface *shsurf,
1281 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001282{
1283 struct workspace *from;
1284 struct workspace *to;
1285 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001286 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001287 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001288
1289 if (workspace == shell->workspaces.current)
1290 return;
1291
Philip Withnall659163d2013-11-25 18:01:36 +00001292 view = get_default_view(shsurf->surface);
1293 if (!view)
1294 return;
1295
1296 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1297
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001298 if (workspace >= shell->workspaces.num)
1299 workspace = shell->workspaces.num - 1;
1300
Jonas Ådahle9d22502012-08-29 22:13:01 +02001301 from = get_current_workspace(shell);
1302 to = get_workspace(shell, workspace);
1303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 wl_list_remove(&view->layer_link);
1305 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001306
Philip Withnall648a4dd2013-11-25 18:01:44 +00001307 shell_surface_update_child_surface_layers(shsurf);
1308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001310 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1311 if (!seat->keyboard)
1312 continue;
1313
1314 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001316 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001317 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001320}
1321
1322static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001323take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001324 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001325 unsigned int index)
1326{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001327 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001329 struct shell_surface *shsurf;
1330 struct workspace *from;
1331 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001332 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001333
Pekka Paalanen01388e22013-04-25 13:57:44 +03001334 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 view = get_default_view(surface);
1336 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001337 index == shell->workspaces.current ||
1338 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001339 return;
1340
1341 from = get_current_workspace(shell);
1342 to = get_workspace(shell, index);
1343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 wl_list_remove(&view->layer_link);
1345 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001346
Philip Withnall659163d2013-11-25 18:01:36 +00001347 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001348 if (shsurf != NULL)
1349 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001350
Jonas Ådahle9d22502012-08-29 22:13:01 +02001351 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001352 drop_focus_state(shell, from, surface);
1353
1354 if (shell->workspaces.anim_from == to &&
1355 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001356 wl_list_remove(&to->layer.link);
1357 wl_list_insert(from->layer.link.prev, &to->layer.link);
1358
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001359 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001360 broadcast_current_workspace_state(shell);
1361
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001362 return;
1363 }
1364
1365 if (shell->workspaces.anim_to != NULL)
1366 finish_workspace_change_animation(shell,
1367 shell->workspaces.anim_from,
1368 shell->workspaces.anim_to);
1369
1370 if (workspace_is_empty(from) &&
1371 workspace_has_only(to, surface))
1372 update_workspace(shell, index, from, to);
1373 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001374 if (shsurf != NULL &&
1375 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001376 wl_list_insert(&shell->workspaces.anim_sticky_list,
1377 &shsurf->workspace_transform.link);
1378
1379 animate_workspace_change(shell, index, from, to);
1380 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001381
1382 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001383
1384 state = ensure_focus_state(shell, seat);
1385 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001386 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001387}
1388
1389static void
1390workspace_manager_move_surface(struct wl_client *client,
1391 struct wl_resource *resource,
1392 struct wl_resource *surface_resource,
1393 uint32_t workspace)
1394{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001395 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001396 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001397 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001398 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001399 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001400
Pekka Paalanen01388e22013-04-25 13:57:44 +03001401 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001402 shell_surface = get_shell_surface(main_surface);
1403 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001405
1406 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001407}
1408
1409static const struct workspace_manager_interface workspace_manager_implementation = {
1410 workspace_manager_move_surface,
1411};
1412
1413static void
1414unbind_resource(struct wl_resource *resource)
1415{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001416 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001417}
1418
1419static void
1420bind_workspace_manager(struct wl_client *client,
1421 void *data, uint32_t version, uint32_t id)
1422{
1423 struct desktop_shell *shell = data;
1424 struct wl_resource *resource;
1425
Jason Ekstranda85118c2013-06-27 20:17:02 -05001426 resource = wl_resource_create(client,
1427 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001428
1429 if (resource == NULL) {
1430 weston_log("couldn't add workspace manager object");
1431 return;
1432 }
1433
Jason Ekstranda85118c2013-06-27 20:17:02 -05001434 wl_resource_set_implementation(resource,
1435 &workspace_manager_implementation,
1436 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001437 wl_list_insert(&shell->workspaces.client_list,
1438 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001439
1440 workspace_manager_send_state(resource,
1441 shell->workspaces.current,
1442 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001443}
1444
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001445static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001446touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1447 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1448{
1449}
1450
1451static void
1452touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1453{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001454 struct weston_touch_move_grab *move =
1455 (struct weston_touch_move_grab *) container_of(
1456 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001457
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001458 if (touch_id == 0)
1459 move->active = 0;
1460
Jonas Ådahl9484b692013-12-02 22:05:03 +01001461 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001462 shell_touch_grab_end(&move->base);
1463 free(move);
1464 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001465}
1466
1467static void
1468touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1469 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1470{
1471 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1472 struct shell_surface *shsurf = move->base.shsurf;
1473 struct weston_surface *es;
1474 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1475 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1476
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001477 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001478 return;
1479
1480 es = shsurf->surface;
1481
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001482 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001483
1484 weston_compositor_schedule_repaint(es->compositor);
1485}
1486
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001487static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001488touch_move_grab_frame(struct weston_touch_grab *grab)
1489{
1490}
1491
1492static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001493touch_move_grab_cancel(struct weston_touch_grab *grab)
1494{
1495 struct weston_touch_move_grab *move =
1496 (struct weston_touch_move_grab *) container_of(
1497 grab, struct shell_touch_grab, grab);
1498
1499 shell_touch_grab_end(&move->base);
1500 free(move);
1501}
1502
Rusty Lynch1084da52013-08-15 09:10:08 -07001503static const struct weston_touch_grab_interface touch_move_grab_interface = {
1504 touch_move_grab_down,
1505 touch_move_grab_up,
1506 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001507 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001508 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001509};
1510
1511static int
1512surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1513{
1514 struct weston_touch_move_grab *move;
1515
1516 if (!shsurf)
1517 return -1;
1518
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001519 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001520 return 0;
1521
1522 move = malloc(sizeof *move);
1523 if (!move)
1524 return -1;
1525
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001526 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001528 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 seat->touch->grab_y;
1531
1532 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1533 seat->touch);
1534
1535 return 0;
1536}
1537
1538static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001539noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001540{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001541}
1542
1543static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001544constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1545{
1546 struct shell_surface *shsurf = move->base.shsurf;
1547 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001548 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001549 const int safety = 50;
1550
1551 x = wl_fixed_to_int(pointer->x + move->dx);
1552 y = wl_fixed_to_int(pointer->y + move->dy);
1553
1554 panel_height = get_output_panel_height(shsurf->shell,
1555 shsurf->surface->output);
1556 bottom = y + shsurf->surface->height - shsurf->margin.bottom;
1557 if (bottom - panel_height < safety)
1558 y = panel_height + safety -
1559 shsurf->surface->height + shsurf->margin.bottom;
1560
1561 if (move->client_initiated &&
1562 y + shsurf->margin.top < panel_height)
1563 y = panel_height - shsurf->margin.top;
1564
1565 *cx = x;
1566 *cy = y;
1567}
1568
1569static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001570move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1571 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001572{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001574 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001575 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001576 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001577
1578 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001579 if (!shsurf)
1580 return;
1581
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001582 constrain_position(move, &cx, &cy);
1583
1584 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001585
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001587}
1588
1589static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001590move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001591 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001592{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001593 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1594 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001595 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001596 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001597
Daniel Stone4dbadb12012-05-30 16:31:51 +01001598 if (pointer->button_count == 0 &&
1599 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001600 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001601 free(grab);
1602 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001603}
1604
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001605static void
1606move_grab_cancel(struct weston_pointer_grab *grab)
1607{
1608 struct shell_grab *shell_grab =
1609 container_of(grab, struct shell_grab, grab);
1610
1611 shell_grab_end(shell_grab);
1612 free(grab);
1613}
1614
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001615static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001616 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001617 move_grab_motion,
1618 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001619 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001620};
1621
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001622static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001623surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1624 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625{
1626 struct weston_move_grab *move;
1627
1628 if (!shsurf)
1629 return -1;
1630
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001631 if (shsurf->grabbed ||
1632 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 return 0;
1634
1635 move = malloc(sizeof *move);
1636 if (!move)
1637 return -1;
1638
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001640 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001642 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001643 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644
1645 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001646 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647
1648 return 0;
1649}
1650
1651static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001652common_surface_move(struct wl_resource *resource,
1653 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001655 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001656 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001657 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001658
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001659 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001660 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001661 seat->pointer->button_count > 0 &&
1662 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001664 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001665 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001666 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001667 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001668 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001669 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001671 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001672 (surface_touch_move(shsurf, seat) < 0))
1673 wl_resource_post_no_memory(resource);
1674 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001675}
1676
Rafael Antognollie2a34552013-12-03 15:35:45 -02001677static void
1678shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1679 struct wl_resource *seat_resource, uint32_t serial)
1680{
1681 common_surface_move(resource, seat_resource, serial);
1682}
1683
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684struct weston_resize_grab {
1685 struct shell_grab base;
1686 uint32_t edges;
1687 int32_t width, height;
1688};
1689
1690static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001691resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1692 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001693{
1694 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001695 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696 struct shell_surface *shsurf = resize->base.shsurf;
1697 int32_t width, height;
1698 wl_fixed_t from_x, from_y;
1699 wl_fixed_t to_x, to_y;
1700
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001701 weston_pointer_move(pointer, x, y);
1702
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001703 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704 return;
1705
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 weston_view_from_global_fixed(shsurf->view,
1707 pointer->grab_x, pointer->grab_y,
1708 &from_x, &from_y);
1709 weston_view_from_global_fixed(shsurf->view,
1710 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711
1712 width = resize->width;
1713 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1714 width += wl_fixed_to_int(from_x - to_x);
1715 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1716 width += wl_fixed_to_int(to_x - from_x);
1717 }
1718
1719 height = resize->height;
1720 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1721 height += wl_fixed_to_int(from_y - to_y);
1722 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1723 height += wl_fixed_to_int(to_y - from_y);
1724 }
1725
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001726 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727}
1728
1729static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001730send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731{
1732 struct shell_surface *shsurf = get_shell_surface(surface);
1733
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001734 assert(shsurf);
1735
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001736 if (shsurf->resource)
1737 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001738 shsurf->resize_edges,
1739 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740}
1741
1742static const struct weston_shell_client shell_client = {
1743 send_configure
1744};
1745
1746static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001747resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748 uint32_t time, uint32_t button, uint32_t state_w)
1749{
1750 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001751 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752 enum wl_pointer_button_state state = state_w;
1753
1754 if (pointer->button_count == 0 &&
1755 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1756 shell_grab_end(&resize->base);
1757 free(grab);
1758 }
1759}
1760
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001761static void
1762resize_grab_cancel(struct weston_pointer_grab *grab)
1763{
1764 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1765
1766 shell_grab_end(&resize->base);
1767 free(grab);
1768}
1769
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001770static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771 noop_grab_focus,
1772 resize_grab_motion,
1773 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001774 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001775};
1776
Giulio Camuffob8366642013-04-25 13:57:46 +03001777/*
1778 * Returns the bounding box of a surface and all its sub-surfaces,
1779 * in the surface coordinates system. */
1780static void
1781surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1782 int32_t *y, int32_t *w, int32_t *h) {
1783 pixman_region32_t region;
1784 pixman_box32_t *box;
1785 struct weston_subsurface *subsurface;
1786
1787 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788 surface->width,
1789 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001790
1791 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1792 pixman_region32_union_rect(&region, &region,
1793 subsurface->position.x,
1794 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001795 subsurface->surface->width,
1796 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001797 }
1798
1799 box = pixman_region32_extents(&region);
1800 if (x)
1801 *x = box->x1;
1802 if (y)
1803 *y = box->y1;
1804 if (w)
1805 *w = box->x2 - box->x1;
1806 if (h)
1807 *h = box->y2 - box->y1;
1808
1809 pixman_region32_fini(&region);
1810}
1811
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001812static int
1813surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001814 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001815{
1816 struct weston_resize_grab *resize;
1817
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001818 if (shsurf->grabbed ||
1819 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820 return 0;
1821
1822 if (edges == 0 || edges > 15 ||
1823 (edges & 3) == 3 || (edges & 12) == 12)
1824 return 0;
1825
1826 resize = malloc(sizeof *resize);
1827 if (!resize)
1828 return -1;
1829
1830 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001831 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1832 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001833
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001834 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001835 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001836 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001837 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001838
1839 return 0;
1840}
1841
1842static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001843common_surface_resize(struct wl_resource *resource,
1844 struct wl_resource *seat_resource, uint32_t serial,
1845 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001846{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001847 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001848 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001849 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001850
Kristian Høgsberge3148752013-05-06 23:19:49 -04001851 if (seat->pointer->button_count == 0 ||
1852 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001853 seat->pointer->focus == NULL)
1854 return;
1855
1856 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1857 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001858 return;
1859
Kristian Høgsberge3148752013-05-06 23:19:49 -04001860 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001861 wl_resource_post_no_memory(resource);
1862}
1863
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001864static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001865shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1866 struct wl_resource *seat_resource, uint32_t serial,
1867 uint32_t edges)
1868{
1869 common_surface_resize(resource, seat_resource, serial, edges);
1870}
1871
1872static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001873busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001874{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001875 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001876 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001877 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001878 wl_fixed_t sx, sy;
1879
Jason Ekstranda7af7042013-10-12 22:38:11 -05001880 view = weston_compositor_pick_view(pointer->seat->compositor,
1881 pointer->x, pointer->y,
1882 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001883
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001884 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1885 shell_grab_end(grab);
1886 free(grab);
1887 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001888}
1889
1890static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001891busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1892 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001893{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001894 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001895}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001896
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001897static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001898busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001899 uint32_t time, uint32_t button, uint32_t state)
1900{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001901 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001902 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001903 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001904
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001905 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001906 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001907 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001908 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001909 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001910 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001911 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001912}
1913
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001914static void
1915busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1916{
1917 struct shell_grab *grab = (struct shell_grab *) base;
1918
1919 shell_grab_end(grab);
1920 free(grab);
1921}
1922
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001923static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001924 busy_cursor_grab_focus,
1925 busy_cursor_grab_motion,
1926 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001927 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001928};
1929
1930static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001931set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001932{
1933 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001934
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001935 if (pointer->grab->interface == &busy_cursor_grab_interface)
1936 return;
1937
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001938 grab = malloc(sizeof *grab);
1939 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001940 return;
1941
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001942 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1943 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001944 /* Mark the shsurf as ungrabbed so that button binding is able
1945 * to move it. */
1946 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001947}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001948
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001949static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001950end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001951{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001952 struct shell_grab *grab;
1953 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001954
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001955 wl_list_for_each(seat, &compositor->seat_list, link) {
1956 if (seat->pointer == NULL)
1957 continue;
1958
1959 grab = (struct shell_grab *) seat->pointer->grab;
1960 if (grab->grab.interface == &busy_cursor_grab_interface &&
1961 wl_resource_get_client(grab->shsurf->resource) == client) {
1962 shell_grab_end(grab);
1963 free(grab);
1964 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001965 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001966}
1967
Scott Moreau9521d5e2012-04-19 13:06:17 -06001968static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001969handle_shell_client_destroy(struct wl_listener *listener, void *data);
1970
1971static int
1972xdg_ping_timeout_handler(void *data)
1973{
1974 struct shell_client *sc = data;
1975 struct weston_seat *seat;
1976 struct shell_surface *shsurf;
1977
1978 /* Client is not responding */
1979 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001980 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1981 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1982 continue;
1983 if (seat->pointer->focus->surface->resource == NULL)
1984 continue;
1985
1986 shsurf = get_shell_surface(seat->pointer->focus->surface);
1987 if (shsurf &&
1988 wl_resource_get_client(shsurf->resource) == sc->client)
1989 set_busy_cursor(shsurf, seat->pointer);
1990 }
1991
1992 return 1;
1993}
1994
1995static void
1996handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1997{
1998 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001999 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002000 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002001 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002002
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002003 if (sc->unresponsive) {
2004 xdg_ping_timeout_handler(sc);
2005 return;
2006 }
2007
2008 sc->ping_serial = serial;
2009 loop = wl_display_get_event_loop(compositor->wl_display);
2010 if (sc->ping_timer == NULL)
2011 sc->ping_timer =
2012 wl_event_loop_add_timer(loop,
2013 xdg_ping_timeout_handler, sc);
2014 if (sc->ping_timer == NULL)
2015 return;
2016
2017 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2018
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002019 if (shell_surface_is_xdg_surface(shsurf) ||
2020 shell_surface_is_xdg_popup(shsurf))
2021 xdg_shell_send_ping(sc->resource, serial);
2022 else if (shell_surface_is_wl_shell_surface(shsurf))
2023 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002024}
2025
Scott Moreauff1db4a2012-04-17 19:06:18 -06002026static void
2027ping_handler(struct weston_surface *surface, uint32_t serial)
2028{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002029 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002030
2031 if (!shsurf)
2032 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002033 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002034 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002035 if (shsurf->surface == shsurf->shell->grab_surface)
2036 return;
2037
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002038 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002039}
2040
2041static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002042handle_pointer_focus(struct wl_listener *listener, void *data)
2043{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002044 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002046 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047 uint32_t serial;
2048
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002050 return;
2051
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002053 serial = wl_display_next_serial(compositor->wl_display);
2054 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002055}
2056
2057static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002058shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2059{
2060 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002061 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002062}
2063
2064static void
2065shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2066{
2067 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002068 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002069}
2070
2071static void
2072handle_keyboard_focus(struct wl_listener *listener, void *data)
2073{
2074 struct weston_keyboard *keyboard = data;
2075 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2076
2077 if (seat->focused_surface) {
2078 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2079 if (shsurf)
2080 shell_surface_lose_keyboard_focus(shsurf);
2081 }
2082
2083 seat->focused_surface = keyboard->focus;
2084
2085 if (seat->focused_surface) {
2086 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2087 if (shsurf)
2088 shell_surface_gain_keyboard_focus(shsurf);
2089 }
2090}
2091
2092static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002093shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002094{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002095 if (sc->ping_serial != serial)
2096 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002097
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002098 sc->unresponsive = 0;
2099 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002100
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002101 if (sc->ping_timer) {
2102 wl_event_source_remove(sc->ping_timer);
2103 sc->ping_timer = NULL;
2104 }
2105
2106}
2107
2108static void
2109shell_surface_pong(struct wl_client *client,
2110 struct wl_resource *resource, uint32_t serial)
2111{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002112 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2113 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002114
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002115 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002116}
2117
2118static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002119set_title(struct shell_surface *shsurf, const char *title)
2120{
2121 free(shsurf->title);
2122 shsurf->title = strdup(title);
2123}
2124
2125static void
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002126set_margin(struct shell_surface *shsurf,
2127 int32_t left, int32_t right, int32_t top, int32_t bottom)
2128{
2129 shsurf->margin.left = left;
2130 shsurf->margin.right = right;
2131 shsurf->margin.top = top;
2132 shsurf->margin.bottom = bottom;
2133}
2134
2135static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002136shell_surface_set_title(struct wl_client *client,
2137 struct wl_resource *resource, const char *title)
2138{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002139 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002140
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002141 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002142}
2143
2144static void
2145shell_surface_set_class(struct wl_client *client,
2146 struct wl_resource *resource, const char *class)
2147{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002148 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002149
2150 free(shsurf->class);
2151 shsurf->class = strdup(class);
2152}
2153
Alex Wu4539b082012-03-01 12:57:46 +08002154static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002155restore_output_mode(struct weston_output *output)
2156{
Hardening57388e42013-09-18 23:56:36 +02002157 if (output->current_mode != output->original_mode ||
2158 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002159 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002160 output->original_mode,
2161 output->original_scale,
2162 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002163}
2164
2165static void
2166restore_all_output_modes(struct weston_compositor *compositor)
2167{
2168 struct weston_output *output;
2169
2170 wl_list_for_each(output, &compositor->output_list, link)
2171 restore_output_mode(output);
2172}
2173
Philip Withnall07926d92013-11-25 18:01:40 +00002174/* The surface will be inserted into the list immediately after the link
2175 * returned by this function (i.e. will be stacked immediately above the
2176 * returned link). */
2177static struct wl_list *
2178shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2179{
2180 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002181 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002182
2183 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002184 case SHELL_SURFACE_XWAYLAND:
2185 return &shsurf->shell->fullscreen_layer.view_list;
2186
2187 case SHELL_SURFACE_NONE:
2188 return NULL;
2189
Kristian Høgsbergead52422014-01-01 13:51:52 -08002190 case SHELL_SURFACE_POPUP:
2191 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002192 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002193 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002194 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002195 /* Move the surface to its parent layer so
2196 * that surfaces which are transient for
2197 * fullscreen surfaces don't get hidden by the
2198 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002199
2200 /* TODO: Handle a parent with multiple views */
2201 parent = get_default_view(shsurf->parent);
2202 if (parent)
2203 return parent->layer_link.prev;
2204 }
Philip Withnall07926d92013-11-25 18:01:40 +00002205
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002206 /* Move the surface to a normal workspace layer so that surfaces
2207 * which were previously fullscreen or transient are no longer
2208 * rendered on top. */
2209 ws = get_current_workspace(shsurf->shell);
2210 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002211 }
2212
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002213 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002214}
2215
Philip Withnall648a4dd2013-11-25 18:01:44 +00002216static void
2217shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2218{
2219 struct shell_surface *child;
2220
2221 /* Move the child layers to the same workspace as shsurf. They will be
2222 * stacked above shsurf. */
2223 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2224 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002225 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002226 weston_view_geometry_dirty(child->view);
2227 wl_list_remove(&child->view->layer_link);
2228 wl_list_insert(shsurf->view->layer_link.prev,
2229 &child->view->layer_link);
2230 weston_view_geometry_dirty(child->view);
2231 weston_surface_damage(child->surface);
2232
2233 /* Recurse. We don’t expect this to recurse very far (if
2234 * at all) because that would imply we have transient
2235 * (or popup) children of transient surfaces, which
2236 * would be unusual. */
2237 shell_surface_update_child_surface_layers(child);
2238 }
2239 }
2240}
2241
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002242/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002243 * geometry to ensure the changes are redrawn.
2244 *
2245 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2246 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002247static void
2248shell_surface_update_layer(struct shell_surface *shsurf)
2249{
2250 struct wl_list *new_layer_link;
2251
2252 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2253
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002254 if (new_layer_link == NULL)
2255 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002256 if (new_layer_link == &shsurf->view->layer_link)
2257 return;
2258
2259 weston_view_geometry_dirty(shsurf->view);
2260 wl_list_remove(&shsurf->view->layer_link);
2261 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2262 weston_view_geometry_dirty(shsurf->view);
2263 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002264
2265 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002266}
2267
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002268static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002269shell_surface_set_parent(struct shell_surface *shsurf,
2270 struct weston_surface *parent)
2271{
2272 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002273
2274 wl_list_remove(&shsurf->children_link);
2275 wl_list_init(&shsurf->children_link);
2276
2277 /* Insert into the parent surface’s child list. */
2278 if (parent != NULL) {
2279 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2280 if (parent_shsurf != NULL)
2281 wl_list_insert(&parent_shsurf->children_list,
2282 &shsurf->children_link);
2283 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002284}
2285
2286static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002287shell_surface_set_output(struct shell_surface *shsurf,
2288 struct weston_output *output)
2289{
2290 struct weston_surface *es = shsurf->surface;
2291
2292 /* get the default output, if the client set it as NULL
2293 check whether the ouput is available */
2294 if (output)
2295 shsurf->output = output;
2296 else if (es->output)
2297 shsurf->output = es->output;
2298 else
2299 shsurf->output = get_default_output(es->compositor);
2300}
2301
2302static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002303surface_clear_next_states(struct shell_surface *shsurf)
2304{
2305 shsurf->next_state.maximized = false;
2306 shsurf->next_state.fullscreen = false;
2307
2308 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2309 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2310 shsurf->state_changed = true;
2311}
2312
2313static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002314set_toplevel(struct shell_surface *shsurf)
2315{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002316 shell_surface_set_parent(shsurf, NULL);
2317 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002318 shsurf->type = SHELL_SURFACE_TOPLEVEL;
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
2325shell_surface_set_toplevel(struct wl_client *client,
2326 struct wl_resource *resource)
2327{
2328 struct shell_surface *surface = wl_resource_get_user_data(resource);
2329
2330 set_toplevel(surface);
2331}
2332
2333static void
2334set_transient(struct shell_surface *shsurf,
2335 struct weston_surface *parent, int x, int y, uint32_t flags)
2336{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002337 assert(parent != NULL);
2338
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002339 shell_surface_set_parent(shsurf, parent);
2340
2341 surface_clear_next_states(shsurf);
2342
Philip Withnallbecb77e2013-11-25 18:01:30 +00002343 shsurf->transient.x = x;
2344 shsurf->transient.y = y;
2345 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002346
Rafael Antognollied207b42013-12-03 15:35:43 -02002347 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002348 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002349 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002350
2351 /* The layer_link is updated in set_surface_type(),
2352 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353}
2354
2355static void
2356shell_surface_set_transient(struct wl_client *client,
2357 struct wl_resource *resource,
2358 struct wl_resource *parent_resource,
2359 int x, int y, uint32_t flags)
2360{
2361 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2362 struct weston_surface *parent =
2363 wl_resource_get_user_data(parent_resource);
2364
2365 set_transient(shsurf, parent, x, y, flags);
2366}
2367
2368static void
2369set_fullscreen(struct shell_surface *shsurf,
2370 uint32_t method,
2371 uint32_t framerate,
2372 struct weston_output *output)
2373{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002374 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002375 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002376
2377 shsurf->fullscreen_output = shsurf->output;
2378 shsurf->fullscreen.type = method;
2379 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002380
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002381 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002382}
2383
2384static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002385weston_view_set_initial_position(struct weston_view *view,
2386 struct desktop_shell *shell);
2387
2388static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002389unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002390{
Philip Withnallf85fe842013-11-25 18:01:37 +00002391 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002392 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2393 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002394 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002395 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002396
Alex Wu4539b082012-03-01 12:57:46 +08002397 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2398 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002399
Alex Wu4539b082012-03-01 12:57:46 +08002400 wl_list_remove(&shsurf->fullscreen.transform.link);
2401 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002402
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 if (shsurf->fullscreen.black_view)
2404 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2405 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002406
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002407 if (shsurf->saved_position_valid)
2408 weston_view_set_position(shsurf->view,
2409 shsurf->saved_x, shsurf->saved_y);
2410 else
2411 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2412
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002413 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002414 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002415 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002416 shsurf->saved_rotation_valid = false;
2417 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002418
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002419 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002420}
2421
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002422static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002423shell_surface_set_fullscreen(struct wl_client *client,
2424 struct wl_resource *resource,
2425 uint32_t method,
2426 uint32_t framerate,
2427 struct wl_resource *output_resource)
2428{
2429 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2430 struct weston_output *output;
2431
2432 if (output_resource)
2433 output = wl_resource_get_user_data(output_resource);
2434 else
2435 output = NULL;
2436
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002437 shell_surface_set_parent(shsurf, NULL);
2438
Rafael Antognolli03b16592013-12-03 15:35:42 -02002439 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002440 shsurf->next_state.fullscreen = true;
2441 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002442 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002443}
2444
2445static void
2446set_popup(struct shell_surface *shsurf,
2447 struct weston_surface *parent,
2448 struct weston_seat *seat,
2449 uint32_t serial,
2450 int32_t x,
2451 int32_t y)
2452{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002453 assert(parent != NULL);
2454
Philip Withnallbecb77e2013-11-25 18:01:30 +00002455 shsurf->popup.shseat = get_shell_seat(seat);
2456 shsurf->popup.serial = serial;
2457 shsurf->popup.x = x;
2458 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002459
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002460 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002461}
2462
2463static void
2464shell_surface_set_popup(struct wl_client *client,
2465 struct wl_resource *resource,
2466 struct wl_resource *seat_resource,
2467 uint32_t serial,
2468 struct wl_resource *parent_resource,
2469 int32_t x, int32_t y, uint32_t flags)
2470{
2471 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002472 struct weston_surface *parent =
2473 wl_resource_get_user_data(parent_resource);
2474
2475 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002476
Rafael Antognolli03b16592013-12-03 15:35:42 -02002477 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002478 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002479 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002480 wl_resource_get_user_data(seat_resource),
2481 serial, x, y);
2482}
2483
2484static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002486{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002487 /* undo all maximized things here */
2488 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002489
2490 if (shsurf->saved_position_valid)
2491 weston_view_set_position(shsurf->view,
2492 shsurf->saved_x, shsurf->saved_y);
2493 else
2494 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002495
2496 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002497 wl_list_insert(&shsurf->view->geometry.transformation_list,
2498 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002499 shsurf->saved_rotation_valid = false;
2500 }
2501
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002502 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002503}
2504
Philip Withnallbecb77e2013-11-25 18:01:30 +00002505static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002506set_minimized(struct weston_surface *surface, uint32_t is_true)
2507{
2508 struct shell_surface *shsurf;
2509 struct workspace *current_ws;
2510 struct weston_seat *seat;
2511 struct weston_surface *focus;
2512 struct weston_view *view;
2513
2514 view = get_default_view(surface);
2515 if (!view)
2516 return;
2517
2518 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2519
2520 shsurf = get_shell_surface(surface);
2521 current_ws = get_current_workspace(shsurf->shell);
2522
2523 wl_list_remove(&view->layer_link);
2524 /* hide or show, depending on the state */
2525 if (is_true) {
2526 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2527
2528 drop_focus_state(shsurf->shell, current_ws, view->surface);
2529 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2530 if (!seat->keyboard)
2531 continue;
2532 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2533 if (focus == view->surface)
2534 weston_keyboard_set_focus(seat->keyboard, NULL);
2535 }
2536 }
2537 else {
2538 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2539
2540 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2541 if (!seat->keyboard)
2542 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002543 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002544 }
2545 }
2546
2547 shell_surface_update_child_surface_layers(shsurf);
2548
2549 weston_view_damage_below(view);
2550}
2551
2552static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002553shell_surface_set_maximized(struct wl_client *client,
2554 struct wl_resource *resource,
2555 struct wl_resource *output_resource)
2556{
2557 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2558 struct weston_output *output;
2559
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002560 surface_clear_next_states(shsurf);
2561 shsurf->next_state.maximized = true;
2562 shsurf->state_changed = true;
2563
2564 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2565 shell_surface_set_parent(shsurf, NULL);
2566
Philip Withnallbecb77e2013-11-25 18:01:30 +00002567 if (output_resource)
2568 output = wl_resource_get_user_data(output_resource);
2569 else
2570 output = NULL;
2571
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002572 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002573
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002574 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002575}
2576
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002577/* This is only ever called from set_surface_type(), so there’s no need to
2578 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002579static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002580reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002581{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002582 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002583 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002584 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002585 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002586
Pekka Paalanen98262232011-12-01 10:42:22 +02002587 return 0;
2588}
2589
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002590static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002591set_full_output(struct shell_surface *shsurf)
2592{
2593 shsurf->saved_x = shsurf->view->geometry.x;
2594 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002595 shsurf->saved_width = shsurf->surface->width;
2596 shsurf->saved_height = shsurf->surface->height;
2597 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002598 shsurf->saved_position_valid = true;
2599
2600 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2601 wl_list_remove(&shsurf->rotation.transform.link);
2602 wl_list_init(&shsurf->rotation.transform.link);
2603 weston_view_geometry_dirty(shsurf->view);
2604 shsurf->saved_rotation_valid = true;
2605 }
2606}
2607
2608static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002609set_surface_type(struct shell_surface *shsurf)
2610{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002611 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002613
Philip Withnallbecb77e2013-11-25 18:01:30 +00002614 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002615
Rafael Antognolli03b16592013-12-03 15:35:42 -02002616 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002617 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002618
2619 switch (shsurf->type) {
2620 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002621 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002622 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002623 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002624 weston_view_set_position(shsurf->view,
2625 pev->geometry.x + shsurf->transient.x,
2626 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002627 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002628 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002629
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002630 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 weston_view_set_position(shsurf->view, shsurf->transient.x,
2632 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002633 break;
2634
Philip Withnall0f640e22013-11-25 18:01:31 +00002635 case SHELL_SURFACE_POPUP:
2636 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002637 default:
2638 break;
2639 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002640
2641 /* Update the surface’s layer. */
2642 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002643}
2644
Tiago Vignattibe143262012-04-16 17:31:41 +03002645static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002646shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002647{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002648 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002649}
2650
Alex Wu21858432012-04-01 20:13:08 +08002651static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002652black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002653
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002655create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002656 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002657 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002658{
2659 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002660 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002661
2662 surface = weston_surface_create(ec);
2663 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002664 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002665 return NULL;
2666 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 view = weston_view_create(surface);
2668 if (surface == NULL) {
2669 weston_log("no memory\n");
2670 weston_surface_destroy(surface);
2671 return NULL;
2672 }
Alex Wu4539b082012-03-01 12:57:46 +08002673
Alex Wu21858432012-04-01 20:13:08 +08002674 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002675 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002676 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002677 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002678 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002679 pixman_region32_fini(&surface->input);
2680 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002681
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002682 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002683 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002684
2685 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002686}
2687
Philip Withnalled8f1a92013-11-25 18:01:43 +00002688static void
2689shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2690{
2691 struct weston_output *output = shsurf->fullscreen_output;
2692
Rafael Antognolli03b16592013-12-03 15:35:42 -02002693 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002694
2695 if (!shsurf->fullscreen.black_view)
2696 shsurf->fullscreen.black_view =
2697 create_black_surface(shsurf->surface->compositor,
2698 shsurf->surface,
2699 output->x, output->y,
2700 output->width,
2701 output->height);
2702
2703 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2704 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2705 wl_list_insert(&shsurf->view->layer_link,
2706 &shsurf->fullscreen.black_view->layer_link);
2707 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2708 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002709
2710 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002711}
2712
Alex Wu4539b082012-03-01 12:57:46 +08002713/* Create black surface and append it to the associated fullscreen surface.
2714 * Handle size dismatch and positioning according to the method. */
2715static void
2716shell_configure_fullscreen(struct shell_surface *shsurf)
2717{
2718 struct weston_output *output = shsurf->fullscreen_output;
2719 struct weston_surface *surface = shsurf->surface;
2720 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002721 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002722 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002723
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002724 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2725 restore_output_mode(output);
2726
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002727 /* Reverse the effect of lower_fullscreen_layer() */
2728 wl_list_remove(&shsurf->view->layer_link);
2729 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2730
Philip Withnalled8f1a92013-11-25 18:01:43 +00002731 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002732
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002734 &surf_width, &surf_height);
2735
Alex Wu4539b082012-03-01 12:57:46 +08002736 switch (shsurf->fullscreen.type) {
2737 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002738 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002740 break;
2741 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002742 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002743 if (output->width == surf_width &&
2744 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745 weston_view_set_position(shsurf->view,
2746 output->x - surf_x,
2747 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002748 break;
2749 }
2750
Alex Wu4539b082012-03-01 12:57:46 +08002751 matrix = &shsurf->fullscreen.transform.matrix;
2752 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002753
Scott Moreau1bad5db2012-08-18 01:04:05 -06002754 output_aspect = (float) output->width /
2755 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 /* XXX: Use surf_width and surf_height here? */
2757 surface_aspect = (float) surface->width /
2758 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002759 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002760 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002761 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002762 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002763 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002764 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002765
Alex Wu4539b082012-03-01 12:57:46 +08002766 weston_matrix_scale(matrix, scale, scale, 1);
2767 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002768 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002769 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002770 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2771 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002772 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002773
Alex Wu4539b082012-03-01 12:57:46 +08002774 break;
2775 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002776 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002777 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002778 surf_width * surface->buffer_viewport.buffer.scale,
2779 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002780 shsurf->fullscreen.framerate};
2781
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002782 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002783 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002784 weston_view_set_position(shsurf->view,
2785 output->x - surf_x,
2786 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002787 shsurf->fullscreen.black_view->surface->width = output->width;
2788 shsurf->fullscreen.black_view->surface->height = output->height;
2789 weston_view_set_position(shsurf->fullscreen.black_view,
2790 output->x - surf_x,
2791 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002792 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002793 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002794 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002795 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002796 }
Alex Wubd3354b2012-04-17 17:20:49 +08002797 }
Alex Wu4539b082012-03-01 12:57:46 +08002798 break;
2799 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002800 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002801 break;
2802 default:
2803 break;
2804 }
2805}
2806
Alex Wu4539b082012-03-01 12:57:46 +08002807static void
2808shell_map_fullscreen(struct shell_surface *shsurf)
2809{
Alex Wubd3354b2012-04-17 17:20:49 +08002810 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002811}
2812
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002813static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002814set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2815{
2816 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002817 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002818 shsurf->transient.x = x;
2819 shsurf->transient.y = y;
2820 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002821
2822 shell_surface_set_parent(shsurf, NULL);
2823
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002824 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002825 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002826}
2827
Kristian Høgsberg47792412014-05-04 13:47:06 -07002828static void
2829shell_interface_set_fullscreen(struct shell_surface *shsurf,
2830 uint32_t method,
2831 uint32_t framerate,
2832 struct weston_output *output)
2833{
2834 surface_clear_next_states(shsurf);
2835 set_fullscreen(shsurf, method, framerate, output);
2836
2837 shsurf->next_state.fullscreen = true;
2838 shsurf->state_changed = true;
2839}
2840
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002841static int
2842shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2843{
2844 return surface_move(shsurf, ws, 1);
2845}
2846
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002847static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002848
2849static void
2850destroy_shell_seat(struct wl_listener *listener, void *data)
2851{
2852 struct shell_seat *shseat =
2853 container_of(listener,
2854 struct shell_seat, seat_destroy_listener);
2855 struct shell_surface *shsurf, *prev = NULL;
2856
2857 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002858 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002859 shseat->popup_grab.client = NULL;
2860
2861 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2862 shsurf->popup.shseat = NULL;
2863 if (prev) {
2864 wl_list_init(&prev->popup.grab_link);
2865 }
2866 prev = shsurf;
2867 }
2868 wl_list_init(&prev->popup.grab_link);
2869 }
2870
2871 wl_list_remove(&shseat->seat_destroy_listener.link);
2872 free(shseat);
2873}
2874
Jason Ekstrand024177c2014-04-21 19:42:58 -05002875static void
2876shell_seat_caps_changed(struct wl_listener *listener, void *data)
2877{
2878 struct shell_seat *seat;
2879
2880 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2881
2882 if (seat->seat->keyboard &&
2883 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2884 wl_signal_add(&seat->seat->keyboard->focus_signal,
2885 &seat->keyboard_focus_listener);
2886 } else if (!seat->seat->keyboard) {
2887 wl_list_init(&seat->keyboard_focus_listener.link);
2888 }
2889
2890 if (seat->seat->pointer &&
2891 wl_list_empty(&seat->pointer_focus_listener.link)) {
2892 wl_signal_add(&seat->seat->pointer->focus_signal,
2893 &seat->pointer_focus_listener);
2894 } else if (!seat->seat->pointer) {
2895 wl_list_init(&seat->pointer_focus_listener.link);
2896 }
2897}
2898
Giulio Camuffo5085a752013-03-25 21:42:45 +01002899static struct shell_seat *
2900create_shell_seat(struct weston_seat *seat)
2901{
2902 struct shell_seat *shseat;
2903
2904 shseat = calloc(1, sizeof *shseat);
2905 if (!shseat) {
2906 weston_log("no memory to allocate shell seat\n");
2907 return NULL;
2908 }
2909
2910 shseat->seat = seat;
2911 wl_list_init(&shseat->popup_grab.surfaces_list);
2912
2913 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2914 wl_signal_add(&seat->destroy_signal,
2915 &shseat->seat_destroy_listener);
2916
Jason Ekstrand024177c2014-04-21 19:42:58 -05002917 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2918 wl_list_init(&shseat->keyboard_focus_listener.link);
2919
2920 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2921 wl_list_init(&shseat->pointer_focus_listener.link);
2922
2923 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2924 wl_signal_add(&seat->updated_caps_signal,
2925 &shseat->caps_changed_listener);
2926 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2927
Giulio Camuffo5085a752013-03-25 21:42:45 +01002928 return shseat;
2929}
2930
2931static struct shell_seat *
2932get_shell_seat(struct weston_seat *seat)
2933{
2934 struct wl_listener *listener;
2935
2936 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002937 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002938
2939 return container_of(listener,
2940 struct shell_seat, seat_destroy_listener);
2941}
2942
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002943static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002944popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002945{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002946 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002947 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002948 struct shell_seat *shseat =
2949 container_of(grab, struct shell_seat, popup_grab.grab);
2950 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002951 wl_fixed_t sx, sy;
2952
Jason Ekstranda7af7042013-10-12 22:38:11 -05002953 view = weston_compositor_pick_view(pointer->seat->compositor,
2954 pointer->x, pointer->y,
2955 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002956
Jason Ekstranda7af7042013-10-12 22:38:11 -05002957 if (view && view->surface->resource &&
2958 wl_resource_get_client(view->surface->resource) == client) {
2959 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002960 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002961 weston_pointer_set_focus(pointer, NULL,
2962 wl_fixed_from_int(0),
2963 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002964 }
2965}
2966
2967static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002968popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2969 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002970{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002971 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002972 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002973 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002974
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002975 weston_pointer_move(pointer, x, y);
2976
Neil Roberts96d790e2013-09-19 17:32:00 +01002977 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 weston_view_from_global_fixed(pointer->focus,
2979 pointer->x, pointer->y,
2980 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002981 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002982 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002983}
2984
2985static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002986popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002987 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002988{
2989 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002990 struct shell_seat *shseat =
2991 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002992 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002993 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002994 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002995 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002996
Neil Roberts96d790e2013-09-19 17:32:00 +01002997 resource_list = &grab->pointer->focus_resource_list;
2998 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002999 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003000 wl_resource_for_each(resource, resource_list) {
3001 wl_pointer_send_button(resource, serial,
3002 time, button, state);
3003 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003004 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003005 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003006 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003007 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003008 }
3009
Daniel Stone4dbadb12012-05-30 16:31:51 +01003010 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003011 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003012}
3013
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003014static void
3015popup_grab_cancel(struct weston_pointer_grab *grab)
3016{
3017 popup_grab_end(grab->pointer);
3018}
3019
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003020static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003021 popup_grab_focus,
3022 popup_grab_motion,
3023 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003024 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003025};
3026
3027static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003028shell_surface_send_popup_done(struct shell_surface *shsurf)
3029{
3030 if (shell_surface_is_wl_shell_surface(shsurf))
3031 wl_shell_surface_send_popup_done(shsurf->resource);
3032 else if (shell_surface_is_xdg_popup(shsurf))
3033 xdg_popup_send_popup_done(shsurf->resource,
3034 shsurf->popup.serial);
3035}
3036
3037static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003038popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003039{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003040 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003041 struct shell_seat *shseat =
3042 container_of(grab, struct shell_seat, popup_grab.grab);
3043 struct shell_surface *shsurf;
3044 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003045
3046 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003047 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003048 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003049 shseat->popup_grab.grab.interface = NULL;
3050 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003051 /* Send the popup_done event to all the popups open */
3052 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003053 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003054 shsurf->popup.shseat = NULL;
3055 if (prev) {
3056 wl_list_init(&prev->popup.grab_link);
3057 }
3058 prev = shsurf;
3059 }
3060 wl_list_init(&prev->popup.grab_link);
3061 wl_list_init(&shseat->popup_grab.surfaces_list);
3062 }
3063}
3064
3065static void
3066add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3067{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003068 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069
3070 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003071 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003073 /* We must make sure here that this popup was opened after
3074 * a mouse press, and not just by moving around with other
3075 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003076 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003077 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003078
Rob Bradforddfe31052013-06-26 19:49:11 +01003079 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003080 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003081 } else {
3082 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003083 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003084}
3085
3086static void
3087remove_popup_grab(struct shell_surface *shsurf)
3088{
3089 struct shell_seat *shseat = shsurf->popup.shseat;
3090
3091 wl_list_remove(&shsurf->popup.grab_link);
3092 wl_list_init(&shsurf->popup.grab_link);
3093 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003094 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003095 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003096 }
3097}
3098
3099static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003100shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003101{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003102 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003103 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003104
Jason Ekstranda7af7042013-10-12 22:38:11 -05003105 shsurf->surface->output = parent_view->output;
3106 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003107
Jason Ekstranda7af7042013-10-12 22:38:11 -05003108 weston_view_set_transform_parent(shsurf->view, parent_view);
3109 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3110 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003111
Kristian Høgsberge3148752013-05-06 23:19:49 -04003112 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003113 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003114 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003115 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003116 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003117 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003118}
3119
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003120static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003121 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003122 shell_surface_move,
3123 shell_surface_resize,
3124 shell_surface_set_toplevel,
3125 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003126 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003127 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003128 shell_surface_set_maximized,
3129 shell_surface_set_title,
3130 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003131};
3132
3133static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003134destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003135{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003136 struct shell_surface *child, *next;
3137
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003138 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3139
Giulio Camuffo5085a752013-03-25 21:42:45 +01003140 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3141 remove_popup_grab(shsurf);
3142 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003143
Alex Wubd3354b2012-04-17 17:20:49 +08003144 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003145 shell_surface_is_top_fullscreen(shsurf))
3146 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003147
Jason Ekstranda7af7042013-10-12 22:38:11 -05003148 if (shsurf->fullscreen.black_view)
3149 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003150
Alex Wubd3354b2012-04-17 17:20:49 +08003151 /* As destroy_resource() use wl_list_for_each_safe(),
3152 * we can always remove the listener.
3153 */
3154 wl_list_remove(&shsurf->surface_destroy_listener.link);
3155 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003156 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003157
Jason Ekstranda7af7042013-10-12 22:38:11 -05003158 weston_view_destroy(shsurf->view);
3159
Philip Withnall648a4dd2013-11-25 18:01:44 +00003160 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003161 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3162 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003163
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003164 wl_list_remove(&shsurf->link);
3165 free(shsurf);
3166}
3167
3168static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003169shell_destroy_shell_surface(struct wl_resource *resource)
3170{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003171 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003172
Bryan Caina46b9462014-04-04 17:41:24 -05003173 if (!wl_list_empty(&shsurf->popup.grab_link))
3174 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003175 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003176}
3177
3178static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003179shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003180{
3181 struct shell_surface *shsurf = container_of(listener,
3182 struct shell_surface,
3183 surface_destroy_listener);
3184
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003185 if (shsurf->resource)
3186 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003187
3188 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003189}
3190
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003191static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003192fade_out_done(struct weston_view_animation *animation, void *data)
3193{
3194 struct shell_surface *shsurf = data;
3195
3196 weston_surface_destroy(shsurf->surface);
3197}
3198
3199static void
3200handle_resource_destroy(struct wl_listener *listener, void *data)
3201{
3202 struct shell_surface *shsurf =
3203 container_of(listener, struct shell_surface,
3204 resource_destroy_listener);
3205
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003206 if (!weston_surface_is_mapped(shsurf->surface))
3207 return;
3208
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003209 shsurf->surface->ref_count++;
3210
3211 pixman_region32_fini(&shsurf->surface->pending.input);
3212 pixman_region32_init(&shsurf->surface->pending.input);
3213 pixman_region32_fini(&shsurf->surface->input);
3214 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003215 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3216 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003217}
3218
3219static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003220shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003221
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003222struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003223get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003224{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003225 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003226 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003227 else
3228 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003229}
3230
Rafael Antognollie2a34552013-12-03 15:35:45 -02003231static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003232create_common_surface(struct shell_client *owner, void *shell,
3233 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003234 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003235{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003236 struct shell_surface *shsurf;
3237
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003238 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003239 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003240 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003241 }
3242
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003243 shsurf = calloc(1, sizeof *shsurf);
3244 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003245 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003246 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003247 }
3248
Jason Ekstranda7af7042013-10-12 22:38:11 -05003249 shsurf->view = weston_view_create(surface);
3250 if (!shsurf->view) {
3251 weston_log("no memory to allocate shell surface\n");
3252 free(shsurf);
3253 return NULL;
3254 }
3255
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003256 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003257 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003258
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003259 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3260 wl_resource_add_destroy_listener(surface->resource,
3261 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003262 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003263
Tiago Vignattibc052c92012-04-19 16:18:18 +03003264 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003265 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003266 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003267 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003268 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003269 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003270 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3271 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003272 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003273 wl_list_init(&shsurf->fullscreen.transform.link);
3274
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003275 shsurf->output = get_default_output(shsurf->shell->compositor);
3276
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003277 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003278 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003279 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003280 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003281
3282 /* init link so its safe to always remove it in destroy_shell_surface */
3283 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003284 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003285
Pekka Paalanen460099f2012-01-20 16:48:25 +02003286 /* empty when not in use */
3287 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003288 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003289
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003290 wl_list_init(&shsurf->workspace_transform.link);
3291
Philip Withnall648a4dd2013-11-25 18:01:44 +00003292 wl_list_init(&shsurf->children_link);
3293 wl_list_init(&shsurf->children_list);
3294 shsurf->parent = NULL;
3295
Pekka Paalanen98262232011-12-01 10:42:22 +02003296 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003297
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003298 shsurf->client = client;
3299
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003300 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003301}
3302
Rafael Antognollie2a34552013-12-03 15:35:45 -02003303static struct shell_surface *
3304create_shell_surface(void *shell, struct weston_surface *surface,
3305 const struct weston_shell_client *client)
3306{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003307 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003308}
3309
Jason Ekstranda7af7042013-10-12 22:38:11 -05003310static struct weston_view *
3311get_primary_view(void *shell, struct shell_surface *shsurf)
3312{
3313 return shsurf->view;
3314}
3315
Tiago Vignattibc052c92012-04-19 16:18:18 +03003316static void
3317shell_get_shell_surface(struct wl_client *client,
3318 struct wl_resource *resource,
3319 uint32_t id,
3320 struct wl_resource *surface_resource)
3321{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003322 struct weston_surface *surface =
3323 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003324 struct shell_client *sc = wl_resource_get_user_data(resource);
3325 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003326 struct shell_surface *shsurf;
3327
3328 if (get_shell_surface(surface)) {
3329 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003330 WL_DISPLAY_ERROR_INVALID_OBJECT,
3331 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003332 return;
3333 }
3334
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003335 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003336 if (!shsurf) {
3337 wl_resource_post_error(surface_resource,
3338 WL_DISPLAY_ERROR_INVALID_OBJECT,
3339 "surface->configure already set");
3340 return;
3341 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003342
Jason Ekstranda85118c2013-06-27 20:17:02 -05003343 shsurf->resource =
3344 wl_resource_create(client,
3345 &wl_shell_surface_interface, 1, id);
3346 wl_resource_set_implementation(shsurf->resource,
3347 &shell_surface_implementation,
3348 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003349}
3350
Rafael Antognollie2a34552013-12-03 15:35:45 -02003351static bool
3352shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3353{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003354 /* A shell surface without a resource is created from xwayland
3355 * and is considered a wl_shell surface for now. */
3356
3357 return shsurf->resource == NULL ||
3358 wl_resource_instance_of(shsurf->resource,
3359 &wl_shell_surface_interface,
3360 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003361}
3362
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003363static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003364 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003365};
3366
Rafael Antognollie2a34552013-12-03 15:35:45 -02003367/****************************
3368 * xdg-shell implementation */
3369
3370static void
3371xdg_surface_destroy(struct wl_client *client,
3372 struct wl_resource *resource)
3373{
3374 wl_resource_destroy(resource);
3375}
3376
3377static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003378xdg_surface_set_parent(struct wl_client *client,
3379 struct wl_resource *resource,
3380 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003381{
3382 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3383 struct weston_surface *parent;
3384
3385 if (parent_resource)
3386 parent = wl_resource_get_user_data(parent_resource);
3387 else
3388 parent = NULL;
3389
3390 shell_surface_set_parent(shsurf, parent);
3391}
3392
3393static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003394xdg_surface_set_margin(struct wl_client *client,
3395 struct wl_resource *resource,
3396 int32_t left,
3397 int32_t right,
3398 int32_t top,
3399 int32_t bottom)
3400{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003401 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3402
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07003403 set_margin(shsurf, left, right, top, bottom);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003404}
3405
3406static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003407xdg_surface_set_app_id(struct wl_client *client,
3408 struct wl_resource *resource,
3409 const char *app_id)
3410{
3411 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3412
3413 free(shsurf->class);
3414 shsurf->class = strdup(app_id);
3415}
3416
3417static void
3418xdg_surface_set_title(struct wl_client *client,
3419 struct wl_resource *resource, const char *title)
3420{
3421 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3422
3423 set_title(shsurf, title);
3424}
3425
3426static void
3427xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3428 struct wl_resource *seat_resource, uint32_t serial)
3429{
3430 common_surface_move(resource, seat_resource, serial);
3431}
3432
3433static void
3434xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3435 struct wl_resource *seat_resource, uint32_t serial,
3436 uint32_t edges)
3437{
3438 common_surface_resize(resource, seat_resource, serial, edges);
3439}
3440
3441static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003442xdg_surface_ack_configure(struct wl_client *client,
3443 struct wl_resource *resource,
3444 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003445{
3446 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3447
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003448 if (shsurf->state_requested) {
3449 shsurf->next_state = shsurf->requested_state;
3450 shsurf->state_changed = true;
3451 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003452 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003453}
3454
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003455static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003456xdg_surface_set_maximized(struct wl_client *client,
3457 struct wl_resource *resource)
3458{
3459 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3460
3461 shsurf->state_requested = true;
3462 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003463 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003464}
3465
3466static void
3467xdg_surface_unset_maximized(struct wl_client *client,
3468 struct wl_resource *resource)
3469{
3470 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3471
3472 shsurf->state_requested = true;
3473 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003474 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003475}
3476
3477static void
3478xdg_surface_set_fullscreen(struct wl_client *client,
3479 struct wl_resource *resource,
3480 struct wl_resource *output_resource)
3481{
3482 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3483 struct weston_output *output;
3484
3485 shsurf->state_requested = true;
3486 shsurf->requested_state.fullscreen = true;
3487
3488 if (output_resource)
3489 output = wl_resource_get_user_data(output_resource);
3490 else
3491 output = NULL;
3492
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003493 shell_surface_set_output(shsurf, output);
3494 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003495
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003496 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003497}
3498
3499static void
3500xdg_surface_unset_fullscreen(struct wl_client *client,
3501 struct wl_resource *resource)
3502{
3503 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3504
3505 shsurf->state_requested = true;
3506 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003507 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003508}
3509
3510static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003511xdg_surface_set_minimized(struct wl_client *client,
3512 struct wl_resource *resource)
3513{
3514 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3515
3516 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3517 return;
3518
3519 /* apply compositor's own minimization logic (hide) */
3520 set_minimized(shsurf->surface, 1);
3521}
3522
Rafael Antognollie2a34552013-12-03 15:35:45 -02003523static const struct xdg_surface_interface xdg_surface_implementation = {
3524 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003525 xdg_surface_set_parent,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003526 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003527 xdg_surface_set_title,
3528 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003529 xdg_surface_move,
3530 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003531 xdg_surface_ack_configure,
3532 xdg_surface_set_maximized,
3533 xdg_surface_unset_maximized,
3534 xdg_surface_set_fullscreen,
3535 xdg_surface_unset_fullscreen,
3536 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003537};
3538
3539static void
3540xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003541 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003542{
3543 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003544 uint32_t *s;
3545 struct wl_array states;
3546 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003547
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003548 assert(shsurf);
3549
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003550 if (!shsurf->resource)
3551 return;
3552
3553 wl_array_init(&states);
3554 if (shsurf->requested_state.fullscreen) {
3555 s = wl_array_add(&states, sizeof *s);
3556 *s = XDG_SURFACE_STATE_FULLSCREEN;
3557 } else if (shsurf->requested_state.maximized) {
3558 s = wl_array_add(&states, sizeof *s);
3559 *s = XDG_SURFACE_STATE_MAXIMIZED;
3560 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003561 if (shsurf->resize_edges != 0) {
3562 s = wl_array_add(&states, sizeof *s);
3563 *s = XDG_SURFACE_STATE_RESIZING;
3564 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003565 if (shsurf->focus_count > 0) {
3566 s = wl_array_add(&states, sizeof *s);
3567 *s = XDG_SURFACE_STATE_ACTIVATED;
3568 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003569
3570 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3571 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3572
3573 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003574}
3575
3576static const struct weston_shell_client xdg_client = {
3577 xdg_send_configure
3578};
3579
3580static void
3581xdg_use_unstable_version(struct wl_client *client,
3582 struct wl_resource *resource,
3583 int32_t version)
3584{
3585 if (version > 1) {
3586 wl_resource_post_error(resource,
3587 1,
3588 "xdg-shell:: version not implemented yet.");
3589 return;
3590 }
3591}
3592
3593static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003594create_xdg_surface(struct shell_client *owner, void *shell,
3595 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003596 const struct weston_shell_client *client)
3597{
3598 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003599
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003600 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003601 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003602
3603 return shsurf;
3604}
3605
3606static void
3607xdg_get_xdg_surface(struct wl_client *client,
3608 struct wl_resource *resource,
3609 uint32_t id,
3610 struct wl_resource *surface_resource)
3611{
3612 struct weston_surface *surface =
3613 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003614 struct shell_client *sc = wl_resource_get_user_data(resource);
3615 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003616 struct shell_surface *shsurf;
3617
3618 if (get_shell_surface(surface)) {
3619 wl_resource_post_error(surface_resource,
3620 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003621 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003622 return;
3623 }
3624
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003625 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003626 if (!shsurf) {
3627 wl_resource_post_error(surface_resource,
3628 WL_DISPLAY_ERROR_INVALID_OBJECT,
3629 "surface->configure already set");
3630 return;
3631 }
3632
3633 shsurf->resource =
3634 wl_resource_create(client,
3635 &xdg_surface_interface, 1, id);
3636 wl_resource_set_implementation(shsurf->resource,
3637 &xdg_surface_implementation,
3638 shsurf, shell_destroy_shell_surface);
3639}
3640
3641static bool
3642shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3643{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003644 return shsurf->resource &&
3645 wl_resource_instance_of(shsurf->resource,
3646 &xdg_surface_interface,
3647 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003648}
3649
3650/* xdg-popup implementation */
3651
3652static void
3653xdg_popup_destroy(struct wl_client *client,
3654 struct wl_resource *resource)
3655{
3656 wl_resource_destroy(resource);
3657}
3658
Rafael Antognollie2a34552013-12-03 15:35:45 -02003659static const struct xdg_popup_interface xdg_popup_implementation = {
3660 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003661};
3662
3663static void
3664xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003665 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003666{
3667}
3668
3669static const struct weston_shell_client xdg_popup_client = {
3670 xdg_popup_send_configure
3671};
3672
3673static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003674create_xdg_popup(struct shell_client *owner, void *shell,
3675 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003676 const struct weston_shell_client *client,
3677 struct weston_surface *parent,
3678 struct shell_seat *seat,
3679 uint32_t serial,
3680 int32_t x, int32_t y)
3681{
3682 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003683
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003684 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003685 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003686 shsurf->popup.shseat = seat;
3687 shsurf->popup.serial = serial;
3688 shsurf->popup.x = x;
3689 shsurf->popup.y = y;
3690 shell_surface_set_parent(shsurf, parent);
3691
3692 return shsurf;
3693}
3694
3695static void
3696xdg_get_xdg_popup(struct wl_client *client,
3697 struct wl_resource *resource,
3698 uint32_t id,
3699 struct wl_resource *surface_resource,
3700 struct wl_resource *parent_resource,
3701 struct wl_resource *seat_resource,
3702 uint32_t serial,
3703 int32_t x, int32_t y, uint32_t flags)
3704{
3705 struct weston_surface *surface =
3706 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003707 struct shell_client *sc = wl_resource_get_user_data(resource);
3708 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003709 struct shell_surface *shsurf;
3710 struct weston_surface *parent;
3711 struct shell_seat *seat;
3712
3713 if (get_shell_surface(surface)) {
3714 wl_resource_post_error(surface_resource,
3715 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003716 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003717 return;
3718 }
3719
3720 if (!parent_resource) {
3721 wl_resource_post_error(surface_resource,
3722 WL_DISPLAY_ERROR_INVALID_OBJECT,
3723 "xdg_shell::get_xdg_popup requires a parent shell surface");
3724 }
3725
3726 parent = wl_resource_get_user_data(parent_resource);
3727 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3728
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003729 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003730 parent, seat, serial, x, y);
3731 if (!shsurf) {
3732 wl_resource_post_error(surface_resource,
3733 WL_DISPLAY_ERROR_INVALID_OBJECT,
3734 "surface->configure already set");
3735 return;
3736 }
3737
3738 shsurf->resource =
3739 wl_resource_create(client,
3740 &xdg_popup_interface, 1, id);
3741 wl_resource_set_implementation(shsurf->resource,
3742 &xdg_popup_implementation,
3743 shsurf, shell_destroy_shell_surface);
3744}
3745
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003746static void
3747xdg_pong(struct wl_client *client,
3748 struct wl_resource *resource, uint32_t serial)
3749{
3750 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003751
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003752 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003753}
3754
Rafael Antognollie2a34552013-12-03 15:35:45 -02003755static bool
3756shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3757{
3758 return wl_resource_instance_of(shsurf->resource,
3759 &xdg_popup_interface,
3760 &xdg_popup_implementation);
3761}
3762
3763static const struct xdg_shell_interface xdg_implementation = {
3764 xdg_use_unstable_version,
3765 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003766 xdg_get_xdg_popup,
3767 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003768};
3769
3770static int
3771xdg_shell_unversioned_dispatch(const void *implementation,
3772 void *_target, uint32_t opcode,
3773 const struct wl_message *message,
3774 union wl_argument *args)
3775{
3776 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003777 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003778
3779 if (opcode != 0) {
3780 wl_resource_post_error(resource,
3781 WL_DISPLAY_ERROR_INVALID_OBJECT,
3782 "must call use_unstable_version first");
3783 return 0;
3784 }
3785
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003786#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003787
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003788 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3789 "shell implementation doesn't match protocol version");
3790
Rafael Antognollie2a34552013-12-03 15:35:45 -02003791 if (args[0].i != XDG_SERVER_VERSION) {
3792 wl_resource_post_error(resource,
3793 WL_DISPLAY_ERROR_INVALID_OBJECT,
3794 "incompatible version, server is %d "
3795 "client wants %d",
3796 XDG_SERVER_VERSION, args[0].i);
3797 return 0;
3798 }
3799
3800 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003801 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003802
3803 return 1;
3804}
3805
3806/* end of xdg-shell implementation */
3807/***********************************/
3808
Kristian Høgsberg07937562011-04-12 17:25:42 -04003809static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003810shell_fade(struct desktop_shell *shell, enum fade_type type);
3811
3812static int
3813screensaver_timeout(void *data)
3814{
3815 struct desktop_shell *shell = data;
3816
3817 shell_fade(shell, FADE_OUT);
3818
3819 return 1;
3820}
3821
3822static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003823handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003824{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003825 struct desktop_shell *shell =
3826 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003827
Pekka Paalanen18027e52011-12-02 16:31:49 +02003828 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003829
3830 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003831 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003832}
3833
3834static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003835launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003836{
3837 if (shell->screensaver.binding)
3838 return;
3839
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003840 if (!shell->screensaver.path) {
3841 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003842 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003843 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003844
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003845 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003846 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003847 return;
3848 }
3849
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003850 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003851 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003852 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003853 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003854}
3855
3856static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003857terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003858{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003859 if (shell->screensaver.process.pid == 0)
3860 return;
3861
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003862 /* Disarm the screensaver timer, otherwise it may fire when the
3863 * compositor is not in the idle state. In that case, the screen will
3864 * be locked, but the wake_signal won't fire on user input, making the
3865 * system unresponsive. */
3866 wl_event_source_timer_update(shell->screensaver.timer, 0);
3867
Pekka Paalanen18027e52011-12-02 16:31:49 +02003868 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003869}
3870
3871static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003872configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003873{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003874 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003875
Jason Ekstranda7af7042013-10-12 22:38:11 -05003876 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3877 if (v->output == ev->output && v != ev) {
3878 weston_view_unmap(v);
3879 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003880 }
3881 }
3882
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003883 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003884
Jason Ekstranda7af7042013-10-12 22:38:11 -05003885 if (wl_list_empty(&ev->layer_link)) {
3886 wl_list_insert(&layer->view_list, &ev->layer_link);
3887 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003888 }
3889}
3890
3891static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003892background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003893{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003894 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003895 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003896
Jason Ekstranda7af7042013-10-12 22:38:11 -05003897 view = container_of(es->views.next, struct weston_view, surface_link);
3898
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003899 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003900}
3901
3902static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003903desktop_shell_set_background(struct wl_client *client,
3904 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003905 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003906 struct wl_resource *surface_resource)
3907{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003908 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003909 struct weston_surface *surface =
3910 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003911 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003912
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003913 if (surface->configure) {
3914 wl_resource_post_error(surface_resource,
3915 WL_DISPLAY_ERROR_INVALID_OBJECT,
3916 "surface role already assigned");
3917 return;
3918 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003919
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3921 weston_view_destroy(view);
3922 view = weston_view_create(surface);
3923
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003924 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003925 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003926 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003927 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003928 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003929 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003930 surface->output->width,
3931 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003932}
3933
3934static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003935panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003936{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003937 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003938 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003939
Jason Ekstranda7af7042013-10-12 22:38:11 -05003940 view = container_of(es->views.next, struct weston_view, surface_link);
3941
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003942 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003943}
3944
3945static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003946desktop_shell_set_panel(struct wl_client *client,
3947 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003948 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003949 struct wl_resource *surface_resource)
3950{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003951 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003952 struct weston_surface *surface =
3953 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003955
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003956 if (surface->configure) {
3957 wl_resource_post_error(surface_resource,
3958 WL_DISPLAY_ERROR_INVALID_OBJECT,
3959 "surface role already assigned");
3960 return;
3961 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003962
Jason Ekstranda7af7042013-10-12 22:38:11 -05003963 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3964 weston_view_destroy(view);
3965 view = weston_view_create(surface);
3966
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003967 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003968 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003969 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003970 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003971 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003972 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003973 surface->output->width,
3974 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003975}
3976
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003977static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003978lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003979{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003980 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003981 struct weston_view *view;
3982
3983 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003984
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003985 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003986 return;
3987
Jason Ekstranda7af7042013-10-12 22:38:11 -05003988 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003989
3990 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003991 wl_list_insert(&shell->lock_layer.view_list,
3992 &view->layer_link);
3993 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003994 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003995 }
3996}
3997
3998static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003999handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004000{
Tiago Vignattibe143262012-04-16 17:31:41 +03004001 struct desktop_shell *shell =
4002 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004003
Martin Minarik6d118362012-06-07 18:01:59 +02004004 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004005 shell->lock_surface = NULL;
4006}
4007
4008static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004009desktop_shell_set_lock_surface(struct wl_client *client,
4010 struct wl_resource *resource,
4011 struct wl_resource *surface_resource)
4012{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004013 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004014 struct weston_surface *surface =
4015 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004016
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004017 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004018
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004019 if (!shell->locked)
4020 return;
4021
Pekka Paalanen98262232011-12-01 10:42:22 +02004022 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004023
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004024 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004025 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004026 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004027
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004028 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004029 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004030 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004031}
4032
4033static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004034resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004035{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004036 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004037
Pekka Paalanen77346a62011-11-30 16:26:35 +02004038 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004039
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004040 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004041 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004042 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004043 &shell->input_panel_layer.link);
4044 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004045 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004046 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004047 wl_list_insert(&shell->compositor->cursor_layer.link,
4048 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004049 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004050 wl_list_insert(&shell->fullscreen_layer.link,
4051 &shell->panel_layer.link);
4052 wl_list_insert(&shell->panel_layer.link,
4053 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004054
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004055 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004056
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004057 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004058 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004059 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004060}
4061
4062static void
4063desktop_shell_unlock(struct wl_client *client,
4064 struct wl_resource *resource)
4065{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004066 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004067
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004068 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004069
4070 if (shell->locked)
4071 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004072}
4073
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004074static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004075desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004076 struct wl_resource *resource,
4077 struct wl_resource *surface_resource)
4078{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004079 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004080
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004081 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004082 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004083}
4084
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004085static void
4086desktop_shell_desktop_ready(struct wl_client *client,
4087 struct wl_resource *resource)
4088{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004089 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004090
4091 shell_fade_startup(shell);
4092}
4093
Kristian Høgsberg75840622011-09-06 13:48:16 -04004094static const struct desktop_shell_interface desktop_shell_implementation = {
4095 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004096 desktop_shell_set_panel,
4097 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004098 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004099 desktop_shell_set_grab_surface,
4100 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004101};
4102
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004103static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004104get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004105{
4106 struct shell_surface *shsurf;
4107
4108 shsurf = get_shell_surface(surface);
4109 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004110 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004111 return shsurf->type;
4112}
4113
Kristian Høgsberg75840622011-09-06 13:48:16 -04004114static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004115move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004116{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004117 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004118 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004119 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004120
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004121 if (seat->pointer->focus == NULL)
4122 return;
4123
4124 focus = seat->pointer->focus->surface;
4125
Pekka Paalanen01388e22013-04-25 13:57:44 +03004126 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004127 if (surface == NULL)
4128 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004129
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004130 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004131 if (shsurf == NULL || shsurf->state.fullscreen ||
4132 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004133 return;
4134
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004135 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004136}
4137
4138static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004139maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4140{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004141 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004142 struct weston_surface *surface;
4143 struct shell_surface *shsurf;
4144
4145 surface = weston_surface_get_main_surface(focus);
4146 if (surface == NULL)
4147 return;
4148
4149 shsurf = get_shell_surface(surface);
4150 if (shsurf == NULL)
4151 return;
4152
4153 if (!shell_surface_is_xdg_surface(shsurf))
4154 return;
4155
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004156 shsurf->state_requested = true;
4157 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004158 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004159}
4160
4161static void
4162fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4163{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004164 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004165 struct weston_surface *surface;
4166 struct shell_surface *shsurf;
4167
4168 surface = weston_surface_get_main_surface(focus);
4169 if (surface == NULL)
4170 return;
4171
4172 shsurf = get_shell_surface(surface);
4173 if (shsurf == NULL)
4174 return;
4175
4176 if (!shell_surface_is_xdg_surface(shsurf))
4177 return;
4178
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004179 shsurf->state_requested = true;
4180 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004181 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004182}
4183
4184static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004185touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4186{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004187 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004188 struct weston_surface *surface;
4189 struct shell_surface *shsurf;
4190
4191 surface = weston_surface_get_main_surface(focus);
4192 if (surface == NULL)
4193 return;
4194
4195 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004196 if (shsurf == NULL || shsurf->state.fullscreen ||
4197 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004198 return;
4199
4200 surface_touch_move(shsurf, (struct weston_seat *) seat);
4201}
4202
4203static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004204resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004205{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004206 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004207 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004208 uint32_t edges = 0;
4209 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004210 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004211
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004212 if (seat->pointer->focus == NULL)
4213 return;
4214
4215 focus = seat->pointer->focus->surface;
4216
Pekka Paalanen01388e22013-04-25 13:57:44 +03004217 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004218 if (surface == NULL)
4219 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004220
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004221 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004222 if (shsurf == NULL || shsurf->state.fullscreen ||
4223 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004224 return;
4225
Jason Ekstranda7af7042013-10-12 22:38:11 -05004226 weston_view_from_global(shsurf->view,
4227 wl_fixed_to_int(seat->pointer->grab_x),
4228 wl_fixed_to_int(seat->pointer->grab_y),
4229 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004230
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004231 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004232 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004233 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004234 edges |= 0;
4235 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004236 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004237
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004238 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004239 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004240 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004241 edges |= 0;
4242 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004243 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004244
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004245 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004246}
4247
4248static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004249surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004250 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004251{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004252 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004253 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004254 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004255 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004256
Pekka Paalanen01388e22013-04-25 13:57:44 +03004257 /* XXX: broken for windows containing sub-surfaces */
4258 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004259 if (surface == NULL)
4260 return;
4261
4262 shsurf = get_shell_surface(surface);
4263 if (!shsurf)
4264 return;
4265
Jason Ekstranda7af7042013-10-12 22:38:11 -05004266 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004267
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 if (shsurf->view->alpha > 1.0)
4269 shsurf->view->alpha = 1.0;
4270 if (shsurf->view->alpha < step)
4271 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004272
Jason Ekstranda7af7042013-10-12 22:38:11 -05004273 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004274 weston_surface_damage(surface);
4275}
4276
4277static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004278do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004279 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004280{
Daniel Stone37816df2012-05-16 18:45:18 +01004281 struct weston_seat *ws = (struct weston_seat *) seat;
4282 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004283 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004284 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004285
4286 wl_list_for_each(output, &compositor->output_list, link) {
4287 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004288 wl_fixed_to_double(seat->pointer->x),
4289 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004290 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004291 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004292 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004293 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004294 increment = -output->zoom.increment;
4295 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004296 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004297 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004298 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004299 else
4300 increment = 0;
4301
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004302 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004303
Scott Moreaue6603982012-06-11 13:07:51 -06004304 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004305 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004306 else if (output->zoom.level > output->zoom.max_level)
4307 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004308 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004309 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004310 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004311
Scott Moreaue6603982012-06-11 13:07:51 -06004312 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004313
Jason Ekstranda7af7042013-10-12 22:38:11 -05004314 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004315 }
4316 }
4317}
4318
Scott Moreauccbf29d2012-02-22 14:21:41 -07004319static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004320zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004321 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004322{
4323 do_zoom(seat, time, 0, axis, value);
4324}
4325
4326static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004327zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004328 void *data)
4329{
4330 do_zoom(seat, time, key, 0, 0);
4331}
4332
4333static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004334terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004335 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004336{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004337 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004338
Daniel Stone325fc2d2012-05-30 16:31:58 +01004339 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004340}
4341
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004342static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004343rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4344 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004345{
4346 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004347 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004348 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004349 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004350 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004351
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004352 weston_pointer_move(pointer, x, y);
4353
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004354 if (!shsurf)
4355 return;
4356
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004357 cx = 0.5f * shsurf->surface->width;
4358 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004359
Daniel Stone37816df2012-05-16 18:45:18 +01004360 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4361 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004362 r = sqrtf(dx * dx + dy * dy);
4363
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004364 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004365 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004366
4367 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004368 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004369 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004370
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004371 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004372 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004373
4374 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004375 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004376 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004377 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004378 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004379
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004380 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004381 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004382 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004383 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004384 wl_list_init(&shsurf->rotation.transform.link);
4385 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004386 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004387 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004388
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004389 /* We need to adjust the position of the surface
4390 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 cposx = shsurf->view->geometry.x + cx;
4392 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004393 dposx = rotate->center.x - cposx;
4394 dposy = rotate->center.y - cposy;
4395 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 weston_view_set_position(shsurf->view,
4397 shsurf->view->geometry.x + dposx,
4398 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004399 }
4400
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004401 /* Repaint implies weston_surface_update_transform(), which
4402 * lazily applies the damage due to rotation update.
4403 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004404 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004405}
4406
4407static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004408rotate_grab_button(struct weston_pointer_grab *grab,
4409 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004410{
4411 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004412 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004413 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004414 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004415 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004416
Daniel Stone4dbadb12012-05-30 16:31:51 +01004417 if (pointer->button_count == 0 &&
4418 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004419 if (shsurf)
4420 weston_matrix_multiply(&shsurf->rotation.rotation,
4421 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004422 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004423 free(rotate);
4424 }
4425}
4426
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004427static void
4428rotate_grab_cancel(struct weston_pointer_grab *grab)
4429{
4430 struct rotate_grab *rotate =
4431 container_of(grab, struct rotate_grab, base.grab);
4432
4433 shell_grab_end(&rotate->base);
4434 free(rotate);
4435}
4436
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004437static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004438 noop_grab_focus,
4439 rotate_grab_motion,
4440 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004441 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004442};
4443
4444static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004445surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004446{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004447 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004448 float dx, dy;
4449 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004450
Pekka Paalanen460099f2012-01-20 16:48:25 +02004451 rotate = malloc(sizeof *rotate);
4452 if (!rotate)
4453 return;
4454
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004456 surface->surface->width * 0.5f,
4457 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004458 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004459
Daniel Stone37816df2012-05-16 18:45:18 +01004460 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4461 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004462 r = sqrtf(dx * dx + dy * dy);
4463 if (r > 20.0f) {
4464 struct weston_matrix inverse;
4465
4466 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004467 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004468 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004469
4470 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004471 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004472 } else {
4473 weston_matrix_init(&surface->rotation.rotation);
4474 weston_matrix_init(&rotate->rotation);
4475 }
4476
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004477 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4478 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004479}
4480
4481static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004482rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004483 void *data)
4484{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004485 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004486 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004487 struct shell_surface *surface;
4488
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004489 if (seat->pointer->focus == NULL)
4490 return;
4491
4492 focus = seat->pointer->focus->surface;
4493
Pekka Paalanen01388e22013-04-25 13:57:44 +03004494 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004495 if (base_surface == NULL)
4496 return;
4497
4498 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004499 if (surface == NULL || surface->state.fullscreen ||
4500 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004501 return;
4502
4503 surface_rotate(surface, seat);
4504}
4505
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004506/* Move all fullscreen layers down to the current workspace and hide their
4507 * black views. The surfaces' state is set to both fullscreen and lowered,
4508 * and this is reversed when such a surface is re-configured, see
4509 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4510 *
4511 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004512 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004513void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004514lower_fullscreen_layer(struct desktop_shell *shell)
4515{
4516 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004517 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004518
4519 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004520 wl_list_for_each_reverse_safe(view, prev,
4521 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004522 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004523 struct shell_surface *shsurf = get_shell_surface(view->surface);
4524
4525 if (!shsurf)
4526 continue;
4527
4528 /* We can have a non-fullscreen popup for a fullscreen surface
4529 * in the fullscreen layer. */
4530 if (shsurf->state.fullscreen) {
4531 /* Hide the black view */
4532 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4533 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004534 weston_view_damage_below(shsurf->fullscreen.black_view);
4535
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004536 }
4537
4538 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004539 wl_list_remove(&view->layer_link);
4540 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4541 weston_view_damage_below(view);
4542 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004543
4544 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004545 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004546}
4547
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004548void
Tiago Vignattibe143262012-04-16 17:31:41 +03004549activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004550 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004551{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004552 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004553 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004554 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004555 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004556 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004557
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004558 lower_fullscreen_layer(shell);
4559
Pekka Paalanen01388e22013-04-25 13:57:44 +03004560 main_surface = weston_surface_get_main_surface(es);
4561
Daniel Stone37816df2012-05-16 18:45:18 +01004562 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004563
Jonas Ådahl8538b222012-08-29 22:13:03 +02004564 state = ensure_focus_state(shell, seat);
4565 if (state == NULL)
4566 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004567
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004568 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004569 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004570
Rafael Antognolli03b16592013-12-03 15:35:42 -02004571 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004572 assert(shsurf);
4573
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004574 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004575 shell_configure_fullscreen(shsurf);
4576 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004577 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004578
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004579 /* Update the surface’s layer. This brings it to the top of the stacking
4580 * order as appropriate. */
4581 shell_surface_update_layer(shsurf);
4582
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004583 if (shell->focus_animation_type != ANIMATION_NONE) {
4584 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004585 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004586 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004587}
4588
Alex Wu21858432012-04-01 20:13:08 +08004589/* no-op func for checking black surface */
4590static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004591black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004592{
4593}
4594
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004595static bool
Alex Wu21858432012-04-01 20:13:08 +08004596is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4597{
4598 if (es->configure == black_surface_configure) {
4599 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004600 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004601 return true;
4602 }
4603 return false;
4604}
4605
Kristian Høgsberg75840622011-09-06 13:48:16 -04004606static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004607activate_binding(struct weston_seat *seat,
4608 struct desktop_shell *shell,
4609 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004610{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004611 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004612
Alex Wu9c35e6b2012-03-05 14:13:13 +08004613 if (!focus)
4614 return;
4615
Pekka Paalanen01388e22013-04-25 13:57:44 +03004616 if (is_black_surface(focus, &main_surface))
4617 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004618
Pekka Paalanen01388e22013-04-25 13:57:44 +03004619 main_surface = weston_surface_get_main_surface(focus);
4620 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004621 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004622
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004623 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004624}
4625
4626static void
4627click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4628 void *data)
4629{
4630 if (seat->pointer->grab != &seat->pointer->default_grab)
4631 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004632 if (seat->pointer->focus == NULL)
4633 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004634
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004635 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004636}
4637
4638static void
4639touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4640{
4641 if (seat->touch->grab != &seat->touch->default_grab)
4642 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004643 if (seat->touch->focus == NULL)
4644 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004645
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004646 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004647}
4648
4649static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004650unfocus_all_seats(struct desktop_shell *shell)
4651{
4652 struct weston_seat *seat, *next;
4653
4654 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4655 if (seat->keyboard == NULL)
4656 continue;
4657
4658 weston_keyboard_set_focus(seat->keyboard, NULL);
4659 }
4660}
4661
4662static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004663lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004664{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004665 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004666
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004667 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004668 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004669 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004670 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004671
4672 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004673
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004674 /* Hide all surfaces by removing the fullscreen, panel and
4675 * toplevel layers. This way nothing else can show or receive
4676 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004677
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004678 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004679 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004680 if (shell->showing_input_panels)
4681 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004682 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004683 wl_list_insert(&shell->compositor->cursor_layer.link,
4684 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004685
Pekka Paalanen77346a62011-11-30 16:26:35 +02004686 launch_screensaver(shell);
4687
Neil Robertsb4a91702014-04-09 16:33:32 +01004688 /* Remove the keyboard focus on all seats. This will be
4689 * restored to the workspace's saved state via
4690 * restore_focus_state when the compositor is unlocked */
4691 unfocus_all_seats(shell);
4692
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004693 /* TODO: disable bindings that should not work while locked. */
4694
4695 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004696}
4697
4698static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004699unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004700{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004701 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004702 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004703 return;
4704 }
4705
4706 /* If desktop-shell client has gone away, unlock immediately. */
4707 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004708 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004709 return;
4710 }
4711
4712 if (shell->prepare_event_sent)
4713 return;
4714
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004715 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004716 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004717}
4718
4719static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004720shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004721{
4722 struct desktop_shell *shell = data;
4723
4724 shell->fade.animation = NULL;
4725
4726 switch (shell->fade.type) {
4727 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004728 weston_surface_destroy(shell->fade.view->surface);
4729 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004730 break;
4731 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004732 lock(shell);
4733 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004734 default:
4735 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004736 }
4737}
4738
Jason Ekstranda7af7042013-10-12 22:38:11 -05004739static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004740shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004741{
4742 struct weston_compositor *compositor = shell->compositor;
4743 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004744 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004745
4746 surface = weston_surface_create(compositor);
4747 if (!surface)
4748 return NULL;
4749
Jason Ekstranda7af7042013-10-12 22:38:11 -05004750 view = weston_view_create(surface);
4751 if (!view) {
4752 weston_surface_destroy(surface);
4753 return NULL;
4754 }
4755
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004756 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004757 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004758 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004759 wl_list_insert(&compositor->fade_layer.view_list,
4760 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004761 pixman_region32_init(&surface->input);
4762
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004764}
4765
4766static void
4767shell_fade(struct desktop_shell *shell, enum fade_type type)
4768{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004769 float tint;
4770
4771 switch (type) {
4772 case FADE_IN:
4773 tint = 0.0;
4774 break;
4775 case FADE_OUT:
4776 tint = 1.0;
4777 break;
4778 default:
4779 weston_log("shell: invalid fade type\n");
4780 return;
4781 }
4782
4783 shell->fade.type = type;
4784
Jason Ekstranda7af7042013-10-12 22:38:11 -05004785 if (shell->fade.view == NULL) {
4786 shell->fade.view = shell_fade_create_surface(shell);
4787 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004788 return;
4789
Jason Ekstranda7af7042013-10-12 22:38:11 -05004790 shell->fade.view->alpha = 1.0 - tint;
4791 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004792 }
4793
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004794 if (shell->fade.view->output == NULL) {
4795 /* If the black view gets a NULL output, we lost the
4796 * last output and we'll just cancel the fade. This
4797 * happens when you close the last window under the
4798 * X11 or Wayland backends. */
4799 shell->locked = false;
4800 weston_surface_destroy(shell->fade.view->surface);
4801 shell->fade.view = NULL;
4802 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004803 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004804 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004805 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004806 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004807 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004808 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004809 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004810}
4811
4812static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004813do_shell_fade_startup(void *data)
4814{
4815 struct desktop_shell *shell = data;
4816
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004817 if (shell->startup_animation_type == ANIMATION_FADE)
4818 shell_fade(shell, FADE_IN);
4819 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004820 weston_surface_destroy(shell->fade.view->surface);
4821 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004822 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004823}
4824
4825static void
4826shell_fade_startup(struct desktop_shell *shell)
4827{
4828 struct wl_event_loop *loop;
4829
4830 if (!shell->fade.startup_timer)
4831 return;
4832
4833 wl_event_source_remove(shell->fade.startup_timer);
4834 shell->fade.startup_timer = NULL;
4835
4836 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4837 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4838}
4839
4840static int
4841fade_startup_timeout(void *data)
4842{
4843 struct desktop_shell *shell = data;
4844
4845 shell_fade_startup(shell);
4846 return 0;
4847}
4848
4849static void
4850shell_fade_init(struct desktop_shell *shell)
4851{
4852 /* Make compositor output all black, and wait for the desktop-shell
4853 * client to signal it is ready, then fade in. The timer triggers a
4854 * fade-in, in case the desktop-shell client takes too long.
4855 */
4856
4857 struct wl_event_loop *loop;
4858
Jason Ekstranda7af7042013-10-12 22:38:11 -05004859 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004860 weston_log("%s: warning: fade surface already exists\n",
4861 __func__);
4862 return;
4863 }
4864
Jason Ekstranda7af7042013-10-12 22:38:11 -05004865 shell->fade.view = shell_fade_create_surface(shell);
4866 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004867 return;
4868
Jason Ekstranda7af7042013-10-12 22:38:11 -05004869 weston_view_update_transform(shell->fade.view);
4870 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004871
4872 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4873 shell->fade.startup_timer =
4874 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4875 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4876}
4877
4878static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004879idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004880{
4881 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004882 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004883 struct weston_seat *seat;
4884
4885 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4886 if (seat->pointer)
4887 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004888
4889 shell_fade(shell, FADE_OUT);
4890 /* lock() is called from shell_fade_done() */
4891}
4892
4893static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004894wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004895{
4896 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004897 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004898
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004899 unlock(shell);
4900}
4901
4902static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004904{
Giulio Camuffob8366642013-04-25 13:57:46 +03004905 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004906 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004907
Jason Ekstranda7af7042013-10-12 22:38:11 -05004908 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004909
4910 x = output->x + (output->width - width) / 2 - surf_x / 2;
4911 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004912
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004913 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004914}
4915
4916static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004917weston_view_set_initial_position(struct weston_view *view,
4918 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004919{
4920 struct weston_compositor *compositor = shell->compositor;
4921 int ix = 0, iy = 0;
4922 int range_x, range_y;
4923 int dx, dy, x, y, panel_height;
4924 struct weston_output *output, *target_output = NULL;
4925 struct weston_seat *seat;
4926
4927 /* As a heuristic place the new window on the same output as the
4928 * pointer. Falling back to the output containing 0, 0.
4929 *
4930 * TODO: Do something clever for touch too?
4931 */
4932 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004933 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004934 ix = wl_fixed_to_int(seat->pointer->x);
4935 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004936 break;
4937 }
4938 }
4939
4940 wl_list_for_each(output, &compositor->output_list, link) {
4941 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4942 target_output = output;
4943 break;
4944 }
4945 }
4946
4947 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004948 weston_view_set_position(view, 10 + random() % 400,
4949 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004950 return;
4951 }
4952
4953 /* Valid range within output where the surface will still be onscreen.
4954 * If this is negative it means that the surface is bigger than
4955 * output.
4956 */
4957 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004958 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004959 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004960 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004961
Rob Bradford4cb88c72012-08-13 15:18:44 +01004962 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004963 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004964 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004965 dx = 0;
4966
4967 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004968 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004969 else
4970 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004971
4972 x = target_output->x + dx;
4973 y = target_output->y + dy;
4974
Jason Ekstranda7af7042013-10-12 22:38:11 -05004975 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004976}
4977
4978static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004979map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004980 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004981{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004982 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004983 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004984 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004985 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004986
Pekka Paalanen77346a62011-11-30 16:26:35 +02004987 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004988 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004989 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004990 if (shsurf->state.fullscreen) {
4991 center_on_output(shsurf->view, shsurf->fullscreen_output);
4992 shell_map_fullscreen(shsurf);
4993 } else if (shsurf->state.maximized) {
4994 /* use surface configure to set the geometry */
4995 panel_height = get_output_panel_height(shell, shsurf->output);
4996 surface_subsurfaces_boundingbox(shsurf->surface,
4997 &surf_x, &surf_y, NULL, NULL);
4998 weston_view_set_position(shsurf->view,
4999 shsurf->output->x - surf_x,
5000 shsurf->output->y +
5001 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005002 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005003 weston_view_set_initial_position(shsurf->view, shell);
5004 }
Juan Zhao96879df2012-02-07 08:45:41 +08005005 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005006 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005007 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005008 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005009 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005010 weston_view_set_position(shsurf->view,
5011 shsurf->view->geometry.x + sx,
5012 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005013 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005014 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005015 default:
5016 ;
5017 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005018
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005019 /* Surface stacking order, see also activate(). */
5020 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005021
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 if (shsurf->type != SHELL_SURFACE_NONE) {
5023 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005024 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005025 shsurf->surface->output = shsurf->output;
5026 shsurf->view->output = shsurf->output;
5027 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005028 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005029
Jason Ekstranda7af7042013-10-12 22:38:11 -05005030 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005031 /* XXX: xwayland's using the same fields for transient type */
5032 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005033 if (shsurf->transient.flags ==
5034 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5035 break;
5036 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005037 if (shsurf->state.relative &&
5038 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5039 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005040 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005041 break;
5042 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005043 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005044 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005045 case SHELL_SURFACE_POPUP:
5046 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005047 default:
5048 break;
5049 }
5050
Rafael Antognolli03b16592013-12-03 15:35:42 -02005051 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5052 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005053 {
5054 switch (shell->win_animation_type) {
5055 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005056 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005057 break;
5058 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005059 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005060 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005061 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005062 default:
5063 break;
5064 }
5065 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005066}
5067
5068static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005069configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005070 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005071{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005072 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005073 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005074 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005075
Pekka Paalanen77346a62011-11-30 16:26:35 +02005076 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005077
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005078 assert(shsurf);
5079
Rafael Antognolli03b16592013-12-03 15:35:42 -02005080 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005081 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005082 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005083 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005084 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5085 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005086 mx = shsurf->output->x - surf_x;
5087 my = shsurf->output->y +
5088 get_output_panel_height(shell,shsurf->output) - surf_y;
5089 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005090 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005091 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005092 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005093
Alex Wu4539b082012-03-01 12:57:46 +08005094 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005095 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005096 wl_list_for_each(view, &surface->views, surface_link)
5097 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005098
Rafael Antognolli03b16592013-12-03 15:35:42 -02005099 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005100 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005101 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005102}
5103
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005104static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005105shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005106{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005107 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005108 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005109 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005110
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005111 assert(shsurf);
5112
5113 shell = shsurf->shell;
5114
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005115 if (!weston_surface_is_mapped(es) &&
5116 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005117 remove_popup_grab(shsurf);
5118 }
5119
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005120 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005121 return;
5122
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005123 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005124 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005125 type_changed = 1;
5126 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005127
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005128 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005129 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005130 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005131 shsurf->last_width != es->width ||
5132 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005133 float from_x, from_y;
5134 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005135
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005136 if (shsurf->resize_edges) {
5137 sx = 0;
5138 sy = 0;
5139 }
5140
5141 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5142 sx = shsurf->last_width - es->width;
5143 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5144 sy = shsurf->last_height - es->height;
5145
5146 shsurf->last_width = es->width;
5147 shsurf->last_height = es->height;
5148
Jason Ekstranda7af7042013-10-12 22:38:11 -05005149 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5150 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005151 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005152 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005153 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005154 }
5155}
5156
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005157static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005158
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005159static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005160desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005161{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005162 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005163 struct desktop_shell *shell =
5164 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005165
5166 shell->child.process.pid = 0;
5167 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005168
5169 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5170 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005171 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005172 shell->child.deathstamp = time;
5173 shell->child.deathcount = 0;
5174 }
5175
5176 shell->child.deathcount++;
5177 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005178 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005179 return;
5180 }
5181
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005182 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005183 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005184 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005185}
5186
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005187static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005188desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5189{
5190 struct desktop_shell *shell;
5191
5192 shell = container_of(listener, struct desktop_shell,
5193 child.client_destroy_listener);
5194
5195 shell->child.client = NULL;
5196}
5197
5198static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005199launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005200{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005201 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005202
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005203 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005204 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005205 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005206 desktop_shell_sigchld);
5207
5208 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005209 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005210
5211 shell->child.client_destroy_listener.notify =
5212 desktop_shell_client_destroy;
5213 wl_client_add_destroy_listener(shell->child.client,
5214 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005215}
5216
5217static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005218handle_shell_client_destroy(struct wl_listener *listener, void *data)
5219{
5220 struct shell_client *sc =
5221 container_of(listener, struct shell_client, destroy_listener);
5222
5223 if (sc->ping_timer)
5224 wl_event_source_remove(sc->ping_timer);
5225 free(sc);
5226}
5227
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005228static struct shell_client *
5229shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5230 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005231{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005232 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005233
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005234 sc = zalloc(sizeof *sc);
5235 if (sc == NULL) {
5236 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005237 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005238 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005239
5240 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005241 if (sc->resource == NULL) {
5242 free(sc);
5243 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005244 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005245 }
5246
5247 sc->client = client;
5248 sc->shell = shell;
5249 sc->destroy_listener.notify = handle_shell_client_destroy;
5250 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5251
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005252 return sc;
5253}
5254
5255static void
5256bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5257{
5258 struct desktop_shell *shell = data;
5259 struct shell_client *sc;
5260
5261 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5262 if (sc)
5263 wl_resource_set_implementation(sc->resource,
5264 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005265 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005266}
5267
5268static void
5269bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5270{
5271 struct desktop_shell *shell = data;
5272 struct shell_client *sc;
5273
5274 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5275 if (sc)
5276 wl_resource_set_dispatcher(sc->resource,
5277 xdg_shell_unversioned_dispatch,
5278 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005279}
5280
5281static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005282unbind_desktop_shell(struct wl_resource *resource)
5283{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005284 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005285
5286 if (shell->locked)
5287 resume_desktop(shell);
5288
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005289 shell->child.desktop_shell = NULL;
5290 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005291}
5292
5293static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005294bind_desktop_shell(struct wl_client *client,
5295 void *data, uint32_t version, uint32_t id)
5296{
Tiago Vignattibe143262012-04-16 17:31:41 +03005297 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005298 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005299
Jason Ekstranda85118c2013-06-27 20:17:02 -05005300 resource = wl_resource_create(client, &desktop_shell_interface,
5301 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005302
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005303 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005304 wl_resource_set_implementation(resource,
5305 &desktop_shell_implementation,
5306 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005307 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005308
5309 if (version < 2)
5310 shell_fade_startup(shell);
5311
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005312 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005313 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005314
5315 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5316 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005317 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005318}
5319
Pekka Paalanen6e168112011-11-24 11:34:05 +02005320static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005321screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005322{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005323 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005324 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005325
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005326 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005327 return;
5328
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005329 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005330 if (!shell->locked)
5331 return;
5332
Jason Ekstranda7af7042013-10-12 22:38:11 -05005333 view = container_of(surface->views.next, struct weston_view, surface_link);
5334 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005335
Jason Ekstranda7af7042013-10-12 22:38:11 -05005336 if (wl_list_empty(&view->layer_link)) {
5337 wl_list_insert(shell->lock_layer.view_list.prev,
5338 &view->layer_link);
5339 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005340 wl_event_source_timer_update(shell->screensaver.timer,
5341 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005342 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005343 }
5344}
5345
5346static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005347screensaver_set_surface(struct wl_client *client,
5348 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005349 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005350 struct wl_resource *output_resource)
5351{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005352 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005353 struct weston_surface *surface =
5354 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005355 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005356 struct weston_view *view, *next;
5357
5358 /* Make sure we only have one view */
5359 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5360 weston_view_destroy(view);
5361 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005362
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005363 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005364 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005365 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005366}
5367
5368static const struct screensaver_interface screensaver_implementation = {
5369 screensaver_set_surface
5370};
5371
5372static void
5373unbind_screensaver(struct wl_resource *resource)
5374{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005375 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005376
Pekka Paalanen77346a62011-11-30 16:26:35 +02005377 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005378}
5379
5380static void
5381bind_screensaver(struct wl_client *client,
5382 void *data, uint32_t version, uint32_t id)
5383{
Tiago Vignattibe143262012-04-16 17:31:41 +03005384 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005385 struct wl_resource *resource;
5386
Jason Ekstranda85118c2013-06-27 20:17:02 -05005387 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005388
Pekka Paalanen77346a62011-11-30 16:26:35 +02005389 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005390 wl_resource_set_implementation(resource,
5391 &screensaver_implementation,
5392 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005393 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005394 return;
5395 }
5396
5397 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5398 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005399 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005400}
5401
Kristian Høgsberg07045392012-02-19 18:52:44 -05005402struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005403 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005404 struct weston_surface *current;
5405 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005406 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005407 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005408};
5409
5410static void
5411switcher_next(struct switcher *switcher)
5412{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005413 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005414 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005415 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005416 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005417
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005418 /* temporary re-display minimized surfaces */
5419 struct weston_view *tmp;
5420 struct weston_view **minimized;
5421 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5422 wl_list_remove(&view->layer_link);
5423 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5424 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5425 *minimized = view;
5426 }
5427
Jason Ekstranda7af7042013-10-12 22:38:11 -05005428 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005429 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005430 if (shsurf &&
5431 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5432 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005433 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005434 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005435 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005436 next = view->surface;
5437 prev = view->surface;
5438 view->alpha = 0.25;
5439 weston_view_geometry_dirty(view);
5440 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005441 }
Alex Wu1659daa2012-04-01 20:13:09 +08005442
Jason Ekstranda7af7042013-10-12 22:38:11 -05005443 if (is_black_surface(view->surface, NULL)) {
5444 view->alpha = 0.25;
5445 weston_view_geometry_dirty(view);
5446 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005447 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005448 }
5449
5450 if (next == NULL)
5451 next = first;
5452
Alex Wu07b26062012-03-12 16:06:01 +08005453 if (next == NULL)
5454 return;
5455
Kristian Høgsberg07045392012-02-19 18:52:44 -05005456 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005457 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005458
5459 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005460 wl_list_for_each(view, &next->views, surface_link)
5461 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005462
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005463 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005464 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005465 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005466}
5467
5468static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005469switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005470{
5471 struct switcher *switcher =
5472 container_of(listener, struct switcher, listener);
5473
5474 switcher_next(switcher);
5475}
5476
5477static void
Daniel Stone351eb612012-05-31 15:27:47 -04005478switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005479{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005480 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005481 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005482 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005483
Jason Ekstranda7af7042013-10-12 22:38:11 -05005484 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005485 if (is_focus_view(view))
5486 continue;
5487
Jason Ekstranda7af7042013-10-12 22:38:11 -05005488 view->alpha = 1.0;
5489 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005490 }
5491
Alex Wu07b26062012-03-12 16:06:01 +08005492 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005493 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005494 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005495 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005496 weston_keyboard_end_grab(keyboard);
5497 if (keyboard->input_method_resource)
5498 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005499
5500 /* re-hide surfaces that were temporary shown during the switch */
5501 struct weston_view **minimized;
5502 wl_array_for_each(minimized, &switcher->minimized_array) {
5503 /* with the exception of the current selected */
5504 if ((*minimized)->surface != switcher->current) {
5505 wl_list_remove(&(*minimized)->layer_link);
5506 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5507 weston_view_damage_below(*minimized);
5508 }
5509 }
5510 wl_array_release(&switcher->minimized_array);
5511
Kristian Høgsberg07045392012-02-19 18:52:44 -05005512 free(switcher);
5513}
5514
5515static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005516switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005517 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005518{
5519 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005520 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005521
Daniel Stonec9785ea2012-05-30 16:31:52 +01005522 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005523 switcher_next(switcher);
5524}
5525
5526static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005527switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005528 uint32_t mods_depressed, uint32_t mods_latched,
5529 uint32_t mods_locked, uint32_t group)
5530{
5531 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005532 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005533
Daniel Stone351eb612012-05-31 15:27:47 -04005534 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5535 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005536}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005537
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005538static void
5539switcher_cancel(struct weston_keyboard_grab *grab)
5540{
5541 struct switcher *switcher = container_of(grab, struct switcher, grab);
5542
5543 switcher_destroy(switcher);
5544}
5545
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005546static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005547 switcher_key,
5548 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005549 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005550};
5551
5552static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005553switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005554 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005555{
Tiago Vignattibe143262012-04-16 17:31:41 +03005556 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005557 struct switcher *switcher;
5558
5559 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005560 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005561 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005562 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005563 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005564 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005565
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005566 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005567 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005568 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005569 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5570 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005571 switcher_next(switcher);
5572}
5573
Pekka Paalanen3c647232011-12-22 13:43:43 +02005574static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005575backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005576 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005577{
5578 struct weston_compositor *compositor = data;
5579 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005580 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005581
5582 /* TODO: we're limiting to simple use cases, where we assume just
5583 * control on the primary display. We'd have to extend later if we
5584 * ever get support for setting backlights on random desktop LCD
5585 * panels though */
5586 output = get_default_output(compositor);
5587 if (!output)
5588 return;
5589
5590 if (!output->set_backlight)
5591 return;
5592
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005593 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5594 backlight_new = output->backlight_current - 25;
5595 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5596 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005597
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005598 if (backlight_new < 5)
5599 backlight_new = 5;
5600 if (backlight_new > 255)
5601 backlight_new = 255;
5602
5603 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005604 output->set_backlight(output, output->backlight_current);
5605}
5606
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005607struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005608 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005609 struct weston_seat *seat;
5610 uint32_t key[2];
5611 int key_released[2];
5612};
5613
5614static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005615debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005616 uint32_t key, uint32_t state)
5617{
5618 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005619 struct weston_compositor *ec = db->seat->compositor;
5620 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005621 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005622 uint32_t serial;
5623 int send = 0, terminate = 0;
5624 int check_binding = 1;
5625 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005626 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005627
5628 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5629 /* Do not run bindings on key releases */
5630 check_binding = 0;
5631
5632 for (i = 0; i < 2; i++)
5633 if (key == db->key[i])
5634 db->key_released[i] = 1;
5635
5636 if (db->key_released[0] && db->key_released[1]) {
5637 /* All key releases been swalled so end the grab */
5638 terminate = 1;
5639 } else if (key != db->key[0] && key != db->key[1]) {
5640 /* Should not swallow release of other keys */
5641 send = 1;
5642 }
5643 } else if (key == db->key[0] && !db->key_released[0]) {
5644 /* Do not check bindings for the first press of the binding
5645 * key. This allows it to be used as a debug shortcut.
5646 * We still need to swallow this event. */
5647 check_binding = 0;
5648 } else if (db->key[1]) {
5649 /* If we already ran a binding don't process another one since
5650 * we can't keep track of all the binding keys that were
5651 * pressed in order to swallow the release events. */
5652 send = 1;
5653 check_binding = 0;
5654 }
5655
5656 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005657 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5658 key, state)) {
5659 /* We ran a binding so swallow the press and keep the
5660 * grab to swallow the released too. */
5661 send = 0;
5662 terminate = 0;
5663 db->key[1] = key;
5664 } else {
5665 /* Terminate the grab since the key pressed is not a
5666 * debug binding key. */
5667 send = 1;
5668 terminate = 1;
5669 }
5670 }
5671
5672 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005673 serial = wl_display_next_serial(display);
5674 resource_list = &grab->keyboard->focus_resource_list;
5675 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005676 wl_keyboard_send_key(resource, serial, time, key, state);
5677 }
5678 }
5679
5680 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005681 weston_keyboard_end_grab(grab->keyboard);
5682 if (grab->keyboard->input_method_resource)
5683 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005684 free(db);
5685 }
5686}
5687
5688static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005689debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005690 uint32_t mods_depressed, uint32_t mods_latched,
5691 uint32_t mods_locked, uint32_t group)
5692{
5693 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005694 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005695
Neil Roberts96d790e2013-09-19 17:32:00 +01005696 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005697
Neil Roberts96d790e2013-09-19 17:32:00 +01005698 wl_resource_for_each(resource, resource_list) {
5699 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5700 mods_latched, mods_locked, group);
5701 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005702}
5703
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005704static void
5705debug_binding_cancel(struct weston_keyboard_grab *grab)
5706{
5707 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5708
5709 weston_keyboard_end_grab(grab->keyboard);
5710 free(db);
5711}
5712
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005713struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005714 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005715 debug_binding_modifiers,
5716 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005717};
5718
5719static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005720debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005721{
5722 struct debug_binding_grab *grab;
5723
5724 grab = calloc(1, sizeof *grab);
5725 if (!grab)
5726 return;
5727
5728 grab->seat = (struct weston_seat *) seat;
5729 grab->key[0] = key;
5730 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005731 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005732}
5733
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005734static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005735force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005736 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005737{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005738 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005739 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005740 struct desktop_shell *shell = data;
5741 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005742 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005743
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005744 focus_surface = seat->keyboard->focus;
5745 if (!focus_surface)
5746 return;
5747
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005748 wl_signal_emit(&compositor->kill_signal, focus_surface);
5749
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005750 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005751 wl_client_get_credentials(client, &pid, NULL, NULL);
5752
5753 /* Skip clients that we launched ourselves (the credentials of
5754 * the socketpair is ours) */
5755 if (pid == getpid())
5756 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005757
Daniel Stone325fc2d2012-05-30 16:31:58 +01005758 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005759}
5760
5761static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005762workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005763 uint32_t key, void *data)
5764{
5765 struct desktop_shell *shell = data;
5766 unsigned int new_index = shell->workspaces.current;
5767
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005768 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005769 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005770 if (new_index != 0)
5771 new_index--;
5772
5773 change_workspace(shell, new_index);
5774}
5775
5776static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005777workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005778 uint32_t key, void *data)
5779{
5780 struct desktop_shell *shell = data;
5781 unsigned int new_index = shell->workspaces.current;
5782
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005783 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005784 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005785 if (new_index < shell->workspaces.num - 1)
5786 new_index++;
5787
5788 change_workspace(shell, new_index);
5789}
5790
5791static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005792workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005793 uint32_t key, void *data)
5794{
5795 struct desktop_shell *shell = data;
5796 unsigned int new_index;
5797
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005798 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005799 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005800 new_index = key - KEY_F1;
5801 if (new_index >= shell->workspaces.num)
5802 new_index = shell->workspaces.num - 1;
5803
5804 change_workspace(shell, new_index);
5805}
5806
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005807static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005808workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005809 uint32_t key, void *data)
5810{
5811 struct desktop_shell *shell = data;
5812 unsigned int new_index = shell->workspaces.current;
5813
5814 if (shell->locked)
5815 return;
5816
5817 if (new_index != 0)
5818 new_index--;
5819
5820 take_surface_to_workspace_by_seat(shell, seat, new_index);
5821}
5822
5823static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005824workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005825 uint32_t key, void *data)
5826{
5827 struct desktop_shell *shell = data;
5828 unsigned int new_index = shell->workspaces.current;
5829
5830 if (shell->locked)
5831 return;
5832
5833 if (new_index < shell->workspaces.num - 1)
5834 new_index++;
5835
5836 take_surface_to_workspace_by_seat(shell, seat, new_index);
5837}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005838
5839static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005840shell_reposition_view_on_output_destroy(struct weston_view *view)
5841{
5842 struct weston_output *output, *first_output;
5843 struct weston_compositor *ec = view->surface->compositor;
5844 struct shell_surface *shsurf;
5845 float x, y;
5846 int visible;
5847
5848 x = view->geometry.x;
5849 y = view->geometry.y;
5850
5851 /* At this point the destroyed output is not in the list anymore.
5852 * If the view is still visible somewhere, we leave where it is,
5853 * otherwise, move it to the first output. */
5854 visible = 0;
5855 wl_list_for_each(output, &ec->output_list, link) {
5856 if (pixman_region32_contains_point(&output->region,
5857 x, y, NULL)) {
5858 visible = 1;
5859 break;
5860 }
5861 }
5862
5863 if (!visible) {
5864 first_output = container_of(ec->output_list.next,
5865 struct weston_output, link);
5866
5867 x = first_output->x + first_output->width / 4;
5868 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005869
5870 weston_view_set_position(view, x, y);
5871 } else {
5872 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005873 }
5874
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005875
5876 shsurf = get_shell_surface(view->surface);
5877
5878 if (shsurf) {
5879 shsurf->saved_position_valid = false;
5880 shsurf->next_state.maximized = false;
5881 shsurf->next_state.fullscreen = false;
5882 shsurf->state_changed = true;
5883 }
5884}
5885
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005886void
5887shell_for_each_layer(struct desktop_shell *shell,
5888 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005889{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005890 struct workspace **ws;
5891
5892 func(shell, &shell->fullscreen_layer, data);
5893 func(shell, &shell->panel_layer, data);
5894 func(shell, &shell->background_layer, data);
5895 func(shell, &shell->lock_layer, data);
5896 func(shell, &shell->input_panel_layer, data);
5897
5898 wl_array_for_each(ws, &shell->workspaces.array)
5899 func(shell, &(*ws)->layer, data);
5900}
5901
5902static void
5903shell_output_destroy_move_layer(struct desktop_shell *shell,
5904 struct weston_layer *layer,
5905 void *data)
5906{
5907 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005908 struct weston_view *view;
5909
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005910 wl_list_for_each(view, &layer->view_list, layer_link) {
5911 if (view->output != output)
5912 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005913
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005914 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005915 }
5916}
5917
5918static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005919handle_output_destroy(struct wl_listener *listener, void *data)
5920{
5921 struct shell_output *output_listener =
5922 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005923 struct weston_output *output = output_listener->output;
5924 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005925
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005926 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005927
Xiong Zhang6b481422013-10-23 13:58:32 +08005928 wl_list_remove(&output_listener->destroy_listener.link);
5929 wl_list_remove(&output_listener->link);
5930 free(output_listener);
5931}
5932
5933static void
5934create_shell_output(struct desktop_shell *shell,
5935 struct weston_output *output)
5936{
5937 struct shell_output *shell_output;
5938
5939 shell_output = zalloc(sizeof *shell_output);
5940 if (shell_output == NULL)
5941 return;
5942
5943 shell_output->output = output;
5944 shell_output->shell = shell;
5945 shell_output->destroy_listener.notify = handle_output_destroy;
5946 wl_signal_add(&output->destroy_signal,
5947 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005948 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005949}
5950
5951static void
5952handle_output_create(struct wl_listener *listener, void *data)
5953{
5954 struct desktop_shell *shell =
5955 container_of(listener, struct desktop_shell, output_create_listener);
5956 struct weston_output *output = (struct weston_output *)data;
5957
5958 create_shell_output(shell, output);
5959}
5960
5961static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005962handle_output_move_layer(struct desktop_shell *shell,
5963 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005964{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005965 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005966 struct weston_view *view;
5967 float x, y;
5968
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005969 wl_list_for_each(view, &layer->view_list, layer_link) {
5970 if (view->output != output)
5971 continue;
5972
5973 x = view->geometry.x + output->move_x;
5974 y = view->geometry.y + output->move_y;
5975 weston_view_set_position(view, x, y);
5976 }
5977}
5978
5979static void
5980handle_output_move(struct wl_listener *listener, void *data)
5981{
5982 struct desktop_shell *shell;
5983
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005984 shell = container_of(listener, struct desktop_shell,
5985 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005986
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005987 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005988}
5989
5990static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005991setup_output_destroy_handler(struct weston_compositor *ec,
5992 struct desktop_shell *shell)
5993{
5994 struct weston_output *output;
5995
5996 wl_list_init(&shell->output_list);
5997 wl_list_for_each(output, &ec->output_list, link)
5998 create_shell_output(shell, output);
5999
6000 shell->output_create_listener.notify = handle_output_create;
6001 wl_signal_add(&ec->output_created_signal,
6002 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006003
6004 shell->output_move_listener.notify = handle_output_move;
6005 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006006}
6007
6008static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006009shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006010{
Tiago Vignattibe143262012-04-16 17:31:41 +03006011 struct desktop_shell *shell =
6012 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006013 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006014 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006015
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006016 /* Force state to unlocked so we don't try to fade */
6017 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006018 if (shell->child.client)
6019 wl_client_destroy(shell->child.client);
6020
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006021 wl_list_remove(&shell->idle_listener.link);
6022 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006023
6024 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006025
Xiong Zhang6b481422013-10-23 13:58:32 +08006026 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6027 wl_list_remove(&shell_output->destroy_listener.link);
6028 wl_list_remove(&shell_output->link);
6029 free(shell_output);
6030 }
6031
6032 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006033 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006034
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006035 wl_array_for_each(ws, &shell->workspaces.array)
6036 workspace_destroy(*ws);
6037 wl_array_release(&shell->workspaces.array);
6038
Pekka Paalanen3c647232011-12-22 13:43:43 +02006039 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006040 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006041 free(shell);
6042}
6043
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006044static void
6045shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6046{
6047 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006048 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006049
6050 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006051 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6052 MODIFIER_CTRL | MODIFIER_ALT,
6053 terminate_binding, ec);
6054 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6055 click_to_activate_binding,
6056 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006057 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6058 click_to_activate_binding,
6059 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006060 weston_compositor_add_touch_binding(ec, 0,
6061 touch_to_activate_binding,
6062 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006063 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6064 MODIFIER_SUPER | MODIFIER_ALT,
6065 surface_opacity_binding, NULL);
6066 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6067 MODIFIER_SUPER, zoom_axis_binding,
6068 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006069
6070 /* configurable bindings */
6071 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006072 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6073 zoom_key_binding, NULL);
6074 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6075 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006076 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6077 maximize_binding, NULL);
6078 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6079 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006080 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6081 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006082 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006083 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6084 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006085 weston_compositor_add_button_binding(ec, BTN_LEFT,
6086 mod | MODIFIER_SHIFT,
6087 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006088
6089 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6090 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6091 rotate_binding, NULL);
6092
Daniel Stone325fc2d2012-05-30 16:31:58 +01006093 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6094 shell);
6095 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6096 ec);
6097 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6098 backlight_binding, ec);
6099 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6100 ec);
6101 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6102 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006103 weston_compositor_add_key_binding(ec, KEY_K, mod,
6104 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006105 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6106 workspace_up_binding, shell);
6107 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6108 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006109 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6110 workspace_move_surface_up_binding,
6111 shell);
6112 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6113 workspace_move_surface_down_binding,
6114 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006115
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006116 if (shell->exposay_modifier)
6117 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6118 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006119
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006120 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6121 if (shell->workspaces.num > 1) {
6122 num_workspace_bindings = shell->workspaces.num;
6123 if (num_workspace_bindings > 6)
6124 num_workspace_bindings = 6;
6125 for (i = 0; i < num_workspace_bindings; i++)
6126 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6127 workspace_f_binding,
6128 shell);
6129 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006130
6131 /* Debug bindings */
6132 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6133 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006134}
6135
Jason Ekstrand024177c2014-04-21 19:42:58 -05006136static void
6137handle_seat_created(struct wl_listener *listener, void *data)
6138{
6139 struct weston_seat *seat = data;
6140
6141 create_shell_seat(seat);
6142}
6143
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006144WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006145module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006146 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006147{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006148 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006149 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006150 struct workspace **pws;
6151 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006152 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006153
Peter Huttererf3d62272013-08-08 11:57:05 +10006154 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006155 if (shell == NULL)
6156 return -1;
6157
Kristian Høgsberg75840622011-09-06 13:48:16 -04006158 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006159
6160 shell->destroy_listener.notify = shell_destroy;
6161 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006162 shell->idle_listener.notify = idle_handler;
6163 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6164 shell->wake_listener.notify = wake_handler;
6165 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006166
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006167 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006168 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006169 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006170 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006171 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006172 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006173 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006174 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006175 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006176 ec->shell_interface.set_title = set_title;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07006177 ec->shell_interface.set_margin = set_margin;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006178
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006179 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6180 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006181 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6182 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006183 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006184
6185 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006186 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006187
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006188 if (input_panel_setup(shell) < 0)
6189 return -1;
6190
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006191 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006192
Daniel Stonedf8133b2013-11-19 11:37:14 +01006193 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6194 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6195
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006196 for (i = 0; i < shell->workspaces.num; i++) {
6197 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6198 if (pws == NULL)
6199 return -1;
6200
6201 *pws = workspace_create();
6202 if (*pws == NULL)
6203 return -1;
6204 }
6205 activate_workspace(shell, 0);
6206
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006207 weston_layer_init(&shell->minimized_layer, NULL);
6208
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006209 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006210 wl_list_init(&shell->workspaces.animation.link);
6211 shell->workspaces.animation.frame = animate_workspace_change_frame;
6212
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006213 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006214 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006215 return -1;
6216
Rafael Antognollie2a34552013-12-03 15:35:45 -02006217 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6218 shell, bind_xdg_shell) == NULL)
6219 return -1;
6220
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006221 if (wl_global_create(ec->wl_display,
6222 &desktop_shell_interface, 2,
6223 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006224 return -1;
6225
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006226 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6227 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006228 return -1;
6229
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006230 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6231 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006232 return -1;
6233
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006234 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006235
Xiong Zhang6b481422013-10-23 13:58:32 +08006236 setup_output_destroy_handler(ec, shell);
6237
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006238 loop = wl_display_get_event_loop(ec->wl_display);
6239 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006240
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006241 shell->screensaver.timer =
6242 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6243
Jason Ekstrand024177c2014-04-21 19:42:58 -05006244 wl_list_for_each(seat, &ec->seat_list, link)
6245 handle_seat_created(NULL, seat);
6246 shell->seat_create_listener.notify = handle_seat_created;
6247 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006248
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006249 screenshooter_create(ec);
6250
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006251 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006252
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006253 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006254
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006255 return 0;
6256}