blob: d7cd9c823f85903f7ef364c0f3a4bfa86f7c957e [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)
2012 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05002013 xdg_surface_send_deactivated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002014}
2015
2016static void
2017shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2018{
2019 if (shsurf->focus_count++ == 0)
2020 if (shell_surface_is_xdg_surface(shsurf))
Jasper St. Pierreb223a722014-02-08 18:11:53 -05002021 xdg_surface_send_activated(shsurf->resource);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002022}
2023
2024static void
2025handle_keyboard_focus(struct wl_listener *listener, void *data)
2026{
2027 struct weston_keyboard *keyboard = data;
2028 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2029
2030 if (seat->focused_surface) {
2031 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2032 if (shsurf)
2033 shell_surface_lose_keyboard_focus(shsurf);
2034 }
2035
2036 seat->focused_surface = keyboard->focus;
2037
2038 if (seat->focused_surface) {
2039 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2040 if (shsurf)
2041 shell_surface_gain_keyboard_focus(shsurf);
2042 }
2043}
2044
2045static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002046shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002047{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002048 if (sc->ping_serial != serial)
2049 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002050
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002051 sc->unresponsive = 0;
2052 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002053
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002054 if (sc->ping_timer) {
2055 wl_event_source_remove(sc->ping_timer);
2056 sc->ping_timer = NULL;
2057 }
2058
2059}
2060
2061static void
2062shell_surface_pong(struct wl_client *client,
2063 struct wl_resource *resource, uint32_t serial)
2064{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002065 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2066 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002067
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002068 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002069}
2070
2071static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002072set_title(struct shell_surface *shsurf, const char *title)
2073{
2074 free(shsurf->title);
2075 shsurf->title = strdup(title);
2076}
2077
2078static void
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002079set_margin(struct shell_surface *shsurf,
2080 int32_t left, int32_t right, int32_t top, int32_t bottom)
2081{
2082 shsurf->margin.left = left;
2083 shsurf->margin.right = right;
2084 shsurf->margin.top = top;
2085 shsurf->margin.bottom = bottom;
2086}
2087
2088static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002089shell_surface_set_title(struct wl_client *client,
2090 struct wl_resource *resource, const char *title)
2091{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002092 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002093
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002094 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002095}
2096
2097static void
2098shell_surface_set_class(struct wl_client *client,
2099 struct wl_resource *resource, const char *class)
2100{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002101 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002102
2103 free(shsurf->class);
2104 shsurf->class = strdup(class);
2105}
2106
Alex Wu4539b082012-03-01 12:57:46 +08002107static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002108restore_output_mode(struct weston_output *output)
2109{
Hardening57388e42013-09-18 23:56:36 +02002110 if (output->current_mode != output->original_mode ||
2111 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002112 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002113 output->original_mode,
2114 output->original_scale,
2115 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002116}
2117
2118static void
2119restore_all_output_modes(struct weston_compositor *compositor)
2120{
2121 struct weston_output *output;
2122
2123 wl_list_for_each(output, &compositor->output_list, link)
2124 restore_output_mode(output);
2125}
2126
Philip Withnallbecb77e2013-11-25 18:01:30 +00002127static int
2128get_output_panel_height(struct desktop_shell *shell,
2129 struct weston_output *output)
2130{
2131 struct weston_view *view;
2132 int panel_height = 0;
2133
2134 if (!output)
2135 return 0;
2136
2137 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2138 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002139 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002140 break;
2141 }
2142 }
2143
2144 return panel_height;
2145}
2146
Philip Withnall07926d92013-11-25 18:01:40 +00002147/* The surface will be inserted into the list immediately after the link
2148 * returned by this function (i.e. will be stacked immediately above the
2149 * returned link). */
2150static struct wl_list *
2151shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2152{
2153 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002154 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002155
2156 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002157 case SHELL_SURFACE_XWAYLAND:
2158 return &shsurf->shell->fullscreen_layer.view_list;
2159
2160 case SHELL_SURFACE_NONE:
2161 return NULL;
2162
Kristian Høgsbergead52422014-01-01 13:51:52 -08002163 case SHELL_SURFACE_POPUP:
2164 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002165 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002166 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002167 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002168 /* Move the surface to its parent layer so
2169 * that surfaces which are transient for
2170 * fullscreen surfaces don't get hidden by the
2171 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002172
2173 /* TODO: Handle a parent with multiple views */
2174 parent = get_default_view(shsurf->parent);
2175 if (parent)
2176 return parent->layer_link.prev;
2177 }
Philip Withnall07926d92013-11-25 18:01:40 +00002178
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002179 /* Move the surface to a normal workspace layer so that surfaces
2180 * which were previously fullscreen or transient are no longer
2181 * rendered on top. */
2182 ws = get_current_workspace(shsurf->shell);
2183 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002184 }
2185
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002186 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002187}
2188
Philip Withnall648a4dd2013-11-25 18:01:44 +00002189static void
2190shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2191{
2192 struct shell_surface *child;
2193
2194 /* Move the child layers to the same workspace as shsurf. They will be
2195 * stacked above shsurf. */
2196 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2197 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002198 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002199 weston_view_geometry_dirty(child->view);
2200 wl_list_remove(&child->view->layer_link);
2201 wl_list_insert(shsurf->view->layer_link.prev,
2202 &child->view->layer_link);
2203 weston_view_geometry_dirty(child->view);
2204 weston_surface_damage(child->surface);
2205
2206 /* Recurse. We don’t expect this to recurse very far (if
2207 * at all) because that would imply we have transient
2208 * (or popup) children of transient surfaces, which
2209 * would be unusual. */
2210 shell_surface_update_child_surface_layers(child);
2211 }
2212 }
2213}
2214
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002215/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002216 * geometry to ensure the changes are redrawn.
2217 *
2218 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2219 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002220static void
2221shell_surface_update_layer(struct shell_surface *shsurf)
2222{
2223 struct wl_list *new_layer_link;
2224
2225 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2226
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002227 if (new_layer_link == NULL)
2228 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002229 if (new_layer_link == &shsurf->view->layer_link)
2230 return;
2231
2232 weston_view_geometry_dirty(shsurf->view);
2233 wl_list_remove(&shsurf->view->layer_link);
2234 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2235 weston_view_geometry_dirty(shsurf->view);
2236 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002237
2238 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002239}
2240
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002241static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002242shell_surface_set_parent(struct shell_surface *shsurf,
2243 struct weston_surface *parent)
2244{
2245 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002246
2247 wl_list_remove(&shsurf->children_link);
2248 wl_list_init(&shsurf->children_link);
2249
2250 /* Insert into the parent surface’s child list. */
2251 if (parent != NULL) {
2252 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2253 if (parent_shsurf != NULL)
2254 wl_list_insert(&parent_shsurf->children_list,
2255 &shsurf->children_link);
2256 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002257}
2258
2259static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002260shell_surface_set_output(struct shell_surface *shsurf,
2261 struct weston_output *output)
2262{
2263 struct weston_surface *es = shsurf->surface;
2264
2265 /* get the default output, if the client set it as NULL
2266 check whether the ouput is available */
2267 if (output)
2268 shsurf->output = output;
2269 else if (es->output)
2270 shsurf->output = es->output;
2271 else
2272 shsurf->output = get_default_output(es->compositor);
2273}
2274
2275static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002276surface_clear_next_states(struct shell_surface *shsurf)
2277{
2278 shsurf->next_state.maximized = false;
2279 shsurf->next_state.fullscreen = false;
2280
2281 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2282 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2283 shsurf->state_changed = true;
2284}
2285
2286static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287set_toplevel(struct shell_surface *shsurf)
2288{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002289 shell_surface_set_parent(shsurf, NULL);
2290 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002291 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002292
2293 /* The layer_link is updated in set_surface_type(),
2294 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002295}
2296
2297static void
2298shell_surface_set_toplevel(struct wl_client *client,
2299 struct wl_resource *resource)
2300{
2301 struct shell_surface *surface = wl_resource_get_user_data(resource);
2302
2303 set_toplevel(surface);
2304}
2305
2306static void
2307set_transient(struct shell_surface *shsurf,
2308 struct weston_surface *parent, int x, int y, uint32_t flags)
2309{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002310 assert(parent != NULL);
2311
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002312 shell_surface_set_parent(shsurf, parent);
2313
2314 surface_clear_next_states(shsurf);
2315
Philip Withnallbecb77e2013-11-25 18:01:30 +00002316 shsurf->transient.x = x;
2317 shsurf->transient.y = y;
2318 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002319
Rafael Antognollied207b42013-12-03 15:35:43 -02002320 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002321 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002322 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002323
2324 /* The layer_link is updated in set_surface_type(),
2325 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002326}
2327
2328static void
2329shell_surface_set_transient(struct wl_client *client,
2330 struct wl_resource *resource,
2331 struct wl_resource *parent_resource,
2332 int x, int y, uint32_t flags)
2333{
2334 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2335 struct weston_surface *parent =
2336 wl_resource_get_user_data(parent_resource);
2337
2338 set_transient(shsurf, parent, x, y, flags);
2339}
2340
2341static void
2342set_fullscreen(struct shell_surface *shsurf,
2343 uint32_t method,
2344 uint32_t framerate,
2345 struct weston_output *output)
2346{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002347 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002348
2349 shsurf->fullscreen_output = shsurf->output;
2350 shsurf->fullscreen.type = method;
2351 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002352
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002353 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002354
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002355 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002356 shsurf->output->width,
2357 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002358
2359 /* The layer_link is updated in set_surface_type(),
2360 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361}
2362
2363static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002364weston_view_set_initial_position(struct weston_view *view,
2365 struct desktop_shell *shell);
2366
2367static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002368unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002369{
Philip Withnallf85fe842013-11-25 18:01:37 +00002370 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002371 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2372 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002373 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002374 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002375
Alex Wu4539b082012-03-01 12:57:46 +08002376 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2377 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002378
Alex Wu4539b082012-03-01 12:57:46 +08002379 wl_list_remove(&shsurf->fullscreen.transform.link);
2380 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002381
Jason Ekstranda7af7042013-10-12 22:38:11 -05002382 if (shsurf->fullscreen.black_view)
2383 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2384 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002385
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002386 if (shsurf->saved_position_valid)
2387 weston_view_set_position(shsurf->view,
2388 shsurf->saved_x, shsurf->saved_y);
2389 else
2390 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2391
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002392 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002393 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002394 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002395 shsurf->saved_rotation_valid = false;
2396 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002397
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002398 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002399}
2400
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002401static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402shell_surface_set_fullscreen(struct wl_client *client,
2403 struct wl_resource *resource,
2404 uint32_t method,
2405 uint32_t framerate,
2406 struct wl_resource *output_resource)
2407{
2408 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2409 struct weston_output *output;
2410
2411 if (output_resource)
2412 output = wl_resource_get_user_data(output_resource);
2413 else
2414 output = NULL;
2415
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002416 shell_surface_set_parent(shsurf, NULL);
2417
Rafael Antognolli03b16592013-12-03 15:35:42 -02002418 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002419 set_fullscreen(shsurf, method, framerate, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002420
2421 shsurf->next_state.fullscreen = true;
2422 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002423}
2424
2425static void
2426set_popup(struct shell_surface *shsurf,
2427 struct weston_surface *parent,
2428 struct weston_seat *seat,
2429 uint32_t serial,
2430 int32_t x,
2431 int32_t y)
2432{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002433 assert(parent != NULL);
2434
Philip Withnallbecb77e2013-11-25 18:01:30 +00002435 shsurf->popup.shseat = get_shell_seat(seat);
2436 shsurf->popup.serial = serial;
2437 shsurf->popup.x = x;
2438 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002439
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002440 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002441}
2442
2443static void
2444shell_surface_set_popup(struct wl_client *client,
2445 struct wl_resource *resource,
2446 struct wl_resource *seat_resource,
2447 uint32_t serial,
2448 struct wl_resource *parent_resource,
2449 int32_t x, int32_t y, uint32_t flags)
2450{
2451 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002452 struct weston_surface *parent =
2453 wl_resource_get_user_data(parent_resource);
2454
2455 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002456
Rafael Antognolli03b16592013-12-03 15:35:42 -02002457 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002458 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002459 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002460 wl_resource_get_user_data(seat_resource),
2461 serial, x, y);
2462}
2463
2464static void
2465set_maximized(struct shell_surface *shsurf,
2466 struct weston_output *output)
2467{
2468 struct desktop_shell *shell;
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002469 uint32_t panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002470
Philip Withnall352e7ed2013-11-25 18:01:35 +00002471 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002472
2473 shell = shell_surface_get_shell(shsurf);
2474 panel_height = get_output_panel_height(shell, shsurf->output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002475
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002476 shsurf->client->send_configure(shsurf->surface,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002477 shsurf->output->width,
2478 shsurf->output->height - panel_height);
2479
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002480 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002481}
2482
2483static void
2484unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002485{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002486 /* undo all maximized things here */
2487 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002488
2489 if (shsurf->saved_position_valid)
2490 weston_view_set_position(shsurf->view,
2491 shsurf->saved_x, shsurf->saved_y);
2492 else
2493 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002494
2495 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002496 wl_list_insert(&shsurf->view->geometry.transformation_list,
2497 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002498 shsurf->saved_rotation_valid = false;
2499 }
2500
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002501 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002502}
2503
Philip Withnallbecb77e2013-11-25 18:01:30 +00002504static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002505set_minimized(struct weston_surface *surface, uint32_t is_true)
2506{
2507 struct shell_surface *shsurf;
2508 struct workspace *current_ws;
2509 struct weston_seat *seat;
2510 struct weston_surface *focus;
2511 struct weston_view *view;
2512
2513 view = get_default_view(surface);
2514 if (!view)
2515 return;
2516
2517 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2518
2519 shsurf = get_shell_surface(surface);
2520 current_ws = get_current_workspace(shsurf->shell);
2521
2522 wl_list_remove(&view->layer_link);
2523 /* hide or show, depending on the state */
2524 if (is_true) {
2525 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2526
2527 drop_focus_state(shsurf->shell, current_ws, view->surface);
2528 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2529 if (!seat->keyboard)
2530 continue;
2531 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2532 if (focus == view->surface)
2533 weston_keyboard_set_focus(seat->keyboard, NULL);
2534 }
2535 }
2536 else {
2537 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2538
2539 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2540 if (!seat->keyboard)
2541 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002542 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002543 }
2544 }
2545
2546 shell_surface_update_child_surface_layers(shsurf);
2547
2548 weston_view_damage_below(view);
2549}
2550
2551static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002552shell_surface_set_maximized(struct wl_client *client,
2553 struct wl_resource *resource,
2554 struct wl_resource *output_resource)
2555{
2556 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2557 struct weston_output *output;
2558
2559 if (output_resource)
2560 output = wl_resource_get_user_data(output_resource);
2561 else
2562 output = NULL;
2563
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002564 shell_surface_set_parent(shsurf, NULL);
2565
Rafael Antognolli03b16592013-12-03 15:35:42 -02002566 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002567 set_maximized(shsurf, output);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002568
2569 shsurf->next_state.maximized = true;
2570 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002571}
2572
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002573/* This is only ever called from set_surface_type(), so there’s no need to
2574 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002575static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002576reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002577{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002578 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002579 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002580 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002581 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002582
Pekka Paalanen98262232011-12-01 10:42:22 +02002583 return 0;
2584}
2585
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002586static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002587set_full_output(struct shell_surface *shsurf)
2588{
2589 shsurf->saved_x = shsurf->view->geometry.x;
2590 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002591 shsurf->saved_width = shsurf->surface->width;
2592 shsurf->saved_height = shsurf->surface->height;
2593 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002594 shsurf->saved_position_valid = true;
2595
2596 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2597 wl_list_remove(&shsurf->rotation.transform.link);
2598 wl_list_init(&shsurf->rotation.transform.link);
2599 weston_view_geometry_dirty(shsurf->view);
2600 shsurf->saved_rotation_valid = true;
2601 }
2602}
2603
2604static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002605set_surface_type(struct shell_surface *shsurf)
2606{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002607 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002609
Philip Withnallbecb77e2013-11-25 18:01:30 +00002610 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002611
Rafael Antognolli03b16592013-12-03 15:35:42 -02002612 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002613 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002614
2615 switch (shsurf->type) {
2616 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002617 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002618 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002619 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002620 weston_view_set_position(shsurf->view,
2621 pev->geometry.x + shsurf->transient.x,
2622 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002623 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002624 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002625
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002626 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002627 weston_view_set_position(shsurf->view, shsurf->transient.x,
2628 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002629 break;
2630
Philip Withnall0f640e22013-11-25 18:01:31 +00002631 case SHELL_SURFACE_POPUP:
2632 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002633 default:
2634 break;
2635 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002636
2637 /* Update the surface’s layer. */
2638 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002639}
2640
Tiago Vignattibe143262012-04-16 17:31:41 +03002641static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002642shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002643{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002644 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002645}
2646
Alex Wu21858432012-04-01 20:13:08 +08002647static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002648black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002649
Jason Ekstranda7af7042013-10-12 22:38:11 -05002650static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002651create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002652 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002653 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002654{
2655 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002657
2658 surface = weston_surface_create(ec);
2659 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002660 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002661 return NULL;
2662 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 view = weston_view_create(surface);
2664 if (surface == NULL) {
2665 weston_log("no memory\n");
2666 weston_surface_destroy(surface);
2667 return NULL;
2668 }
Alex Wu4539b082012-03-01 12:57:46 +08002669
Alex Wu21858432012-04-01 20:13:08 +08002670 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002671 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002672 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002673 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002674 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002675 pixman_region32_fini(&surface->input);
2676 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002677
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002678 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002679 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002680
2681 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002682}
2683
Philip Withnalled8f1a92013-11-25 18:01:43 +00002684static void
2685shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2686{
2687 struct weston_output *output = shsurf->fullscreen_output;
2688
Rafael Antognolli03b16592013-12-03 15:35:42 -02002689 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002690
2691 if (!shsurf->fullscreen.black_view)
2692 shsurf->fullscreen.black_view =
2693 create_black_surface(shsurf->surface->compositor,
2694 shsurf->surface,
2695 output->x, output->y,
2696 output->width,
2697 output->height);
2698
2699 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2700 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2701 wl_list_insert(&shsurf->view->layer_link,
2702 &shsurf->fullscreen.black_view->layer_link);
2703 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2704 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002705
2706 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002707}
2708
Alex Wu4539b082012-03-01 12:57:46 +08002709/* Create black surface and append it to the associated fullscreen surface.
2710 * Handle size dismatch and positioning according to the method. */
2711static void
2712shell_configure_fullscreen(struct shell_surface *shsurf)
2713{
2714 struct weston_output *output = shsurf->fullscreen_output;
2715 struct weston_surface *surface = shsurf->surface;
2716 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002717 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002718 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002719
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002720 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2721 restore_output_mode(output);
2722
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002723 /* Reverse the effect of lower_fullscreen_layer() */
2724 wl_list_remove(&shsurf->view->layer_link);
2725 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2726
Philip Withnalled8f1a92013-11-25 18:01:43 +00002727 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002728
Jason Ekstranda7af7042013-10-12 22:38:11 -05002729 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002730 &surf_width, &surf_height);
2731
Alex Wu4539b082012-03-01 12:57:46 +08002732 switch (shsurf->fullscreen.type) {
2733 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002734 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002735 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002736 break;
2737 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002738 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002739 if (output->width == surf_width &&
2740 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741 weston_view_set_position(shsurf->view,
2742 output->x - surf_x,
2743 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002744 break;
2745 }
2746
Alex Wu4539b082012-03-01 12:57:46 +08002747 matrix = &shsurf->fullscreen.transform.matrix;
2748 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002749
Scott Moreau1bad5db2012-08-18 01:04:05 -06002750 output_aspect = (float) output->width /
2751 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 /* XXX: Use surf_width and surf_height here? */
2753 surface_aspect = (float) surface->width /
2754 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002755 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002756 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002757 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002758 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002759 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002760 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002761
Alex Wu4539b082012-03-01 12:57:46 +08002762 weston_matrix_scale(matrix, scale, scale, 1);
2763 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002764 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002765 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002766 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2767 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002768 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002769
Alex Wu4539b082012-03-01 12:57:46 +08002770 break;
2771 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002772 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002773 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002774 surf_width * surface->buffer_viewport.buffer.scale,
2775 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002776 shsurf->fullscreen.framerate};
2777
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002778 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002779 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002780 weston_view_set_position(shsurf->view,
2781 output->x - surf_x,
2782 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002783 shsurf->fullscreen.black_view->surface->width = output->width;
2784 shsurf->fullscreen.black_view->surface->height = output->height;
2785 weston_view_set_position(shsurf->fullscreen.black_view,
2786 output->x - surf_x,
2787 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002788 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002789 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002790 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002792 }
Alex Wubd3354b2012-04-17 17:20:49 +08002793 }
Alex Wu4539b082012-03-01 12:57:46 +08002794 break;
2795 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002796 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002797 break;
2798 default:
2799 break;
2800 }
2801}
2802
Alex Wu4539b082012-03-01 12:57:46 +08002803static void
2804shell_map_fullscreen(struct shell_surface *shsurf)
2805{
Alex Wubd3354b2012-04-17 17:20:49 +08002806 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002807}
2808
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002809static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002810set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2811{
2812 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002813 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002814 shsurf->transient.x = x;
2815 shsurf->transient.y = y;
2816 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002817
2818 shell_surface_set_parent(shsurf, NULL);
2819
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002820 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002821 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002822}
2823
Kristian Høgsberg47792412014-05-04 13:47:06 -07002824static void
2825shell_interface_set_fullscreen(struct shell_surface *shsurf,
2826 uint32_t method,
2827 uint32_t framerate,
2828 struct weston_output *output)
2829{
2830 surface_clear_next_states(shsurf);
2831 set_fullscreen(shsurf, method, framerate, output);
2832
2833 shsurf->next_state.fullscreen = true;
2834 shsurf->state_changed = true;
2835}
2836
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002837static int
2838shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2839{
2840 return surface_move(shsurf, ws, 1);
2841}
2842
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002843static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002844
2845static void
2846destroy_shell_seat(struct wl_listener *listener, void *data)
2847{
2848 struct shell_seat *shseat =
2849 container_of(listener,
2850 struct shell_seat, seat_destroy_listener);
2851 struct shell_surface *shsurf, *prev = NULL;
2852
2853 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002854 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002855 shseat->popup_grab.client = NULL;
2856
2857 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2858 shsurf->popup.shseat = NULL;
2859 if (prev) {
2860 wl_list_init(&prev->popup.grab_link);
2861 }
2862 prev = shsurf;
2863 }
2864 wl_list_init(&prev->popup.grab_link);
2865 }
2866
2867 wl_list_remove(&shseat->seat_destroy_listener.link);
2868 free(shseat);
2869}
2870
Jason Ekstrand024177c2014-04-21 19:42:58 -05002871static void
2872shell_seat_caps_changed(struct wl_listener *listener, void *data)
2873{
2874 struct shell_seat *seat;
2875
2876 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2877
2878 if (seat->seat->keyboard &&
2879 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2880 wl_signal_add(&seat->seat->keyboard->focus_signal,
2881 &seat->keyboard_focus_listener);
2882 } else if (!seat->seat->keyboard) {
2883 wl_list_init(&seat->keyboard_focus_listener.link);
2884 }
2885
2886 if (seat->seat->pointer &&
2887 wl_list_empty(&seat->pointer_focus_listener.link)) {
2888 wl_signal_add(&seat->seat->pointer->focus_signal,
2889 &seat->pointer_focus_listener);
2890 } else if (!seat->seat->pointer) {
2891 wl_list_init(&seat->pointer_focus_listener.link);
2892 }
2893}
2894
Giulio Camuffo5085a752013-03-25 21:42:45 +01002895static struct shell_seat *
2896create_shell_seat(struct weston_seat *seat)
2897{
2898 struct shell_seat *shseat;
2899
2900 shseat = calloc(1, sizeof *shseat);
2901 if (!shseat) {
2902 weston_log("no memory to allocate shell seat\n");
2903 return NULL;
2904 }
2905
2906 shseat->seat = seat;
2907 wl_list_init(&shseat->popup_grab.surfaces_list);
2908
2909 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2910 wl_signal_add(&seat->destroy_signal,
2911 &shseat->seat_destroy_listener);
2912
Jason Ekstrand024177c2014-04-21 19:42:58 -05002913 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2914 wl_list_init(&shseat->keyboard_focus_listener.link);
2915
2916 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2917 wl_list_init(&shseat->pointer_focus_listener.link);
2918
2919 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2920 wl_signal_add(&seat->updated_caps_signal,
2921 &shseat->caps_changed_listener);
2922 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2923
Giulio Camuffo5085a752013-03-25 21:42:45 +01002924 return shseat;
2925}
2926
2927static struct shell_seat *
2928get_shell_seat(struct weston_seat *seat)
2929{
2930 struct wl_listener *listener;
2931
2932 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002933 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934
2935 return container_of(listener,
2936 struct shell_seat, seat_destroy_listener);
2937}
2938
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002939static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002940popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002941{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002942 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002944 struct shell_seat *shseat =
2945 container_of(grab, struct shell_seat, popup_grab.grab);
2946 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002947 wl_fixed_t sx, sy;
2948
Jason Ekstranda7af7042013-10-12 22:38:11 -05002949 view = weston_compositor_pick_view(pointer->seat->compositor,
2950 pointer->x, pointer->y,
2951 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002952
Jason Ekstranda7af7042013-10-12 22:38:11 -05002953 if (view && view->surface->resource &&
2954 wl_resource_get_client(view->surface->resource) == client) {
2955 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002956 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002957 weston_pointer_set_focus(pointer, NULL,
2958 wl_fixed_from_int(0),
2959 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002960 }
2961}
2962
2963static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002964popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2965 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002966{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002967 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002968 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002969 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002970
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002971 weston_pointer_move(pointer, x, y);
2972
Neil Roberts96d790e2013-09-19 17:32:00 +01002973 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002974 weston_view_from_global_fixed(pointer->focus,
2975 pointer->x, pointer->y,
2976 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002977 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002978 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002979}
2980
2981static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002982popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002983 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002984{
2985 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002986 struct shell_seat *shseat =
2987 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002988 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002989 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002990 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002991 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002992
Neil Roberts96d790e2013-09-19 17:32:00 +01002993 resource_list = &grab->pointer->focus_resource_list;
2994 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002995 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002996 wl_resource_for_each(resource, resource_list) {
2997 wl_pointer_send_button(resource, serial,
2998 time, button, state);
2999 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003000 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003001 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003002 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003003 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003004 }
3005
Daniel Stone4dbadb12012-05-30 16:31:51 +01003006 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003007 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003008}
3009
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003010static void
3011popup_grab_cancel(struct weston_pointer_grab *grab)
3012{
3013 popup_grab_end(grab->pointer);
3014}
3015
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003016static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003017 popup_grab_focus,
3018 popup_grab_motion,
3019 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003020 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003021};
3022
3023static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003024shell_surface_send_popup_done(struct shell_surface *shsurf)
3025{
3026 if (shell_surface_is_wl_shell_surface(shsurf))
3027 wl_shell_surface_send_popup_done(shsurf->resource);
3028 else if (shell_surface_is_xdg_popup(shsurf))
3029 xdg_popup_send_popup_done(shsurf->resource,
3030 shsurf->popup.serial);
3031}
3032
3033static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003034popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003035{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003036 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003037 struct shell_seat *shseat =
3038 container_of(grab, struct shell_seat, popup_grab.grab);
3039 struct shell_surface *shsurf;
3040 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003041
3042 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003043 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003044 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003045 shseat->popup_grab.grab.interface = NULL;
3046 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003047 /* Send the popup_done event to all the popups open */
3048 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003049 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003050 shsurf->popup.shseat = NULL;
3051 if (prev) {
3052 wl_list_init(&prev->popup.grab_link);
3053 }
3054 prev = shsurf;
3055 }
3056 wl_list_init(&prev->popup.grab_link);
3057 wl_list_init(&shseat->popup_grab.surfaces_list);
3058 }
3059}
3060
3061static void
3062add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3063{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003064 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003065
3066 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003067 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003068 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003069 /* We must make sure here that this popup was opened after
3070 * a mouse press, and not just by moving around with other
3071 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003072 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003073 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074
Rob Bradforddfe31052013-06-26 19:49:11 +01003075 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003076 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003077 } else {
3078 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003079 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003080}
3081
3082static void
3083remove_popup_grab(struct shell_surface *shsurf)
3084{
3085 struct shell_seat *shseat = shsurf->popup.shseat;
3086
3087 wl_list_remove(&shsurf->popup.grab_link);
3088 wl_list_init(&shsurf->popup.grab_link);
3089 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003090 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003091 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003092 }
3093}
3094
3095static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003096shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003097{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003098 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003099 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003100
Jason Ekstranda7af7042013-10-12 22:38:11 -05003101 shsurf->surface->output = parent_view->output;
3102 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003103
Jason Ekstranda7af7042013-10-12 22:38:11 -05003104 weston_view_set_transform_parent(shsurf->view, parent_view);
3105 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3106 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003107
Kristian Høgsberge3148752013-05-06 23:19:49 -04003108 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003109 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003110 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003111 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003112 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003113 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003114}
3115
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003116static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003117 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003118 shell_surface_move,
3119 shell_surface_resize,
3120 shell_surface_set_toplevel,
3121 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003122 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003123 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003124 shell_surface_set_maximized,
3125 shell_surface_set_title,
3126 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003127};
3128
3129static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003130destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003131{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003132 struct shell_surface *child, *next;
3133
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003134 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3135
Giulio Camuffo5085a752013-03-25 21:42:45 +01003136 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3137 remove_popup_grab(shsurf);
3138 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003139
Alex Wubd3354b2012-04-17 17:20:49 +08003140 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003141 shell_surface_is_top_fullscreen(shsurf))
3142 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003143
Jason Ekstranda7af7042013-10-12 22:38:11 -05003144 if (shsurf->fullscreen.black_view)
3145 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003146
Alex Wubd3354b2012-04-17 17:20:49 +08003147 /* As destroy_resource() use wl_list_for_each_safe(),
3148 * we can always remove the listener.
3149 */
3150 wl_list_remove(&shsurf->surface_destroy_listener.link);
3151 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003152 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003153
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 weston_view_destroy(shsurf->view);
3155
Philip Withnall648a4dd2013-11-25 18:01:44 +00003156 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003157 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3158 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003159
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003160 wl_list_remove(&shsurf->link);
3161 free(shsurf);
3162}
3163
3164static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003165shell_destroy_shell_surface(struct wl_resource *resource)
3166{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003167 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003168
Bryan Caina46b9462014-04-04 17:41:24 -05003169 if (!wl_list_empty(&shsurf->popup.grab_link))
3170 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003171 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003172}
3173
3174static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003175shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003176{
3177 struct shell_surface *shsurf = container_of(listener,
3178 struct shell_surface,
3179 surface_destroy_listener);
3180
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003181 if (shsurf->resource)
3182 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003183
3184 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003185}
3186
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003187static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003188fade_out_done(struct weston_view_animation *animation, void *data)
3189{
3190 struct shell_surface *shsurf = data;
3191
3192 weston_surface_destroy(shsurf->surface);
3193}
3194
3195static void
3196handle_resource_destroy(struct wl_listener *listener, void *data)
3197{
3198 struct shell_surface *shsurf =
3199 container_of(listener, struct shell_surface,
3200 resource_destroy_listener);
3201
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003202 if (!weston_surface_is_mapped(shsurf->surface))
3203 return;
3204
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003205 shsurf->surface->ref_count++;
3206
3207 pixman_region32_fini(&shsurf->surface->pending.input);
3208 pixman_region32_init(&shsurf->surface->pending.input);
3209 pixman_region32_fini(&shsurf->surface->input);
3210 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003211 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3212 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003213}
3214
3215static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003216shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003217
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003218struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003219get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003220{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003221 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003222 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003223 else
3224 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003225}
3226
Rafael Antognollie2a34552013-12-03 15:35:45 -02003227static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003228create_common_surface(struct shell_client *owner, void *shell,
3229 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003230 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003231{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003232 struct shell_surface *shsurf;
3233
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003234 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003235 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003236 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003237 }
3238
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003239 shsurf = calloc(1, sizeof *shsurf);
3240 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003241 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003242 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003243 }
3244
Jason Ekstranda7af7042013-10-12 22:38:11 -05003245 shsurf->view = weston_view_create(surface);
3246 if (!shsurf->view) {
3247 weston_log("no memory to allocate shell surface\n");
3248 free(shsurf);
3249 return NULL;
3250 }
3251
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003252 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003253 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003254
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003255 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3256 wl_resource_add_destroy_listener(surface->resource,
3257 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003258 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003259
Tiago Vignattibc052c92012-04-19 16:18:18 +03003260 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003261 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003262 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003263 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003264 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003265 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003266 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3267 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003268 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003269 wl_list_init(&shsurf->fullscreen.transform.link);
3270
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003271 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003272 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003273 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003274 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003275
3276 /* init link so its safe to always remove it in destroy_shell_surface */
3277 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003278 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003279
Pekka Paalanen460099f2012-01-20 16:48:25 +02003280 /* empty when not in use */
3281 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003282 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003283
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003284 wl_list_init(&shsurf->workspace_transform.link);
3285
Philip Withnall648a4dd2013-11-25 18:01:44 +00003286 wl_list_init(&shsurf->children_link);
3287 wl_list_init(&shsurf->children_list);
3288 shsurf->parent = NULL;
3289
Pekka Paalanen98262232011-12-01 10:42:22 +02003290 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003291
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003292 shsurf->client = client;
3293
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003294 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003295}
3296
Rafael Antognollie2a34552013-12-03 15:35:45 -02003297static struct shell_surface *
3298create_shell_surface(void *shell, struct weston_surface *surface,
3299 const struct weston_shell_client *client)
3300{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003301 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003302}
3303
Jason Ekstranda7af7042013-10-12 22:38:11 -05003304static struct weston_view *
3305get_primary_view(void *shell, struct shell_surface *shsurf)
3306{
3307 return shsurf->view;
3308}
3309
Tiago Vignattibc052c92012-04-19 16:18:18 +03003310static void
3311shell_get_shell_surface(struct wl_client *client,
3312 struct wl_resource *resource,
3313 uint32_t id,
3314 struct wl_resource *surface_resource)
3315{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003316 struct weston_surface *surface =
3317 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003318 struct shell_client *sc = wl_resource_get_user_data(resource);
3319 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003320 struct shell_surface *shsurf;
3321
3322 if (get_shell_surface(surface)) {
3323 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003324 WL_DISPLAY_ERROR_INVALID_OBJECT,
3325 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003326 return;
3327 }
3328
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003329 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003330 if (!shsurf) {
3331 wl_resource_post_error(surface_resource,
3332 WL_DISPLAY_ERROR_INVALID_OBJECT,
3333 "surface->configure already set");
3334 return;
3335 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003336
Jason Ekstranda85118c2013-06-27 20:17:02 -05003337 shsurf->resource =
3338 wl_resource_create(client,
3339 &wl_shell_surface_interface, 1, id);
3340 wl_resource_set_implementation(shsurf->resource,
3341 &shell_surface_implementation,
3342 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003343}
3344
Rafael Antognollie2a34552013-12-03 15:35:45 -02003345static bool
3346shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3347{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003348 /* A shell surface without a resource is created from xwayland
3349 * and is considered a wl_shell surface for now. */
3350
3351 return shsurf->resource == NULL ||
3352 wl_resource_instance_of(shsurf->resource,
3353 &wl_shell_surface_interface,
3354 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003355}
3356
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003357static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003358 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003359};
3360
Rafael Antognollie2a34552013-12-03 15:35:45 -02003361/****************************
3362 * xdg-shell implementation */
3363
3364static void
3365xdg_surface_destroy(struct wl_client *client,
3366 struct wl_resource *resource)
3367{
3368 wl_resource_destroy(resource);
3369}
3370
3371static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003372xdg_surface_set_parent(struct wl_client *client,
3373 struct wl_resource *resource,
3374 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003375{
3376 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3377 struct weston_surface *parent;
3378
3379 if (parent_resource)
3380 parent = wl_resource_get_user_data(parent_resource);
3381 else
3382 parent = NULL;
3383
3384 shell_surface_set_parent(shsurf, parent);
3385}
3386
3387static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003388xdg_surface_set_margin(struct wl_client *client,
3389 struct wl_resource *resource,
3390 int32_t left,
3391 int32_t right,
3392 int32_t top,
3393 int32_t bottom)
3394{
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07003395 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3396
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07003397 set_margin(shsurf, left, right, top, bottom);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003398}
3399
3400static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003401xdg_surface_set_app_id(struct wl_client *client,
3402 struct wl_resource *resource,
3403 const char *app_id)
3404{
3405 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3406
3407 free(shsurf->class);
3408 shsurf->class = strdup(app_id);
3409}
3410
3411static void
3412xdg_surface_set_title(struct wl_client *client,
3413 struct wl_resource *resource, const char *title)
3414{
3415 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3416
3417 set_title(shsurf, title);
3418}
3419
3420static void
3421xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3422 struct wl_resource *seat_resource, uint32_t serial)
3423{
3424 common_surface_move(resource, seat_resource, serial);
3425}
3426
3427static void
3428xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3429 struct wl_resource *seat_resource, uint32_t serial,
3430 uint32_t edges)
3431{
3432 common_surface_resize(resource, seat_resource, serial, edges);
3433}
3434
3435static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003436xdg_surface_ack_configure(struct wl_client *client,
3437 struct wl_resource *resource,
3438 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003439{
3440 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3441
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003442 if (shsurf->state_requested) {
3443 shsurf->next_state = shsurf->requested_state;
3444 shsurf->state_changed = true;
3445 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003446 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003447}
3448
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003449static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003450xdg_surface_set_maximized(struct wl_client *client,
3451 struct wl_resource *resource)
3452{
3453 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3454
3455 shsurf->state_requested = true;
3456 shsurf->requested_state.maximized = true;
3457 set_maximized(shsurf, NULL);
3458}
3459
3460static void
3461xdg_surface_unset_maximized(struct wl_client *client,
3462 struct wl_resource *resource)
3463{
3464 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3465
3466 shsurf->state_requested = true;
3467 shsurf->requested_state.maximized = false;
3468 shsurf->client->send_configure(shsurf->surface, 0, 0);
3469}
3470
3471static void
3472xdg_surface_set_fullscreen(struct wl_client *client,
3473 struct wl_resource *resource,
3474 struct wl_resource *output_resource)
3475{
3476 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3477 struct weston_output *output;
3478
3479 shsurf->state_requested = true;
3480 shsurf->requested_state.fullscreen = true;
3481
3482 if (output_resource)
3483 output = wl_resource_get_user_data(output_resource);
3484 else
3485 output = NULL;
3486
3487 shsurf->recommended_output = output;
3488
3489 set_fullscreen(shsurf,
3490 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3491 0, shsurf->recommended_output);
3492}
3493
3494static void
3495xdg_surface_unset_fullscreen(struct wl_client *client,
3496 struct wl_resource *resource)
3497{
3498 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3499
3500 shsurf->state_requested = true;
3501 shsurf->requested_state.fullscreen = false;
3502 shsurf->client->send_configure(shsurf->surface, 0, 0);
3503}
3504
3505static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003506xdg_surface_set_minimized(struct wl_client *client,
3507 struct wl_resource *resource)
3508{
3509 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3510
3511 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3512 return;
3513
3514 /* apply compositor's own minimization logic (hide) */
3515 set_minimized(shsurf->surface, 1);
3516}
3517
Rafael Antognollie2a34552013-12-03 15:35:45 -02003518static const struct xdg_surface_interface xdg_surface_implementation = {
3519 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003520 xdg_surface_set_parent,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003521 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003522 xdg_surface_set_title,
3523 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003524 xdg_surface_move,
3525 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003526 xdg_surface_ack_configure,
3527 xdg_surface_set_maximized,
3528 xdg_surface_unset_maximized,
3529 xdg_surface_set_fullscreen,
3530 xdg_surface_unset_fullscreen,
3531 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003532};
3533
3534static void
3535xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003536 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003537{
3538 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003539 uint32_t *s;
3540 struct wl_array states;
3541 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003542
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003543 assert(shsurf);
3544
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003545 if (!shsurf->resource)
3546 return;
3547
3548 wl_array_init(&states);
3549 if (shsurf->requested_state.fullscreen) {
3550 s = wl_array_add(&states, sizeof *s);
3551 *s = XDG_SURFACE_STATE_FULLSCREEN;
3552 } else if (shsurf->requested_state.maximized) {
3553 s = wl_array_add(&states, sizeof *s);
3554 *s = XDG_SURFACE_STATE_MAXIMIZED;
3555 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003556 if (shsurf->resize_edges != 0) {
3557 s = wl_array_add(&states, sizeof *s);
3558 *s = XDG_SURFACE_STATE_RESIZING;
3559 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003560
3561 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3562 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3563
3564 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003565}
3566
3567static const struct weston_shell_client xdg_client = {
3568 xdg_send_configure
3569};
3570
3571static void
3572xdg_use_unstable_version(struct wl_client *client,
3573 struct wl_resource *resource,
3574 int32_t version)
3575{
3576 if (version > 1) {
3577 wl_resource_post_error(resource,
3578 1,
3579 "xdg-shell:: version not implemented yet.");
3580 return;
3581 }
3582}
3583
3584static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003585create_xdg_surface(struct shell_client *owner, void *shell,
3586 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003587 const struct weston_shell_client *client)
3588{
3589 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003590
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003591 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003592 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003593
3594 return shsurf;
3595}
3596
3597static void
3598xdg_get_xdg_surface(struct wl_client *client,
3599 struct wl_resource *resource,
3600 uint32_t id,
3601 struct wl_resource *surface_resource)
3602{
3603 struct weston_surface *surface =
3604 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003605 struct shell_client *sc = wl_resource_get_user_data(resource);
3606 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003607 struct shell_surface *shsurf;
3608
3609 if (get_shell_surface(surface)) {
3610 wl_resource_post_error(surface_resource,
3611 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003612 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003613 return;
3614 }
3615
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003616 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003617 if (!shsurf) {
3618 wl_resource_post_error(surface_resource,
3619 WL_DISPLAY_ERROR_INVALID_OBJECT,
3620 "surface->configure already set");
3621 return;
3622 }
3623
3624 shsurf->resource =
3625 wl_resource_create(client,
3626 &xdg_surface_interface, 1, id);
3627 wl_resource_set_implementation(shsurf->resource,
3628 &xdg_surface_implementation,
3629 shsurf, shell_destroy_shell_surface);
3630}
3631
3632static bool
3633shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3634{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003635 return shsurf->resource &&
3636 wl_resource_instance_of(shsurf->resource,
3637 &xdg_surface_interface,
3638 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003639}
3640
3641/* xdg-popup implementation */
3642
3643static void
3644xdg_popup_destroy(struct wl_client *client,
3645 struct wl_resource *resource)
3646{
3647 wl_resource_destroy(resource);
3648}
3649
Rafael Antognollie2a34552013-12-03 15:35:45 -02003650static const struct xdg_popup_interface xdg_popup_implementation = {
3651 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003652};
3653
3654static void
3655xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003656 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003657{
3658}
3659
3660static const struct weston_shell_client xdg_popup_client = {
3661 xdg_popup_send_configure
3662};
3663
3664static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003665create_xdg_popup(struct shell_client *owner, void *shell,
3666 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003667 const struct weston_shell_client *client,
3668 struct weston_surface *parent,
3669 struct shell_seat *seat,
3670 uint32_t serial,
3671 int32_t x, int32_t y)
3672{
3673 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003674
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003675 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003676 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003677 shsurf->popup.shseat = seat;
3678 shsurf->popup.serial = serial;
3679 shsurf->popup.x = x;
3680 shsurf->popup.y = y;
3681 shell_surface_set_parent(shsurf, parent);
3682
3683 return shsurf;
3684}
3685
3686static void
3687xdg_get_xdg_popup(struct wl_client *client,
3688 struct wl_resource *resource,
3689 uint32_t id,
3690 struct wl_resource *surface_resource,
3691 struct wl_resource *parent_resource,
3692 struct wl_resource *seat_resource,
3693 uint32_t serial,
3694 int32_t x, int32_t y, uint32_t flags)
3695{
3696 struct weston_surface *surface =
3697 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003698 struct shell_client *sc = wl_resource_get_user_data(resource);
3699 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003700 struct shell_surface *shsurf;
3701 struct weston_surface *parent;
3702 struct shell_seat *seat;
3703
3704 if (get_shell_surface(surface)) {
3705 wl_resource_post_error(surface_resource,
3706 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003707 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003708 return;
3709 }
3710
3711 if (!parent_resource) {
3712 wl_resource_post_error(surface_resource,
3713 WL_DISPLAY_ERROR_INVALID_OBJECT,
3714 "xdg_shell::get_xdg_popup requires a parent shell surface");
3715 }
3716
3717 parent = wl_resource_get_user_data(parent_resource);
3718 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3719
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003720 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003721 parent, seat, serial, x, y);
3722 if (!shsurf) {
3723 wl_resource_post_error(surface_resource,
3724 WL_DISPLAY_ERROR_INVALID_OBJECT,
3725 "surface->configure already set");
3726 return;
3727 }
3728
3729 shsurf->resource =
3730 wl_resource_create(client,
3731 &xdg_popup_interface, 1, id);
3732 wl_resource_set_implementation(shsurf->resource,
3733 &xdg_popup_implementation,
3734 shsurf, shell_destroy_shell_surface);
3735}
3736
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003737static void
3738xdg_pong(struct wl_client *client,
3739 struct wl_resource *resource, uint32_t serial)
3740{
3741 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003742
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003743 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003744}
3745
Rafael Antognollie2a34552013-12-03 15:35:45 -02003746static bool
3747shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3748{
3749 return wl_resource_instance_of(shsurf->resource,
3750 &xdg_popup_interface,
3751 &xdg_popup_implementation);
3752}
3753
3754static const struct xdg_shell_interface xdg_implementation = {
3755 xdg_use_unstable_version,
3756 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003757 xdg_get_xdg_popup,
3758 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003759};
3760
3761static int
3762xdg_shell_unversioned_dispatch(const void *implementation,
3763 void *_target, uint32_t opcode,
3764 const struct wl_message *message,
3765 union wl_argument *args)
3766{
3767 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003768 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003769
3770 if (opcode != 0) {
3771 wl_resource_post_error(resource,
3772 WL_DISPLAY_ERROR_INVALID_OBJECT,
3773 "must call use_unstable_version first");
3774 return 0;
3775 }
3776
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003777#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003778
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003779 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3780 "shell implementation doesn't match protocol version");
3781
Rafael Antognollie2a34552013-12-03 15:35:45 -02003782 if (args[0].i != XDG_SERVER_VERSION) {
3783 wl_resource_post_error(resource,
3784 WL_DISPLAY_ERROR_INVALID_OBJECT,
3785 "incompatible version, server is %d "
3786 "client wants %d",
3787 XDG_SERVER_VERSION, args[0].i);
3788 return 0;
3789 }
3790
3791 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003792 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003793
3794 return 1;
3795}
3796
3797/* end of xdg-shell implementation */
3798/***********************************/
3799
Kristian Høgsberg07937562011-04-12 17:25:42 -04003800static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003801shell_fade(struct desktop_shell *shell, enum fade_type type);
3802
3803static int
3804screensaver_timeout(void *data)
3805{
3806 struct desktop_shell *shell = data;
3807
3808 shell_fade(shell, FADE_OUT);
3809
3810 return 1;
3811}
3812
3813static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003814handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003815{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003816 struct desktop_shell *shell =
3817 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003818
Pekka Paalanen18027e52011-12-02 16:31:49 +02003819 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003820
3821 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003822 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003823}
3824
3825static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003826launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003827{
3828 if (shell->screensaver.binding)
3829 return;
3830
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003831 if (!shell->screensaver.path) {
3832 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003833 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003834 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003835
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003836 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003837 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003838 return;
3839 }
3840
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003841 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003842 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003843 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003844 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003845}
3846
3847static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003848terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003849{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003850 if (shell->screensaver.process.pid == 0)
3851 return;
3852
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003853 /* Disarm the screensaver timer, otherwise it may fire when the
3854 * compositor is not in the idle state. In that case, the screen will
3855 * be locked, but the wake_signal won't fire on user input, making the
3856 * system unresponsive. */
3857 wl_event_source_timer_update(shell->screensaver.timer, 0);
3858
Pekka Paalanen18027e52011-12-02 16:31:49 +02003859 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003860}
3861
3862static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003863configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003864{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003865 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003866
Jason Ekstranda7af7042013-10-12 22:38:11 -05003867 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3868 if (v->output == ev->output && v != ev) {
3869 weston_view_unmap(v);
3870 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003871 }
3872 }
3873
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003874 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003875
Jason Ekstranda7af7042013-10-12 22:38:11 -05003876 if (wl_list_empty(&ev->layer_link)) {
3877 wl_list_insert(&layer->view_list, &ev->layer_link);
3878 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003879 }
3880}
3881
3882static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003883background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003884{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003885 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003886 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003887
Jason Ekstranda7af7042013-10-12 22:38:11 -05003888 view = container_of(es->views.next, struct weston_view, surface_link);
3889
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003890 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003891}
3892
3893static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003894desktop_shell_set_background(struct wl_client *client,
3895 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003896 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003897 struct wl_resource *surface_resource)
3898{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003899 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003900 struct weston_surface *surface =
3901 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003902 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003903
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003904 if (surface->configure) {
3905 wl_resource_post_error(surface_resource,
3906 WL_DISPLAY_ERROR_INVALID_OBJECT,
3907 "surface role already assigned");
3908 return;
3909 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003910
Jason Ekstranda7af7042013-10-12 22:38:11 -05003911 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3912 weston_view_destroy(view);
3913 view = weston_view_create(surface);
3914
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003915 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003916 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003917 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003918 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003919 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003920 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003921 surface->output->width,
3922 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003923}
3924
3925static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003926panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003927{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003928 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003929 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003930
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931 view = container_of(es->views.next, struct weston_view, surface_link);
3932
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003933 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003934}
3935
3936static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003937desktop_shell_set_panel(struct wl_client *client,
3938 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003939 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003940 struct wl_resource *surface_resource)
3941{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003942 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003943 struct weston_surface *surface =
3944 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003945 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003946
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003947 if (surface->configure) {
3948 wl_resource_post_error(surface_resource,
3949 WL_DISPLAY_ERROR_INVALID_OBJECT,
3950 "surface role already assigned");
3951 return;
3952 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003953
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3955 weston_view_destroy(view);
3956 view = weston_view_create(surface);
3957
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003958 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003959 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003960 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003961 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003962 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003963 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003964 surface->output->width,
3965 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003966}
3967
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003968static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003969lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003970{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003971 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003972 struct weston_view *view;
3973
3974 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003975
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003976 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003977 return;
3978
Jason Ekstranda7af7042013-10-12 22:38:11 -05003979 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003980
3981 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003982 wl_list_insert(&shell->lock_layer.view_list,
3983 &view->layer_link);
3984 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003985 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003986 }
3987}
3988
3989static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003990handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003991{
Tiago Vignattibe143262012-04-16 17:31:41 +03003992 struct desktop_shell *shell =
3993 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003994
Martin Minarik6d118362012-06-07 18:01:59 +02003995 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003996 shell->lock_surface = NULL;
3997}
3998
3999static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004000desktop_shell_set_lock_surface(struct wl_client *client,
4001 struct wl_resource *resource,
4002 struct wl_resource *surface_resource)
4003{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004004 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004005 struct weston_surface *surface =
4006 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004007
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004008 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004009
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004010 if (!shell->locked)
4011 return;
4012
Pekka Paalanen98262232011-12-01 10:42:22 +02004013 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004014
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004015 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004016 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004017 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004018
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004019 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004020 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004021 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004022}
4023
4024static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004025resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004026{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004027 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004028
Pekka Paalanen77346a62011-11-30 16:26:35 +02004029 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004030
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004031 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004032 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004033 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004034 &shell->input_panel_layer.link);
4035 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004036 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004037 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004038 wl_list_insert(&shell->compositor->cursor_layer.link,
4039 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004040 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004041 wl_list_insert(&shell->fullscreen_layer.link,
4042 &shell->panel_layer.link);
4043 wl_list_insert(&shell->panel_layer.link,
4044 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004045
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004046 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004047
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004048 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004049 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004050 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004051}
4052
4053static void
4054desktop_shell_unlock(struct wl_client *client,
4055 struct wl_resource *resource)
4056{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004057 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004058
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004059 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004060
4061 if (shell->locked)
4062 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004063}
4064
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004065static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004066desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004067 struct wl_resource *resource,
4068 struct wl_resource *surface_resource)
4069{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004070 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004071
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004072 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004073 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004074}
4075
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004076static void
4077desktop_shell_desktop_ready(struct wl_client *client,
4078 struct wl_resource *resource)
4079{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004080 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004081
4082 shell_fade_startup(shell);
4083}
4084
Kristian Høgsberg75840622011-09-06 13:48:16 -04004085static const struct desktop_shell_interface desktop_shell_implementation = {
4086 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004087 desktop_shell_set_panel,
4088 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004089 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004090 desktop_shell_set_grab_surface,
4091 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004092};
4093
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004094static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004095get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004096{
4097 struct shell_surface *shsurf;
4098
4099 shsurf = get_shell_surface(surface);
4100 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004101 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004102 return shsurf->type;
4103}
4104
Kristian Høgsberg75840622011-09-06 13:48:16 -04004105static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004106move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004107{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004108 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004109 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004110 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004111
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004112 if (seat->pointer->focus == NULL)
4113 return;
4114
4115 focus = seat->pointer->focus->surface;
4116
Pekka Paalanen01388e22013-04-25 13:57:44 +03004117 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004118 if (surface == NULL)
4119 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004120
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004121 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004122 if (shsurf == NULL || shsurf->state.fullscreen ||
4123 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004124 return;
4125
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004126 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004127}
4128
4129static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004130maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4131{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004132 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004133 struct weston_surface *surface;
4134 struct shell_surface *shsurf;
4135
4136 surface = weston_surface_get_main_surface(focus);
4137 if (surface == NULL)
4138 return;
4139
4140 shsurf = get_shell_surface(surface);
4141 if (shsurf == NULL)
4142 return;
4143
4144 if (!shell_surface_is_xdg_surface(shsurf))
4145 return;
4146
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004147 shsurf->state_requested = true;
4148 shsurf->requested_state.maximized = !shsurf->state.maximized;
4149 if (shsurf->requested_state.maximized)
4150 set_maximized(shsurf, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004151}
4152
4153static void
4154fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4155{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004156 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004157 struct weston_surface *surface;
4158 struct shell_surface *shsurf;
4159
4160 surface = weston_surface_get_main_surface(focus);
4161 if (surface == NULL)
4162 return;
4163
4164 shsurf = get_shell_surface(surface);
4165 if (shsurf == NULL)
4166 return;
4167
4168 if (!shell_surface_is_xdg_surface(shsurf))
4169 return;
4170
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004171 shsurf->state_requested = true;
4172 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
4173 if (shsurf->requested_state.fullscreen)
4174 set_fullscreen(shsurf,
4175 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
4176 0, shsurf->recommended_output);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004177}
4178
4179static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004180touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4181{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004182 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004183 struct weston_surface *surface;
4184 struct shell_surface *shsurf;
4185
4186 surface = weston_surface_get_main_surface(focus);
4187 if (surface == NULL)
4188 return;
4189
4190 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004191 if (shsurf == NULL || shsurf->state.fullscreen ||
4192 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004193 return;
4194
4195 surface_touch_move(shsurf, (struct weston_seat *) seat);
4196}
4197
4198static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004199resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004200{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004201 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004202 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004203 uint32_t edges = 0;
4204 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004205 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004206
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004207 if (seat->pointer->focus == NULL)
4208 return;
4209
4210 focus = seat->pointer->focus->surface;
4211
Pekka Paalanen01388e22013-04-25 13:57:44 +03004212 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004213 if (surface == NULL)
4214 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004215
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004216 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004217 if (shsurf == NULL || shsurf->state.fullscreen ||
4218 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004219 return;
4220
Jason Ekstranda7af7042013-10-12 22:38:11 -05004221 weston_view_from_global(shsurf->view,
4222 wl_fixed_to_int(seat->pointer->grab_x),
4223 wl_fixed_to_int(seat->pointer->grab_y),
4224 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004225
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004226 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004227 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004228 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004229 edges |= 0;
4230 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004231 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004232
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004233 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004234 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004235 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004236 edges |= 0;
4237 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004238 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004239
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004240 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004241}
4242
4243static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004244surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004245 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004246{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004247 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004248 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004249 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004250 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004251
Pekka Paalanen01388e22013-04-25 13:57:44 +03004252 /* XXX: broken for windows containing sub-surfaces */
4253 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004254 if (surface == NULL)
4255 return;
4256
4257 shsurf = get_shell_surface(surface);
4258 if (!shsurf)
4259 return;
4260
Jason Ekstranda7af7042013-10-12 22:38:11 -05004261 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004262
Jason Ekstranda7af7042013-10-12 22:38:11 -05004263 if (shsurf->view->alpha > 1.0)
4264 shsurf->view->alpha = 1.0;
4265 if (shsurf->view->alpha < step)
4266 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004267
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004269 weston_surface_damage(surface);
4270}
4271
4272static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004273do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004274 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004275{
Daniel Stone37816df2012-05-16 18:45:18 +01004276 struct weston_seat *ws = (struct weston_seat *) seat;
4277 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004278 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004279 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004280
4281 wl_list_for_each(output, &compositor->output_list, link) {
4282 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004283 wl_fixed_to_double(seat->pointer->x),
4284 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004285 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004286 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004287 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004288 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004289 increment = -output->zoom.increment;
4290 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004291 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004292 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004293 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004294 else
4295 increment = 0;
4296
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004297 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004298
Scott Moreaue6603982012-06-11 13:07:51 -06004299 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004300 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004301 else if (output->zoom.level > output->zoom.max_level)
4302 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004303 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004304 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004305 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004306
Scott Moreaue6603982012-06-11 13:07:51 -06004307 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004308
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004310 }
4311 }
4312}
4313
Scott Moreauccbf29d2012-02-22 14:21:41 -07004314static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004315zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004316 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004317{
4318 do_zoom(seat, time, 0, axis, value);
4319}
4320
4321static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004322zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004323 void *data)
4324{
4325 do_zoom(seat, time, key, 0, 0);
4326}
4327
4328static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004329terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004330 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004331{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004332 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004333
Daniel Stone325fc2d2012-05-30 16:31:58 +01004334 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004335}
4336
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004337static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004338rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4339 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004340{
4341 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004342 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004343 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004344 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004345 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004346
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004347 weston_pointer_move(pointer, x, y);
4348
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004349 if (!shsurf)
4350 return;
4351
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004352 cx = 0.5f * shsurf->surface->width;
4353 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004354
Daniel Stone37816df2012-05-16 18:45:18 +01004355 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4356 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004357 r = sqrtf(dx * dx + dy * dy);
4358
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004359 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004361
4362 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004363 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004364 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004365
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004366 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004367 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004368
4369 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004370 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004371 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004372 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004373 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004374
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004375 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004376 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004377 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004378 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004379 wl_list_init(&shsurf->rotation.transform.link);
4380 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004381 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004382 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004383
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004384 /* We need to adjust the position of the surface
4385 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004386 cposx = shsurf->view->geometry.x + cx;
4387 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004388 dposx = rotate->center.x - cposx;
4389 dposy = rotate->center.y - cposy;
4390 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 weston_view_set_position(shsurf->view,
4392 shsurf->view->geometry.x + dposx,
4393 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004394 }
4395
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004396 /* Repaint implies weston_surface_update_transform(), which
4397 * lazily applies the damage due to rotation update.
4398 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004399 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004400}
4401
4402static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004403rotate_grab_button(struct weston_pointer_grab *grab,
4404 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004405{
4406 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004407 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004408 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004409 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004410 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004411
Daniel Stone4dbadb12012-05-30 16:31:51 +01004412 if (pointer->button_count == 0 &&
4413 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004414 if (shsurf)
4415 weston_matrix_multiply(&shsurf->rotation.rotation,
4416 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004417 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004418 free(rotate);
4419 }
4420}
4421
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004422static void
4423rotate_grab_cancel(struct weston_pointer_grab *grab)
4424{
4425 struct rotate_grab *rotate =
4426 container_of(grab, struct rotate_grab, base.grab);
4427
4428 shell_grab_end(&rotate->base);
4429 free(rotate);
4430}
4431
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004432static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004433 noop_grab_focus,
4434 rotate_grab_motion,
4435 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004436 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004437};
4438
4439static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004440surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004441{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004442 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004443 float dx, dy;
4444 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004445
Pekka Paalanen460099f2012-01-20 16:48:25 +02004446 rotate = malloc(sizeof *rotate);
4447 if (!rotate)
4448 return;
4449
Jason Ekstranda7af7042013-10-12 22:38:11 -05004450 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004451 surface->surface->width * 0.5f,
4452 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004453 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004454
Daniel Stone37816df2012-05-16 18:45:18 +01004455 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4456 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004457 r = sqrtf(dx * dx + dy * dy);
4458 if (r > 20.0f) {
4459 struct weston_matrix inverse;
4460
4461 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004462 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004463 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004464
4465 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004466 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004467 } else {
4468 weston_matrix_init(&surface->rotation.rotation);
4469 weston_matrix_init(&rotate->rotation);
4470 }
4471
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004472 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4473 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004474}
4475
4476static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004477rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004478 void *data)
4479{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004480 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004481 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004482 struct shell_surface *surface;
4483
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004484 if (seat->pointer->focus == NULL)
4485 return;
4486
4487 focus = seat->pointer->focus->surface;
4488
Pekka Paalanen01388e22013-04-25 13:57:44 +03004489 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004490 if (base_surface == NULL)
4491 return;
4492
4493 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004494 if (surface == NULL || surface->state.fullscreen ||
4495 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004496 return;
4497
4498 surface_rotate(surface, seat);
4499}
4500
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004501/* Move all fullscreen layers down to the current workspace and hide their
4502 * black views. The surfaces' state is set to both fullscreen and lowered,
4503 * and this is reversed when such a surface is re-configured, see
4504 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4505 *
4506 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004507 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004508void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004509lower_fullscreen_layer(struct desktop_shell *shell)
4510{
4511 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004512 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004513
4514 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004515 wl_list_for_each_reverse_safe(view, prev,
4516 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004517 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004518 struct shell_surface *shsurf = get_shell_surface(view->surface);
4519
4520 if (!shsurf)
4521 continue;
4522
4523 /* We can have a non-fullscreen popup for a fullscreen surface
4524 * in the fullscreen layer. */
4525 if (shsurf->state.fullscreen) {
4526 /* Hide the black view */
4527 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4528 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004529 weston_view_damage_below(shsurf->fullscreen.black_view);
4530
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004531 }
4532
4533 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004534 wl_list_remove(&view->layer_link);
4535 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4536 weston_view_damage_below(view);
4537 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004538
4539 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004540 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004541}
4542
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004543void
Tiago Vignattibe143262012-04-16 17:31:41 +03004544activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004545 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004546{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004547 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004548 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004549 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004550 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004551 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004552
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004553 lower_fullscreen_layer(shell);
4554
Pekka Paalanen01388e22013-04-25 13:57:44 +03004555 main_surface = weston_surface_get_main_surface(es);
4556
Daniel Stone37816df2012-05-16 18:45:18 +01004557 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004558
Jonas Ådahl8538b222012-08-29 22:13:03 +02004559 state = ensure_focus_state(shell, seat);
4560 if (state == NULL)
4561 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004562
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004563 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004564 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004565
Rafael Antognolli03b16592013-12-03 15:35:42 -02004566 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004567 assert(shsurf);
4568
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004569 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004570 shell_configure_fullscreen(shsurf);
4571 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004572 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004573
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004574 /* Update the surface’s layer. This brings it to the top of the stacking
4575 * order as appropriate. */
4576 shell_surface_update_layer(shsurf);
4577
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004578 if (shell->focus_animation_type != ANIMATION_NONE) {
4579 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004580 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004581 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004582}
4583
Alex Wu21858432012-04-01 20:13:08 +08004584/* no-op func for checking black surface */
4585static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004586black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004587{
4588}
4589
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004590static bool
Alex Wu21858432012-04-01 20:13:08 +08004591is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4592{
4593 if (es->configure == black_surface_configure) {
4594 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004595 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004596 return true;
4597 }
4598 return false;
4599}
4600
Kristian Høgsberg75840622011-09-06 13:48:16 -04004601static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004602activate_binding(struct weston_seat *seat,
4603 struct desktop_shell *shell,
4604 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004605{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004606 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004607
Alex Wu9c35e6b2012-03-05 14:13:13 +08004608 if (!focus)
4609 return;
4610
Pekka Paalanen01388e22013-04-25 13:57:44 +03004611 if (is_black_surface(focus, &main_surface))
4612 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004613
Pekka Paalanen01388e22013-04-25 13:57:44 +03004614 main_surface = weston_surface_get_main_surface(focus);
4615 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004616 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004617
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004618 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004619}
4620
4621static void
4622click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4623 void *data)
4624{
4625 if (seat->pointer->grab != &seat->pointer->default_grab)
4626 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004627 if (seat->pointer->focus == NULL)
4628 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004629
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004630 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004631}
4632
4633static void
4634touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4635{
4636 if (seat->touch->grab != &seat->touch->default_grab)
4637 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004638 if (seat->touch->focus == NULL)
4639 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004640
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004641 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004642}
4643
4644static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004645unfocus_all_seats(struct desktop_shell *shell)
4646{
4647 struct weston_seat *seat, *next;
4648
4649 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4650 if (seat->keyboard == NULL)
4651 continue;
4652
4653 weston_keyboard_set_focus(seat->keyboard, NULL);
4654 }
4655}
4656
4657static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004658lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004659{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004660 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004661
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004662 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004663 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004664 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004665 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004666
4667 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004668
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004669 /* Hide all surfaces by removing the fullscreen, panel and
4670 * toplevel layers. This way nothing else can show or receive
4671 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004672
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004673 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004674 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004675 if (shell->showing_input_panels)
4676 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004677 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004678 wl_list_insert(&shell->compositor->cursor_layer.link,
4679 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004680
Pekka Paalanen77346a62011-11-30 16:26:35 +02004681 launch_screensaver(shell);
4682
Neil Robertsb4a91702014-04-09 16:33:32 +01004683 /* Remove the keyboard focus on all seats. This will be
4684 * restored to the workspace's saved state via
4685 * restore_focus_state when the compositor is unlocked */
4686 unfocus_all_seats(shell);
4687
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004688 /* TODO: disable bindings that should not work while locked. */
4689
4690 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004691}
4692
4693static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004694unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004695{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004696 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004697 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004698 return;
4699 }
4700
4701 /* If desktop-shell client has gone away, unlock immediately. */
4702 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004703 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004704 return;
4705 }
4706
4707 if (shell->prepare_event_sent)
4708 return;
4709
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004710 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004711 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004712}
4713
4714static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004715shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004716{
4717 struct desktop_shell *shell = data;
4718
4719 shell->fade.animation = NULL;
4720
4721 switch (shell->fade.type) {
4722 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004723 weston_surface_destroy(shell->fade.view->surface);
4724 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004725 break;
4726 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004727 lock(shell);
4728 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004729 default:
4730 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004731 }
4732}
4733
Jason Ekstranda7af7042013-10-12 22:38:11 -05004734static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004735shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004736{
4737 struct weston_compositor *compositor = shell->compositor;
4738 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004739 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004740
4741 surface = weston_surface_create(compositor);
4742 if (!surface)
4743 return NULL;
4744
Jason Ekstranda7af7042013-10-12 22:38:11 -05004745 view = weston_view_create(surface);
4746 if (!view) {
4747 weston_surface_destroy(surface);
4748 return NULL;
4749 }
4750
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004751 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004752 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004753 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004754 wl_list_insert(&compositor->fade_layer.view_list,
4755 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004756 pixman_region32_init(&surface->input);
4757
Jason Ekstranda7af7042013-10-12 22:38:11 -05004758 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004759}
4760
4761static void
4762shell_fade(struct desktop_shell *shell, enum fade_type type)
4763{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004764 float tint;
4765
4766 switch (type) {
4767 case FADE_IN:
4768 tint = 0.0;
4769 break;
4770 case FADE_OUT:
4771 tint = 1.0;
4772 break;
4773 default:
4774 weston_log("shell: invalid fade type\n");
4775 return;
4776 }
4777
4778 shell->fade.type = type;
4779
Jason Ekstranda7af7042013-10-12 22:38:11 -05004780 if (shell->fade.view == NULL) {
4781 shell->fade.view = shell_fade_create_surface(shell);
4782 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004783 return;
4784
Jason Ekstranda7af7042013-10-12 22:38:11 -05004785 shell->fade.view->alpha = 1.0 - tint;
4786 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004787 }
4788
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004789 if (shell->fade.view->output == NULL) {
4790 /* If the black view gets a NULL output, we lost the
4791 * last output and we'll just cancel the fade. This
4792 * happens when you close the last window under the
4793 * X11 or Wayland backends. */
4794 shell->locked = false;
4795 weston_surface_destroy(shell->fade.view->surface);
4796 shell->fade.view = NULL;
4797 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004798 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004799 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004800 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004801 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004802 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004803 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004804 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004805}
4806
4807static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004808do_shell_fade_startup(void *data)
4809{
4810 struct desktop_shell *shell = data;
4811
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004812 if (shell->startup_animation_type == ANIMATION_FADE)
4813 shell_fade(shell, FADE_IN);
4814 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004815 weston_surface_destroy(shell->fade.view->surface);
4816 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004817 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004818}
4819
4820static void
4821shell_fade_startup(struct desktop_shell *shell)
4822{
4823 struct wl_event_loop *loop;
4824
4825 if (!shell->fade.startup_timer)
4826 return;
4827
4828 wl_event_source_remove(shell->fade.startup_timer);
4829 shell->fade.startup_timer = NULL;
4830
4831 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4832 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4833}
4834
4835static int
4836fade_startup_timeout(void *data)
4837{
4838 struct desktop_shell *shell = data;
4839
4840 shell_fade_startup(shell);
4841 return 0;
4842}
4843
4844static void
4845shell_fade_init(struct desktop_shell *shell)
4846{
4847 /* Make compositor output all black, and wait for the desktop-shell
4848 * client to signal it is ready, then fade in. The timer triggers a
4849 * fade-in, in case the desktop-shell client takes too long.
4850 */
4851
4852 struct wl_event_loop *loop;
4853
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004855 weston_log("%s: warning: fade surface already exists\n",
4856 __func__);
4857 return;
4858 }
4859
Jason Ekstranda7af7042013-10-12 22:38:11 -05004860 shell->fade.view = shell_fade_create_surface(shell);
4861 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004862 return;
4863
Jason Ekstranda7af7042013-10-12 22:38:11 -05004864 weston_view_update_transform(shell->fade.view);
4865 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004866
4867 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4868 shell->fade.startup_timer =
4869 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4870 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4871}
4872
4873static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004874idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004875{
4876 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004877 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004878 struct weston_seat *seat;
4879
4880 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4881 if (seat->pointer)
4882 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004883
4884 shell_fade(shell, FADE_OUT);
4885 /* lock() is called from shell_fade_done() */
4886}
4887
4888static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004889wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004890{
4891 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004892 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004893
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004894 unlock(shell);
4895}
4896
4897static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004898center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004899{
Giulio Camuffob8366642013-04-25 13:57:46 +03004900 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004901 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004902
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004904
4905 x = output->x + (output->width - width) / 2 - surf_x / 2;
4906 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004907
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004908 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004909}
4910
4911static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004912weston_view_set_initial_position(struct weston_view *view,
4913 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004914{
4915 struct weston_compositor *compositor = shell->compositor;
4916 int ix = 0, iy = 0;
4917 int range_x, range_y;
4918 int dx, dy, x, y, panel_height;
4919 struct weston_output *output, *target_output = NULL;
4920 struct weston_seat *seat;
4921
4922 /* As a heuristic place the new window on the same output as the
4923 * pointer. Falling back to the output containing 0, 0.
4924 *
4925 * TODO: Do something clever for touch too?
4926 */
4927 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004928 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004929 ix = wl_fixed_to_int(seat->pointer->x);
4930 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004931 break;
4932 }
4933 }
4934
4935 wl_list_for_each(output, &compositor->output_list, link) {
4936 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4937 target_output = output;
4938 break;
4939 }
4940 }
4941
4942 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004943 weston_view_set_position(view, 10 + random() % 400,
4944 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004945 return;
4946 }
4947
4948 /* Valid range within output where the surface will still be onscreen.
4949 * If this is negative it means that the surface is bigger than
4950 * output.
4951 */
4952 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004953 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004954 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004955 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004956
Rob Bradford4cb88c72012-08-13 15:18:44 +01004957 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004958 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004959 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004960 dx = 0;
4961
4962 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004963 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004964 else
4965 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004966
4967 x = target_output->x + dx;
4968 y = target_output->y + dy;
4969
Jason Ekstranda7af7042013-10-12 22:38:11 -05004970 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004971}
4972
4973static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004974map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004975 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004976{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004977 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004978 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004979 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004980 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004981
Pekka Paalanen77346a62011-11-30 16:26:35 +02004982 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004983 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004984 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004985 if (shsurf->state.fullscreen) {
4986 center_on_output(shsurf->view, shsurf->fullscreen_output);
4987 shell_map_fullscreen(shsurf);
4988 } else if (shsurf->state.maximized) {
4989 /* use surface configure to set the geometry */
4990 panel_height = get_output_panel_height(shell, shsurf->output);
4991 surface_subsurfaces_boundingbox(shsurf->surface,
4992 &surf_x, &surf_y, NULL, NULL);
4993 weston_view_set_position(shsurf->view,
4994 shsurf->output->x - surf_x,
4995 shsurf->output->y +
4996 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004997 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004998 weston_view_set_initial_position(shsurf->view, shell);
4999 }
Juan Zhao96879df2012-02-07 08:45:41 +08005000 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005001 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005002 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005003 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005004 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005005 weston_view_set_position(shsurf->view,
5006 shsurf->view->geometry.x + sx,
5007 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005008 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005009 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005010 default:
5011 ;
5012 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005013
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005014 /* Surface stacking order, see also activate(). */
5015 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005016
Jason Ekstranda7af7042013-10-12 22:38:11 -05005017 if (shsurf->type != SHELL_SURFACE_NONE) {
5018 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005019 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005020 shsurf->surface->output = shsurf->output;
5021 shsurf->view->output = shsurf->output;
5022 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005023 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005024
Jason Ekstranda7af7042013-10-12 22:38:11 -05005025 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005026 /* XXX: xwayland's using the same fields for transient type */
5027 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005028 if (shsurf->transient.flags ==
5029 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5030 break;
5031 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005032 if (shsurf->state.relative &&
5033 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5034 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005035 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005036 break;
5037 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005038 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005039 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005040 case SHELL_SURFACE_POPUP:
5041 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005042 default:
5043 break;
5044 }
5045
Rafael Antognolli03b16592013-12-03 15:35:42 -02005046 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5047 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005048 {
5049 switch (shell->win_animation_type) {
5050 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005051 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005052 break;
5053 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005054 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005055 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005056 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005057 default:
5058 break;
5059 }
5060 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005061}
5062
5063static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005064configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005065 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005066{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005067 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005068 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005069 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005070
Pekka Paalanen77346a62011-11-30 16:26:35 +02005071 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005072
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005073 assert(shsurf);
5074
Rafael Antognolli03b16592013-12-03 15:35:42 -02005075 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005076 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005077 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005078 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005079 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5080 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005081 mx = shsurf->output->x - surf_x;
5082 my = shsurf->output->y +
5083 get_output_panel_height(shell,shsurf->output) - surf_y;
5084 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005085 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005086 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005087 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005088
Alex Wu4539b082012-03-01 12:57:46 +08005089 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005090 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005091 wl_list_for_each(view, &surface->views, surface_link)
5092 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005093
Rafael Antognolli03b16592013-12-03 15:35:42 -02005094 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005095 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005096 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005097}
5098
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005099static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005100shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005101{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005102 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005103 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005104 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005105
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005106 assert(shsurf);
5107
5108 shell = shsurf->shell;
5109
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005110 if (!weston_surface_is_mapped(es) &&
5111 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005112 remove_popup_grab(shsurf);
5113 }
5114
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005115 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005116 return;
5117
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005118 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005119 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005120 type_changed = 1;
5121 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005122
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005123 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005124 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005125 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005126 shsurf->last_width != es->width ||
5127 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005128 float from_x, from_y;
5129 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005130
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005131 if (shsurf->resize_edges) {
5132 sx = 0;
5133 sy = 0;
5134 }
5135
5136 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5137 sx = shsurf->last_width - es->width;
5138 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5139 sy = shsurf->last_height - es->height;
5140
5141 shsurf->last_width = es->width;
5142 shsurf->last_height = es->height;
5143
Jason Ekstranda7af7042013-10-12 22:38:11 -05005144 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5145 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005146 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005147 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005148 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005149 }
5150}
5151
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005152static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005153
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005154static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005155desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005156{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005157 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005158 struct desktop_shell *shell =
5159 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005160
5161 shell->child.process.pid = 0;
5162 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005163
5164 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5165 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005166 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005167 shell->child.deathstamp = time;
5168 shell->child.deathcount = 0;
5169 }
5170
5171 shell->child.deathcount++;
5172 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005173 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005174 return;
5175 }
5176
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005177 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005178 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005179 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005180}
5181
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005182static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005183desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5184{
5185 struct desktop_shell *shell;
5186
5187 shell = container_of(listener, struct desktop_shell,
5188 child.client_destroy_listener);
5189
5190 shell->child.client = NULL;
5191}
5192
5193static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005194launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005195{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005196 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005197
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005198 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005199 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005200 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005201 desktop_shell_sigchld);
5202
5203 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005204 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005205
5206 shell->child.client_destroy_listener.notify =
5207 desktop_shell_client_destroy;
5208 wl_client_add_destroy_listener(shell->child.client,
5209 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005210}
5211
5212static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005213handle_shell_client_destroy(struct wl_listener *listener, void *data)
5214{
5215 struct shell_client *sc =
5216 container_of(listener, struct shell_client, destroy_listener);
5217
5218 if (sc->ping_timer)
5219 wl_event_source_remove(sc->ping_timer);
5220 free(sc);
5221}
5222
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005223static struct shell_client *
5224shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5225 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005226{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005227 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005228
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005229 sc = zalloc(sizeof *sc);
5230 if (sc == NULL) {
5231 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005232 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005233 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005234
5235 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005236 if (sc->resource == NULL) {
5237 free(sc);
5238 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005239 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005240 }
5241
5242 sc->client = client;
5243 sc->shell = shell;
5244 sc->destroy_listener.notify = handle_shell_client_destroy;
5245 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5246
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005247 return sc;
5248}
5249
5250static void
5251bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5252{
5253 struct desktop_shell *shell = data;
5254 struct shell_client *sc;
5255
5256 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5257 if (sc)
5258 wl_resource_set_implementation(sc->resource,
5259 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005260 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005261}
5262
5263static void
5264bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5265{
5266 struct desktop_shell *shell = data;
5267 struct shell_client *sc;
5268
5269 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5270 if (sc)
5271 wl_resource_set_dispatcher(sc->resource,
5272 xdg_shell_unversioned_dispatch,
5273 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005274}
5275
5276static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005277unbind_desktop_shell(struct wl_resource *resource)
5278{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005279 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005280
5281 if (shell->locked)
5282 resume_desktop(shell);
5283
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005284 shell->child.desktop_shell = NULL;
5285 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005286}
5287
5288static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005289bind_desktop_shell(struct wl_client *client,
5290 void *data, uint32_t version, uint32_t id)
5291{
Tiago Vignattibe143262012-04-16 17:31:41 +03005292 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005293 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005294
Jason Ekstranda85118c2013-06-27 20:17:02 -05005295 resource = wl_resource_create(client, &desktop_shell_interface,
5296 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005297
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005298 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005299 wl_resource_set_implementation(resource,
5300 &desktop_shell_implementation,
5301 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005302 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005303
5304 if (version < 2)
5305 shell_fade_startup(shell);
5306
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005307 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005308 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005309
5310 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5311 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005312 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005313}
5314
Pekka Paalanen6e168112011-11-24 11:34:05 +02005315static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005316screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005317{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005318 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005319 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005320
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005321 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005322 return;
5323
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005324 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005325 if (!shell->locked)
5326 return;
5327
Jason Ekstranda7af7042013-10-12 22:38:11 -05005328 view = container_of(surface->views.next, struct weston_view, surface_link);
5329 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005330
Jason Ekstranda7af7042013-10-12 22:38:11 -05005331 if (wl_list_empty(&view->layer_link)) {
5332 wl_list_insert(shell->lock_layer.view_list.prev,
5333 &view->layer_link);
5334 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005335 wl_event_source_timer_update(shell->screensaver.timer,
5336 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005337 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005338 }
5339}
5340
5341static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005342screensaver_set_surface(struct wl_client *client,
5343 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005344 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005345 struct wl_resource *output_resource)
5346{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005347 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005348 struct weston_surface *surface =
5349 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005350 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005351 struct weston_view *view, *next;
5352
5353 /* Make sure we only have one view */
5354 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5355 weston_view_destroy(view);
5356 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005357
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005358 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005359 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005360 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005361}
5362
5363static const struct screensaver_interface screensaver_implementation = {
5364 screensaver_set_surface
5365};
5366
5367static void
5368unbind_screensaver(struct wl_resource *resource)
5369{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005370 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005371
Pekka Paalanen77346a62011-11-30 16:26:35 +02005372 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005373}
5374
5375static void
5376bind_screensaver(struct wl_client *client,
5377 void *data, uint32_t version, uint32_t id)
5378{
Tiago Vignattibe143262012-04-16 17:31:41 +03005379 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005380 struct wl_resource *resource;
5381
Jason Ekstranda85118c2013-06-27 20:17:02 -05005382 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005383
Pekka Paalanen77346a62011-11-30 16:26:35 +02005384 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005385 wl_resource_set_implementation(resource,
5386 &screensaver_implementation,
5387 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005388 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005389 return;
5390 }
5391
5392 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5393 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005394 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005395}
5396
Kristian Høgsberg07045392012-02-19 18:52:44 -05005397struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005398 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005399 struct weston_surface *current;
5400 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005401 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005402 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005403};
5404
5405static void
5406switcher_next(struct switcher *switcher)
5407{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005408 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005409 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005410 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005411 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005412
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005413 /* temporary re-display minimized surfaces */
5414 struct weston_view *tmp;
5415 struct weston_view **minimized;
5416 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5417 wl_list_remove(&view->layer_link);
5418 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5419 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5420 *minimized = view;
5421 }
5422
Jason Ekstranda7af7042013-10-12 22:38:11 -05005423 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005424 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005425 if (shsurf &&
5426 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5427 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005428 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005429 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005430 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005431 next = view->surface;
5432 prev = view->surface;
5433 view->alpha = 0.25;
5434 weston_view_geometry_dirty(view);
5435 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005436 }
Alex Wu1659daa2012-04-01 20:13:09 +08005437
Jason Ekstranda7af7042013-10-12 22:38:11 -05005438 if (is_black_surface(view->surface, NULL)) {
5439 view->alpha = 0.25;
5440 weston_view_geometry_dirty(view);
5441 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005442 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005443 }
5444
5445 if (next == NULL)
5446 next = first;
5447
Alex Wu07b26062012-03-12 16:06:01 +08005448 if (next == NULL)
5449 return;
5450
Kristian Høgsberg07045392012-02-19 18:52:44 -05005451 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005452 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005453
5454 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005455 wl_list_for_each(view, &next->views, surface_link)
5456 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005457
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005458 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005459 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005460 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005461}
5462
5463static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005464switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005465{
5466 struct switcher *switcher =
5467 container_of(listener, struct switcher, listener);
5468
5469 switcher_next(switcher);
5470}
5471
5472static void
Daniel Stone351eb612012-05-31 15:27:47 -04005473switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005474{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005475 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005476 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005477 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005478
Jason Ekstranda7af7042013-10-12 22:38:11 -05005479 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005480 if (is_focus_view(view))
5481 continue;
5482
Jason Ekstranda7af7042013-10-12 22:38:11 -05005483 view->alpha = 1.0;
5484 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005485 }
5486
Alex Wu07b26062012-03-12 16:06:01 +08005487 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005488 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005489 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005490 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005491 weston_keyboard_end_grab(keyboard);
5492 if (keyboard->input_method_resource)
5493 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005494
5495 /* re-hide surfaces that were temporary shown during the switch */
5496 struct weston_view **minimized;
5497 wl_array_for_each(minimized, &switcher->minimized_array) {
5498 /* with the exception of the current selected */
5499 if ((*minimized)->surface != switcher->current) {
5500 wl_list_remove(&(*minimized)->layer_link);
5501 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5502 weston_view_damage_below(*minimized);
5503 }
5504 }
5505 wl_array_release(&switcher->minimized_array);
5506
Kristian Høgsberg07045392012-02-19 18:52:44 -05005507 free(switcher);
5508}
5509
5510static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005511switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005512 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005513{
5514 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005515 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005516
Daniel Stonec9785ea2012-05-30 16:31:52 +01005517 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005518 switcher_next(switcher);
5519}
5520
5521static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005522switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005523 uint32_t mods_depressed, uint32_t mods_latched,
5524 uint32_t mods_locked, uint32_t group)
5525{
5526 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005527 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005528
Daniel Stone351eb612012-05-31 15:27:47 -04005529 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5530 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005531}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005532
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005533static void
5534switcher_cancel(struct weston_keyboard_grab *grab)
5535{
5536 struct switcher *switcher = container_of(grab, struct switcher, grab);
5537
5538 switcher_destroy(switcher);
5539}
5540
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005541static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005542 switcher_key,
5543 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005544 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005545};
5546
5547static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005548switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005549 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005550{
Tiago Vignattibe143262012-04-16 17:31:41 +03005551 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005552 struct switcher *switcher;
5553
5554 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005555 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005556 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005557 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005558 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005559 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005560
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005561 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005562 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005563 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005564 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5565 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005566 switcher_next(switcher);
5567}
5568
Pekka Paalanen3c647232011-12-22 13:43:43 +02005569static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005570backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005571 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005572{
5573 struct weston_compositor *compositor = data;
5574 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005575 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005576
5577 /* TODO: we're limiting to simple use cases, where we assume just
5578 * control on the primary display. We'd have to extend later if we
5579 * ever get support for setting backlights on random desktop LCD
5580 * panels though */
5581 output = get_default_output(compositor);
5582 if (!output)
5583 return;
5584
5585 if (!output->set_backlight)
5586 return;
5587
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005588 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5589 backlight_new = output->backlight_current - 25;
5590 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5591 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005592
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005593 if (backlight_new < 5)
5594 backlight_new = 5;
5595 if (backlight_new > 255)
5596 backlight_new = 255;
5597
5598 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005599 output->set_backlight(output, output->backlight_current);
5600}
5601
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005602struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005603 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005604 struct weston_seat *seat;
5605 uint32_t key[2];
5606 int key_released[2];
5607};
5608
5609static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005610debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005611 uint32_t key, uint32_t state)
5612{
5613 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005614 struct weston_compositor *ec = db->seat->compositor;
5615 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005616 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005617 uint32_t serial;
5618 int send = 0, terminate = 0;
5619 int check_binding = 1;
5620 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005621 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005622
5623 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5624 /* Do not run bindings on key releases */
5625 check_binding = 0;
5626
5627 for (i = 0; i < 2; i++)
5628 if (key == db->key[i])
5629 db->key_released[i] = 1;
5630
5631 if (db->key_released[0] && db->key_released[1]) {
5632 /* All key releases been swalled so end the grab */
5633 terminate = 1;
5634 } else if (key != db->key[0] && key != db->key[1]) {
5635 /* Should not swallow release of other keys */
5636 send = 1;
5637 }
5638 } else if (key == db->key[0] && !db->key_released[0]) {
5639 /* Do not check bindings for the first press of the binding
5640 * key. This allows it to be used as a debug shortcut.
5641 * We still need to swallow this event. */
5642 check_binding = 0;
5643 } else if (db->key[1]) {
5644 /* If we already ran a binding don't process another one since
5645 * we can't keep track of all the binding keys that were
5646 * pressed in order to swallow the release events. */
5647 send = 1;
5648 check_binding = 0;
5649 }
5650
5651 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005652 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5653 key, state)) {
5654 /* We ran a binding so swallow the press and keep the
5655 * grab to swallow the released too. */
5656 send = 0;
5657 terminate = 0;
5658 db->key[1] = key;
5659 } else {
5660 /* Terminate the grab since the key pressed is not a
5661 * debug binding key. */
5662 send = 1;
5663 terminate = 1;
5664 }
5665 }
5666
5667 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005668 serial = wl_display_next_serial(display);
5669 resource_list = &grab->keyboard->focus_resource_list;
5670 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005671 wl_keyboard_send_key(resource, serial, time, key, state);
5672 }
5673 }
5674
5675 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005676 weston_keyboard_end_grab(grab->keyboard);
5677 if (grab->keyboard->input_method_resource)
5678 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005679 free(db);
5680 }
5681}
5682
5683static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005684debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005685 uint32_t mods_depressed, uint32_t mods_latched,
5686 uint32_t mods_locked, uint32_t group)
5687{
5688 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005689 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005690
Neil Roberts96d790e2013-09-19 17:32:00 +01005691 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005692
Neil Roberts96d790e2013-09-19 17:32:00 +01005693 wl_resource_for_each(resource, resource_list) {
5694 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5695 mods_latched, mods_locked, group);
5696 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005697}
5698
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005699static void
5700debug_binding_cancel(struct weston_keyboard_grab *grab)
5701{
5702 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5703
5704 weston_keyboard_end_grab(grab->keyboard);
5705 free(db);
5706}
5707
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005708struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005709 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005710 debug_binding_modifiers,
5711 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005712};
5713
5714static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005715debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005716{
5717 struct debug_binding_grab *grab;
5718
5719 grab = calloc(1, sizeof *grab);
5720 if (!grab)
5721 return;
5722
5723 grab->seat = (struct weston_seat *) seat;
5724 grab->key[0] = key;
5725 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005726 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005727}
5728
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005729static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005730force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005731 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005732{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005733 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005734 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005735 struct desktop_shell *shell = data;
5736 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005737 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005738
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005739 focus_surface = seat->keyboard->focus;
5740 if (!focus_surface)
5741 return;
5742
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005743 wl_signal_emit(&compositor->kill_signal, focus_surface);
5744
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005745 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005746 wl_client_get_credentials(client, &pid, NULL, NULL);
5747
5748 /* Skip clients that we launched ourselves (the credentials of
5749 * the socketpair is ours) */
5750 if (pid == getpid())
5751 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005752
Daniel Stone325fc2d2012-05-30 16:31:58 +01005753 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005754}
5755
5756static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005757workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005758 uint32_t key, void *data)
5759{
5760 struct desktop_shell *shell = data;
5761 unsigned int new_index = shell->workspaces.current;
5762
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005763 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005764 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005765 if (new_index != 0)
5766 new_index--;
5767
5768 change_workspace(shell, new_index);
5769}
5770
5771static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005772workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005773 uint32_t key, void *data)
5774{
5775 struct desktop_shell *shell = data;
5776 unsigned int new_index = shell->workspaces.current;
5777
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005778 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005779 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005780 if (new_index < shell->workspaces.num - 1)
5781 new_index++;
5782
5783 change_workspace(shell, new_index);
5784}
5785
5786static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005787workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005788 uint32_t key, void *data)
5789{
5790 struct desktop_shell *shell = data;
5791 unsigned int new_index;
5792
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005793 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005794 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005795 new_index = key - KEY_F1;
5796 if (new_index >= shell->workspaces.num)
5797 new_index = shell->workspaces.num - 1;
5798
5799 change_workspace(shell, new_index);
5800}
5801
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005802static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005803workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005804 uint32_t key, void *data)
5805{
5806 struct desktop_shell *shell = data;
5807 unsigned int new_index = shell->workspaces.current;
5808
5809 if (shell->locked)
5810 return;
5811
5812 if (new_index != 0)
5813 new_index--;
5814
5815 take_surface_to_workspace_by_seat(shell, seat, new_index);
5816}
5817
5818static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005819workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005820 uint32_t key, void *data)
5821{
5822 struct desktop_shell *shell = data;
5823 unsigned int new_index = shell->workspaces.current;
5824
5825 if (shell->locked)
5826 return;
5827
5828 if (new_index < shell->workspaces.num - 1)
5829 new_index++;
5830
5831 take_surface_to_workspace_by_seat(shell, seat, new_index);
5832}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005833
5834static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005835shell_reposition_view_on_output_destroy(struct weston_view *view)
5836{
5837 struct weston_output *output, *first_output;
5838 struct weston_compositor *ec = view->surface->compositor;
5839 struct shell_surface *shsurf;
5840 float x, y;
5841 int visible;
5842
5843 x = view->geometry.x;
5844 y = view->geometry.y;
5845
5846 /* At this point the destroyed output is not in the list anymore.
5847 * If the view is still visible somewhere, we leave where it is,
5848 * otherwise, move it to the first output. */
5849 visible = 0;
5850 wl_list_for_each(output, &ec->output_list, link) {
5851 if (pixman_region32_contains_point(&output->region,
5852 x, y, NULL)) {
5853 visible = 1;
5854 break;
5855 }
5856 }
5857
5858 if (!visible) {
5859 first_output = container_of(ec->output_list.next,
5860 struct weston_output, link);
5861
5862 x = first_output->x + first_output->width / 4;
5863 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005864
5865 weston_view_set_position(view, x, y);
5866 } else {
5867 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005868 }
5869
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005870
5871 shsurf = get_shell_surface(view->surface);
5872
5873 if (shsurf) {
5874 shsurf->saved_position_valid = false;
5875 shsurf->next_state.maximized = false;
5876 shsurf->next_state.fullscreen = false;
5877 shsurf->state_changed = true;
5878 }
5879}
5880
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005881void
5882shell_for_each_layer(struct desktop_shell *shell,
5883 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005884{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005885 struct workspace **ws;
5886
5887 func(shell, &shell->fullscreen_layer, data);
5888 func(shell, &shell->panel_layer, data);
5889 func(shell, &shell->background_layer, data);
5890 func(shell, &shell->lock_layer, data);
5891 func(shell, &shell->input_panel_layer, data);
5892
5893 wl_array_for_each(ws, &shell->workspaces.array)
5894 func(shell, &(*ws)->layer, data);
5895}
5896
5897static void
5898shell_output_destroy_move_layer(struct desktop_shell *shell,
5899 struct weston_layer *layer,
5900 void *data)
5901{
5902 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005903 struct weston_view *view;
5904
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005905 wl_list_for_each(view, &layer->view_list, layer_link) {
5906 if (view->output != output)
5907 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005908
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005909 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005910 }
5911}
5912
5913static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005914handle_output_destroy(struct wl_listener *listener, void *data)
5915{
5916 struct shell_output *output_listener =
5917 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005918 struct weston_output *output = output_listener->output;
5919 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005920
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005921 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005922
Xiong Zhang6b481422013-10-23 13:58:32 +08005923 wl_list_remove(&output_listener->destroy_listener.link);
5924 wl_list_remove(&output_listener->link);
5925 free(output_listener);
5926}
5927
5928static void
5929create_shell_output(struct desktop_shell *shell,
5930 struct weston_output *output)
5931{
5932 struct shell_output *shell_output;
5933
5934 shell_output = zalloc(sizeof *shell_output);
5935 if (shell_output == NULL)
5936 return;
5937
5938 shell_output->output = output;
5939 shell_output->shell = shell;
5940 shell_output->destroy_listener.notify = handle_output_destroy;
5941 wl_signal_add(&output->destroy_signal,
5942 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005943 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005944}
5945
5946static void
5947handle_output_create(struct wl_listener *listener, void *data)
5948{
5949 struct desktop_shell *shell =
5950 container_of(listener, struct desktop_shell, output_create_listener);
5951 struct weston_output *output = (struct weston_output *)data;
5952
5953 create_shell_output(shell, output);
5954}
5955
5956static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005957handle_output_move_layer(struct desktop_shell *shell,
5958 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005959{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005960 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005961 struct weston_view *view;
5962 float x, y;
5963
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005964 wl_list_for_each(view, &layer->view_list, layer_link) {
5965 if (view->output != output)
5966 continue;
5967
5968 x = view->geometry.x + output->move_x;
5969 y = view->geometry.y + output->move_y;
5970 weston_view_set_position(view, x, y);
5971 }
5972}
5973
5974static void
5975handle_output_move(struct wl_listener *listener, void *data)
5976{
5977 struct desktop_shell *shell;
5978
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005979 shell = container_of(listener, struct desktop_shell,
5980 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005981
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005982 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005983}
5984
5985static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005986setup_output_destroy_handler(struct weston_compositor *ec,
5987 struct desktop_shell *shell)
5988{
5989 struct weston_output *output;
5990
5991 wl_list_init(&shell->output_list);
5992 wl_list_for_each(output, &ec->output_list, link)
5993 create_shell_output(shell, output);
5994
5995 shell->output_create_listener.notify = handle_output_create;
5996 wl_signal_add(&ec->output_created_signal,
5997 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005998
5999 shell->output_move_listener.notify = handle_output_move;
6000 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006001}
6002
6003static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006004shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006005{
Tiago Vignattibe143262012-04-16 17:31:41 +03006006 struct desktop_shell *shell =
6007 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006008 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006009 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006010
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006011 /* Force state to unlocked so we don't try to fade */
6012 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006013 if (shell->child.client)
6014 wl_client_destroy(shell->child.client);
6015
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006016 wl_list_remove(&shell->idle_listener.link);
6017 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006018
6019 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006020
Xiong Zhang6b481422013-10-23 13:58:32 +08006021 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6022 wl_list_remove(&shell_output->destroy_listener.link);
6023 wl_list_remove(&shell_output->link);
6024 free(shell_output);
6025 }
6026
6027 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006028 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006029
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006030 wl_array_for_each(ws, &shell->workspaces.array)
6031 workspace_destroy(*ws);
6032 wl_array_release(&shell->workspaces.array);
6033
Pekka Paalanen3c647232011-12-22 13:43:43 +02006034 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006035 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006036 free(shell);
6037}
6038
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006039static void
6040shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6041{
6042 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006043 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006044
6045 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006046 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6047 MODIFIER_CTRL | MODIFIER_ALT,
6048 terminate_binding, ec);
6049 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6050 click_to_activate_binding,
6051 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006052 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6053 click_to_activate_binding,
6054 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006055 weston_compositor_add_touch_binding(ec, 0,
6056 touch_to_activate_binding,
6057 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006058 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6059 MODIFIER_SUPER | MODIFIER_ALT,
6060 surface_opacity_binding, NULL);
6061 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6062 MODIFIER_SUPER, zoom_axis_binding,
6063 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006064
6065 /* configurable bindings */
6066 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006067 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6068 zoom_key_binding, NULL);
6069 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6070 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006071 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6072 maximize_binding, NULL);
6073 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6074 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006075 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6076 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006077 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006078 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6079 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006080 weston_compositor_add_button_binding(ec, BTN_LEFT,
6081 mod | MODIFIER_SHIFT,
6082 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006083
6084 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6085 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6086 rotate_binding, NULL);
6087
Daniel Stone325fc2d2012-05-30 16:31:58 +01006088 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6089 shell);
6090 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6091 ec);
6092 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6093 backlight_binding, ec);
6094 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6095 ec);
6096 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6097 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006098 weston_compositor_add_key_binding(ec, KEY_K, mod,
6099 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006100 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6101 workspace_up_binding, shell);
6102 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6103 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006104 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6105 workspace_move_surface_up_binding,
6106 shell);
6107 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6108 workspace_move_surface_down_binding,
6109 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006110
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006111 if (shell->exposay_modifier)
6112 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6113 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006114
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006115 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6116 if (shell->workspaces.num > 1) {
6117 num_workspace_bindings = shell->workspaces.num;
6118 if (num_workspace_bindings > 6)
6119 num_workspace_bindings = 6;
6120 for (i = 0; i < num_workspace_bindings; i++)
6121 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6122 workspace_f_binding,
6123 shell);
6124 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006125
6126 /* Debug bindings */
6127 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6128 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006129}
6130
Jason Ekstrand024177c2014-04-21 19:42:58 -05006131static void
6132handle_seat_created(struct wl_listener *listener, void *data)
6133{
6134 struct weston_seat *seat = data;
6135
6136 create_shell_seat(seat);
6137}
6138
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006139WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006140module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006141 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006142{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006143 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006144 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006145 struct workspace **pws;
6146 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006147 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006148
Peter Huttererf3d62272013-08-08 11:57:05 +10006149 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006150 if (shell == NULL)
6151 return -1;
6152
Kristian Høgsberg75840622011-09-06 13:48:16 -04006153 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006154
6155 shell->destroy_listener.notify = shell_destroy;
6156 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006157 shell->idle_listener.notify = idle_handler;
6158 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6159 shell->wake_listener.notify = wake_handler;
6160 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006161
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006162 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006163 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006164 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006165 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006166 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006167 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006168 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006169 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006170 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006171 ec->shell_interface.set_title = set_title;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07006172 ec->shell_interface.set_margin = set_margin;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006173
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006174 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6175 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006176 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6177 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006178 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006179
6180 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006181 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006182
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006183 if (input_panel_setup(shell) < 0)
6184 return -1;
6185
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006186 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006187
Daniel Stonedf8133b2013-11-19 11:37:14 +01006188 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6189 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6190
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006191 for (i = 0; i < shell->workspaces.num; i++) {
6192 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6193 if (pws == NULL)
6194 return -1;
6195
6196 *pws = workspace_create();
6197 if (*pws == NULL)
6198 return -1;
6199 }
6200 activate_workspace(shell, 0);
6201
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006202 weston_layer_init(&shell->minimized_layer, NULL);
6203
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006204 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006205 wl_list_init(&shell->workspaces.animation.link);
6206 shell->workspaces.animation.frame = animate_workspace_change_frame;
6207
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006208 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006209 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006210 return -1;
6211
Rafael Antognollie2a34552013-12-03 15:35:45 -02006212 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6213 shell, bind_xdg_shell) == NULL)
6214 return -1;
6215
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006216 if (wl_global_create(ec->wl_display,
6217 &desktop_shell_interface, 2,
6218 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006219 return -1;
6220
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006221 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6222 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006223 return -1;
6224
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006225 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6226 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006227 return -1;
6228
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006229 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006230
Xiong Zhang6b481422013-10-23 13:58:32 +08006231 setup_output_destroy_handler(ec, shell);
6232
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006233 loop = wl_display_get_event_loop(ec->wl_display);
6234 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006235
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006236 shell->screensaver.timer =
6237 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6238
Jason Ekstrand024177c2014-04-21 19:42:58 -05006239 wl_list_for_each(seat, &ec->seat_list, link)
6240 handle_seat_created(NULL, seat);
6241 shell->seat_create_listener.notify = handle_seat_created;
6242 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006243
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006244 screenshooter_create(ec);
6245
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006246 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006247
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006248 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006249
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006250 return 0;
6251}