blob: 1704b84504ce319ee31a248f69037da2732caf3d [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500112 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600113 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700115 struct wl_listener resource_destroy_listener;
116
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400117 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800122 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200125 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200160 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400162
163 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200164
165 struct {
166 bool maximized;
167 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200168 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100169 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500170 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200171 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500172 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500173
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700174 struct {
175 int left, right, top, bottom;
176 } margin;
177
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500178 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700197 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198};
199
Rusty Lynch1084da52013-08-15 09:10:08 -0700200struct weston_touch_move_grab {
201 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800202 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700203 wl_fixed_t dx, dy;
204};
205
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500208 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200210 float x;
211 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 } center;
213};
214
Giulio Camuffo5085a752013-03-25 21:42:45 +0100215struct shell_seat {
216 struct weston_seat *seat;
217 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500218 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219
Jason Ekstrand024177c2014-04-21 19:42:58 -0500220 struct wl_listener caps_changed_listener;
221 struct wl_listener pointer_focus_listener;
222 struct wl_listener keyboard_focus_listener;
223
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400225 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100226 struct wl_list surfaces_list;
227 struct wl_client *client;
228 int32_t initial_up;
229 } popup_grab;
230};
231
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800232struct shell_client {
233 struct wl_resource *resource;
234 struct wl_client *client;
235 struct desktop_shell *shell;
236 struct wl_listener destroy_listener;
237 struct wl_event_source *ping_timer;
238 uint32_t ping_serial;
239 int unresponsive;
240};
241
Alex Wubd3354b2012-04-17 17:20:49 +0800242static struct desktop_shell *
243shell_surface_get_shell(struct shell_surface *shsurf);
244
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400246surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500247
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300248static void
249shell_fade_startup(struct desktop_shell *shell);
250
Philip Withnallbecb77e2013-11-25 18:01:30 +0000251static struct shell_seat *
252get_shell_seat(struct weston_seat *seat);
253
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700254static int
255get_output_panel_height(struct desktop_shell *shell,
256 struct weston_output *output);
257
Philip Withnall648a4dd2013-11-25 18:01:44 +0000258static void
259shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
260
Alex Wubd3354b2012-04-17 17:20:49 +0800261static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200262shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
263
264static bool
265shell_surface_is_xdg_surface(struct shell_surface *shsurf);
266
267static bool
268shell_surface_is_xdg_popup(struct shell_surface *shsurf);
269
270static void
271shell_surface_set_parent(struct shell_surface *shsurf,
272 struct weston_surface *parent);
273
274static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800275shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
276{
277 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800279
280 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800283 return false;
284
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
286 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800287 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800289}
290
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500291static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400292destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300293{
294 struct shell_grab *grab;
295
296 grab = container_of(listener, struct shell_grab,
297 shsurf_destroy_listener);
298
299 grab->shsurf = NULL;
300}
301
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800302struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303get_default_view(struct weston_surface *surface)
304{
305 struct shell_surface *shsurf;
306 struct weston_view *view;
307
308 if (!surface || wl_list_empty(&surface->views))
309 return NULL;
310
311 shsurf = get_shell_surface(surface);
312 if (shsurf)
313 return shsurf->view;
314
315 wl_list_for_each(view, &surface->views, surface_link)
316 if (weston_view_is_mapped(view))
317 return view;
318
319 return container_of(surface->views.next, struct weston_view, surface_link);
320}
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400323popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400324
325static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300326shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400327 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300328 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300332 struct desktop_shell *shell = shsurf->shell;
333
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400334 popup_grab_end(pointer);
335
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336 grab->grab.interface = interface;
337 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500339 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400340 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700342 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400343 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400344 if (shell->child.desktop_shell) {
345 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
346 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500347 weston_pointer_set_focus(pointer,
348 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400349 wl_fixed_from_int(0),
350 wl_fixed_from_int(0));
351 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300352}
353
354static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400355shell_surface_state_changed(struct shell_surface *shsurf)
356{
357 if (shell_surface_is_xdg_surface(shsurf)) {
358 shsurf->client->send_configure(shsurf->surface,
359 shsurf->surface->width,
360 shsurf->surface->height);
361 }
362}
363
364static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300365shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300366{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700367 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400368 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700369 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400370
371 if (grab->shsurf->resize_edges) {
372 grab->shsurf->resize_edges = 0;
373 shell_surface_state_changed(grab->shsurf);
374 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700375 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700377 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378}
379
380static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700381shell_touch_grab_start(struct shell_touch_grab *grab,
382 const struct weston_touch_grab_interface *interface,
383 struct shell_surface *shsurf,
384 struct weston_touch *touch)
385{
386 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800387
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700388 if (touch->seat->pointer)
389 popup_grab_end(touch->seat->pointer);
390
Rusty Lynch1084da52013-08-15 09:10:08 -0700391 grab->grab.interface = interface;
392 grab->shsurf = shsurf;
393 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
394 wl_signal_add(&shsurf->destroy_signal,
395 &grab->shsurf_destroy_listener);
396
397 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700398 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700399
400 weston_touch_start_grab(touch, &grab->grab);
401 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500402 weston_touch_set_focus(touch->seat,
403 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700404}
405
406static void
407shell_touch_grab_end(struct shell_touch_grab *grab)
408{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700409 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700410 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700411 grab->shsurf->grabbed = 0;
412 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700413
414 weston_touch_end_grab(grab->touch);
415}
416
417static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800419 struct weston_output *output);
420
Daniel Stone496ca172012-05-30 16:31:42 +0100421static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300422get_modifier(char *modifier)
423{
424 if (!modifier)
425 return MODIFIER_SUPER;
426
427 if (!strcmp("ctrl", modifier))
428 return MODIFIER_CTRL;
429 else if (!strcmp("alt", modifier))
430 return MODIFIER_ALT;
431 else if (!strcmp("super", modifier))
432 return MODIFIER_SUPER;
433 else
434 return MODIFIER_SUPER;
435}
436
Juan Zhaoe10d2792012-04-25 19:09:52 +0800437static enum animation_type
438get_animation_type(char *animation)
439{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800440 if (!animation)
441 return ANIMATION_NONE;
442
Juan Zhaoe10d2792012-04-25 19:09:52 +0800443 if (!strcmp("zoom", animation))
444 return ANIMATION_ZOOM;
445 else if (!strcmp("fade", animation))
446 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100447 else if (!strcmp("dim-layer", animation))
448 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800449 else
450 return ANIMATION_NONE;
451}
452
Alex Wu4539b082012-03-01 12:57:46 +0800453static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400454shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200455{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400456 struct weston_config_section *section;
457 int duration;
458 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200459
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400460 section = weston_config_get_section(shell->compositor->config,
461 "screensaver", NULL, NULL);
462 weston_config_section_get_string(section,
463 "path", &shell->screensaver.path, NULL);
464 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200465 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400466
467 section = weston_config_get_section(shell->compositor->config,
468 "shell", NULL, NULL);
469 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100470 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100471 shell->client = s;
472 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 "binding-modifier", &s, "super");
474 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200475 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800476
477 weston_config_section_get_string(section,
478 "exposay-modifier", &s, "none");
479 if (strcmp(s, "none") == 0)
480 shell->exposay_modifier = 0;
481 else
482 shell->exposay_modifier = get_modifier(s);
483 free(s);
484
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400485 weston_config_section_get_string(section, "animation", &s, "none");
486 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200487 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700488 weston_config_section_get_string(section,
489 "startup-animation", &s, "fade");
490 shell->startup_animation_type = get_animation_type(s);
491 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700492 if (shell->startup_animation_type == ANIMATION_ZOOM)
493 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100494 weston_config_section_get_string(section, "focus-animation", &s, "none");
495 shell->focus_animation_type = get_animation_type(s);
496 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400497 weston_config_section_get_uint(section, "num-workspaces",
498 &shell->workspaces.num,
499 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200500}
501
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800502struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100503get_default_output(struct weston_compositor *compositor)
504{
505 return container_of(compositor->output_list.next,
506 struct weston_output, link);
507}
508
509
510/* no-op func for checking focus surface */
511static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600512focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100513{
514}
515
516static struct focus_surface *
517get_focus_surface(struct weston_surface *surface)
518{
519 if (surface->configure == focus_surface_configure)
520 return surface->configure_private;
521 else
522 return NULL;
523}
524
525static bool
526is_focus_surface (struct weston_surface *es)
527{
528 return (es->configure == focus_surface_configure);
529}
530
531static bool
532is_focus_view (struct weston_view *view)
533{
534 return is_focus_surface (view->surface);
535}
536
537static struct focus_surface *
538create_focus_surface(struct weston_compositor *ec,
539 struct weston_output *output)
540{
541 struct focus_surface *fsurf = NULL;
542 struct weston_surface *surface = NULL;
543
544 fsurf = malloc(sizeof *fsurf);
545 if (!fsurf)
546 return NULL;
547
548 fsurf->surface = weston_surface_create(ec);
549 surface = fsurf->surface;
550 if (surface == NULL) {
551 free(fsurf);
552 return NULL;
553 }
554
555 surface->configure = focus_surface_configure;
556 surface->output = output;
557 surface->configure_private = fsurf;
558
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800559 fsurf->view = weston_view_create(surface);
560 if (fsurf->view == NULL) {
561 weston_surface_destroy(surface);
562 free(fsurf);
563 return NULL;
564 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100565 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100566
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600567 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600568 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100569 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
570 pixman_region32_fini(&surface->opaque);
571 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
572 output->width, output->height);
573 pixman_region32_fini(&surface->input);
574 pixman_region32_init(&surface->input);
575
576 wl_list_init(&fsurf->workspace_transform.link);
577
578 return fsurf;
579}
580
581static void
582focus_surface_destroy(struct focus_surface *fsurf)
583{
584 weston_surface_destroy(fsurf->surface);
585 free(fsurf);
586}
587
588static void
589focus_animation_done(struct weston_view_animation *animation, void *data)
590{
591 struct workspace *ws = data;
592
593 ws->focus_animation = NULL;
594}
595
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200596static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200597focus_state_destroy(struct focus_state *state)
598{
599 wl_list_remove(&state->seat_destroy_listener.link);
600 wl_list_remove(&state->surface_destroy_listener.link);
601 free(state);
602}
603
604static void
605focus_state_seat_destroy(struct wl_listener *listener, void *data)
606{
607 struct focus_state *state = container_of(listener,
608 struct focus_state,
609 seat_destroy_listener);
610
611 wl_list_remove(&state->link);
612 focus_state_destroy(state);
613}
614
615static void
616focus_state_surface_destroy(struct wl_listener *listener, void *data)
617{
618 struct focus_state *state = container_of(listener,
619 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400620 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400621 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500622 struct weston_surface *main_surface, *next;
623 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200624
Pekka Paalanen01388e22013-04-25 13:57:44 +0300625 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
626
Kristian Høgsberge3778222012-07-31 17:29:30 -0400627 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500628 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
629 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400630 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100631 if (is_focus_view(view))
632 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400633
Jason Ekstranda7af7042013-10-12 22:38:11 -0500634 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400635 break;
636 }
637
Pekka Paalanen01388e22013-04-25 13:57:44 +0300638 /* if the focus was a sub-surface, activate its main surface */
639 if (main_surface != state->keyboard_focus)
640 next = main_surface;
641
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100642 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100644 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100645 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400646 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100647 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
648 if (state->ws->focus_animation)
649 weston_view_animation_destroy(state->ws->focus_animation);
650
651 state->ws->focus_animation = weston_fade_run(
652 state->ws->fsurf_front->view,
653 state->ws->fsurf_front->view->alpha, 0.0, 300,
654 focus_animation_done, state->ws);
655 }
656
Kristian Høgsberge3778222012-07-31 17:29:30 -0400657 wl_list_remove(&state->link);
658 focus_state_destroy(state);
659 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200660}
661
662static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400663focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200664{
Jonas Ådahl04769742012-06-13 00:01:24 +0200665 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200666
667 state = malloc(sizeof *state);
668 if (state == NULL)
669 return NULL;
670
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100671 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400672 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200673 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400674 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200675
676 state->seat_destroy_listener.notify = focus_state_seat_destroy;
677 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400678 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200679 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400680 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200681
682 return state;
683}
684
Jonas Ådahl8538b222012-08-29 22:13:03 +0200685static struct focus_state *
686ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
687{
688 struct workspace *ws = get_current_workspace(shell);
689 struct focus_state *state;
690
691 wl_list_for_each(state, &ws->focus_list, link)
692 if (state->seat == seat)
693 break;
694
695 if (&state->link == &ws->focus_list)
696 state = focus_state_create(seat, ws);
697
698 return state;
699}
700
Jonas Ådahl04769742012-06-13 00:01:24 +0200701static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800702focus_state_set_focus(struct focus_state *state,
703 struct weston_surface *surface)
704{
705 if (state->keyboard_focus) {
706 wl_list_remove(&state->surface_destroy_listener.link);
707 wl_list_init(&state->surface_destroy_listener.link);
708 }
709
710 state->keyboard_focus = surface;
711 if (surface)
712 wl_signal_add(&surface->destroy_signal,
713 &state->surface_destroy_listener);
714}
715
716static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400717restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200718{
719 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400720 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100721 struct wl_list pending_seat_list;
722 struct weston_seat *seat, *next_seat;
723
724 /* Temporarily steal the list of seats so that we can keep
725 * track of the seats we've already processed */
726 wl_list_init(&pending_seat_list);
727 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
728 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200729
730 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100731 wl_list_remove(&state->seat->link);
732 wl_list_insert(&shell->compositor->seat_list,
733 &state->seat->link);
734
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800735 if (state->seat->keyboard == NULL)
736 continue;
737
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400738 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200739
Kristian Høgsberge3148752013-05-06 23:19:49 -0400740 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200741 }
Neil Roberts4237f502014-04-09 16:33:31 +0100742
743 /* For any remaining seats that we don't have a focus state
744 * for we'll reset the keyboard focus to NULL */
745 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
746 wl_list_insert(&shell->compositor->seat_list, &seat->link);
747
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300748 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100749 continue;
750
751 weston_keyboard_set_focus(seat->keyboard, NULL);
752 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200753}
754
755static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200756replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
757 struct weston_seat *seat)
758{
759 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200760
761 wl_list_for_each(state, &ws->focus_list, link) {
762 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800763 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200764 return;
765 }
766 }
767}
768
769static void
770drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
771 struct weston_surface *surface)
772{
773 struct focus_state *state;
774
775 wl_list_for_each(state, &ws->focus_list, link)
776 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800777 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200778}
779
780static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100781animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
782 struct weston_view *from, struct weston_view *to)
783{
784 struct weston_output *output;
785 bool focus_surface_created = false;
786
787 /* FIXME: Only support dim animation using two layers */
788 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
789 return;
790
791 output = get_default_output(shell->compositor);
792 if (ws->fsurf_front == NULL && (from || to)) {
793 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800794 if (ws->fsurf_front == NULL)
795 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100796 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800797
798 ws->fsurf_back = create_focus_surface(shell->compositor, output);
799 if (ws->fsurf_back == NULL) {
800 focus_surface_destroy(ws->fsurf_front);
801 return;
802 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100803 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800804
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100805 focus_surface_created = true;
806 } else {
807 wl_list_remove(&ws->fsurf_front->view->layer_link);
808 wl_list_remove(&ws->fsurf_back->view->layer_link);
809 }
810
811 if (ws->focus_animation) {
812 weston_view_animation_destroy(ws->focus_animation);
813 ws->focus_animation = NULL;
814 }
815
816 if (to)
817 wl_list_insert(&to->layer_link,
818 &ws->fsurf_front->view->layer_link);
819 else if (from)
820 wl_list_insert(&ws->layer.view_list,
821 &ws->fsurf_front->view->layer_link);
822
823 if (focus_surface_created) {
824 ws->focus_animation = weston_fade_run(
825 ws->fsurf_front->view,
826 ws->fsurf_front->view->alpha, 0.6, 300,
827 focus_animation_done, ws);
828 } else if (from) {
829 wl_list_insert(&from->layer_link,
830 &ws->fsurf_back->view->layer_link);
831 ws->focus_animation = weston_stable_fade_run(
832 ws->fsurf_front->view, 0.0,
833 ws->fsurf_back->view, 0.6,
834 focus_animation_done, ws);
835 } else if (to) {
836 wl_list_insert(&ws->layer.view_list,
837 &ws->fsurf_back->view->layer_link);
838 ws->focus_animation = weston_stable_fade_run(
839 ws->fsurf_front->view, 0.0,
840 ws->fsurf_back->view, 0.6,
841 focus_animation_done, ws);
842 }
843}
844
845static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846workspace_destroy(struct workspace *ws)
847{
Jonas Ådahl04769742012-06-13 00:01:24 +0200848 struct focus_state *state, *next;
849
850 wl_list_for_each_safe(state, next, &ws->focus_list, link)
851 focus_state_destroy(state);
852
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 if (ws->fsurf_front)
854 focus_surface_destroy(ws->fsurf_front);
855 if (ws->fsurf_back)
856 focus_surface_destroy(ws->fsurf_back);
857
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200858 free(ws);
859}
860
Jonas Ådahl04769742012-06-13 00:01:24 +0200861static void
862seat_destroyed(struct wl_listener *listener, void *data)
863{
864 struct weston_seat *seat = data;
865 struct focus_state *state, *next;
866 struct workspace *ws = container_of(listener,
867 struct workspace,
868 seat_destroyed_listener);
869
870 wl_list_for_each_safe(state, next, &ws->focus_list, link)
871 if (state->seat == seat)
872 wl_list_remove(&state->link);
873}
874
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200875static struct workspace *
876workspace_create(void)
877{
878 struct workspace *ws = malloc(sizeof *ws);
879 if (ws == NULL)
880 return NULL;
881
882 weston_layer_init(&ws->layer, NULL);
883
Jonas Ådahl04769742012-06-13 00:01:24 +0200884 wl_list_init(&ws->focus_list);
885 wl_list_init(&ws->seat_destroyed_listener.link);
886 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100887 ws->fsurf_front = NULL;
888 ws->fsurf_back = NULL;
889 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200890
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200891 return ws;
892}
893
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200894static int
895workspace_is_empty(struct workspace *ws)
896{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200898}
899
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200900static struct workspace *
901get_workspace(struct desktop_shell *shell, unsigned int index)
902{
903 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200904 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905 pws += index;
906 return *pws;
907}
908
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800909struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200910get_current_workspace(struct desktop_shell *shell)
911{
912 return get_workspace(shell, shell->workspaces.current);
913}
914
915static void
916activate_workspace(struct desktop_shell *shell, unsigned int index)
917{
918 struct workspace *ws;
919
920 ws = get_workspace(shell, index);
921 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
922
923 shell->workspaces.current = index;
924}
925
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200926static unsigned int
927get_output_height(struct weston_output *output)
928{
929 return abs(output->region.extents.y1 - output->region.extents.y2);
930}
931
932static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100933view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200934{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935 struct weston_transform *transform;
936
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100937 if (is_focus_view(view)) {
938 struct focus_surface *fsurf = get_focus_surface(view->surface);
939 transform = &fsurf->workspace_transform;
940 } else {
941 struct shell_surface *shsurf = get_shell_surface(view->surface);
942 transform = &shsurf->workspace_transform;
943 }
944
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200945 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100947 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200948
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100949 weston_matrix_init(&transform->matrix);
950 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200951 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953}
954
955static void
956workspace_translate_out(struct workspace *ws, double fraction)
957{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959 unsigned int height;
960 double d;
961
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
963 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964 d = height * fraction;
965
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967 }
968}
969
970static void
971workspace_translate_in(struct workspace *ws, double fraction)
972{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200974 unsigned int height;
975 double d;
976
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
978 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200979
980 if (fraction > 0)
981 d = -(height - height * fraction);
982 else
983 d = height + height * fraction;
984
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100985 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 }
987}
988
989static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200990broadcast_current_workspace_state(struct desktop_shell *shell)
991{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700992 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200993
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700994 wl_resource_for_each(resource, &shell->workspaces.client_list)
995 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200996 shell->workspaces.current,
997 shell->workspaces.num);
998}
999
1000static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001001reverse_workspace_change_animation(struct desktop_shell *shell,
1002 unsigned int index,
1003 struct workspace *from,
1004 struct workspace *to)
1005{
1006 shell->workspaces.current = index;
1007
1008 shell->workspaces.anim_to = to;
1009 shell->workspaces.anim_from = from;
1010 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1011 shell->workspaces.anim_timestamp = 0;
1012
Scott Moreau4272e632012-08-13 09:58:41 -06001013 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001014}
1015
1016static void
1017workspace_deactivate_transforms(struct workspace *ws)
1018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001020 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001023 if (is_focus_view(view)) {
1024 struct focus_surface *fsurf = get_focus_surface(view->surface);
1025 transform = &fsurf->workspace_transform;
1026 } else {
1027 struct shell_surface *shsurf = get_shell_surface(view->surface);
1028 transform = &shsurf->workspace_transform;
1029 }
1030
1031 if (!wl_list_empty(&transform->link)) {
1032 wl_list_remove(&transform->link);
1033 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001034 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001036 }
1037}
1038
1039static void
1040finish_workspace_change_animation(struct desktop_shell *shell,
1041 struct workspace *from,
1042 struct workspace *to)
1043{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001044 struct weston_view *view;
1045
Scott Moreau4272e632012-08-13 09:58:41 -06001046 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001047
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001048 /* Views that extend past the bottom of the output are still
1049 * visible after the workspace animation ends but before its layer
1050 * is hidden. In that case, we need to damage below those views so
1051 * that the screen is properly repainted. */
1052 wl_list_for_each(view, &from->layer.view_list, layer_link)
1053 weston_view_damage_below(view);
1054
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055 wl_list_remove(&shell->workspaces.animation.link);
1056 workspace_deactivate_transforms(from);
1057 workspace_deactivate_transforms(to);
1058 shell->workspaces.anim_to = NULL;
1059
1060 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1061}
1062
1063static void
1064animate_workspace_change_frame(struct weston_animation *animation,
1065 struct weston_output *output, uint32_t msecs)
1066{
1067 struct desktop_shell *shell =
1068 container_of(animation, struct desktop_shell,
1069 workspaces.animation);
1070 struct workspace *from = shell->workspaces.anim_from;
1071 struct workspace *to = shell->workspaces.anim_to;
1072 uint32_t t;
1073 double x, y;
1074
1075 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1076 finish_workspace_change_animation(shell, from, to);
1077 return;
1078 }
1079
1080 if (shell->workspaces.anim_timestamp == 0) {
1081 if (shell->workspaces.anim_current == 0.0)
1082 shell->workspaces.anim_timestamp = msecs;
1083 else
1084 shell->workspaces.anim_timestamp =
1085 msecs -
1086 /* Invers of movement function 'y' below. */
1087 (asin(1.0 - shell->workspaces.anim_current) *
1088 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1089 M_2_PI);
1090 }
1091
1092 t = msecs - shell->workspaces.anim_timestamp;
1093
1094 /*
1095 * x = [0, π/2]
1096 * y(x) = sin(x)
1097 */
1098 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1099 y = sin(x);
1100
1101 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001102 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001103
1104 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1105 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1106 shell->workspaces.anim_current = y;
1107
Scott Moreau4272e632012-08-13 09:58:41 -06001108 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001110 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001112}
1113
1114static void
1115animate_workspace_change(struct desktop_shell *shell,
1116 unsigned int index,
1117 struct workspace *from,
1118 struct workspace *to)
1119{
1120 struct weston_output *output;
1121
1122 int dir;
1123
1124 if (index > shell->workspaces.current)
1125 dir = -1;
1126 else
1127 dir = 1;
1128
1129 shell->workspaces.current = index;
1130
1131 shell->workspaces.anim_dir = dir;
1132 shell->workspaces.anim_from = from;
1133 shell->workspaces.anim_to = to;
1134 shell->workspaces.anim_current = 0.0;
1135 shell->workspaces.anim_timestamp = 0;
1136
1137 output = container_of(shell->compositor->output_list.next,
1138 struct weston_output, link);
1139 wl_list_insert(&output->animation_list,
1140 &shell->workspaces.animation.link);
1141
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001142 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143
1144 workspace_translate_in(to, 0);
1145
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001146 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001147
Scott Moreau4272e632012-08-13 09:58:41 -06001148 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149}
1150
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001151static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001152update_workspace(struct desktop_shell *shell, unsigned int index,
1153 struct workspace *from, struct workspace *to)
1154{
1155 shell->workspaces.current = index;
1156 wl_list_insert(&from->layer.link, &to->layer.link);
1157 wl_list_remove(&from->layer.link);
1158}
1159
1160static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001161change_workspace(struct desktop_shell *shell, unsigned int index)
1162{
1163 struct workspace *from;
1164 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001165 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001166
1167 if (index == shell->workspaces.current)
1168 return;
1169
1170 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001172 return;
1173
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174 from = get_current_workspace(shell);
1175 to = get_workspace(shell, index);
1176
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001177 if (shell->workspaces.anim_from == to &&
1178 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001179 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001180 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001181 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182 return;
1183 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185 if (shell->workspaces.anim_to != NULL)
1186 finish_workspace_change_animation(shell,
1187 shell->workspaces.anim_from,
1188 shell->workspaces.anim_to);
1189
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001190 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001191
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001192 if (shell->focus_animation_type != ANIMATION_NONE) {
1193 wl_list_for_each(state, &from->focus_list, link)
1194 if (state->keyboard_focus)
1195 animate_focus_change(shell, from,
1196 get_default_view(state->keyboard_focus), NULL);
1197
1198 wl_list_for_each(state, &to->focus_list, link)
1199 if (state->keyboard_focus)
1200 animate_focus_change(shell, to,
1201 NULL, get_default_view(state->keyboard_focus));
1202 }
1203
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204 if (workspace_is_empty(to) && workspace_is_empty(from))
1205 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001206 else
1207 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001208
1209 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001210}
1211
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212static bool
1213workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1214{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001216 struct wl_list *e;
1217
1218 if (wl_list_empty(list))
1219 return false;
1220
1221 e = list->next;
1222
1223 if (e->next != list)
1224 return false;
1225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227}
1228
1229static void
Philip Withnall659163d2013-11-25 18:01:36 +00001230move_surface_to_workspace(struct desktop_shell *shell,
1231 struct shell_surface *shsurf,
1232 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001233{
1234 struct workspace *from;
1235 struct workspace *to;
1236 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001237 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001238 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001239
1240 if (workspace == shell->workspaces.current)
1241 return;
1242
Philip Withnall659163d2013-11-25 18:01:36 +00001243 view = get_default_view(shsurf->surface);
1244 if (!view)
1245 return;
1246
1247 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1248
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001249 if (workspace >= shell->workspaces.num)
1250 workspace = shell->workspaces.num - 1;
1251
Jonas Ådahle9d22502012-08-29 22:13:01 +02001252 from = get_current_workspace(shell);
1253 to = get_workspace(shell, workspace);
1254
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 wl_list_remove(&view->layer_link);
1256 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001257
Philip Withnall648a4dd2013-11-25 18:01:44 +00001258 shell_surface_update_child_surface_layers(shsurf);
1259
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001261 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1262 if (!seat->keyboard)
1263 continue;
1264
1265 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001267 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001268 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001271}
1272
1273static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001275 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276 unsigned int index)
1277{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001278 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 struct shell_surface *shsurf;
1281 struct workspace *from;
1282 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001283 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001284
Pekka Paalanen01388e22013-04-25 13:57:44 +03001285 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view = get_default_view(surface);
1287 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001288 index == shell->workspaces.current ||
1289 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001290 return;
1291
1292 from = get_current_workspace(shell);
1293 to = get_workspace(shell, index);
1294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 wl_list_remove(&view->layer_link);
1296 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297
Philip Withnall659163d2013-11-25 18:01:36 +00001298 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001299 if (shsurf != NULL)
1300 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001301
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001303 drop_focus_state(shell, from, surface);
1304
1305 if (shell->workspaces.anim_from == to &&
1306 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001307 wl_list_remove(&to->layer.link);
1308 wl_list_insert(from->layer.link.prev, &to->layer.link);
1309
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001310 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001311 broadcast_current_workspace_state(shell);
1312
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001313 return;
1314 }
1315
1316 if (shell->workspaces.anim_to != NULL)
1317 finish_workspace_change_animation(shell,
1318 shell->workspaces.anim_from,
1319 shell->workspaces.anim_to);
1320
1321 if (workspace_is_empty(from) &&
1322 workspace_has_only(to, surface))
1323 update_workspace(shell, index, from, to);
1324 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001325 if (shsurf != NULL &&
1326 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001327 wl_list_insert(&shell->workspaces.anim_sticky_list,
1328 &shsurf->workspace_transform.link);
1329
1330 animate_workspace_change(shell, index, from, to);
1331 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001332
1333 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001334
1335 state = ensure_focus_state(shell, seat);
1336 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001337 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001338}
1339
1340static void
1341workspace_manager_move_surface(struct wl_client *client,
1342 struct wl_resource *resource,
1343 struct wl_resource *surface_resource,
1344 uint32_t workspace)
1345{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001346 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001347 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001348 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001349 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001350 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001351
Pekka Paalanen01388e22013-04-25 13:57:44 +03001352 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001353 shell_surface = get_shell_surface(main_surface);
1354 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001356
1357 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001358}
1359
1360static const struct workspace_manager_interface workspace_manager_implementation = {
1361 workspace_manager_move_surface,
1362};
1363
1364static void
1365unbind_resource(struct wl_resource *resource)
1366{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001367 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001368}
1369
1370static void
1371bind_workspace_manager(struct wl_client *client,
1372 void *data, uint32_t version, uint32_t id)
1373{
1374 struct desktop_shell *shell = data;
1375 struct wl_resource *resource;
1376
Jason Ekstranda85118c2013-06-27 20:17:02 -05001377 resource = wl_resource_create(client,
1378 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001379
1380 if (resource == NULL) {
1381 weston_log("couldn't add workspace manager object");
1382 return;
1383 }
1384
Jason Ekstranda85118c2013-06-27 20:17:02 -05001385 wl_resource_set_implementation(resource,
1386 &workspace_manager_implementation,
1387 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001388 wl_list_insert(&shell->workspaces.client_list,
1389 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001390
1391 workspace_manager_send_state(resource,
1392 shell->workspaces.current,
1393 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001394}
1395
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001396static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001397touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1398 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1399{
1400}
1401
1402static void
1403touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1404{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001405 struct weston_touch_move_grab *move =
1406 (struct weston_touch_move_grab *) container_of(
1407 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001408
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001409 if (touch_id == 0)
1410 move->active = 0;
1411
Jonas Ådahl9484b692013-12-02 22:05:03 +01001412 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001413 shell_touch_grab_end(&move->base);
1414 free(move);
1415 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001416}
1417
1418static void
1419touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1420 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1421{
1422 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1423 struct shell_surface *shsurf = move->base.shsurf;
1424 struct weston_surface *es;
1425 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1426 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1427
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001428 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001429 return;
1430
1431 es = shsurf->surface;
1432
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001433 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001434
1435 weston_compositor_schedule_repaint(es->compositor);
1436}
1437
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001438static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001439touch_move_grab_frame(struct weston_touch_grab *grab)
1440{
1441}
1442
1443static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001444touch_move_grab_cancel(struct weston_touch_grab *grab)
1445{
1446 struct weston_touch_move_grab *move =
1447 (struct weston_touch_move_grab *) container_of(
1448 grab, struct shell_touch_grab, grab);
1449
1450 shell_touch_grab_end(&move->base);
1451 free(move);
1452}
1453
Rusty Lynch1084da52013-08-15 09:10:08 -07001454static const struct weston_touch_grab_interface touch_move_grab_interface = {
1455 touch_move_grab_down,
1456 touch_move_grab_up,
1457 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001458 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001459 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001460};
1461
1462static int
1463surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1464{
1465 struct weston_touch_move_grab *move;
1466
1467 if (!shsurf)
1468 return -1;
1469
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001470 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001471 return 0;
1472
1473 move = malloc(sizeof *move);
1474 if (!move)
1475 return -1;
1476
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001477 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001479 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001481 seat->touch->grab_y;
1482
1483 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1484 seat->touch);
1485
1486 return 0;
1487}
1488
1489static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001490noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001491{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001492}
1493
1494static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001495constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1496{
1497 struct shell_surface *shsurf = move->base.shsurf;
1498 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001499 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001500 const int safety = 50;
1501
1502 x = wl_fixed_to_int(pointer->x + move->dx);
1503 y = wl_fixed_to_int(pointer->y + move->dy);
1504
1505 panel_height = get_output_panel_height(shsurf->shell,
1506 shsurf->surface->output);
1507 bottom = y + shsurf->surface->height - shsurf->margin.bottom;
1508 if (bottom - panel_height < safety)
1509 y = panel_height + safety -
1510 shsurf->surface->height + shsurf->margin.bottom;
1511
1512 if (move->client_initiated &&
1513 y + shsurf->margin.top < panel_height)
1514 y = panel_height - shsurf->margin.top;
1515
1516 *cx = x;
1517 *cy = y;
1518}
1519
1520static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001521move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1522 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001523{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001524 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001525 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001526 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001527 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001528
1529 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001530 if (!shsurf)
1531 return;
1532
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001533 constrain_position(move, &cx, &cy);
1534
1535 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001538}
1539
1540static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001541move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001542 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001543{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001544 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1545 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001546 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001547 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001548
Daniel Stone4dbadb12012-05-30 16:31:51 +01001549 if (pointer->button_count == 0 &&
1550 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001551 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001552 free(grab);
1553 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001554}
1555
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001556static void
1557move_grab_cancel(struct weston_pointer_grab *grab)
1558{
1559 struct shell_grab *shell_grab =
1560 container_of(grab, struct shell_grab, grab);
1561
1562 shell_grab_end(shell_grab);
1563 free(grab);
1564}
1565
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001566static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001567 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001568 move_grab_motion,
1569 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001570 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001571};
1572
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001573static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001574surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1575 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001576{
1577 struct weston_move_grab *move;
1578
1579 if (!shsurf)
1580 return -1;
1581
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001582 if (shsurf->grabbed ||
1583 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584 return 0;
1585
1586 move = malloc(sizeof *move);
1587 if (!move)
1588 return -1;
1589
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001591 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001593 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001594 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595
1596 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001597 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598
1599 return 0;
1600}
1601
1602static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001603common_surface_move(struct wl_resource *resource,
1604 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001606 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001607 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001608 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001609
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001610 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001611 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001612 seat->pointer->button_count > 0 &&
1613 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001615 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001616 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001617 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001618 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001619 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001620 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001622 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001623 (surface_touch_move(shsurf, seat) < 0))
1624 wl_resource_post_no_memory(resource);
1625 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001626}
1627
Rafael Antognollie2a34552013-12-03 15:35:45 -02001628static void
1629shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1630 struct wl_resource *seat_resource, uint32_t serial)
1631{
1632 common_surface_move(resource, seat_resource, serial);
1633}
1634
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001635struct weston_resize_grab {
1636 struct shell_grab base;
1637 uint32_t edges;
1638 int32_t width, height;
1639};
1640
1641static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001642resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1643 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644{
1645 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001646 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647 struct shell_surface *shsurf = resize->base.shsurf;
1648 int32_t width, height;
1649 wl_fixed_t from_x, from_y;
1650 wl_fixed_t to_x, to_y;
1651
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001652 weston_pointer_move(pointer, x, y);
1653
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001654 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001655 return;
1656
Jason Ekstranda7af7042013-10-12 22:38:11 -05001657 weston_view_from_global_fixed(shsurf->view,
1658 pointer->grab_x, pointer->grab_y,
1659 &from_x, &from_y);
1660 weston_view_from_global_fixed(shsurf->view,
1661 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662
1663 width = resize->width;
1664 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1665 width += wl_fixed_to_int(from_x - to_x);
1666 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1667 width += wl_fixed_to_int(to_x - from_x);
1668 }
1669
1670 height = resize->height;
1671 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1672 height += wl_fixed_to_int(from_y - to_y);
1673 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1674 height += wl_fixed_to_int(to_y - from_y);
1675 }
1676
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001677 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678}
1679
1680static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001681send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001682{
1683 struct shell_surface *shsurf = get_shell_surface(surface);
1684
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001685 assert(shsurf);
1686
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001687 if (shsurf->resource)
1688 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001689 shsurf->resize_edges,
1690 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001691}
1692
1693static const struct weston_shell_client shell_client = {
1694 send_configure
1695};
1696
1697static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699 uint32_t time, uint32_t button, uint32_t state_w)
1700{
1701 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001702 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703 enum wl_pointer_button_state state = state_w;
1704
1705 if (pointer->button_count == 0 &&
1706 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1707 shell_grab_end(&resize->base);
1708 free(grab);
1709 }
1710}
1711
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001712static void
1713resize_grab_cancel(struct weston_pointer_grab *grab)
1714{
1715 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1716
1717 shell_grab_end(&resize->base);
1718 free(grab);
1719}
1720
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001721static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001722 noop_grab_focus,
1723 resize_grab_motion,
1724 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001725 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726};
1727
Giulio Camuffob8366642013-04-25 13:57:46 +03001728/*
1729 * Returns the bounding box of a surface and all its sub-surfaces,
1730 * in the surface coordinates system. */
1731static void
1732surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1733 int32_t *y, int32_t *w, int32_t *h) {
1734 pixman_region32_t region;
1735 pixman_box32_t *box;
1736 struct weston_subsurface *subsurface;
1737
1738 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 surface->width,
1740 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001741
1742 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1743 pixman_region32_union_rect(&region, &region,
1744 subsurface->position.x,
1745 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 subsurface->surface->width,
1747 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001748 }
1749
1750 box = pixman_region32_extents(&region);
1751 if (x)
1752 *x = box->x1;
1753 if (y)
1754 *y = box->y1;
1755 if (w)
1756 *w = box->x2 - box->x1;
1757 if (h)
1758 *h = box->y2 - box->y1;
1759
1760 pixman_region32_fini(&region);
1761}
1762
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001763static int
1764surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001765 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001766{
1767 struct weston_resize_grab *resize;
1768
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001769 if (shsurf->grabbed ||
1770 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771 return 0;
1772
1773 if (edges == 0 || edges > 15 ||
1774 (edges & 3) == 3 || (edges & 12) == 12)
1775 return 0;
1776
1777 resize = malloc(sizeof *resize);
1778 if (!resize)
1779 return -1;
1780
1781 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001782 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1783 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001784
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001785 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001786 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001787 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001788 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001789
1790 return 0;
1791}
1792
1793static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001794common_surface_resize(struct wl_resource *resource,
1795 struct wl_resource *seat_resource, uint32_t serial,
1796 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001797{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001798 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001799 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001800 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001801
Kristian Høgsberge3148752013-05-06 23:19:49 -04001802 if (seat->pointer->button_count == 0 ||
1803 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001804 seat->pointer->focus == NULL)
1805 return;
1806
1807 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1808 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001809 return;
1810
Kristian Høgsberge3148752013-05-06 23:19:49 -04001811 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001812 wl_resource_post_no_memory(resource);
1813}
1814
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001815static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001816shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1817 struct wl_resource *seat_resource, uint32_t serial,
1818 uint32_t edges)
1819{
1820 common_surface_resize(resource, seat_resource, serial, edges);
1821}
1822
1823static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001824busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001825{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001827 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001829 wl_fixed_t sx, sy;
1830
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 view = weston_compositor_pick_view(pointer->seat->compositor,
1832 pointer->x, pointer->y,
1833 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001834
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001835 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1836 shell_grab_end(grab);
1837 free(grab);
1838 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001839}
1840
1841static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001842busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1843 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001844{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001845 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001847
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001849busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001850 uint32_t time, uint32_t button, uint32_t state)
1851{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001852 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001853 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001854 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001855
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001856 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001857 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001858 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001859 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001860 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001861 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001862 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001863}
1864
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001865static void
1866busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1867{
1868 struct shell_grab *grab = (struct shell_grab *) base;
1869
1870 shell_grab_end(grab);
1871 free(grab);
1872}
1873
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001874static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001875 busy_cursor_grab_focus,
1876 busy_cursor_grab_motion,
1877 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001878 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001879};
1880
1881static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001882set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883{
1884 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001885
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001886 if (pointer->grab->interface == &busy_cursor_grab_interface)
1887 return;
1888
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001889 grab = malloc(sizeof *grab);
1890 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001891 return;
1892
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001893 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1894 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001895 /* Mark the shsurf as ungrabbed so that button binding is able
1896 * to move it. */
1897 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001898}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001899
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001900static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001901end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001902{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001903 struct shell_grab *grab;
1904 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001905
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001906 wl_list_for_each(seat, &compositor->seat_list, link) {
1907 if (seat->pointer == NULL)
1908 continue;
1909
1910 grab = (struct shell_grab *) seat->pointer->grab;
1911 if (grab->grab.interface == &busy_cursor_grab_interface &&
1912 wl_resource_get_client(grab->shsurf->resource) == client) {
1913 shell_grab_end(grab);
1914 free(grab);
1915 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001916 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001917}
1918
Scott Moreau9521d5e2012-04-19 13:06:17 -06001919static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001920handle_shell_client_destroy(struct wl_listener *listener, void *data);
1921
1922static int
1923xdg_ping_timeout_handler(void *data)
1924{
1925 struct shell_client *sc = data;
1926 struct weston_seat *seat;
1927 struct shell_surface *shsurf;
1928
1929 /* Client is not responding */
1930 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001931 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1932 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1933 continue;
1934 if (seat->pointer->focus->surface->resource == NULL)
1935 continue;
1936
1937 shsurf = get_shell_surface(seat->pointer->focus->surface);
1938 if (shsurf &&
1939 wl_resource_get_client(shsurf->resource) == sc->client)
1940 set_busy_cursor(shsurf, seat->pointer);
1941 }
1942
1943 return 1;
1944}
1945
1946static void
1947handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1948{
1949 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05001950 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001951 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001952 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001953
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001954 if (sc->unresponsive) {
1955 xdg_ping_timeout_handler(sc);
1956 return;
1957 }
1958
1959 sc->ping_serial = serial;
1960 loop = wl_display_get_event_loop(compositor->wl_display);
1961 if (sc->ping_timer == NULL)
1962 sc->ping_timer =
1963 wl_event_loop_add_timer(loop,
1964 xdg_ping_timeout_handler, sc);
1965 if (sc->ping_timer == NULL)
1966 return;
1967
1968 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1969
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001970 if (shell_surface_is_xdg_surface(shsurf) ||
1971 shell_surface_is_xdg_popup(shsurf))
1972 xdg_shell_send_ping(sc->resource, serial);
1973 else if (shell_surface_is_wl_shell_surface(shsurf))
1974 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001975}
1976
Scott Moreauff1db4a2012-04-17 19:06:18 -06001977static void
1978ping_handler(struct weston_surface *surface, uint32_t serial)
1979{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001980 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001981
1982 if (!shsurf)
1983 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001984 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001985 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001986 if (shsurf->surface == shsurf->shell->grab_surface)
1987 return;
1988
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001989 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001990}
1991
1992static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001993handle_pointer_focus(struct wl_listener *listener, void *data)
1994{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001995 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001997 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001998 uint32_t serial;
1999
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002001 return;
2002
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002004 serial = wl_display_next_serial(compositor->wl_display);
2005 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002006}
2007
2008static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002009shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2010{
2011 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002012 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002013}
2014
2015static void
2016shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2017{
2018 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002019 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002020}
2021
2022static void
2023handle_keyboard_focus(struct wl_listener *listener, void *data)
2024{
2025 struct weston_keyboard *keyboard = data;
2026 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2027
2028 if (seat->focused_surface) {
2029 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2030 if (shsurf)
2031 shell_surface_lose_keyboard_focus(shsurf);
2032 }
2033
2034 seat->focused_surface = keyboard->focus;
2035
2036 if (seat->focused_surface) {
2037 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2038 if (shsurf)
2039 shell_surface_gain_keyboard_focus(shsurf);
2040 }
2041}
2042
2043static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002044shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002045{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002046 if (sc->ping_serial != serial)
2047 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002048
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002049 sc->unresponsive = 0;
2050 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002051
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002052 if (sc->ping_timer) {
2053 wl_event_source_remove(sc->ping_timer);
2054 sc->ping_timer = NULL;
2055 }
2056
2057}
2058
2059static void
2060shell_surface_pong(struct wl_client *client,
2061 struct wl_resource *resource, uint32_t serial)
2062{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002063 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2064 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002065
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002066 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002067}
2068
2069static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002070set_title(struct shell_surface *shsurf, const char *title)
2071{
2072 free(shsurf->title);
2073 shsurf->title = strdup(title);
2074}
2075
2076static void
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002077set_margin(struct shell_surface *shsurf,
2078 int32_t left, int32_t right, int32_t top, int32_t bottom)
2079{
2080 shsurf->margin.left = left;
2081 shsurf->margin.right = right;
2082 shsurf->margin.top = top;
2083 shsurf->margin.bottom = bottom;
2084}
2085
2086static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002087shell_surface_set_title(struct wl_client *client,
2088 struct wl_resource *resource, const char *title)
2089{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002090 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002091
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002092 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002093}
2094
2095static void
2096shell_surface_set_class(struct wl_client *client,
2097 struct wl_resource *resource, const char *class)
2098{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002099 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002100
2101 free(shsurf->class);
2102 shsurf->class = strdup(class);
2103}
2104
Alex Wu4539b082012-03-01 12:57:46 +08002105static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002106restore_output_mode(struct weston_output *output)
2107{
Hardening57388e42013-09-18 23:56:36 +02002108 if (output->current_mode != output->original_mode ||
2109 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002110 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002111 output->original_mode,
2112 output->original_scale,
2113 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002114}
2115
2116static void
2117restore_all_output_modes(struct weston_compositor *compositor)
2118{
2119 struct weston_output *output;
2120
2121 wl_list_for_each(output, &compositor->output_list, link)
2122 restore_output_mode(output);
2123}
2124
Philip Withnallbecb77e2013-11-25 18:01:30 +00002125static int
2126get_output_panel_height(struct desktop_shell *shell,
2127 struct weston_output *output)
2128{
2129 struct weston_view *view;
2130 int panel_height = 0;
2131
2132 if (!output)
2133 return 0;
2134
2135 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2136 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002137 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002138 break;
2139 }
2140 }
2141
2142 return panel_height;
2143}
2144
Philip Withnall07926d92013-11-25 18:01:40 +00002145/* The surface will be inserted into the list immediately after the link
2146 * returned by this function (i.e. will be stacked immediately above the
2147 * returned link). */
2148static struct wl_list *
2149shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2150{
2151 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002152 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002153
2154 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002155 case SHELL_SURFACE_XWAYLAND:
2156 return &shsurf->shell->fullscreen_layer.view_list;
2157
2158 case SHELL_SURFACE_NONE:
2159 return NULL;
2160
Kristian Høgsbergead52422014-01-01 13:51:52 -08002161 case SHELL_SURFACE_POPUP:
2162 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002163 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002164 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002165 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002166 /* Move the surface to its parent layer so
2167 * that surfaces which are transient for
2168 * fullscreen surfaces don't get hidden by the
2169 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002170
2171 /* TODO: Handle a parent with multiple views */
2172 parent = get_default_view(shsurf->parent);
2173 if (parent)
2174 return parent->layer_link.prev;
2175 }
Philip Withnall07926d92013-11-25 18:01:40 +00002176
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002177 /* Move the surface to a normal workspace layer so that surfaces
2178 * which were previously fullscreen or transient are no longer
2179 * rendered on top. */
2180 ws = get_current_workspace(shsurf->shell);
2181 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002182 }
2183
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002184 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002185}
2186
Philip Withnall648a4dd2013-11-25 18:01:44 +00002187static void
2188shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2189{
2190 struct shell_surface *child;
2191
2192 /* Move the child layers to the same workspace as shsurf. They will be
2193 * stacked above shsurf. */
2194 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2195 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002196 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002197 weston_view_geometry_dirty(child->view);
2198 wl_list_remove(&child->view->layer_link);
2199 wl_list_insert(shsurf->view->layer_link.prev,
2200 &child->view->layer_link);
2201 weston_view_geometry_dirty(child->view);
2202 weston_surface_damage(child->surface);
2203
2204 /* Recurse. We don’t expect this to recurse very far (if
2205 * at all) because that would imply we have transient
2206 * (or popup) children of transient surfaces, which
2207 * would be unusual. */
2208 shell_surface_update_child_surface_layers(child);
2209 }
2210 }
2211}
2212
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002213/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002214 * geometry to ensure the changes are redrawn.
2215 *
2216 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2217 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002218static void
2219shell_surface_update_layer(struct shell_surface *shsurf)
2220{
2221 struct wl_list *new_layer_link;
2222
2223 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2224
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002225 if (new_layer_link == NULL)
2226 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002227 if (new_layer_link == &shsurf->view->layer_link)
2228 return;
2229
2230 weston_view_geometry_dirty(shsurf->view);
2231 wl_list_remove(&shsurf->view->layer_link);
2232 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2233 weston_view_geometry_dirty(shsurf->view);
2234 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002235
2236 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002237}
2238
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002239static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002240shell_surface_set_parent(struct shell_surface *shsurf,
2241 struct weston_surface *parent)
2242{
2243 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002244
2245 wl_list_remove(&shsurf->children_link);
2246 wl_list_init(&shsurf->children_link);
2247
2248 /* Insert into the parent surface’s child list. */
2249 if (parent != NULL) {
2250 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2251 if (parent_shsurf != NULL)
2252 wl_list_insert(&parent_shsurf->children_list,
2253 &shsurf->children_link);
2254 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002255}
2256
2257static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002258shell_surface_set_output(struct shell_surface *shsurf,
2259 struct weston_output *output)
2260{
2261 struct weston_surface *es = shsurf->surface;
2262
2263 /* get the default output, if the client set it as NULL
2264 check whether the ouput is available */
2265 if (output)
2266 shsurf->output = output;
2267 else if (es->output)
2268 shsurf->output = es->output;
2269 else
2270 shsurf->output = get_default_output(es->compositor);
2271}
2272
2273static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002274surface_clear_next_states(struct shell_surface *shsurf)
2275{
2276 shsurf->next_state.maximized = false;
2277 shsurf->next_state.fullscreen = false;
2278
2279 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2280 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2281 shsurf->state_changed = true;
2282}
2283
2284static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002285set_toplevel(struct shell_surface *shsurf)
2286{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002287 shell_surface_set_parent(shsurf, NULL);
2288 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002289 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002290
2291 /* The layer_link is updated in set_surface_type(),
2292 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002293}
2294
2295static void
2296shell_surface_set_toplevel(struct wl_client *client,
2297 struct wl_resource *resource)
2298{
2299 struct shell_surface *surface = wl_resource_get_user_data(resource);
2300
2301 set_toplevel(surface);
2302}
2303
2304static void
2305set_transient(struct shell_surface *shsurf,
2306 struct weston_surface *parent, int x, int y, uint32_t flags)
2307{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002308 assert(parent != NULL);
2309
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002310 shell_surface_set_parent(shsurf, parent);
2311
2312 surface_clear_next_states(shsurf);
2313
Philip Withnallbecb77e2013-11-25 18:01:30 +00002314 shsurf->transient.x = x;
2315 shsurf->transient.y = y;
2316 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002317
Rafael Antognollied207b42013-12-03 15:35:43 -02002318 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002319 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002320 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002321
2322 /* The layer_link is updated in set_surface_type(),
2323 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002324}
2325
2326static void
2327shell_surface_set_transient(struct wl_client *client,
2328 struct wl_resource *resource,
2329 struct wl_resource *parent_resource,
2330 int x, int y, uint32_t flags)
2331{
2332 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2333 struct weston_surface *parent =
2334 wl_resource_get_user_data(parent_resource);
2335
2336 set_transient(shsurf, parent, x, y, flags);
2337}
2338
2339static void
2340set_fullscreen(struct shell_surface *shsurf,
2341 uint32_t method,
2342 uint32_t framerate,
2343 struct weston_output *output)
2344{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002345 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002346
2347 shsurf->fullscreen_output = shsurf->output;
2348 shsurf->fullscreen.type = method;
2349 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002350
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002351 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002352
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002353 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002354 shsurf->output->width,
2355 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002356
2357 /* The layer_link is updated in set_surface_type(),
2358 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002359}
2360
2361static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002362weston_view_set_initial_position(struct weston_view *view,
2363 struct desktop_shell *shell);
2364
2365static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002367{
Philip Withnallf85fe842013-11-25 18:01:37 +00002368 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002369 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2370 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002371 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002372 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002373
Alex Wu4539b082012-03-01 12:57:46 +08002374 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2375 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002376
Alex Wu4539b082012-03-01 12:57:46 +08002377 wl_list_remove(&shsurf->fullscreen.transform.link);
2378 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002379
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 if (shsurf->fullscreen.black_view)
2381 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2382 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002383
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002384 if (shsurf->saved_position_valid)
2385 weston_view_set_position(shsurf->view,
2386 shsurf->saved_x, shsurf->saved_y);
2387 else
2388 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2389
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002390 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002392 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002393 shsurf->saved_rotation_valid = false;
2394 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002395
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002396 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002397}
2398
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002399static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002400shell_surface_set_fullscreen(struct wl_client *client,
2401 struct wl_resource *resource,
2402 uint32_t method,
2403 uint32_t framerate,
2404 struct wl_resource *output_resource)
2405{
2406 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2407 struct weston_output *output;
2408
2409 if (output_resource)
2410 output = wl_resource_get_user_data(output_resource);
2411 else
2412 output = NULL;
2413
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002414 shell_surface_set_parent(shsurf, NULL);
2415
Rafael Antognolli03b16592013-12-03 15:35:42 -02002416 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002418
2419 shsurf->next_state.fullscreen = true;
2420 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002421}
2422
2423static void
2424set_popup(struct shell_surface *shsurf,
2425 struct weston_surface *parent,
2426 struct weston_seat *seat,
2427 uint32_t serial,
2428 int32_t x,
2429 int32_t y)
2430{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002431 assert(parent != NULL);
2432
Philip Withnallbecb77e2013-11-25 18:01:30 +00002433 shsurf->popup.shseat = get_shell_seat(seat);
2434 shsurf->popup.serial = serial;
2435 shsurf->popup.x = x;
2436 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002437
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002438 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002439}
2440
2441static void
2442shell_surface_set_popup(struct wl_client *client,
2443 struct wl_resource *resource,
2444 struct wl_resource *seat_resource,
2445 uint32_t serial,
2446 struct wl_resource *parent_resource,
2447 int32_t x, int32_t y, uint32_t flags)
2448{
2449 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002450 struct weston_surface *parent =
2451 wl_resource_get_user_data(parent_resource);
2452
2453 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002454
Rafael Antognolli03b16592013-12-03 15:35:42 -02002455 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002456 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002457 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002458 wl_resource_get_user_data(seat_resource),
2459 serial, x, y);
2460}
2461
2462static void
2463set_maximized(struct shell_surface *shsurf,
2464 struct weston_output *output)
2465{
2466 struct desktop_shell *shell;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002467 uint32_t panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002468
Philip Withnall352e7ed2013-11-25 18:01:35 +00002469 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002470
2471 shell = shell_surface_get_shell(shsurf);
2472 panel_height = get_output_panel_height(shell, shsurf->output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002473
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002474 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002475 shsurf->output->width,
2476 shsurf->output->height - panel_height);
2477
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002478 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002479}
2480
2481static void
2482unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002483{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002484 /* undo all maximized things here */
2485 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002486
2487 if (shsurf->saved_position_valid)
2488 weston_view_set_position(shsurf->view,
2489 shsurf->saved_x, shsurf->saved_y);
2490 else
2491 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002492
2493 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002494 wl_list_insert(&shsurf->view->geometry.transformation_list,
2495 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002496 shsurf->saved_rotation_valid = false;
2497 }
2498
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002499 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002500}
2501
Philip Withnallbecb77e2013-11-25 18:01:30 +00002502static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002503set_minimized(struct weston_surface *surface, uint32_t is_true)
2504{
2505 struct shell_surface *shsurf;
2506 struct workspace *current_ws;
2507 struct weston_seat *seat;
2508 struct weston_surface *focus;
2509 struct weston_view *view;
2510
2511 view = get_default_view(surface);
2512 if (!view)
2513 return;
2514
2515 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2516
2517 shsurf = get_shell_surface(surface);
2518 current_ws = get_current_workspace(shsurf->shell);
2519
2520 wl_list_remove(&view->layer_link);
2521 /* hide or show, depending on the state */
2522 if (is_true) {
2523 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2524
2525 drop_focus_state(shsurf->shell, current_ws, view->surface);
2526 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2527 if (!seat->keyboard)
2528 continue;
2529 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2530 if (focus == view->surface)
2531 weston_keyboard_set_focus(seat->keyboard, NULL);
2532 }
2533 }
2534 else {
2535 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2536
2537 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2538 if (!seat->keyboard)
2539 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002540 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002541 }
2542 }
2543
2544 shell_surface_update_child_surface_layers(shsurf);
2545
2546 weston_view_damage_below(view);
2547}
2548
2549static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002550shell_surface_set_maximized(struct wl_client *client,
2551 struct wl_resource *resource,
2552 struct wl_resource *output_resource)
2553{
2554 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2555 struct weston_output *output;
2556
2557 if (output_resource)
2558 output = wl_resource_get_user_data(output_resource);
2559 else
2560 output = NULL;
2561
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002562 shell_surface_set_parent(shsurf, NULL);
2563
Rafael Antognolli03b16592013-12-03 15:35:42 -02002564 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002565 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002566
2567 shsurf->next_state.maximized = true;
2568 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002569}
2570
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002571/* This is only ever called from set_surface_type(), so there’s no need to
2572 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002573static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002575{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002576 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002577 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002578 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002579 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002580
Pekka Paalanen98262232011-12-01 10:42:22 +02002581 return 0;
2582}
2583
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002584static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002585set_full_output(struct shell_surface *shsurf)
2586{
2587 shsurf->saved_x = shsurf->view->geometry.x;
2588 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002589 shsurf->saved_width = shsurf->surface->width;
2590 shsurf->saved_height = shsurf->surface->height;
2591 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002592 shsurf->saved_position_valid = true;
2593
2594 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2595 wl_list_remove(&shsurf->rotation.transform.link);
2596 wl_list_init(&shsurf->rotation.transform.link);
2597 weston_view_geometry_dirty(shsurf->view);
2598 shsurf->saved_rotation_valid = true;
2599 }
2600}
2601
2602static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002603set_surface_type(struct shell_surface *shsurf)
2604{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002605 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002606 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002607
Philip Withnallbecb77e2013-11-25 18:01:30 +00002608 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002609
Rafael Antognolli03b16592013-12-03 15:35:42 -02002610 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002611 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002612
2613 switch (shsurf->type) {
2614 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002615 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002616 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002617 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002618 weston_view_set_position(shsurf->view,
2619 pev->geometry.x + shsurf->transient.x,
2620 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002621 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002622 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002623
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002624 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 weston_view_set_position(shsurf->view, shsurf->transient.x,
2626 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002627 break;
2628
Philip Withnall0f640e22013-11-25 18:01:31 +00002629 case SHELL_SURFACE_POPUP:
2630 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002631 default:
2632 break;
2633 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002634
2635 /* Update the surface’s layer. */
2636 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002637}
2638
Tiago Vignattibe143262012-04-16 17:31:41 +03002639static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002640shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002641{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002642 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002643}
2644
Alex Wu21858432012-04-01 20:13:08 +08002645static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002646black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002647
Jason Ekstranda7af7042013-10-12 22:38:11 -05002648static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002649create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002650 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002651 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002652{
2653 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002655
2656 surface = weston_surface_create(ec);
2657 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002658 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002659 return NULL;
2660 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002661 view = weston_view_create(surface);
2662 if (surface == NULL) {
2663 weston_log("no memory\n");
2664 weston_surface_destroy(surface);
2665 return NULL;
2666 }
Alex Wu4539b082012-03-01 12:57:46 +08002667
Alex Wu21858432012-04-01 20:13:08 +08002668 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002669 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002670 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002671 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002672 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002673 pixman_region32_fini(&surface->input);
2674 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002675
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002676 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002677 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002678
2679 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002680}
2681
Philip Withnalled8f1a92013-11-25 18:01:43 +00002682static void
2683shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2684{
2685 struct weston_output *output = shsurf->fullscreen_output;
2686
Rafael Antognolli03b16592013-12-03 15:35:42 -02002687 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002688
2689 if (!shsurf->fullscreen.black_view)
2690 shsurf->fullscreen.black_view =
2691 create_black_surface(shsurf->surface->compositor,
2692 shsurf->surface,
2693 output->x, output->y,
2694 output->width,
2695 output->height);
2696
2697 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2698 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2699 wl_list_insert(&shsurf->view->layer_link,
2700 &shsurf->fullscreen.black_view->layer_link);
2701 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2702 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002703
2704 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002705}
2706
Alex Wu4539b082012-03-01 12:57:46 +08002707/* Create black surface and append it to the associated fullscreen surface.
2708 * Handle size dismatch and positioning according to the method. */
2709static void
2710shell_configure_fullscreen(struct shell_surface *shsurf)
2711{
2712 struct weston_output *output = shsurf->fullscreen_output;
2713 struct weston_surface *surface = shsurf->surface;
2714 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002715 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002716 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002717
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002718 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2719 restore_output_mode(output);
2720
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002721 /* Reverse the effect of lower_fullscreen_layer() */
2722 wl_list_remove(&shsurf->view->layer_link);
2723 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2724
Philip Withnalled8f1a92013-11-25 18:01:43 +00002725 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002726
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002728 &surf_width, &surf_height);
2729
Alex Wu4539b082012-03-01 12:57:46 +08002730 switch (shsurf->fullscreen.type) {
2731 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002732 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002734 break;
2735 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002736 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002737 if (output->width == surf_width &&
2738 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 weston_view_set_position(shsurf->view,
2740 output->x - surf_x,
2741 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002742 break;
2743 }
2744
Alex Wu4539b082012-03-01 12:57:46 +08002745 matrix = &shsurf->fullscreen.transform.matrix;
2746 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002747
Scott Moreau1bad5db2012-08-18 01:04:05 -06002748 output_aspect = (float) output->width /
2749 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 /* XXX: Use surf_width and surf_height here? */
2751 surface_aspect = (float) surface->width /
2752 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002753 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002754 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002755 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002756 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002757 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002758 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002759
Alex Wu4539b082012-03-01 12:57:46 +08002760 weston_matrix_scale(matrix, scale, scale, 1);
2761 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002762 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002763 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002764 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2765 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002766 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002767
Alex Wu4539b082012-03-01 12:57:46 +08002768 break;
2769 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002770 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002771 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002772 surf_width * surface->buffer_viewport.buffer.scale,
2773 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002774 shsurf->fullscreen.framerate};
2775
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002776 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002777 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002778 weston_view_set_position(shsurf->view,
2779 output->x - surf_x,
2780 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002781 shsurf->fullscreen.black_view->surface->width = output->width;
2782 shsurf->fullscreen.black_view->surface->height = output->height;
2783 weston_view_set_position(shsurf->fullscreen.black_view,
2784 output->x - surf_x,
2785 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002786 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002787 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002788 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002789 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002790 }
Alex Wubd3354b2012-04-17 17:20:49 +08002791 }
Alex Wu4539b082012-03-01 12:57:46 +08002792 break;
2793 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002794 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002795 break;
2796 default:
2797 break;
2798 }
2799}
2800
Alex Wu4539b082012-03-01 12:57:46 +08002801static void
2802shell_map_fullscreen(struct shell_surface *shsurf)
2803{
Alex Wubd3354b2012-04-17 17:20:49 +08002804 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002805}
2806
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002807static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002808set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2809{
2810 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002811 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002812 shsurf->transient.x = x;
2813 shsurf->transient.y = y;
2814 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002815
2816 shell_surface_set_parent(shsurf, NULL);
2817
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002818 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002819 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002820}
2821
Kristian Høgsberg47792412014-05-04 13:47:06 -07002822static void
2823shell_interface_set_fullscreen(struct shell_surface *shsurf,
2824 uint32_t method,
2825 uint32_t framerate,
2826 struct weston_output *output)
2827{
2828 surface_clear_next_states(shsurf);
2829 set_fullscreen(shsurf, method, framerate, output);
2830
2831 shsurf->next_state.fullscreen = true;
2832 shsurf->state_changed = true;
2833}
2834
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002835static int
2836shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2837{
2838 return surface_move(shsurf, ws, 1);
2839}
2840
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002841static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002842
2843static void
2844destroy_shell_seat(struct wl_listener *listener, void *data)
2845{
2846 struct shell_seat *shseat =
2847 container_of(listener,
2848 struct shell_seat, seat_destroy_listener);
2849 struct shell_surface *shsurf, *prev = NULL;
2850
2851 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002852 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002853 shseat->popup_grab.client = NULL;
2854
2855 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2856 shsurf->popup.shseat = NULL;
2857 if (prev) {
2858 wl_list_init(&prev->popup.grab_link);
2859 }
2860 prev = shsurf;
2861 }
2862 wl_list_init(&prev->popup.grab_link);
2863 }
2864
2865 wl_list_remove(&shseat->seat_destroy_listener.link);
2866 free(shseat);
2867}
2868
Jason Ekstrand024177c2014-04-21 19:42:58 -05002869static void
2870shell_seat_caps_changed(struct wl_listener *listener, void *data)
2871{
2872 struct shell_seat *seat;
2873
2874 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2875
2876 if (seat->seat->keyboard &&
2877 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2878 wl_signal_add(&seat->seat->keyboard->focus_signal,
2879 &seat->keyboard_focus_listener);
2880 } else if (!seat->seat->keyboard) {
2881 wl_list_init(&seat->keyboard_focus_listener.link);
2882 }
2883
2884 if (seat->seat->pointer &&
2885 wl_list_empty(&seat->pointer_focus_listener.link)) {
2886 wl_signal_add(&seat->seat->pointer->focus_signal,
2887 &seat->pointer_focus_listener);
2888 } else if (!seat->seat->pointer) {
2889 wl_list_init(&seat->pointer_focus_listener.link);
2890 }
2891}
2892
Giulio Camuffo5085a752013-03-25 21:42:45 +01002893static struct shell_seat *
2894create_shell_seat(struct weston_seat *seat)
2895{
2896 struct shell_seat *shseat;
2897
2898 shseat = calloc(1, sizeof *shseat);
2899 if (!shseat) {
2900 weston_log("no memory to allocate shell seat\n");
2901 return NULL;
2902 }
2903
2904 shseat->seat = seat;
2905 wl_list_init(&shseat->popup_grab.surfaces_list);
2906
2907 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2908 wl_signal_add(&seat->destroy_signal,
2909 &shseat->seat_destroy_listener);
2910
Jason Ekstrand024177c2014-04-21 19:42:58 -05002911 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2912 wl_list_init(&shseat->keyboard_focus_listener.link);
2913
2914 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2915 wl_list_init(&shseat->pointer_focus_listener.link);
2916
2917 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2918 wl_signal_add(&seat->updated_caps_signal,
2919 &shseat->caps_changed_listener);
2920 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2921
Giulio Camuffo5085a752013-03-25 21:42:45 +01002922 return shseat;
2923}
2924
2925static struct shell_seat *
2926get_shell_seat(struct weston_seat *seat)
2927{
2928 struct wl_listener *listener;
2929
2930 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002931 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002932
2933 return container_of(listener,
2934 struct shell_seat, seat_destroy_listener);
2935}
2936
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002937static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002938popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002939{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002940 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002941 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002942 struct shell_seat *shseat =
2943 container_of(grab, struct shell_seat, popup_grab.grab);
2944 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002945 wl_fixed_t sx, sy;
2946
Jason Ekstranda7af7042013-10-12 22:38:11 -05002947 view = weston_compositor_pick_view(pointer->seat->compositor,
2948 pointer->x, pointer->y,
2949 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002950
Jason Ekstranda7af7042013-10-12 22:38:11 -05002951 if (view && view->surface->resource &&
2952 wl_resource_get_client(view->surface->resource) == client) {
2953 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002954 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002955 weston_pointer_set_focus(pointer, NULL,
2956 wl_fixed_from_int(0),
2957 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002958 }
2959}
2960
2961static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002962popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2963 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002964{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002965 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002966 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002967 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002968
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002969 weston_pointer_move(pointer, x, y);
2970
Neil Roberts96d790e2013-09-19 17:32:00 +01002971 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002972 weston_view_from_global_fixed(pointer->focus,
2973 pointer->x, pointer->y,
2974 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002975 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002976 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002977}
2978
2979static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002980popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002981 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002982{
2983 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002984 struct shell_seat *shseat =
2985 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002986 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002987 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002988 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002989 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002990
Neil Roberts96d790e2013-09-19 17:32:00 +01002991 resource_list = &grab->pointer->focus_resource_list;
2992 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002993 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002994 wl_resource_for_each(resource, resource_list) {
2995 wl_pointer_send_button(resource, serial,
2996 time, button, state);
2997 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002998 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002999 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003000 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003001 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003002 }
3003
Daniel Stone4dbadb12012-05-30 16:31:51 +01003004 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003005 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006}
3007
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003008static void
3009popup_grab_cancel(struct weston_pointer_grab *grab)
3010{
3011 popup_grab_end(grab->pointer);
3012}
3013
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003014static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003015 popup_grab_focus,
3016 popup_grab_motion,
3017 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003018 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003019};
3020
3021static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003022shell_surface_send_popup_done(struct shell_surface *shsurf)
3023{
3024 if (shell_surface_is_wl_shell_surface(shsurf))
3025 wl_shell_surface_send_popup_done(shsurf->resource);
3026 else if (shell_surface_is_xdg_popup(shsurf))
3027 xdg_popup_send_popup_done(shsurf->resource,
3028 shsurf->popup.serial);
3029}
3030
3031static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003032popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003033{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003034 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003035 struct shell_seat *shseat =
3036 container_of(grab, struct shell_seat, popup_grab.grab);
3037 struct shell_surface *shsurf;
3038 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003039
3040 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003041 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003042 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003043 shseat->popup_grab.grab.interface = NULL;
3044 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003045 /* Send the popup_done event to all the popups open */
3046 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003047 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003048 shsurf->popup.shseat = NULL;
3049 if (prev) {
3050 wl_list_init(&prev->popup.grab_link);
3051 }
3052 prev = shsurf;
3053 }
3054 wl_list_init(&prev->popup.grab_link);
3055 wl_list_init(&shseat->popup_grab.surfaces_list);
3056 }
3057}
3058
3059static void
3060add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3061{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003062 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003063
3064 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003065 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003066 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003067 /* We must make sure here that this popup was opened after
3068 * a mouse press, and not just by moving around with other
3069 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003070 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003071 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003072
Rob Bradforddfe31052013-06-26 19:49:11 +01003073 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003074 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003075 } else {
3076 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003077 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003078}
3079
3080static void
3081remove_popup_grab(struct shell_surface *shsurf)
3082{
3083 struct shell_seat *shseat = shsurf->popup.shseat;
3084
3085 wl_list_remove(&shsurf->popup.grab_link);
3086 wl_list_init(&shsurf->popup.grab_link);
3087 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003088 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003089 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003090 }
3091}
3092
3093static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003094shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003095{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003096 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003097 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003098
Jason Ekstranda7af7042013-10-12 22:38:11 -05003099 shsurf->surface->output = parent_view->output;
3100 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003101
Jason Ekstranda7af7042013-10-12 22:38:11 -05003102 weston_view_set_transform_parent(shsurf->view, parent_view);
3103 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3104 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003105
Kristian Høgsberge3148752013-05-06 23:19:49 -04003106 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003107 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003108 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003109 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003110 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003111 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003112}
3113
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003114static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003115 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116 shell_surface_move,
3117 shell_surface_resize,
3118 shell_surface_set_toplevel,
3119 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003120 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003121 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003122 shell_surface_set_maximized,
3123 shell_surface_set_title,
3124 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003125};
3126
3127static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003128destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003129{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003130 struct shell_surface *child, *next;
3131
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003132 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3133
Giulio Camuffo5085a752013-03-25 21:42:45 +01003134 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3135 remove_popup_grab(shsurf);
3136 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003137
Alex Wubd3354b2012-04-17 17:20:49 +08003138 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003139 shell_surface_is_top_fullscreen(shsurf))
3140 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003141
Jason Ekstranda7af7042013-10-12 22:38:11 -05003142 if (shsurf->fullscreen.black_view)
3143 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003144
Alex Wubd3354b2012-04-17 17:20:49 +08003145 /* As destroy_resource() use wl_list_for_each_safe(),
3146 * we can always remove the listener.
3147 */
3148 wl_list_remove(&shsurf->surface_destroy_listener.link);
3149 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003150 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003151
Jason Ekstranda7af7042013-10-12 22:38:11 -05003152 weston_view_destroy(shsurf->view);
3153
Philip Withnall648a4dd2013-11-25 18:01:44 +00003154 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003155 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3156 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003157
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003158 wl_list_remove(&shsurf->link);
3159 free(shsurf);
3160}
3161
3162static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003163shell_destroy_shell_surface(struct wl_resource *resource)
3164{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003165 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003166
Bryan Caina46b9462014-04-04 17:41:24 -05003167 if (!wl_list_empty(&shsurf->popup.grab_link))
3168 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003169 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003170}
3171
3172static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003173shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003174{
3175 struct shell_surface *shsurf = container_of(listener,
3176 struct shell_surface,
3177 surface_destroy_listener);
3178
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003179 if (shsurf->resource)
3180 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003181
3182 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003183}
3184
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003185static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003186fade_out_done(struct weston_view_animation *animation, void *data)
3187{
3188 struct shell_surface *shsurf = data;
3189
3190 weston_surface_destroy(shsurf->surface);
3191}
3192
3193static void
3194handle_resource_destroy(struct wl_listener *listener, void *data)
3195{
3196 struct shell_surface *shsurf =
3197 container_of(listener, struct shell_surface,
3198 resource_destroy_listener);
3199
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003200 if (!weston_surface_is_mapped(shsurf->surface))
3201 return;
3202
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003203 shsurf->surface->ref_count++;
3204
3205 pixman_region32_fini(&shsurf->surface->pending.input);
3206 pixman_region32_init(&shsurf->surface->pending.input);
3207 pixman_region32_fini(&shsurf->surface->input);
3208 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003209 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3210 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003211}
3212
3213static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003214shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003215
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003216struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003217get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003218{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003219 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003220 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003221 else
3222 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003223}
3224
Rafael Antognollie2a34552013-12-03 15:35:45 -02003225static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003226create_common_surface(struct shell_client *owner, void *shell,
3227 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003228 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003229{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003230 struct shell_surface *shsurf;
3231
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003232 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003233 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003234 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003235 }
3236
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003237 shsurf = calloc(1, sizeof *shsurf);
3238 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003239 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003240 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003241 }
3242
Jason Ekstranda7af7042013-10-12 22:38:11 -05003243 shsurf->view = weston_view_create(surface);
3244 if (!shsurf->view) {
3245 weston_log("no memory to allocate shell surface\n");
3246 free(shsurf);
3247 return NULL;
3248 }
3249
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003250 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003251 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003252
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003253 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3254 wl_resource_add_destroy_listener(surface->resource,
3255 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003256 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003257
Tiago Vignattibc052c92012-04-19 16:18:18 +03003258 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003259 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003260 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003261 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003262 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003263 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003264 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3265 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003266 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003267 wl_list_init(&shsurf->fullscreen.transform.link);
3268
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003269 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003270 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003271 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003272 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003273
3274 /* init link so its safe to always remove it in destroy_shell_surface */
3275 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003276 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003277
Pekka Paalanen460099f2012-01-20 16:48:25 +02003278 /* empty when not in use */
3279 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003280 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003281
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003282 wl_list_init(&shsurf->workspace_transform.link);
3283
Philip Withnall648a4dd2013-11-25 18:01:44 +00003284 wl_list_init(&shsurf->children_link);
3285 wl_list_init(&shsurf->children_list);
3286 shsurf->parent = NULL;
3287
Pekka Paalanen98262232011-12-01 10:42:22 +02003288 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003289
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003290 shsurf->client = client;
3291
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003292 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003293}
3294
Rafael Antognollie2a34552013-12-03 15:35:45 -02003295static struct shell_surface *
3296create_shell_surface(void *shell, struct weston_surface *surface,
3297 const struct weston_shell_client *client)
3298{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003299 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003300}
3301
Jason Ekstranda7af7042013-10-12 22:38:11 -05003302static struct weston_view *
3303get_primary_view(void *shell, struct shell_surface *shsurf)
3304{
3305 return shsurf->view;
3306}
3307
Tiago Vignattibc052c92012-04-19 16:18:18 +03003308static void
3309shell_get_shell_surface(struct wl_client *client,
3310 struct wl_resource *resource,
3311 uint32_t id,
3312 struct wl_resource *surface_resource)
3313{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003314 struct weston_surface *surface =
3315 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003316 struct shell_client *sc = wl_resource_get_user_data(resource);
3317 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003318 struct shell_surface *shsurf;
3319
3320 if (get_shell_surface(surface)) {
3321 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003322 WL_DISPLAY_ERROR_INVALID_OBJECT,
3323 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003324 return;
3325 }
3326
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003327 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003328 if (!shsurf) {
3329 wl_resource_post_error(surface_resource,
3330 WL_DISPLAY_ERROR_INVALID_OBJECT,
3331 "surface->configure already set");
3332 return;
3333 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003334
Jason Ekstranda85118c2013-06-27 20:17:02 -05003335 shsurf->resource =
3336 wl_resource_create(client,
3337 &wl_shell_surface_interface, 1, id);
3338 wl_resource_set_implementation(shsurf->resource,
3339 &shell_surface_implementation,
3340 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003341}
3342
Rafael Antognollie2a34552013-12-03 15:35:45 -02003343static bool
3344shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3345{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003346 /* A shell surface without a resource is created from xwayland
3347 * and is considered a wl_shell surface for now. */
3348
3349 return shsurf->resource == NULL ||
3350 wl_resource_instance_of(shsurf->resource,
3351 &wl_shell_surface_interface,
3352 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003353}
3354
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003355static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003356 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003357};
3358
Rafael Antognollie2a34552013-12-03 15:35:45 -02003359/****************************
3360 * xdg-shell implementation */
3361
3362static void
3363xdg_surface_destroy(struct wl_client *client,
3364 struct wl_resource *resource)
3365{
3366 wl_resource_destroy(resource);
3367}
3368
3369static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003370xdg_surface_set_parent(struct wl_client *client,
3371 struct wl_resource *resource,
3372 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003373{
3374 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3375 struct weston_surface *parent;
3376
3377 if (parent_resource)
3378 parent = wl_resource_get_user_data(parent_resource);
3379 else
3380 parent = NULL;
3381
3382 shell_surface_set_parent(shsurf, parent);
3383}
3384
3385static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003386xdg_surface_set_margin(struct wl_client *client,
3387 struct wl_resource *resource,
3388 int32_t left,
3389 int32_t right,
3390 int32_t top,
3391 int32_t bottom)
3392{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3394
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07003395 set_margin(shsurf, left, right, top, bottom);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003396}
3397
3398static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003399xdg_surface_set_app_id(struct wl_client *client,
3400 struct wl_resource *resource,
3401 const char *app_id)
3402{
3403 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3404
3405 free(shsurf->class);
3406 shsurf->class = strdup(app_id);
3407}
3408
3409static void
3410xdg_surface_set_title(struct wl_client *client,
3411 struct wl_resource *resource, const char *title)
3412{
3413 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3414
3415 set_title(shsurf, title);
3416}
3417
3418static void
3419xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3420 struct wl_resource *seat_resource, uint32_t serial)
3421{
3422 common_surface_move(resource, seat_resource, serial);
3423}
3424
3425static void
3426xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3427 struct wl_resource *seat_resource, uint32_t serial,
3428 uint32_t edges)
3429{
3430 common_surface_resize(resource, seat_resource, serial, edges);
3431}
3432
3433static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003434xdg_surface_ack_configure(struct wl_client *client,
3435 struct wl_resource *resource,
3436 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003437{
3438 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3439
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003440 if (shsurf->state_requested) {
3441 shsurf->next_state = shsurf->requested_state;
3442 shsurf->state_changed = true;
3443 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003444 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003445}
3446
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003447static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003448xdg_surface_set_maximized(struct wl_client *client,
3449 struct wl_resource *resource)
3450{
3451 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3452
3453 shsurf->state_requested = true;
3454 shsurf->requested_state.maximized = true;
3455 set_maximized(shsurf, NULL);
3456}
3457
3458static void
3459xdg_surface_unset_maximized(struct wl_client *client,
3460 struct wl_resource *resource)
3461{
3462 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3463
3464 shsurf->state_requested = true;
3465 shsurf->requested_state.maximized = false;
3466 shsurf->client->send_configure(shsurf->surface, 0, 0);
3467}
3468
3469static void
3470xdg_surface_set_fullscreen(struct wl_client *client,
3471 struct wl_resource *resource,
3472 struct wl_resource *output_resource)
3473{
3474 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3475 struct weston_output *output;
3476
3477 shsurf->state_requested = true;
3478 shsurf->requested_state.fullscreen = true;
3479
3480 if (output_resource)
3481 output = wl_resource_get_user_data(output_resource);
3482 else
3483 output = NULL;
3484
3485 shsurf->recommended_output = output;
3486
3487 set_fullscreen(shsurf,
3488 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3489 0, shsurf->recommended_output);
3490}
3491
3492static void
3493xdg_surface_unset_fullscreen(struct wl_client *client,
3494 struct wl_resource *resource)
3495{
3496 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3497
3498 shsurf->state_requested = true;
3499 shsurf->requested_state.fullscreen = false;
3500 shsurf->client->send_configure(shsurf->surface, 0, 0);
3501}
3502
3503static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003504xdg_surface_set_minimized(struct wl_client *client,
3505 struct wl_resource *resource)
3506{
3507 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3508
3509 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3510 return;
3511
3512 /* apply compositor's own minimization logic (hide) */
3513 set_minimized(shsurf->surface, 1);
3514}
3515
Rafael Antognollie2a34552013-12-03 15:35:45 -02003516static const struct xdg_surface_interface xdg_surface_implementation = {
3517 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003518 xdg_surface_set_parent,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003519 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003520 xdg_surface_set_title,
3521 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003522 xdg_surface_move,
3523 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003524 xdg_surface_ack_configure,
3525 xdg_surface_set_maximized,
3526 xdg_surface_unset_maximized,
3527 xdg_surface_set_fullscreen,
3528 xdg_surface_unset_fullscreen,
3529 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003530};
3531
3532static void
3533xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003534 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003535{
3536 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003537 uint32_t *s;
3538 struct wl_array states;
3539 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003540
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003541 assert(shsurf);
3542
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003543 if (!shsurf->resource)
3544 return;
3545
3546 wl_array_init(&states);
3547 if (shsurf->requested_state.fullscreen) {
3548 s = wl_array_add(&states, sizeof *s);
3549 *s = XDG_SURFACE_STATE_FULLSCREEN;
3550 } else if (shsurf->requested_state.maximized) {
3551 s = wl_array_add(&states, sizeof *s);
3552 *s = XDG_SURFACE_STATE_MAXIMIZED;
3553 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003554 if (shsurf->resize_edges != 0) {
3555 s = wl_array_add(&states, sizeof *s);
3556 *s = XDG_SURFACE_STATE_RESIZING;
3557 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003558 if (shsurf->focus_count > 0) {
3559 s = wl_array_add(&states, sizeof *s);
3560 *s = XDG_SURFACE_STATE_ACTIVATED;
3561 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003562
3563 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3564 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3565
3566 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003567}
3568
3569static const struct weston_shell_client xdg_client = {
3570 xdg_send_configure
3571};
3572
3573static void
3574xdg_use_unstable_version(struct wl_client *client,
3575 struct wl_resource *resource,
3576 int32_t version)
3577{
3578 if (version > 1) {
3579 wl_resource_post_error(resource,
3580 1,
3581 "xdg-shell:: version not implemented yet.");
3582 return;
3583 }
3584}
3585
3586static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003587create_xdg_surface(struct shell_client *owner, void *shell,
3588 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003589 const struct weston_shell_client *client)
3590{
3591 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003592
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003593 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003594 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003595
3596 return shsurf;
3597}
3598
3599static void
3600xdg_get_xdg_surface(struct wl_client *client,
3601 struct wl_resource *resource,
3602 uint32_t id,
3603 struct wl_resource *surface_resource)
3604{
3605 struct weston_surface *surface =
3606 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003607 struct shell_client *sc = wl_resource_get_user_data(resource);
3608 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003609 struct shell_surface *shsurf;
3610
3611 if (get_shell_surface(surface)) {
3612 wl_resource_post_error(surface_resource,
3613 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003614 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003615 return;
3616 }
3617
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003618 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003619 if (!shsurf) {
3620 wl_resource_post_error(surface_resource,
3621 WL_DISPLAY_ERROR_INVALID_OBJECT,
3622 "surface->configure already set");
3623 return;
3624 }
3625
3626 shsurf->resource =
3627 wl_resource_create(client,
3628 &xdg_surface_interface, 1, id);
3629 wl_resource_set_implementation(shsurf->resource,
3630 &xdg_surface_implementation,
3631 shsurf, shell_destroy_shell_surface);
3632}
3633
3634static bool
3635shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3636{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003637 return shsurf->resource &&
3638 wl_resource_instance_of(shsurf->resource,
3639 &xdg_surface_interface,
3640 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003641}
3642
3643/* xdg-popup implementation */
3644
3645static void
3646xdg_popup_destroy(struct wl_client *client,
3647 struct wl_resource *resource)
3648{
3649 wl_resource_destroy(resource);
3650}
3651
Rafael Antognollie2a34552013-12-03 15:35:45 -02003652static const struct xdg_popup_interface xdg_popup_implementation = {
3653 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003654};
3655
3656static void
3657xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003658 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003659{
3660}
3661
3662static const struct weston_shell_client xdg_popup_client = {
3663 xdg_popup_send_configure
3664};
3665
3666static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003667create_xdg_popup(struct shell_client *owner, void *shell,
3668 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003669 const struct weston_shell_client *client,
3670 struct weston_surface *parent,
3671 struct shell_seat *seat,
3672 uint32_t serial,
3673 int32_t x, int32_t y)
3674{
3675 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003676
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003677 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003678 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003679 shsurf->popup.shseat = seat;
3680 shsurf->popup.serial = serial;
3681 shsurf->popup.x = x;
3682 shsurf->popup.y = y;
3683 shell_surface_set_parent(shsurf, parent);
3684
3685 return shsurf;
3686}
3687
3688static void
3689xdg_get_xdg_popup(struct wl_client *client,
3690 struct wl_resource *resource,
3691 uint32_t id,
3692 struct wl_resource *surface_resource,
3693 struct wl_resource *parent_resource,
3694 struct wl_resource *seat_resource,
3695 uint32_t serial,
3696 int32_t x, int32_t y, uint32_t flags)
3697{
3698 struct weston_surface *surface =
3699 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003700 struct shell_client *sc = wl_resource_get_user_data(resource);
3701 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003702 struct shell_surface *shsurf;
3703 struct weston_surface *parent;
3704 struct shell_seat *seat;
3705
3706 if (get_shell_surface(surface)) {
3707 wl_resource_post_error(surface_resource,
3708 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003709 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003710 return;
3711 }
3712
3713 if (!parent_resource) {
3714 wl_resource_post_error(surface_resource,
3715 WL_DISPLAY_ERROR_INVALID_OBJECT,
3716 "xdg_shell::get_xdg_popup requires a parent shell surface");
3717 }
3718
3719 parent = wl_resource_get_user_data(parent_resource);
3720 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3721
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003722 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003723 parent, seat, serial, x, y);
3724 if (!shsurf) {
3725 wl_resource_post_error(surface_resource,
3726 WL_DISPLAY_ERROR_INVALID_OBJECT,
3727 "surface->configure already set");
3728 return;
3729 }
3730
3731 shsurf->resource =
3732 wl_resource_create(client,
3733 &xdg_popup_interface, 1, id);
3734 wl_resource_set_implementation(shsurf->resource,
3735 &xdg_popup_implementation,
3736 shsurf, shell_destroy_shell_surface);
3737}
3738
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003739static void
3740xdg_pong(struct wl_client *client,
3741 struct wl_resource *resource, uint32_t serial)
3742{
3743 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003744
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003745 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003746}
3747
Rafael Antognollie2a34552013-12-03 15:35:45 -02003748static bool
3749shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3750{
3751 return wl_resource_instance_of(shsurf->resource,
3752 &xdg_popup_interface,
3753 &xdg_popup_implementation);
3754}
3755
3756static const struct xdg_shell_interface xdg_implementation = {
3757 xdg_use_unstable_version,
3758 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003759 xdg_get_xdg_popup,
3760 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003761};
3762
3763static int
3764xdg_shell_unversioned_dispatch(const void *implementation,
3765 void *_target, uint32_t opcode,
3766 const struct wl_message *message,
3767 union wl_argument *args)
3768{
3769 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003770 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003771
3772 if (opcode != 0) {
3773 wl_resource_post_error(resource,
3774 WL_DISPLAY_ERROR_INVALID_OBJECT,
3775 "must call use_unstable_version first");
3776 return 0;
3777 }
3778
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003779#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003780
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003781 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3782 "shell implementation doesn't match protocol version");
3783
Rafael Antognollie2a34552013-12-03 15:35:45 -02003784 if (args[0].i != XDG_SERVER_VERSION) {
3785 wl_resource_post_error(resource,
3786 WL_DISPLAY_ERROR_INVALID_OBJECT,
3787 "incompatible version, server is %d "
3788 "client wants %d",
3789 XDG_SERVER_VERSION, args[0].i);
3790 return 0;
3791 }
3792
3793 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003794 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003795
3796 return 1;
3797}
3798
3799/* end of xdg-shell implementation */
3800/***********************************/
3801
Kristian Høgsberg07937562011-04-12 17:25:42 -04003802static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003803shell_fade(struct desktop_shell *shell, enum fade_type type);
3804
3805static int
3806screensaver_timeout(void *data)
3807{
3808 struct desktop_shell *shell = data;
3809
3810 shell_fade(shell, FADE_OUT);
3811
3812 return 1;
3813}
3814
3815static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003816handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003817{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003818 struct desktop_shell *shell =
3819 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003820
Pekka Paalanen18027e52011-12-02 16:31:49 +02003821 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003822
3823 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003824 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003825}
3826
3827static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003828launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003829{
3830 if (shell->screensaver.binding)
3831 return;
3832
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003833 if (!shell->screensaver.path) {
3834 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003835 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003836 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003837
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003838 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003839 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003840 return;
3841 }
3842
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003843 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003844 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003845 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003846 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003847}
3848
3849static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003850terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003851{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003852 if (shell->screensaver.process.pid == 0)
3853 return;
3854
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003855 /* Disarm the screensaver timer, otherwise it may fire when the
3856 * compositor is not in the idle state. In that case, the screen will
3857 * be locked, but the wake_signal won't fire on user input, making the
3858 * system unresponsive. */
3859 wl_event_source_timer_update(shell->screensaver.timer, 0);
3860
Pekka Paalanen18027e52011-12-02 16:31:49 +02003861 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003862}
3863
3864static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003865configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003866{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003867 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003868
Jason Ekstranda7af7042013-10-12 22:38:11 -05003869 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3870 if (v->output == ev->output && v != ev) {
3871 weston_view_unmap(v);
3872 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003873 }
3874 }
3875
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003876 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003877
Jason Ekstranda7af7042013-10-12 22:38:11 -05003878 if (wl_list_empty(&ev->layer_link)) {
3879 wl_list_insert(&layer->view_list, &ev->layer_link);
3880 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003881 }
3882}
3883
3884static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003885background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003886{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003887 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003888 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003889
Jason Ekstranda7af7042013-10-12 22:38:11 -05003890 view = container_of(es->views.next, struct weston_view, surface_link);
3891
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003892 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003893}
3894
3895static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003896desktop_shell_set_background(struct wl_client *client,
3897 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003898 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003899 struct wl_resource *surface_resource)
3900{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003901 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003902 struct weston_surface *surface =
3903 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003904 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003905
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003906 if (surface->configure) {
3907 wl_resource_post_error(surface_resource,
3908 WL_DISPLAY_ERROR_INVALID_OBJECT,
3909 "surface role already assigned");
3910 return;
3911 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003912
Jason Ekstranda7af7042013-10-12 22:38:11 -05003913 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3914 weston_view_destroy(view);
3915 view = weston_view_create(surface);
3916
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003917 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003918 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003919 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003921 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003922 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003923 surface->output->width,
3924 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003925}
3926
3927static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003928panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003929{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003930 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003932
Jason Ekstranda7af7042013-10-12 22:38:11 -05003933 view = container_of(es->views.next, struct weston_view, surface_link);
3934
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003935 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003936}
3937
3938static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003939desktop_shell_set_panel(struct wl_client *client,
3940 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003941 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003942 struct wl_resource *surface_resource)
3943{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003944 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003945 struct weston_surface *surface =
3946 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003947 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003948
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003949 if (surface->configure) {
3950 wl_resource_post_error(surface_resource,
3951 WL_DISPLAY_ERROR_INVALID_OBJECT,
3952 "surface role already assigned");
3953 return;
3954 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003955
Jason Ekstranda7af7042013-10-12 22:38:11 -05003956 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3957 weston_view_destroy(view);
3958 view = weston_view_create(surface);
3959
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003960 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003961 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003962 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003963 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003964 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003965 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003966 surface->output->width,
3967 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003968}
3969
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003970static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003971lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003972{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003973 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003974 struct weston_view *view;
3975
3976 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003977
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003978 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003979 return;
3980
Jason Ekstranda7af7042013-10-12 22:38:11 -05003981 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003982
3983 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003984 wl_list_insert(&shell->lock_layer.view_list,
3985 &view->layer_link);
3986 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003987 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003988 }
3989}
3990
3991static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003992handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003993{
Tiago Vignattibe143262012-04-16 17:31:41 +03003994 struct desktop_shell *shell =
3995 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003996
Martin Minarik6d118362012-06-07 18:01:59 +02003997 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003998 shell->lock_surface = NULL;
3999}
4000
4001static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004002desktop_shell_set_lock_surface(struct wl_client *client,
4003 struct wl_resource *resource,
4004 struct wl_resource *surface_resource)
4005{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004006 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004007 struct weston_surface *surface =
4008 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004009
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004010 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004011
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004012 if (!shell->locked)
4013 return;
4014
Pekka Paalanen98262232011-12-01 10:42:22 +02004015 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004016
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004017 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004018 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004019 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004020
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004021 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004022 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004023 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004024}
4025
4026static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004027resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004028{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004029 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004030
Pekka Paalanen77346a62011-11-30 16:26:35 +02004031 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004032
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004033 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004034 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004035 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004036 &shell->input_panel_layer.link);
4037 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004038 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004039 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004040 wl_list_insert(&shell->compositor->cursor_layer.link,
4041 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004042 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004043 wl_list_insert(&shell->fullscreen_layer.link,
4044 &shell->panel_layer.link);
4045 wl_list_insert(&shell->panel_layer.link,
4046 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004047
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004048 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004049
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004050 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004051 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004052 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004053}
4054
4055static void
4056desktop_shell_unlock(struct wl_client *client,
4057 struct wl_resource *resource)
4058{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004059 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004060
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004061 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004062
4063 if (shell->locked)
4064 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004065}
4066
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004067static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004068desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004069 struct wl_resource *resource,
4070 struct wl_resource *surface_resource)
4071{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004072 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004073
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004074 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004075 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004076}
4077
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004078static void
4079desktop_shell_desktop_ready(struct wl_client *client,
4080 struct wl_resource *resource)
4081{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004082 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004083
4084 shell_fade_startup(shell);
4085}
4086
Kristian Høgsberg75840622011-09-06 13:48:16 -04004087static const struct desktop_shell_interface desktop_shell_implementation = {
4088 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004089 desktop_shell_set_panel,
4090 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004091 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004092 desktop_shell_set_grab_surface,
4093 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004094};
4095
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004096static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004097get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004098{
4099 struct shell_surface *shsurf;
4100
4101 shsurf = get_shell_surface(surface);
4102 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004103 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004104 return shsurf->type;
4105}
4106
Kristian Høgsberg75840622011-09-06 13:48:16 -04004107static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004108move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004109{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004110 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004111 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004112 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004113
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004114 if (seat->pointer->focus == NULL)
4115 return;
4116
4117 focus = seat->pointer->focus->surface;
4118
Pekka Paalanen01388e22013-04-25 13:57:44 +03004119 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004120 if (surface == NULL)
4121 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004122
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004123 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004124 if (shsurf == NULL || shsurf->state.fullscreen ||
4125 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004126 return;
4127
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004128 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004129}
4130
4131static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004132maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4133{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004134 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004135 struct weston_surface *surface;
4136 struct shell_surface *shsurf;
4137
4138 surface = weston_surface_get_main_surface(focus);
4139 if (surface == NULL)
4140 return;
4141
4142 shsurf = get_shell_surface(surface);
4143 if (shsurf == NULL)
4144 return;
4145
4146 if (!shell_surface_is_xdg_surface(shsurf))
4147 return;
4148
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004149 shsurf->state_requested = true;
4150 shsurf->requested_state.maximized = !shsurf->state.maximized;
4151 if (shsurf->requested_state.maximized)
4152 set_maximized(shsurf, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004153}
4154
4155static void
4156fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4157{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004158 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004159 struct weston_surface *surface;
4160 struct shell_surface *shsurf;
4161
4162 surface = weston_surface_get_main_surface(focus);
4163 if (surface == NULL)
4164 return;
4165
4166 shsurf = get_shell_surface(surface);
4167 if (shsurf == NULL)
4168 return;
4169
4170 if (!shell_surface_is_xdg_surface(shsurf))
4171 return;
4172
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004173 shsurf->state_requested = true;
4174 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
4175 if (shsurf->requested_state.fullscreen)
4176 set_fullscreen(shsurf,
4177 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
4178 0, shsurf->recommended_output);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004179}
4180
4181static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004182touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4183{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004184 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004185 struct weston_surface *surface;
4186 struct shell_surface *shsurf;
4187
4188 surface = weston_surface_get_main_surface(focus);
4189 if (surface == NULL)
4190 return;
4191
4192 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004193 if (shsurf == NULL || shsurf->state.fullscreen ||
4194 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004195 return;
4196
4197 surface_touch_move(shsurf, (struct weston_seat *) seat);
4198}
4199
4200static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004201resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004202{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004203 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004204 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004205 uint32_t edges = 0;
4206 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004207 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004208
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004209 if (seat->pointer->focus == NULL)
4210 return;
4211
4212 focus = seat->pointer->focus->surface;
4213
Pekka Paalanen01388e22013-04-25 13:57:44 +03004214 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004215 if (surface == NULL)
4216 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004217
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004218 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004219 if (shsurf == NULL || shsurf->state.fullscreen ||
4220 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004221 return;
4222
Jason Ekstranda7af7042013-10-12 22:38:11 -05004223 weston_view_from_global(shsurf->view,
4224 wl_fixed_to_int(seat->pointer->grab_x),
4225 wl_fixed_to_int(seat->pointer->grab_y),
4226 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004227
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004228 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004229 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004230 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004231 edges |= 0;
4232 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004233 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004234
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004235 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004236 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004237 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004238 edges |= 0;
4239 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004240 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004241
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004242 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004243}
4244
4245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004246surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004247 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004248{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004249 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004250 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004251 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004252 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004253
Pekka Paalanen01388e22013-04-25 13:57:44 +03004254 /* XXX: broken for windows containing sub-surfaces */
4255 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004256 if (surface == NULL)
4257 return;
4258
4259 shsurf = get_shell_surface(surface);
4260 if (!shsurf)
4261 return;
4262
Jason Ekstranda7af7042013-10-12 22:38:11 -05004263 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004264
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265 if (shsurf->view->alpha > 1.0)
4266 shsurf->view->alpha = 1.0;
4267 if (shsurf->view->alpha < step)
4268 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004269
Jason Ekstranda7af7042013-10-12 22:38:11 -05004270 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004271 weston_surface_damage(surface);
4272}
4273
4274static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004275do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004276 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004277{
Daniel Stone37816df2012-05-16 18:45:18 +01004278 struct weston_seat *ws = (struct weston_seat *) seat;
4279 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004280 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004281 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004282
4283 wl_list_for_each(output, &compositor->output_list, link) {
4284 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004285 wl_fixed_to_double(seat->pointer->x),
4286 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004287 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004288 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004289 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004290 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004291 increment = -output->zoom.increment;
4292 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004293 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004294 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004295 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004296 else
4297 increment = 0;
4298
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004299 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004300
Scott Moreaue6603982012-06-11 13:07:51 -06004301 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004302 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004303 else if (output->zoom.level > output->zoom.max_level)
4304 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004305 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004306 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004307 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004308
Scott Moreaue6603982012-06-11 13:07:51 -06004309 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004310
Jason Ekstranda7af7042013-10-12 22:38:11 -05004311 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004312 }
4313 }
4314}
4315
Scott Moreauccbf29d2012-02-22 14:21:41 -07004316static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004317zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004318 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004319{
4320 do_zoom(seat, time, 0, axis, value);
4321}
4322
4323static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004324zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004325 void *data)
4326{
4327 do_zoom(seat, time, key, 0, 0);
4328}
4329
4330static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004331terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004332 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004333{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004334 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004335
Daniel Stone325fc2d2012-05-30 16:31:58 +01004336 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004337}
4338
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004339static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004340rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4341 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004342{
4343 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004344 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004345 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004346 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004347 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004348
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004349 weston_pointer_move(pointer, x, y);
4350
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004351 if (!shsurf)
4352 return;
4353
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004354 cx = 0.5f * shsurf->surface->width;
4355 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004356
Daniel Stone37816df2012-05-16 18:45:18 +01004357 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4358 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004359 r = sqrtf(dx * dx + dy * dy);
4360
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004361 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004362 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004363
4364 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004365 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004366 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004367
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004368 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004369 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004370
4371 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004372 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004373 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004374 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004375 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004376
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004377 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004378 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004379 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004380 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004381 wl_list_init(&shsurf->rotation.transform.link);
4382 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004383 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004384 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004385
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004386 /* We need to adjust the position of the surface
4387 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 cposx = shsurf->view->geometry.x + cx;
4389 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004390 dposx = rotate->center.x - cposx;
4391 dposy = rotate->center.y - cposy;
4392 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 weston_view_set_position(shsurf->view,
4394 shsurf->view->geometry.x + dposx,
4395 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004396 }
4397
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004398 /* Repaint implies weston_surface_update_transform(), which
4399 * lazily applies the damage due to rotation update.
4400 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004401 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004402}
4403
4404static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004405rotate_grab_button(struct weston_pointer_grab *grab,
4406 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004407{
4408 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004409 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004410 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004411 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004412 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004413
Daniel Stone4dbadb12012-05-30 16:31:51 +01004414 if (pointer->button_count == 0 &&
4415 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004416 if (shsurf)
4417 weston_matrix_multiply(&shsurf->rotation.rotation,
4418 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004419 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004420 free(rotate);
4421 }
4422}
4423
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004424static void
4425rotate_grab_cancel(struct weston_pointer_grab *grab)
4426{
4427 struct rotate_grab *rotate =
4428 container_of(grab, struct rotate_grab, base.grab);
4429
4430 shell_grab_end(&rotate->base);
4431 free(rotate);
4432}
4433
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004434static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004435 noop_grab_focus,
4436 rotate_grab_motion,
4437 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004438 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004439};
4440
4441static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004442surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004443{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004444 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004445 float dx, dy;
4446 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004447
Pekka Paalanen460099f2012-01-20 16:48:25 +02004448 rotate = malloc(sizeof *rotate);
4449 if (!rotate)
4450 return;
4451
Jason Ekstranda7af7042013-10-12 22:38:11 -05004452 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004453 surface->surface->width * 0.5f,
4454 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004455 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004456
Daniel Stone37816df2012-05-16 18:45:18 +01004457 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4458 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004459 r = sqrtf(dx * dx + dy * dy);
4460 if (r > 20.0f) {
4461 struct weston_matrix inverse;
4462
4463 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004464 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004465 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004466
4467 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004468 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004469 } else {
4470 weston_matrix_init(&surface->rotation.rotation);
4471 weston_matrix_init(&rotate->rotation);
4472 }
4473
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004474 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4475 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004476}
4477
4478static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004479rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004480 void *data)
4481{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004482 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004483 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004484 struct shell_surface *surface;
4485
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004486 if (seat->pointer->focus == NULL)
4487 return;
4488
4489 focus = seat->pointer->focus->surface;
4490
Pekka Paalanen01388e22013-04-25 13:57:44 +03004491 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004492 if (base_surface == NULL)
4493 return;
4494
4495 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004496 if (surface == NULL || surface->state.fullscreen ||
4497 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004498 return;
4499
4500 surface_rotate(surface, seat);
4501}
4502
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004503/* Move all fullscreen layers down to the current workspace and hide their
4504 * black views. The surfaces' state is set to both fullscreen and lowered,
4505 * and this is reversed when such a surface is re-configured, see
4506 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4507 *
4508 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004509 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004510void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004511lower_fullscreen_layer(struct desktop_shell *shell)
4512{
4513 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004514 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004515
4516 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004517 wl_list_for_each_reverse_safe(view, prev,
4518 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004519 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004520 struct shell_surface *shsurf = get_shell_surface(view->surface);
4521
4522 if (!shsurf)
4523 continue;
4524
4525 /* We can have a non-fullscreen popup for a fullscreen surface
4526 * in the fullscreen layer. */
4527 if (shsurf->state.fullscreen) {
4528 /* Hide the black view */
4529 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4530 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004531 weston_view_damage_below(shsurf->fullscreen.black_view);
4532
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004533 }
4534
4535 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004536 wl_list_remove(&view->layer_link);
4537 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4538 weston_view_damage_below(view);
4539 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004540
4541 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004542 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004543}
4544
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004545void
Tiago Vignattibe143262012-04-16 17:31:41 +03004546activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004547 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004548{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004549 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004550 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004551 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004552 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004553 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004554
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004555 lower_fullscreen_layer(shell);
4556
Pekka Paalanen01388e22013-04-25 13:57:44 +03004557 main_surface = weston_surface_get_main_surface(es);
4558
Daniel Stone37816df2012-05-16 18:45:18 +01004559 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004560
Jonas Ådahl8538b222012-08-29 22:13:03 +02004561 state = ensure_focus_state(shell, seat);
4562 if (state == NULL)
4563 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004564
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004565 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004566 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004567
Rafael Antognolli03b16592013-12-03 15:35:42 -02004568 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004569 assert(shsurf);
4570
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004571 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004572 shell_configure_fullscreen(shsurf);
4573 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004574 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004575
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004576 /* Update the surface’s layer. This brings it to the top of the stacking
4577 * order as appropriate. */
4578 shell_surface_update_layer(shsurf);
4579
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004580 if (shell->focus_animation_type != ANIMATION_NONE) {
4581 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004582 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004583 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004584}
4585
Alex Wu21858432012-04-01 20:13:08 +08004586/* no-op func for checking black surface */
4587static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004588black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004589{
4590}
4591
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004592static bool
Alex Wu21858432012-04-01 20:13:08 +08004593is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4594{
4595 if (es->configure == black_surface_configure) {
4596 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004597 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004598 return true;
4599 }
4600 return false;
4601}
4602
Kristian Høgsberg75840622011-09-06 13:48:16 -04004603static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004604activate_binding(struct weston_seat *seat,
4605 struct desktop_shell *shell,
4606 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004607{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004608 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004609
Alex Wu9c35e6b2012-03-05 14:13:13 +08004610 if (!focus)
4611 return;
4612
Pekka Paalanen01388e22013-04-25 13:57:44 +03004613 if (is_black_surface(focus, &main_surface))
4614 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004615
Pekka Paalanen01388e22013-04-25 13:57:44 +03004616 main_surface = weston_surface_get_main_surface(focus);
4617 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004618 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004619
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004620 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004621}
4622
4623static void
4624click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4625 void *data)
4626{
4627 if (seat->pointer->grab != &seat->pointer->default_grab)
4628 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004629 if (seat->pointer->focus == NULL)
4630 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004631
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004632 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004633}
4634
4635static void
4636touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4637{
4638 if (seat->touch->grab != &seat->touch->default_grab)
4639 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004640 if (seat->touch->focus == NULL)
4641 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004642
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004643 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004644}
4645
4646static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004647unfocus_all_seats(struct desktop_shell *shell)
4648{
4649 struct weston_seat *seat, *next;
4650
4651 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4652 if (seat->keyboard == NULL)
4653 continue;
4654
4655 weston_keyboard_set_focus(seat->keyboard, NULL);
4656 }
4657}
4658
4659static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004660lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004661{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004662 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004663
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004664 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004665 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004666 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004667 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004668
4669 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004670
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004671 /* Hide all surfaces by removing the fullscreen, panel and
4672 * toplevel layers. This way nothing else can show or receive
4673 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004674
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004675 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004676 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004677 if (shell->showing_input_panels)
4678 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004679 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004680 wl_list_insert(&shell->compositor->cursor_layer.link,
4681 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004682
Pekka Paalanen77346a62011-11-30 16:26:35 +02004683 launch_screensaver(shell);
4684
Neil Robertsb4a91702014-04-09 16:33:32 +01004685 /* Remove the keyboard focus on all seats. This will be
4686 * restored to the workspace's saved state via
4687 * restore_focus_state when the compositor is unlocked */
4688 unfocus_all_seats(shell);
4689
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004690 /* TODO: disable bindings that should not work while locked. */
4691
4692 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004693}
4694
4695static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004696unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004697{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004698 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004699 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004700 return;
4701 }
4702
4703 /* If desktop-shell client has gone away, unlock immediately. */
4704 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004705 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004706 return;
4707 }
4708
4709 if (shell->prepare_event_sent)
4710 return;
4711
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004712 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004713 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004714}
4715
4716static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004717shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004718{
4719 struct desktop_shell *shell = data;
4720
4721 shell->fade.animation = NULL;
4722
4723 switch (shell->fade.type) {
4724 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004725 weston_surface_destroy(shell->fade.view->surface);
4726 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004727 break;
4728 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004729 lock(shell);
4730 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004731 default:
4732 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004733 }
4734}
4735
Jason Ekstranda7af7042013-10-12 22:38:11 -05004736static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004737shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004738{
4739 struct weston_compositor *compositor = shell->compositor;
4740 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004741 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004742
4743 surface = weston_surface_create(compositor);
4744 if (!surface)
4745 return NULL;
4746
Jason Ekstranda7af7042013-10-12 22:38:11 -05004747 view = weston_view_create(surface);
4748 if (!view) {
4749 weston_surface_destroy(surface);
4750 return NULL;
4751 }
4752
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004753 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004754 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004755 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004756 wl_list_insert(&compositor->fade_layer.view_list,
4757 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004758 pixman_region32_init(&surface->input);
4759
Jason Ekstranda7af7042013-10-12 22:38:11 -05004760 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004761}
4762
4763static void
4764shell_fade(struct desktop_shell *shell, enum fade_type type)
4765{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004766 float tint;
4767
4768 switch (type) {
4769 case FADE_IN:
4770 tint = 0.0;
4771 break;
4772 case FADE_OUT:
4773 tint = 1.0;
4774 break;
4775 default:
4776 weston_log("shell: invalid fade type\n");
4777 return;
4778 }
4779
4780 shell->fade.type = type;
4781
Jason Ekstranda7af7042013-10-12 22:38:11 -05004782 if (shell->fade.view == NULL) {
4783 shell->fade.view = shell_fade_create_surface(shell);
4784 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004785 return;
4786
Jason Ekstranda7af7042013-10-12 22:38:11 -05004787 shell->fade.view->alpha = 1.0 - tint;
4788 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004789 }
4790
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004791 if (shell->fade.view->output == NULL) {
4792 /* If the black view gets a NULL output, we lost the
4793 * last output and we'll just cancel the fade. This
4794 * happens when you close the last window under the
4795 * X11 or Wayland backends. */
4796 shell->locked = false;
4797 weston_surface_destroy(shell->fade.view->surface);
4798 shell->fade.view = NULL;
4799 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004800 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004801 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004802 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004803 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004804 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004805 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004806 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004807}
4808
4809static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004810do_shell_fade_startup(void *data)
4811{
4812 struct desktop_shell *shell = data;
4813
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004814 if (shell->startup_animation_type == ANIMATION_FADE)
4815 shell_fade(shell, FADE_IN);
4816 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004817 weston_surface_destroy(shell->fade.view->surface);
4818 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004819 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004820}
4821
4822static void
4823shell_fade_startup(struct desktop_shell *shell)
4824{
4825 struct wl_event_loop *loop;
4826
4827 if (!shell->fade.startup_timer)
4828 return;
4829
4830 wl_event_source_remove(shell->fade.startup_timer);
4831 shell->fade.startup_timer = NULL;
4832
4833 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4834 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4835}
4836
4837static int
4838fade_startup_timeout(void *data)
4839{
4840 struct desktop_shell *shell = data;
4841
4842 shell_fade_startup(shell);
4843 return 0;
4844}
4845
4846static void
4847shell_fade_init(struct desktop_shell *shell)
4848{
4849 /* Make compositor output all black, and wait for the desktop-shell
4850 * client to signal it is ready, then fade in. The timer triggers a
4851 * fade-in, in case the desktop-shell client takes too long.
4852 */
4853
4854 struct wl_event_loop *loop;
4855
Jason Ekstranda7af7042013-10-12 22:38:11 -05004856 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004857 weston_log("%s: warning: fade surface already exists\n",
4858 __func__);
4859 return;
4860 }
4861
Jason Ekstranda7af7042013-10-12 22:38:11 -05004862 shell->fade.view = shell_fade_create_surface(shell);
4863 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004864 return;
4865
Jason Ekstranda7af7042013-10-12 22:38:11 -05004866 weston_view_update_transform(shell->fade.view);
4867 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004868
4869 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4870 shell->fade.startup_timer =
4871 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4872 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4873}
4874
4875static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004876idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004877{
4878 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004879 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004880 struct weston_seat *seat;
4881
4882 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4883 if (seat->pointer)
4884 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004885
4886 shell_fade(shell, FADE_OUT);
4887 /* lock() is called from shell_fade_done() */
4888}
4889
4890static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004891wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004892{
4893 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004894 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004895
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004896 unlock(shell);
4897}
4898
4899static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004900center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004901{
Giulio Camuffob8366642013-04-25 13:57:46 +03004902 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004903 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004904
Jason Ekstranda7af7042013-10-12 22:38:11 -05004905 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004906
4907 x = output->x + (output->width - width) / 2 - surf_x / 2;
4908 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004909
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004910 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004911}
4912
4913static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004914weston_view_set_initial_position(struct weston_view *view,
4915 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004916{
4917 struct weston_compositor *compositor = shell->compositor;
4918 int ix = 0, iy = 0;
4919 int range_x, range_y;
4920 int dx, dy, x, y, panel_height;
4921 struct weston_output *output, *target_output = NULL;
4922 struct weston_seat *seat;
4923
4924 /* As a heuristic place the new window on the same output as the
4925 * pointer. Falling back to the output containing 0, 0.
4926 *
4927 * TODO: Do something clever for touch too?
4928 */
4929 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004930 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004931 ix = wl_fixed_to_int(seat->pointer->x);
4932 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004933 break;
4934 }
4935 }
4936
4937 wl_list_for_each(output, &compositor->output_list, link) {
4938 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4939 target_output = output;
4940 break;
4941 }
4942 }
4943
4944 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004945 weston_view_set_position(view, 10 + random() % 400,
4946 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004947 return;
4948 }
4949
4950 /* Valid range within output where the surface will still be onscreen.
4951 * If this is negative it means that the surface is bigger than
4952 * output.
4953 */
4954 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004955 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004956 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004957 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004958
Rob Bradford4cb88c72012-08-13 15:18:44 +01004959 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004960 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004961 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004962 dx = 0;
4963
4964 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004965 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004966 else
4967 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004968
4969 x = target_output->x + dx;
4970 y = target_output->y + dy;
4971
Jason Ekstranda7af7042013-10-12 22:38:11 -05004972 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004973}
4974
4975static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004976map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004977 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004978{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004979 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004980 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004981 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004982 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004983
Pekka Paalanen77346a62011-11-30 16:26:35 +02004984 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004985 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004986 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004987 if (shsurf->state.fullscreen) {
4988 center_on_output(shsurf->view, shsurf->fullscreen_output);
4989 shell_map_fullscreen(shsurf);
4990 } else if (shsurf->state.maximized) {
4991 /* use surface configure to set the geometry */
4992 panel_height = get_output_panel_height(shell, shsurf->output);
4993 surface_subsurfaces_boundingbox(shsurf->surface,
4994 &surf_x, &surf_y, NULL, NULL);
4995 weston_view_set_position(shsurf->view,
4996 shsurf->output->x - surf_x,
4997 shsurf->output->y +
4998 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004999 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005000 weston_view_set_initial_position(shsurf->view, shell);
5001 }
Juan Zhao96879df2012-02-07 08:45:41 +08005002 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005003 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005004 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005005 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005006 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005007 weston_view_set_position(shsurf->view,
5008 shsurf->view->geometry.x + sx,
5009 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005010 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005011 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005012 default:
5013 ;
5014 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005015
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005016 /* Surface stacking order, see also activate(). */
5017 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005018
Jason Ekstranda7af7042013-10-12 22:38:11 -05005019 if (shsurf->type != SHELL_SURFACE_NONE) {
5020 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005021 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 shsurf->surface->output = shsurf->output;
5023 shsurf->view->output = shsurf->output;
5024 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005025 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005026
Jason Ekstranda7af7042013-10-12 22:38:11 -05005027 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005028 /* XXX: xwayland's using the same fields for transient type */
5029 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005030 if (shsurf->transient.flags ==
5031 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5032 break;
5033 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005034 if (shsurf->state.relative &&
5035 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5036 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005037 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005038 break;
5039 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005040 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005041 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005042 case SHELL_SURFACE_POPUP:
5043 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005044 default:
5045 break;
5046 }
5047
Rafael Antognolli03b16592013-12-03 15:35:42 -02005048 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5049 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005050 {
5051 switch (shell->win_animation_type) {
5052 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005053 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005054 break;
5055 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005056 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005057 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005058 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005059 default:
5060 break;
5061 }
5062 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005063}
5064
5065static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005066configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005067 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005068{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005069 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005070 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005071 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005072
Pekka Paalanen77346a62011-11-30 16:26:35 +02005073 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005074
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005075 assert(shsurf);
5076
Rafael Antognolli03b16592013-12-03 15:35:42 -02005077 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005078 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005079 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005080 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005081 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5082 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005083 mx = shsurf->output->x - surf_x;
5084 my = shsurf->output->y +
5085 get_output_panel_height(shell,shsurf->output) - surf_y;
5086 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005087 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005088 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005089 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005090
Alex Wu4539b082012-03-01 12:57:46 +08005091 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005092 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005093 wl_list_for_each(view, &surface->views, surface_link)
5094 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005095
Rafael Antognolli03b16592013-12-03 15:35:42 -02005096 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005097 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005098 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005099}
5100
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005101static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005102shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005103{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005104 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005105 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005106 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005107
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005108 assert(shsurf);
5109
5110 shell = shsurf->shell;
5111
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005112 if (!weston_surface_is_mapped(es) &&
5113 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005114 remove_popup_grab(shsurf);
5115 }
5116
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005117 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005118 return;
5119
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005120 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005121 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005122 type_changed = 1;
5123 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005124
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005125 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005126 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005127 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005128 shsurf->last_width != es->width ||
5129 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005130 float from_x, from_y;
5131 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005132
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005133 if (shsurf->resize_edges) {
5134 sx = 0;
5135 sy = 0;
5136 }
5137
5138 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5139 sx = shsurf->last_width - es->width;
5140 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5141 sy = shsurf->last_height - es->height;
5142
5143 shsurf->last_width = es->width;
5144 shsurf->last_height = es->height;
5145
Jason Ekstranda7af7042013-10-12 22:38:11 -05005146 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5147 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005148 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005149 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005150 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005151 }
5152}
5153
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005154static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005155
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005156static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005157desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005158{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005159 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005160 struct desktop_shell *shell =
5161 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005162
5163 shell->child.process.pid = 0;
5164 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005165
5166 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5167 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005168 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005169 shell->child.deathstamp = time;
5170 shell->child.deathcount = 0;
5171 }
5172
5173 shell->child.deathcount++;
5174 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005175 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005176 return;
5177 }
5178
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005179 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005180 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005181 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005182}
5183
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005184static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005185desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5186{
5187 struct desktop_shell *shell;
5188
5189 shell = container_of(listener, struct desktop_shell,
5190 child.client_destroy_listener);
5191
5192 shell->child.client = NULL;
5193}
5194
5195static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005196launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005197{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005198 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005199
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005200 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005201 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005202 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005203 desktop_shell_sigchld);
5204
5205 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005206 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005207
5208 shell->child.client_destroy_listener.notify =
5209 desktop_shell_client_destroy;
5210 wl_client_add_destroy_listener(shell->child.client,
5211 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005212}
5213
5214static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005215handle_shell_client_destroy(struct wl_listener *listener, void *data)
5216{
5217 struct shell_client *sc =
5218 container_of(listener, struct shell_client, destroy_listener);
5219
5220 if (sc->ping_timer)
5221 wl_event_source_remove(sc->ping_timer);
5222 free(sc);
5223}
5224
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005225static struct shell_client *
5226shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5227 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005228{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005229 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005230
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005231 sc = zalloc(sizeof *sc);
5232 if (sc == NULL) {
5233 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005234 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005235 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005236
5237 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005238 if (sc->resource == NULL) {
5239 free(sc);
5240 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005241 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005242 }
5243
5244 sc->client = client;
5245 sc->shell = shell;
5246 sc->destroy_listener.notify = handle_shell_client_destroy;
5247 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5248
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005249 return sc;
5250}
5251
5252static void
5253bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5254{
5255 struct desktop_shell *shell = data;
5256 struct shell_client *sc;
5257
5258 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5259 if (sc)
5260 wl_resource_set_implementation(sc->resource,
5261 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005262 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005263}
5264
5265static void
5266bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5267{
5268 struct desktop_shell *shell = data;
5269 struct shell_client *sc;
5270
5271 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5272 if (sc)
5273 wl_resource_set_dispatcher(sc->resource,
5274 xdg_shell_unversioned_dispatch,
5275 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005276}
5277
5278static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005279unbind_desktop_shell(struct wl_resource *resource)
5280{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005281 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005282
5283 if (shell->locked)
5284 resume_desktop(shell);
5285
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005286 shell->child.desktop_shell = NULL;
5287 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005288}
5289
5290static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005291bind_desktop_shell(struct wl_client *client,
5292 void *data, uint32_t version, uint32_t id)
5293{
Tiago Vignattibe143262012-04-16 17:31:41 +03005294 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005295 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005296
Jason Ekstranda85118c2013-06-27 20:17:02 -05005297 resource = wl_resource_create(client, &desktop_shell_interface,
5298 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005299
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005300 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005301 wl_resource_set_implementation(resource,
5302 &desktop_shell_implementation,
5303 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005304 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005305
5306 if (version < 2)
5307 shell_fade_startup(shell);
5308
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005309 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005310 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005311
5312 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5313 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005314 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005315}
5316
Pekka Paalanen6e168112011-11-24 11:34:05 +02005317static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005318screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005319{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005320 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005321 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005322
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005323 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005324 return;
5325
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005326 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005327 if (!shell->locked)
5328 return;
5329
Jason Ekstranda7af7042013-10-12 22:38:11 -05005330 view = container_of(surface->views.next, struct weston_view, surface_link);
5331 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005332
Jason Ekstranda7af7042013-10-12 22:38:11 -05005333 if (wl_list_empty(&view->layer_link)) {
5334 wl_list_insert(shell->lock_layer.view_list.prev,
5335 &view->layer_link);
5336 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005337 wl_event_source_timer_update(shell->screensaver.timer,
5338 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005339 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005340 }
5341}
5342
5343static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005344screensaver_set_surface(struct wl_client *client,
5345 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005346 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005347 struct wl_resource *output_resource)
5348{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005349 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005350 struct weston_surface *surface =
5351 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005352 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005353 struct weston_view *view, *next;
5354
5355 /* Make sure we only have one view */
5356 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5357 weston_view_destroy(view);
5358 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005359
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005360 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005361 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005362 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005363}
5364
5365static const struct screensaver_interface screensaver_implementation = {
5366 screensaver_set_surface
5367};
5368
5369static void
5370unbind_screensaver(struct wl_resource *resource)
5371{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005372 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005373
Pekka Paalanen77346a62011-11-30 16:26:35 +02005374 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005375}
5376
5377static void
5378bind_screensaver(struct wl_client *client,
5379 void *data, uint32_t version, uint32_t id)
5380{
Tiago Vignattibe143262012-04-16 17:31:41 +03005381 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005382 struct wl_resource *resource;
5383
Jason Ekstranda85118c2013-06-27 20:17:02 -05005384 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005385
Pekka Paalanen77346a62011-11-30 16:26:35 +02005386 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005387 wl_resource_set_implementation(resource,
5388 &screensaver_implementation,
5389 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005390 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005391 return;
5392 }
5393
5394 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5395 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005396 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005397}
5398
Kristian Høgsberg07045392012-02-19 18:52:44 -05005399struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005400 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005401 struct weston_surface *current;
5402 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005403 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005404 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005405};
5406
5407static void
5408switcher_next(struct switcher *switcher)
5409{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005410 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005411 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005412 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005413 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005414
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005415 /* temporary re-display minimized surfaces */
5416 struct weston_view *tmp;
5417 struct weston_view **minimized;
5418 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5419 wl_list_remove(&view->layer_link);
5420 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5421 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5422 *minimized = view;
5423 }
5424
Jason Ekstranda7af7042013-10-12 22:38:11 -05005425 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005426 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005427 if (shsurf &&
5428 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5429 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005430 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005431 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005432 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005433 next = view->surface;
5434 prev = view->surface;
5435 view->alpha = 0.25;
5436 weston_view_geometry_dirty(view);
5437 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005438 }
Alex Wu1659daa2012-04-01 20:13:09 +08005439
Jason Ekstranda7af7042013-10-12 22:38:11 -05005440 if (is_black_surface(view->surface, NULL)) {
5441 view->alpha = 0.25;
5442 weston_view_geometry_dirty(view);
5443 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005444 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005445 }
5446
5447 if (next == NULL)
5448 next = first;
5449
Alex Wu07b26062012-03-12 16:06:01 +08005450 if (next == NULL)
5451 return;
5452
Kristian Høgsberg07045392012-02-19 18:52:44 -05005453 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005454 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005455
5456 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005457 wl_list_for_each(view, &next->views, surface_link)
5458 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005459
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005460 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005461 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005462 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005463}
5464
5465static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005466switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005467{
5468 struct switcher *switcher =
5469 container_of(listener, struct switcher, listener);
5470
5471 switcher_next(switcher);
5472}
5473
5474static void
Daniel Stone351eb612012-05-31 15:27:47 -04005475switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005476{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005477 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005478 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005479 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005480
Jason Ekstranda7af7042013-10-12 22:38:11 -05005481 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005482 if (is_focus_view(view))
5483 continue;
5484
Jason Ekstranda7af7042013-10-12 22:38:11 -05005485 view->alpha = 1.0;
5486 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005487 }
5488
Alex Wu07b26062012-03-12 16:06:01 +08005489 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005490 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005491 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005492 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005493 weston_keyboard_end_grab(keyboard);
5494 if (keyboard->input_method_resource)
5495 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005496
5497 /* re-hide surfaces that were temporary shown during the switch */
5498 struct weston_view **minimized;
5499 wl_array_for_each(minimized, &switcher->minimized_array) {
5500 /* with the exception of the current selected */
5501 if ((*minimized)->surface != switcher->current) {
5502 wl_list_remove(&(*minimized)->layer_link);
5503 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5504 weston_view_damage_below(*minimized);
5505 }
5506 }
5507 wl_array_release(&switcher->minimized_array);
5508
Kristian Høgsberg07045392012-02-19 18:52:44 -05005509 free(switcher);
5510}
5511
5512static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005513switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005514 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005515{
5516 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005517 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005518
Daniel Stonec9785ea2012-05-30 16:31:52 +01005519 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005520 switcher_next(switcher);
5521}
5522
5523static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005524switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005525 uint32_t mods_depressed, uint32_t mods_latched,
5526 uint32_t mods_locked, uint32_t group)
5527{
5528 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005529 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005530
Daniel Stone351eb612012-05-31 15:27:47 -04005531 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5532 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005533}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005534
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005535static void
5536switcher_cancel(struct weston_keyboard_grab *grab)
5537{
5538 struct switcher *switcher = container_of(grab, struct switcher, grab);
5539
5540 switcher_destroy(switcher);
5541}
5542
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005543static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005544 switcher_key,
5545 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005546 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005547};
5548
5549static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005550switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005551 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005552{
Tiago Vignattibe143262012-04-16 17:31:41 +03005553 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005554 struct switcher *switcher;
5555
5556 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005557 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005558 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005559 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005560 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005561 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005562
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005563 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005564 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005565 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005566 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5567 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005568 switcher_next(switcher);
5569}
5570
Pekka Paalanen3c647232011-12-22 13:43:43 +02005571static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005572backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005573 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005574{
5575 struct weston_compositor *compositor = data;
5576 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005577 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005578
5579 /* TODO: we're limiting to simple use cases, where we assume just
5580 * control on the primary display. We'd have to extend later if we
5581 * ever get support for setting backlights on random desktop LCD
5582 * panels though */
5583 output = get_default_output(compositor);
5584 if (!output)
5585 return;
5586
5587 if (!output->set_backlight)
5588 return;
5589
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005590 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5591 backlight_new = output->backlight_current - 25;
5592 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5593 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005594
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005595 if (backlight_new < 5)
5596 backlight_new = 5;
5597 if (backlight_new > 255)
5598 backlight_new = 255;
5599
5600 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005601 output->set_backlight(output, output->backlight_current);
5602}
5603
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005604struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005605 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005606 struct weston_seat *seat;
5607 uint32_t key[2];
5608 int key_released[2];
5609};
5610
5611static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005612debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005613 uint32_t key, uint32_t state)
5614{
5615 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005616 struct weston_compositor *ec = db->seat->compositor;
5617 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005618 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005619 uint32_t serial;
5620 int send = 0, terminate = 0;
5621 int check_binding = 1;
5622 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005623 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005624
5625 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5626 /* Do not run bindings on key releases */
5627 check_binding = 0;
5628
5629 for (i = 0; i < 2; i++)
5630 if (key == db->key[i])
5631 db->key_released[i] = 1;
5632
5633 if (db->key_released[0] && db->key_released[1]) {
5634 /* All key releases been swalled so end the grab */
5635 terminate = 1;
5636 } else if (key != db->key[0] && key != db->key[1]) {
5637 /* Should not swallow release of other keys */
5638 send = 1;
5639 }
5640 } else if (key == db->key[0] && !db->key_released[0]) {
5641 /* Do not check bindings for the first press of the binding
5642 * key. This allows it to be used as a debug shortcut.
5643 * We still need to swallow this event. */
5644 check_binding = 0;
5645 } else if (db->key[1]) {
5646 /* If we already ran a binding don't process another one since
5647 * we can't keep track of all the binding keys that were
5648 * pressed in order to swallow the release events. */
5649 send = 1;
5650 check_binding = 0;
5651 }
5652
5653 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005654 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5655 key, state)) {
5656 /* We ran a binding so swallow the press and keep the
5657 * grab to swallow the released too. */
5658 send = 0;
5659 terminate = 0;
5660 db->key[1] = key;
5661 } else {
5662 /* Terminate the grab since the key pressed is not a
5663 * debug binding key. */
5664 send = 1;
5665 terminate = 1;
5666 }
5667 }
5668
5669 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005670 serial = wl_display_next_serial(display);
5671 resource_list = &grab->keyboard->focus_resource_list;
5672 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005673 wl_keyboard_send_key(resource, serial, time, key, state);
5674 }
5675 }
5676
5677 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005678 weston_keyboard_end_grab(grab->keyboard);
5679 if (grab->keyboard->input_method_resource)
5680 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005681 free(db);
5682 }
5683}
5684
5685static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005686debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005687 uint32_t mods_depressed, uint32_t mods_latched,
5688 uint32_t mods_locked, uint32_t group)
5689{
5690 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005691 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005692
Neil Roberts96d790e2013-09-19 17:32:00 +01005693 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005694
Neil Roberts96d790e2013-09-19 17:32:00 +01005695 wl_resource_for_each(resource, resource_list) {
5696 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5697 mods_latched, mods_locked, group);
5698 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005699}
5700
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005701static void
5702debug_binding_cancel(struct weston_keyboard_grab *grab)
5703{
5704 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5705
5706 weston_keyboard_end_grab(grab->keyboard);
5707 free(db);
5708}
5709
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005710struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005711 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005712 debug_binding_modifiers,
5713 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005714};
5715
5716static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005717debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005718{
5719 struct debug_binding_grab *grab;
5720
5721 grab = calloc(1, sizeof *grab);
5722 if (!grab)
5723 return;
5724
5725 grab->seat = (struct weston_seat *) seat;
5726 grab->key[0] = key;
5727 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005728 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005729}
5730
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005731static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005732force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005733 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005734{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005735 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005736 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005737 struct desktop_shell *shell = data;
5738 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005739 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005740
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005741 focus_surface = seat->keyboard->focus;
5742 if (!focus_surface)
5743 return;
5744
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005745 wl_signal_emit(&compositor->kill_signal, focus_surface);
5746
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005747 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005748 wl_client_get_credentials(client, &pid, NULL, NULL);
5749
5750 /* Skip clients that we launched ourselves (the credentials of
5751 * the socketpair is ours) */
5752 if (pid == getpid())
5753 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005754
Daniel Stone325fc2d2012-05-30 16:31:58 +01005755 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005756}
5757
5758static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005759workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005760 uint32_t key, void *data)
5761{
5762 struct desktop_shell *shell = data;
5763 unsigned int new_index = shell->workspaces.current;
5764
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005765 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005766 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005767 if (new_index != 0)
5768 new_index--;
5769
5770 change_workspace(shell, new_index);
5771}
5772
5773static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005774workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005775 uint32_t key, void *data)
5776{
5777 struct desktop_shell *shell = data;
5778 unsigned int new_index = shell->workspaces.current;
5779
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005780 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005781 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005782 if (new_index < shell->workspaces.num - 1)
5783 new_index++;
5784
5785 change_workspace(shell, new_index);
5786}
5787
5788static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005789workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005790 uint32_t key, void *data)
5791{
5792 struct desktop_shell *shell = data;
5793 unsigned int new_index;
5794
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005795 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005796 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005797 new_index = key - KEY_F1;
5798 if (new_index >= shell->workspaces.num)
5799 new_index = shell->workspaces.num - 1;
5800
5801 change_workspace(shell, new_index);
5802}
5803
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005804static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005805workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005806 uint32_t key, void *data)
5807{
5808 struct desktop_shell *shell = data;
5809 unsigned int new_index = shell->workspaces.current;
5810
5811 if (shell->locked)
5812 return;
5813
5814 if (new_index != 0)
5815 new_index--;
5816
5817 take_surface_to_workspace_by_seat(shell, seat, new_index);
5818}
5819
5820static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005821workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005822 uint32_t key, void *data)
5823{
5824 struct desktop_shell *shell = data;
5825 unsigned int new_index = shell->workspaces.current;
5826
5827 if (shell->locked)
5828 return;
5829
5830 if (new_index < shell->workspaces.num - 1)
5831 new_index++;
5832
5833 take_surface_to_workspace_by_seat(shell, seat, new_index);
5834}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005835
5836static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005837shell_reposition_view_on_output_destroy(struct weston_view *view)
5838{
5839 struct weston_output *output, *first_output;
5840 struct weston_compositor *ec = view->surface->compositor;
5841 struct shell_surface *shsurf;
5842 float x, y;
5843 int visible;
5844
5845 x = view->geometry.x;
5846 y = view->geometry.y;
5847
5848 /* At this point the destroyed output is not in the list anymore.
5849 * If the view is still visible somewhere, we leave where it is,
5850 * otherwise, move it to the first output. */
5851 visible = 0;
5852 wl_list_for_each(output, &ec->output_list, link) {
5853 if (pixman_region32_contains_point(&output->region,
5854 x, y, NULL)) {
5855 visible = 1;
5856 break;
5857 }
5858 }
5859
5860 if (!visible) {
5861 first_output = container_of(ec->output_list.next,
5862 struct weston_output, link);
5863
5864 x = first_output->x + first_output->width / 4;
5865 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005866
5867 weston_view_set_position(view, x, y);
5868 } else {
5869 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005870 }
5871
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005872
5873 shsurf = get_shell_surface(view->surface);
5874
5875 if (shsurf) {
5876 shsurf->saved_position_valid = false;
5877 shsurf->next_state.maximized = false;
5878 shsurf->next_state.fullscreen = false;
5879 shsurf->state_changed = true;
5880 }
5881}
5882
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005883void
5884shell_for_each_layer(struct desktop_shell *shell,
5885 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005886{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005887 struct workspace **ws;
5888
5889 func(shell, &shell->fullscreen_layer, data);
5890 func(shell, &shell->panel_layer, data);
5891 func(shell, &shell->background_layer, data);
5892 func(shell, &shell->lock_layer, data);
5893 func(shell, &shell->input_panel_layer, data);
5894
5895 wl_array_for_each(ws, &shell->workspaces.array)
5896 func(shell, &(*ws)->layer, data);
5897}
5898
5899static void
5900shell_output_destroy_move_layer(struct desktop_shell *shell,
5901 struct weston_layer *layer,
5902 void *data)
5903{
5904 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005905 struct weston_view *view;
5906
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005907 wl_list_for_each(view, &layer->view_list, layer_link) {
5908 if (view->output != output)
5909 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005910
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005911 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005912 }
5913}
5914
5915static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005916handle_output_destroy(struct wl_listener *listener, void *data)
5917{
5918 struct shell_output *output_listener =
5919 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005920 struct weston_output *output = output_listener->output;
5921 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005922
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005923 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005924
Xiong Zhang6b481422013-10-23 13:58:32 +08005925 wl_list_remove(&output_listener->destroy_listener.link);
5926 wl_list_remove(&output_listener->link);
5927 free(output_listener);
5928}
5929
5930static void
5931create_shell_output(struct desktop_shell *shell,
5932 struct weston_output *output)
5933{
5934 struct shell_output *shell_output;
5935
5936 shell_output = zalloc(sizeof *shell_output);
5937 if (shell_output == NULL)
5938 return;
5939
5940 shell_output->output = output;
5941 shell_output->shell = shell;
5942 shell_output->destroy_listener.notify = handle_output_destroy;
5943 wl_signal_add(&output->destroy_signal,
5944 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005945 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005946}
5947
5948static void
5949handle_output_create(struct wl_listener *listener, void *data)
5950{
5951 struct desktop_shell *shell =
5952 container_of(listener, struct desktop_shell, output_create_listener);
5953 struct weston_output *output = (struct weston_output *)data;
5954
5955 create_shell_output(shell, output);
5956}
5957
5958static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005959handle_output_move_layer(struct desktop_shell *shell,
5960 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005961{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005962 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005963 struct weston_view *view;
5964 float x, y;
5965
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005966 wl_list_for_each(view, &layer->view_list, layer_link) {
5967 if (view->output != output)
5968 continue;
5969
5970 x = view->geometry.x + output->move_x;
5971 y = view->geometry.y + output->move_y;
5972 weston_view_set_position(view, x, y);
5973 }
5974}
5975
5976static void
5977handle_output_move(struct wl_listener *listener, void *data)
5978{
5979 struct desktop_shell *shell;
5980
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005981 shell = container_of(listener, struct desktop_shell,
5982 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005983
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005984 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005985}
5986
5987static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005988setup_output_destroy_handler(struct weston_compositor *ec,
5989 struct desktop_shell *shell)
5990{
5991 struct weston_output *output;
5992
5993 wl_list_init(&shell->output_list);
5994 wl_list_for_each(output, &ec->output_list, link)
5995 create_shell_output(shell, output);
5996
5997 shell->output_create_listener.notify = handle_output_create;
5998 wl_signal_add(&ec->output_created_signal,
5999 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006000
6001 shell->output_move_listener.notify = handle_output_move;
6002 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006003}
6004
6005static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006006shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006007{
Tiago Vignattibe143262012-04-16 17:31:41 +03006008 struct desktop_shell *shell =
6009 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006010 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006011 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006012
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006013 /* Force state to unlocked so we don't try to fade */
6014 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006015 if (shell->child.client)
6016 wl_client_destroy(shell->child.client);
6017
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006018 wl_list_remove(&shell->idle_listener.link);
6019 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006020
6021 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006022
Xiong Zhang6b481422013-10-23 13:58:32 +08006023 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6024 wl_list_remove(&shell_output->destroy_listener.link);
6025 wl_list_remove(&shell_output->link);
6026 free(shell_output);
6027 }
6028
6029 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006030 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006031
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006032 wl_array_for_each(ws, &shell->workspaces.array)
6033 workspace_destroy(*ws);
6034 wl_array_release(&shell->workspaces.array);
6035
Pekka Paalanen3c647232011-12-22 13:43:43 +02006036 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006037 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006038 free(shell);
6039}
6040
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006041static void
6042shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6043{
6044 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006045 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006046
6047 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006048 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6049 MODIFIER_CTRL | MODIFIER_ALT,
6050 terminate_binding, ec);
6051 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6052 click_to_activate_binding,
6053 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006054 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6055 click_to_activate_binding,
6056 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006057 weston_compositor_add_touch_binding(ec, 0,
6058 touch_to_activate_binding,
6059 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006060 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6061 MODIFIER_SUPER | MODIFIER_ALT,
6062 surface_opacity_binding, NULL);
6063 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6064 MODIFIER_SUPER, zoom_axis_binding,
6065 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006066
6067 /* configurable bindings */
6068 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006069 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6070 zoom_key_binding, NULL);
6071 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6072 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006073 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6074 maximize_binding, NULL);
6075 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6076 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006077 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6078 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006079 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006080 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6081 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006082 weston_compositor_add_button_binding(ec, BTN_LEFT,
6083 mod | MODIFIER_SHIFT,
6084 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006085
6086 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6087 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6088 rotate_binding, NULL);
6089
Daniel Stone325fc2d2012-05-30 16:31:58 +01006090 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6091 shell);
6092 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6093 ec);
6094 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6095 backlight_binding, ec);
6096 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6097 ec);
6098 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6099 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006100 weston_compositor_add_key_binding(ec, KEY_K, mod,
6101 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006102 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6103 workspace_up_binding, shell);
6104 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6105 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006106 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6107 workspace_move_surface_up_binding,
6108 shell);
6109 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6110 workspace_move_surface_down_binding,
6111 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006112
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006113 if (shell->exposay_modifier)
6114 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6115 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006116
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006117 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6118 if (shell->workspaces.num > 1) {
6119 num_workspace_bindings = shell->workspaces.num;
6120 if (num_workspace_bindings > 6)
6121 num_workspace_bindings = 6;
6122 for (i = 0; i < num_workspace_bindings; i++)
6123 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6124 workspace_f_binding,
6125 shell);
6126 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006127
6128 /* Debug bindings */
6129 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6130 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006131}
6132
Jason Ekstrand024177c2014-04-21 19:42:58 -05006133static void
6134handle_seat_created(struct wl_listener *listener, void *data)
6135{
6136 struct weston_seat *seat = data;
6137
6138 create_shell_seat(seat);
6139}
6140
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006141WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006142module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006143 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006144{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006145 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006146 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006147 struct workspace **pws;
6148 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006149 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006150
Peter Huttererf3d62272013-08-08 11:57:05 +10006151 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006152 if (shell == NULL)
6153 return -1;
6154
Kristian Høgsberg75840622011-09-06 13:48:16 -04006155 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006156
6157 shell->destroy_listener.notify = shell_destroy;
6158 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006159 shell->idle_listener.notify = idle_handler;
6160 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6161 shell->wake_listener.notify = wake_handler;
6162 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006163
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006164 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006165 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006166 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006167 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006168 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006169 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006170 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006171 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006172 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006173 ec->shell_interface.set_title = set_title;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07006174 ec->shell_interface.set_margin = set_margin;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006175
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006176 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6177 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006178 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6179 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006180 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006181
6182 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006183 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006184
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006185 if (input_panel_setup(shell) < 0)
6186 return -1;
6187
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006188 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006189
Daniel Stonedf8133b2013-11-19 11:37:14 +01006190 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6191 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6192
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006193 for (i = 0; i < shell->workspaces.num; i++) {
6194 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6195 if (pws == NULL)
6196 return -1;
6197
6198 *pws = workspace_create();
6199 if (*pws == NULL)
6200 return -1;
6201 }
6202 activate_workspace(shell, 0);
6203
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006204 weston_layer_init(&shell->minimized_layer, NULL);
6205
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006206 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006207 wl_list_init(&shell->workspaces.animation.link);
6208 shell->workspaces.animation.frame = animate_workspace_change_frame;
6209
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006210 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006211 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006212 return -1;
6213
Rafael Antognollie2a34552013-12-03 15:35:45 -02006214 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6215 shell, bind_xdg_shell) == NULL)
6216 return -1;
6217
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006218 if (wl_global_create(ec->wl_display,
6219 &desktop_shell_interface, 2,
6220 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006221 return -1;
6222
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006223 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6224 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006225 return -1;
6226
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006227 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6228 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006229 return -1;
6230
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006231 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006232
Xiong Zhang6b481422013-10-23 13:58:32 +08006233 setup_output_destroy_handler(ec, shell);
6234
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006235 loop = wl_display_get_event_loop(ec->wl_display);
6236 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006237
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006238 shell->screensaver.timer =
6239 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6240
Jason Ekstrand024177c2014-04-21 19:42:58 -05006241 wl_list_for_each(seat, &ec->seat_list, link)
6242 handle_seat_created(NULL, seat);
6243 shell->seat_create_listener.notify = handle_seat_created;
6244 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006245
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006246 screenshooter_create(ec);
6247
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006248 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006249
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006250 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006251
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006252 return 0;
6253}