blob: 47f6a596b3dd49318287d2b1dbe458de488bbf65 [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"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010039#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020040#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020041#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020042#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050043
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020045#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046
Jonas Ådahl04769742012-06-13 00:01:24 +020047struct focus_state {
48 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040049 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020050 struct weston_surface *keyboard_focus;
51 struct wl_list link;
52 struct wl_listener seat_destroy_listener;
53 struct wl_listener surface_destroy_listener;
54};
55
Philipp Brüschweiler88013572012-08-06 13:44:42 +020056struct input_panel_surface {
Jason Ekstrand51e5b142013-06-14 10:07:58 -050057 struct wl_resource *resource;
58 struct wl_signal destroy_signal;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010059
60 struct desktop_shell *shell;
61
Philipp Brüschweiler88013572012-08-06 13:44:42 +020062 struct wl_list link;
63 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050064 struct weston_view *view;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010065 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020066
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020067 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020068 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020069};
70
Xiong Zhang6b481422013-10-23 13:58:32 +080071struct shell_output {
72 struct desktop_shell *shell;
73 struct weston_output *output;
74 struct wl_listener destroy_listener;
75 struct wl_list link;
76};
77
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050078enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020079 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030081 SHELL_SURFACE_POPUP,
82 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020083};
84
Scott Moreauff1db4a2012-04-17 19:06:18 -060085struct ping_timer {
86 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060087 uint32_t serial;
88};
89
Philip Withnall648a4dd2013-11-25 18:01:44 +000090/*
91 * Surface stacking and ordering.
92 *
93 * This is handled using several linked lists of surfaces, organised into
94 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
95 * above all of the surfaces in the layer below. The set of layers is static and
96 * in the following order (top-most first):
97 * • Lock layer (only ever displayed on its own)
98 * • Cursor layer
99 * • Fullscreen layer
100 * • Panel layer
101 * • Input panel layer
102 * • Workspace layers
103 * • Background layer
104 *
105 * The list of layers may be manipulated to remove whole layers of surfaces from
106 * display. For example, when locking the screen, all layers except the lock
107 * layer are removed.
108 *
109 * A surface’s layer is modified on configuring the surface, in
110 * set_surface_type() (which is only called when the surface’s type change is
111 * _committed_). If a surface’s type changes (e.g. when making a window
112 * fullscreen) its layer changes too.
113 *
114 * In order to allow popup and transient surfaces to be correctly stacked above
115 * their parent surfaces, each surface tracks both its parent surface, and a
116 * linked list of its children. When a surface’s layer is updated, so are the
117 * layers of its children. Note that child surfaces are *not* the same as
118 * subsurfaces — child/parent surfaces are purely for maintaining stacking
119 * order.
120 *
121 * The children_link list of siblings of a surface (i.e. those surfaces which
122 * have the same parent) only contains weston_surfaces which have a
123 * shell_surface. Stacking is not implemented for non-shell_surface
124 * weston_surfaces. This means that the following implication does *not* hold:
125 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
126 */
127
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200128struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500129 struct wl_resource *resource;
130 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200131
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500132 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500133 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600134 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200135 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400136 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000137 struct wl_list children_list; /* child surfaces of this one */
138 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300139 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200140
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400141 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400142 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500143 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200144 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800145 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200146 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800147 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700148 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100149
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500150 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200151 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500152 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200153 } rotation;
154
155 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100156 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500157 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100158 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400159 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500160 } popup;
161
Alex Wu4539b082012-03-01 12:57:46 +0800162 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300163 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400164 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300165 } transient;
166
167 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800168 enum wl_shell_surface_fullscreen_method type;
169 struct weston_transform transform; /* matrix from x, y */
170 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500171 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800172 } fullscreen;
173
Scott Moreauff1db4a2012-04-17 19:06:18 -0600174 struct ping_timer *ping_timer;
175
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200176 struct weston_transform workspace_transform;
177
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500178 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500179 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200180 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100181 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400182
183 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200184
185 struct {
186 bool maximized;
187 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200188 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200189 } state, next_state; /* surface states */
190 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200191};
192
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300193struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400194 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300195 struct shell_surface *shsurf;
196 struct wl_listener shsurf_destroy_listener;
197};
198
Rusty Lynch1084da52013-08-15 09:10:08 -0700199struct shell_touch_grab {
200 struct weston_touch_grab grab;
201 struct shell_surface *shsurf;
202 struct wl_listener shsurf_destroy_listener;
203 struct weston_touch *touch;
204};
205
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300206struct weston_move_grab {
207 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100208 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209};
210
Rusty Lynch1084da52013-08-15 09:10:08 -0700211struct weston_touch_move_grab {
212 struct shell_touch_grab base;
213 wl_fixed_t dx, dy;
214};
215
Pekka Paalanen460099f2012-01-20 16:48:25 +0200216struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300217 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500218 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200219 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200220 float x;
221 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200222 } center;
223};
224
Giulio Camuffo5085a752013-03-25 21:42:45 +0100225struct shell_seat {
226 struct weston_seat *seat;
227 struct wl_listener seat_destroy_listener;
228
229 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400230 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100231 struct wl_list surfaces_list;
232 struct wl_client *client;
233 int32_t initial_up;
234 } popup_grab;
235};
236
Alex Wubd3354b2012-04-17 17:20:49 +0800237static struct desktop_shell *
238shell_surface_get_shell(struct shell_surface *shsurf);
239
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500240static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400241surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500242
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300243static void
244shell_fade_startup(struct desktop_shell *shell);
245
Philip Withnallbecb77e2013-11-25 18:01:30 +0000246static struct shell_seat *
247get_shell_seat(struct weston_seat *seat);
248
Philip Withnall648a4dd2013-11-25 18:01:44 +0000249static void
250shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
251
Alex Wubd3354b2012-04-17 17:20:49 +0800252static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200253shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
254
255static bool
256shell_surface_is_xdg_surface(struct shell_surface *shsurf);
257
258static bool
259shell_surface_is_xdg_popup(struct shell_surface *shsurf);
260
261static void
262shell_surface_set_parent(struct shell_surface *shsurf,
263 struct weston_surface *parent);
264
265static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800266shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
267{
268 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500269 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800270
271 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100272
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800274 return false;
275
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
277 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800278 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800280}
281
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500282static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400283destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284{
285 struct shell_grab *grab;
286
287 grab = container_of(listener, struct shell_grab,
288 shsurf_destroy_listener);
289
290 grab->shsurf = NULL;
291}
292
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800293struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294get_default_view(struct weston_surface *surface)
295{
296 struct shell_surface *shsurf;
297 struct weston_view *view;
298
299 if (!surface || wl_list_empty(&surface->views))
300 return NULL;
301
302 shsurf = get_shell_surface(surface);
303 if (shsurf)
304 return shsurf->view;
305
306 wl_list_for_each(view, &surface->views, surface_link)
307 if (weston_view_is_mapped(view))
308 return view;
309
310 return container_of(surface->views.next, struct weston_view, surface_link);
311}
312
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300313static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400314popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400315
316static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300317shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400318 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300319 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300321 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323 struct desktop_shell *shell = shsurf->shell;
324
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400325 popup_grab_end(pointer);
326
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327 grab->grab.interface = interface;
328 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400329 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500330 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400331 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700333 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400334 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400335 if (shell->child.desktop_shell) {
336 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
337 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 weston_pointer_set_focus(pointer,
339 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400340 wl_fixed_from_int(0),
341 wl_fixed_from_int(0));
342 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300343}
344
345static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300346shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300347{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700348 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400349 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700350 grab->shsurf->grabbed = 0;
351 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300352
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700353 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300354}
355
356static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700357shell_touch_grab_start(struct shell_touch_grab *grab,
358 const struct weston_touch_grab_interface *interface,
359 struct shell_surface *shsurf,
360 struct weston_touch *touch)
361{
362 struct desktop_shell *shell = shsurf->shell;
363
364 grab->grab.interface = interface;
365 grab->shsurf = shsurf;
366 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
367 wl_signal_add(&shsurf->destroy_signal,
368 &grab->shsurf_destroy_listener);
369
370 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700371 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700372
373 weston_touch_start_grab(touch, &grab->grab);
374 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375 weston_touch_set_focus(touch->seat,
376 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700377}
378
379static void
380shell_touch_grab_end(struct shell_touch_grab *grab)
381{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700382 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700383 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700384 grab->shsurf->grabbed = 0;
385 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700386
387 weston_touch_end_grab(grab->touch);
388}
389
390static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500391center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800392 struct weston_output *output);
393
Daniel Stone496ca172012-05-30 16:31:42 +0100394static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300395get_modifier(char *modifier)
396{
397 if (!modifier)
398 return MODIFIER_SUPER;
399
400 if (!strcmp("ctrl", modifier))
401 return MODIFIER_CTRL;
402 else if (!strcmp("alt", modifier))
403 return MODIFIER_ALT;
404 else if (!strcmp("super", modifier))
405 return MODIFIER_SUPER;
406 else
407 return MODIFIER_SUPER;
408}
409
Juan Zhaoe10d2792012-04-25 19:09:52 +0800410static enum animation_type
411get_animation_type(char *animation)
412{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800413 if (!strcmp("zoom", animation))
414 return ANIMATION_ZOOM;
415 else if (!strcmp("fade", animation))
416 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100417 else if (!strcmp("dim-layer", animation))
418 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800419 else
420 return ANIMATION_NONE;
421}
422
Alex Wu4539b082012-03-01 12:57:46 +0800423static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400424shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200425{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400426 struct weston_config_section *section;
427 int duration;
428 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200429
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400430 section = weston_config_get_section(shell->compositor->config,
431 "screensaver", NULL, NULL);
432 weston_config_section_get_string(section,
433 "path", &shell->screensaver.path, NULL);
434 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200435 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400436
437 section = weston_config_get_section(shell->compositor->config,
438 "shell", NULL, NULL);
439 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100440 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100441 shell->client = s;
442 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400443 "binding-modifier", &s, "super");
444 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200445 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400446 weston_config_section_get_string(section, "animation", &s, "none");
447 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200448 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700449 weston_config_section_get_string(section,
450 "startup-animation", &s, "fade");
451 shell->startup_animation_type = get_animation_type(s);
452 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700453 if (shell->startup_animation_type == ANIMATION_ZOOM)
454 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100455 weston_config_section_get_string(section, "focus-animation", &s, "none");
456 shell->focus_animation_type = get_animation_type(s);
457 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400458 weston_config_section_get_uint(section, "num-workspaces",
459 &shell->workspaces.num,
460 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200461}
462
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800463struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100464get_default_output(struct weston_compositor *compositor)
465{
466 return container_of(compositor->output_list.next,
467 struct weston_output, link);
468}
469
470
471/* no-op func for checking focus surface */
472static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600473focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100474{
475}
476
477static struct focus_surface *
478get_focus_surface(struct weston_surface *surface)
479{
480 if (surface->configure == focus_surface_configure)
481 return surface->configure_private;
482 else
483 return NULL;
484}
485
486static bool
487is_focus_surface (struct weston_surface *es)
488{
489 return (es->configure == focus_surface_configure);
490}
491
492static bool
493is_focus_view (struct weston_view *view)
494{
495 return is_focus_surface (view->surface);
496}
497
498static struct focus_surface *
499create_focus_surface(struct weston_compositor *ec,
500 struct weston_output *output)
501{
502 struct focus_surface *fsurf = NULL;
503 struct weston_surface *surface = NULL;
504
505 fsurf = malloc(sizeof *fsurf);
506 if (!fsurf)
507 return NULL;
508
509 fsurf->surface = weston_surface_create(ec);
510 surface = fsurf->surface;
511 if (surface == NULL) {
512 free(fsurf);
513 return NULL;
514 }
515
516 surface->configure = focus_surface_configure;
517 surface->output = output;
518 surface->configure_private = fsurf;
519
520 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100521 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100522
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600523 surface->width = output->width;
524 surface->height = output->height;
525 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100526 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
527 pixman_region32_fini(&surface->opaque);
528 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
529 output->width, output->height);
530 pixman_region32_fini(&surface->input);
531 pixman_region32_init(&surface->input);
532
533 wl_list_init(&fsurf->workspace_transform.link);
534
535 return fsurf;
536}
537
538static void
539focus_surface_destroy(struct focus_surface *fsurf)
540{
541 weston_surface_destroy(fsurf->surface);
542 free(fsurf);
543}
544
545static void
546focus_animation_done(struct weston_view_animation *animation, void *data)
547{
548 struct workspace *ws = data;
549
550 ws->focus_animation = NULL;
551}
552
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200553static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200554focus_state_destroy(struct focus_state *state)
555{
556 wl_list_remove(&state->seat_destroy_listener.link);
557 wl_list_remove(&state->surface_destroy_listener.link);
558 free(state);
559}
560
561static void
562focus_state_seat_destroy(struct wl_listener *listener, void *data)
563{
564 struct focus_state *state = container_of(listener,
565 struct focus_state,
566 seat_destroy_listener);
567
568 wl_list_remove(&state->link);
569 focus_state_destroy(state);
570}
571
572static void
573focus_state_surface_destroy(struct wl_listener *listener, void *data)
574{
575 struct focus_state *state = container_of(listener,
576 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400577 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400578 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 struct weston_surface *main_surface, *next;
580 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200581
Pekka Paalanen01388e22013-04-25 13:57:44 +0300582 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
583
Kristian Høgsberge3778222012-07-31 17:29:30 -0400584 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500585 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
586 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400587 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100588 if (is_focus_view(view))
589 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400590
Jason Ekstranda7af7042013-10-12 22:38:11 -0500591 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400592 break;
593 }
594
Pekka Paalanen01388e22013-04-25 13:57:44 +0300595 /* if the focus was a sub-surface, activate its main surface */
596 if (main_surface != state->keyboard_focus)
597 next = main_surface;
598
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100599 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400600 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100601 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400602 activate(shell, next, state->seat);
603 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100604 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
605 if (state->ws->focus_animation)
606 weston_view_animation_destroy(state->ws->focus_animation);
607
608 state->ws->focus_animation = weston_fade_run(
609 state->ws->fsurf_front->view,
610 state->ws->fsurf_front->view->alpha, 0.0, 300,
611 focus_animation_done, state->ws);
612 }
613
Kristian Høgsberge3778222012-07-31 17:29:30 -0400614 wl_list_remove(&state->link);
615 focus_state_destroy(state);
616 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200617}
618
619static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400620focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200621{
Jonas Ådahl04769742012-06-13 00:01:24 +0200622 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200623
624 state = malloc(sizeof *state);
625 if (state == NULL)
626 return NULL;
627
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400629 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200630 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400631 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200632
633 state->seat_destroy_listener.notify = focus_state_seat_destroy;
634 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400635 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200636 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400637 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200638
639 return state;
640}
641
Jonas Ådahl8538b222012-08-29 22:13:03 +0200642static struct focus_state *
643ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
644{
645 struct workspace *ws = get_current_workspace(shell);
646 struct focus_state *state;
647
648 wl_list_for_each(state, &ws->focus_list, link)
649 if (state->seat == seat)
650 break;
651
652 if (&state->link == &ws->focus_list)
653 state = focus_state_create(seat, ws);
654
655 return state;
656}
657
Jonas Ådahl04769742012-06-13 00:01:24 +0200658static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400659restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200660{
661 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400662 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200663
664 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400665 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200666
Kristian Høgsberge3148752013-05-06 23:19:49 -0400667 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200668 }
669}
670
671static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200672replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
673 struct weston_seat *seat)
674{
675 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400676 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200677
678 wl_list_for_each(state, &ws->focus_list, link) {
679 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400680 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500681 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200682 return;
683 }
684 }
685}
686
687static void
688drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
689 struct weston_surface *surface)
690{
691 struct focus_state *state;
692
693 wl_list_for_each(state, &ws->focus_list, link)
694 if (state->keyboard_focus == surface)
695 state->keyboard_focus = NULL;
696}
697
698static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100699animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
700 struct weston_view *from, struct weston_view *to)
701{
702 struct weston_output *output;
703 bool focus_surface_created = false;
704
705 /* FIXME: Only support dim animation using two layers */
706 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
707 return;
708
709 output = get_default_output(shell->compositor);
710 if (ws->fsurf_front == NULL && (from || to)) {
711 ws->fsurf_front = create_focus_surface(shell->compositor, output);
712 ws->fsurf_back = create_focus_surface(shell->compositor, output);
713 ws->fsurf_front->view->alpha = 0.0;
714 ws->fsurf_back->view->alpha = 0.0;
715 focus_surface_created = true;
716 } else {
717 wl_list_remove(&ws->fsurf_front->view->layer_link);
718 wl_list_remove(&ws->fsurf_back->view->layer_link);
719 }
720
721 if (ws->focus_animation) {
722 weston_view_animation_destroy(ws->focus_animation);
723 ws->focus_animation = NULL;
724 }
725
726 if (to)
727 wl_list_insert(&to->layer_link,
728 &ws->fsurf_front->view->layer_link);
729 else if (from)
730 wl_list_insert(&ws->layer.view_list,
731 &ws->fsurf_front->view->layer_link);
732
733 if (focus_surface_created) {
734 ws->focus_animation = weston_fade_run(
735 ws->fsurf_front->view,
736 ws->fsurf_front->view->alpha, 0.6, 300,
737 focus_animation_done, ws);
738 } else if (from) {
739 wl_list_insert(&from->layer_link,
740 &ws->fsurf_back->view->layer_link);
741 ws->focus_animation = weston_stable_fade_run(
742 ws->fsurf_front->view, 0.0,
743 ws->fsurf_back->view, 0.6,
744 focus_animation_done, ws);
745 } else if (to) {
746 wl_list_insert(&ws->layer.view_list,
747 &ws->fsurf_back->view->layer_link);
748 ws->focus_animation = weston_stable_fade_run(
749 ws->fsurf_front->view, 0.0,
750 ws->fsurf_back->view, 0.6,
751 focus_animation_done, ws);
752 }
753}
754
755static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200756workspace_destroy(struct workspace *ws)
757{
Jonas Ådahl04769742012-06-13 00:01:24 +0200758 struct focus_state *state, *next;
759
760 wl_list_for_each_safe(state, next, &ws->focus_list, link)
761 focus_state_destroy(state);
762
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100763 if (ws->fsurf_front)
764 focus_surface_destroy(ws->fsurf_front);
765 if (ws->fsurf_back)
766 focus_surface_destroy(ws->fsurf_back);
767
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200768 free(ws);
769}
770
Jonas Ådahl04769742012-06-13 00:01:24 +0200771static void
772seat_destroyed(struct wl_listener *listener, void *data)
773{
774 struct weston_seat *seat = data;
775 struct focus_state *state, *next;
776 struct workspace *ws = container_of(listener,
777 struct workspace,
778 seat_destroyed_listener);
779
780 wl_list_for_each_safe(state, next, &ws->focus_list, link)
781 if (state->seat == seat)
782 wl_list_remove(&state->link);
783}
784
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200785static struct workspace *
786workspace_create(void)
787{
788 struct workspace *ws = malloc(sizeof *ws);
789 if (ws == NULL)
790 return NULL;
791
792 weston_layer_init(&ws->layer, NULL);
793
Jonas Ådahl04769742012-06-13 00:01:24 +0200794 wl_list_init(&ws->focus_list);
795 wl_list_init(&ws->seat_destroyed_listener.link);
796 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100797 ws->fsurf_front = NULL;
798 ws->fsurf_back = NULL;
799 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200800
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200801 return ws;
802}
803
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200804static int
805workspace_is_empty(struct workspace *ws)
806{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500807 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200808}
809
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200810static struct workspace *
811get_workspace(struct desktop_shell *shell, unsigned int index)
812{
813 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200814 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200815 pws += index;
816 return *pws;
817}
818
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800819struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200820get_current_workspace(struct desktop_shell *shell)
821{
822 return get_workspace(shell, shell->workspaces.current);
823}
824
825static void
826activate_workspace(struct desktop_shell *shell, unsigned int index)
827{
828 struct workspace *ws;
829
830 ws = get_workspace(shell, index);
831 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
832
833 shell->workspaces.current = index;
834}
835
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200836static unsigned int
837get_output_height(struct weston_output *output)
838{
839 return abs(output->region.extents.y1 - output->region.extents.y2);
840}
841
842static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200844{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200845 struct weston_transform *transform;
846
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100847 if (is_focus_view(view)) {
848 struct focus_surface *fsurf = get_focus_surface(view->surface);
849 transform = &fsurf->workspace_transform;
850 } else {
851 struct shell_surface *shsurf = get_shell_surface(view->surface);
852 transform = &shsurf->workspace_transform;
853 }
854
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500856 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200858
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 weston_matrix_init(&transform->matrix);
860 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200861 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500862 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200863}
864
865static void
866workspace_translate_out(struct workspace *ws, double fraction)
867{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500868 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200869 unsigned int height;
870 double d;
871
Jason Ekstranda7af7042013-10-12 22:38:11 -0500872 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
873 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200874 d = height * fraction;
875
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100876 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200877 }
878}
879
880static void
881workspace_translate_in(struct workspace *ws, double fraction)
882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200884 unsigned int height;
885 double d;
886
Jason Ekstranda7af7042013-10-12 22:38:11 -0500887 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
888 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200889
890 if (fraction > 0)
891 d = -(height - height * fraction);
892 else
893 d = height + height * fraction;
894
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100895 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200896 }
897}
898
899static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200900broadcast_current_workspace_state(struct desktop_shell *shell)
901{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700902 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200903
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700904 wl_resource_for_each(resource, &shell->workspaces.client_list)
905 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200906 shell->workspaces.current,
907 shell->workspaces.num);
908}
909
910static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200911reverse_workspace_change_animation(struct desktop_shell *shell,
912 unsigned int index,
913 struct workspace *from,
914 struct workspace *to)
915{
916 shell->workspaces.current = index;
917
918 shell->workspaces.anim_to = to;
919 shell->workspaces.anim_from = from;
920 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
921 shell->workspaces.anim_timestamp = 0;
922
Scott Moreau4272e632012-08-13 09:58:41 -0600923 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924}
925
926static void
927workspace_deactivate_transforms(struct workspace *ws)
928{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100930 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100933 if (is_focus_view(view)) {
934 struct focus_surface *fsurf = get_focus_surface(view->surface);
935 transform = &fsurf->workspace_transform;
936 } else {
937 struct shell_surface *shsurf = get_shell_surface(view->surface);
938 transform = &shsurf->workspace_transform;
939 }
940
941 if (!wl_list_empty(&transform->link)) {
942 wl_list_remove(&transform->link);
943 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200944 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200946 }
947}
948
949static void
950finish_workspace_change_animation(struct desktop_shell *shell,
951 struct workspace *from,
952 struct workspace *to)
953{
Scott Moreau4272e632012-08-13 09:58:41 -0600954 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200955
956 wl_list_remove(&shell->workspaces.animation.link);
957 workspace_deactivate_transforms(from);
958 workspace_deactivate_transforms(to);
959 shell->workspaces.anim_to = NULL;
960
961 wl_list_remove(&shell->workspaces.anim_from->layer.link);
962}
963
964static void
965animate_workspace_change_frame(struct weston_animation *animation,
966 struct weston_output *output, uint32_t msecs)
967{
968 struct desktop_shell *shell =
969 container_of(animation, struct desktop_shell,
970 workspaces.animation);
971 struct workspace *from = shell->workspaces.anim_from;
972 struct workspace *to = shell->workspaces.anim_to;
973 uint32_t t;
974 double x, y;
975
976 if (workspace_is_empty(from) && workspace_is_empty(to)) {
977 finish_workspace_change_animation(shell, from, to);
978 return;
979 }
980
981 if (shell->workspaces.anim_timestamp == 0) {
982 if (shell->workspaces.anim_current == 0.0)
983 shell->workspaces.anim_timestamp = msecs;
984 else
985 shell->workspaces.anim_timestamp =
986 msecs -
987 /* Invers of movement function 'y' below. */
988 (asin(1.0 - shell->workspaces.anim_current) *
989 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
990 M_2_PI);
991 }
992
993 t = msecs - shell->workspaces.anim_timestamp;
994
995 /*
996 * x = [0, π/2]
997 * y(x) = sin(x)
998 */
999 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1000 y = sin(x);
1001
1002 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001003 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004
1005 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1006 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1007 shell->workspaces.anim_current = y;
1008
Scott Moreau4272e632012-08-13 09:58:41 -06001009 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001011 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001012 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013}
1014
1015static void
1016animate_workspace_change(struct desktop_shell *shell,
1017 unsigned int index,
1018 struct workspace *from,
1019 struct workspace *to)
1020{
1021 struct weston_output *output;
1022
1023 int dir;
1024
1025 if (index > shell->workspaces.current)
1026 dir = -1;
1027 else
1028 dir = 1;
1029
1030 shell->workspaces.current = index;
1031
1032 shell->workspaces.anim_dir = dir;
1033 shell->workspaces.anim_from = from;
1034 shell->workspaces.anim_to = to;
1035 shell->workspaces.anim_current = 0.0;
1036 shell->workspaces.anim_timestamp = 0;
1037
1038 output = container_of(shell->compositor->output_list.next,
1039 struct weston_output, link);
1040 wl_list_insert(&output->animation_list,
1041 &shell->workspaces.animation.link);
1042
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001043 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044
1045 workspace_translate_in(to, 0);
1046
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001047 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001048
Scott Moreau4272e632012-08-13 09:58:41 -06001049 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001050}
1051
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001052static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001053update_workspace(struct desktop_shell *shell, unsigned int index,
1054 struct workspace *from, struct workspace *to)
1055{
1056 shell->workspaces.current = index;
1057 wl_list_insert(&from->layer.link, &to->layer.link);
1058 wl_list_remove(&from->layer.link);
1059}
1060
1061static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001062change_workspace(struct desktop_shell *shell, unsigned int index)
1063{
1064 struct workspace *from;
1065 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001066 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001067
1068 if (index == shell->workspaces.current)
1069 return;
1070
1071 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001073 return;
1074
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001075 from = get_current_workspace(shell);
1076 to = get_workspace(shell, index);
1077
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078 if (shell->workspaces.anim_from == to &&
1079 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001080 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001081 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001082 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001083 return;
1084 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001085
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086 if (shell->workspaces.anim_to != NULL)
1087 finish_workspace_change_animation(shell,
1088 shell->workspaces.anim_from,
1089 shell->workspaces.anim_to);
1090
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001091 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001092
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001093 if (shell->focus_animation_type != ANIMATION_NONE) {
1094 wl_list_for_each(state, &from->focus_list, link)
1095 if (state->keyboard_focus)
1096 animate_focus_change(shell, from,
1097 get_default_view(state->keyboard_focus), NULL);
1098
1099 wl_list_for_each(state, &to->focus_list, link)
1100 if (state->keyboard_focus)
1101 animate_focus_change(shell, to,
1102 NULL, get_default_view(state->keyboard_focus));
1103 }
1104
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001105 if (workspace_is_empty(to) && workspace_is_empty(from))
1106 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 else
1108 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001109
1110 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001111}
1112
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001113static bool
1114workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1115{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001117 struct wl_list *e;
1118
1119 if (wl_list_empty(list))
1120 return false;
1121
1122 e = list->next;
1123
1124 if (e->next != list)
1125 return false;
1126
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001128}
1129
1130static void
Philip Withnall659163d2013-11-25 18:01:36 +00001131move_surface_to_workspace(struct desktop_shell *shell,
1132 struct shell_surface *shsurf,
1133 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001134{
1135 struct workspace *from;
1136 struct workspace *to;
1137 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001138 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001139 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001140
1141 if (workspace == shell->workspaces.current)
1142 return;
1143
Philip Withnall659163d2013-11-25 18:01:36 +00001144 view = get_default_view(shsurf->surface);
1145 if (!view)
1146 return;
1147
1148 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1149
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001150 if (workspace >= shell->workspaces.num)
1151 workspace = shell->workspaces.num - 1;
1152
Jonas Ådahle9d22502012-08-29 22:13:01 +02001153 from = get_current_workspace(shell);
1154 to = get_workspace(shell, workspace);
1155
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 wl_list_remove(&view->layer_link);
1157 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001158
Philip Withnall648a4dd2013-11-25 18:01:44 +00001159 shell_surface_update_child_surface_layers(shsurf);
1160
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001162 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1163 if (!seat->keyboard)
1164 continue;
1165
1166 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001168 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001169 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001170
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001172}
1173
1174static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001176 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001177 unsigned int index)
1178{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001179 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001181 struct shell_surface *shsurf;
1182 struct workspace *from;
1183 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001184 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001185
Pekka Paalanen01388e22013-04-25 13:57:44 +03001186 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 view = get_default_view(surface);
1188 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001189 index == shell->workspaces.current ||
1190 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001191 return;
1192
1193 from = get_current_workspace(shell);
1194 to = get_workspace(shell, index);
1195
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 wl_list_remove(&view->layer_link);
1197 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198
Philip Withnall659163d2013-11-25 18:01:36 +00001199 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001200 if (shsurf != NULL)
1201 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001202
Jonas Ådahle9d22502012-08-29 22:13:01 +02001203 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204 drop_focus_state(shell, from, surface);
1205
1206 if (shell->workspaces.anim_from == to &&
1207 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001208 wl_list_remove(&to->layer.link);
1209 wl_list_insert(from->layer.link.prev, &to->layer.link);
1210
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001211 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001212 broadcast_current_workspace_state(shell);
1213
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 return;
1215 }
1216
1217 if (shell->workspaces.anim_to != NULL)
1218 finish_workspace_change_animation(shell,
1219 shell->workspaces.anim_from,
1220 shell->workspaces.anim_to);
1221
1222 if (workspace_is_empty(from) &&
1223 workspace_has_only(to, surface))
1224 update_workspace(shell, index, from, to);
1225 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001226 if (shsurf != NULL &&
1227 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228 wl_list_insert(&shell->workspaces.anim_sticky_list,
1229 &shsurf->workspace_transform.link);
1230
1231 animate_workspace_change(shell, index, from, to);
1232 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001233
1234 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001235
1236 state = ensure_focus_state(shell, seat);
1237 if (state != NULL)
1238 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001239}
1240
1241static void
1242workspace_manager_move_surface(struct wl_client *client,
1243 struct wl_resource *resource,
1244 struct wl_resource *surface_resource,
1245 uint32_t workspace)
1246{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001247 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001248 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001249 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001250 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001251 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001252
Pekka Paalanen01388e22013-04-25 13:57:44 +03001253 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001254 shell_surface = get_shell_surface(main_surface);
1255 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001257
1258 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001259}
1260
1261static const struct workspace_manager_interface workspace_manager_implementation = {
1262 workspace_manager_move_surface,
1263};
1264
1265static void
1266unbind_resource(struct wl_resource *resource)
1267{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001268 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001269}
1270
1271static void
1272bind_workspace_manager(struct wl_client *client,
1273 void *data, uint32_t version, uint32_t id)
1274{
1275 struct desktop_shell *shell = data;
1276 struct wl_resource *resource;
1277
Jason Ekstranda85118c2013-06-27 20:17:02 -05001278 resource = wl_resource_create(client,
1279 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001280
1281 if (resource == NULL) {
1282 weston_log("couldn't add workspace manager object");
1283 return;
1284 }
1285
Jason Ekstranda85118c2013-06-27 20:17:02 -05001286 wl_resource_set_implementation(resource,
1287 &workspace_manager_implementation,
1288 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001289 wl_list_insert(&shell->workspaces.client_list,
1290 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001291
1292 workspace_manager_send_state(resource,
1293 shell->workspaces.current,
1294 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295}
1296
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001297static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001298touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1299 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1300{
1301}
1302
1303static void
1304touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1305{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001306 struct weston_touch_move_grab *move =
1307 (struct weston_touch_move_grab *) container_of(
1308 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001309
Jonas Ådahl9484b692013-12-02 22:05:03 +01001310 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001311 shell_touch_grab_end(&move->base);
1312 free(move);
1313 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001314}
1315
1316static void
1317touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1318 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1319{
1320 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1321 struct shell_surface *shsurf = move->base.shsurf;
1322 struct weston_surface *es;
1323 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1324 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1325
1326 if (!shsurf)
1327 return;
1328
1329 es = shsurf->surface;
1330
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001331 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001332
1333 weston_compositor_schedule_repaint(es->compositor);
1334}
1335
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001336static void
1337touch_move_grab_cancel(struct weston_touch_grab *grab)
1338{
1339 struct weston_touch_move_grab *move =
1340 (struct weston_touch_move_grab *) container_of(
1341 grab, struct shell_touch_grab, grab);
1342
1343 shell_touch_grab_end(&move->base);
1344 free(move);
1345}
1346
Rusty Lynch1084da52013-08-15 09:10:08 -07001347static const struct weston_touch_grab_interface touch_move_grab_interface = {
1348 touch_move_grab_down,
1349 touch_move_grab_up,
1350 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001351 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001352};
1353
1354static int
1355surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1356{
1357 struct weston_touch_move_grab *move;
1358
1359 if (!shsurf)
1360 return -1;
1361
Rafael Antognolli03b16592013-12-03 15:35:42 -02001362 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001363 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001364 if (shsurf->grabbed)
1365 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001366
1367 move = malloc(sizeof *move);
1368 if (!move)
1369 return -1;
1370
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001372 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001374 seat->touch->grab_y;
1375
1376 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1377 seat->touch);
1378
1379 return 0;
1380}
1381
1382static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001383noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001384{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001385}
1386
1387static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001388move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1389 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001390{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001391 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001392 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001393 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001394 int dx, dy;
1395
1396 weston_pointer_move(pointer, x, y);
1397 dx = wl_fixed_to_int(pointer->x + move->dx);
1398 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001399
1400 if (!shsurf)
1401 return;
1402
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001403 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001404
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001406}
1407
1408static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001409move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001410 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001411{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001412 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1413 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001414 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001415 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001416
Daniel Stone4dbadb12012-05-30 16:31:51 +01001417 if (pointer->button_count == 0 &&
1418 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001419 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001420 free(grab);
1421 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001422}
1423
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001424static void
1425move_grab_cancel(struct weston_pointer_grab *grab)
1426{
1427 struct shell_grab *shell_grab =
1428 container_of(grab, struct shell_grab, grab);
1429
1430 shell_grab_end(shell_grab);
1431 free(grab);
1432}
1433
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001434static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001435 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001436 move_grab_motion,
1437 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001438 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001439};
1440
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001441static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001442surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001443{
1444 struct weston_move_grab *move;
1445
1446 if (!shsurf)
1447 return -1;
1448
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001449 if (shsurf->grabbed)
1450 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001451 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001452 return 0;
1453
1454 move = malloc(sizeof *move);
1455 if (!move)
1456 return -1;
1457
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001459 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001461 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001462
1463 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001464 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001465
1466 return 0;
1467}
1468
1469static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001470common_surface_move(struct wl_resource *resource,
1471 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001472{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001473 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001474 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001475 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001476
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001477 if (seat->pointer &&
1478 seat->pointer->button_count > 0 &&
1479 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001481 if ((surface == shsurf->surface) &&
1482 (surface_move(shsurf, seat) < 0))
1483 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001484 } else if (seat->touch &&
1485 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001487 if ((surface == shsurf->surface) &&
1488 (surface_touch_move(shsurf, seat) < 0))
1489 wl_resource_post_no_memory(resource);
1490 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001491}
1492
Rafael Antognollie2a34552013-12-03 15:35:45 -02001493static void
1494shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1495 struct wl_resource *seat_resource, uint32_t serial)
1496{
1497 common_surface_move(resource, seat_resource, serial);
1498}
1499
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001500struct weston_resize_grab {
1501 struct shell_grab base;
1502 uint32_t edges;
1503 int32_t width, height;
1504};
1505
1506static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001507resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1508 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001509{
1510 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001511 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001512 struct shell_surface *shsurf = resize->base.shsurf;
1513 int32_t width, height;
1514 wl_fixed_t from_x, from_y;
1515 wl_fixed_t to_x, to_y;
1516
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001517 weston_pointer_move(pointer, x, y);
1518
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001519 if (!shsurf)
1520 return;
1521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 weston_view_from_global_fixed(shsurf->view,
1523 pointer->grab_x, pointer->grab_y,
1524 &from_x, &from_y);
1525 weston_view_from_global_fixed(shsurf->view,
1526 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001527
1528 width = resize->width;
1529 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1530 width += wl_fixed_to_int(from_x - to_x);
1531 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1532 width += wl_fixed_to_int(to_x - from_x);
1533 }
1534
1535 height = resize->height;
1536 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1537 height += wl_fixed_to_int(from_y - to_y);
1538 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1539 height += wl_fixed_to_int(to_y - from_y);
1540 }
1541
1542 shsurf->client->send_configure(shsurf->surface,
1543 resize->edges, width, height);
1544}
1545
1546static void
1547send_configure(struct weston_surface *surface,
1548 uint32_t edges, int32_t width, int32_t height)
1549{
1550 struct shell_surface *shsurf = get_shell_surface(surface);
1551
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001552 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553 edges, width, height);
1554}
1555
1556static const struct weston_shell_client shell_client = {
1557 send_configure
1558};
1559
1560static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001561resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562 uint32_t time, uint32_t button, uint32_t state_w)
1563{
1564 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001565 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001566 enum wl_pointer_button_state state = state_w;
1567
1568 if (pointer->button_count == 0 &&
1569 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1570 shell_grab_end(&resize->base);
1571 free(grab);
1572 }
1573}
1574
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001575static void
1576resize_grab_cancel(struct weston_pointer_grab *grab)
1577{
1578 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1579
1580 shell_grab_end(&resize->base);
1581 free(grab);
1582}
1583
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001584static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001585 noop_grab_focus,
1586 resize_grab_motion,
1587 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001588 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001589};
1590
Giulio Camuffob8366642013-04-25 13:57:46 +03001591/*
1592 * Returns the bounding box of a surface and all its sub-surfaces,
1593 * in the surface coordinates system. */
1594static void
1595surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1596 int32_t *y, int32_t *w, int32_t *h) {
1597 pixman_region32_t region;
1598 pixman_box32_t *box;
1599 struct weston_subsurface *subsurface;
1600
1601 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602 surface->width,
1603 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001604
1605 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1606 pixman_region32_union_rect(&region, &region,
1607 subsurface->position.x,
1608 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 subsurface->surface->width,
1610 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001611 }
1612
1613 box = pixman_region32_extents(&region);
1614 if (x)
1615 *x = box->x1;
1616 if (y)
1617 *y = box->y1;
1618 if (w)
1619 *w = box->x2 - box->x1;
1620 if (h)
1621 *h = box->y2 - box->y1;
1622
1623 pixman_region32_fini(&region);
1624}
1625
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001626static int
1627surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001628 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629{
1630 struct weston_resize_grab *resize;
1631
Rafael Antognolli03b16592013-12-03 15:35:42 -02001632 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 return 0;
1634
1635 if (edges == 0 || edges > 15 ||
1636 (edges & 3) == 3 || (edges & 12) == 12)
1637 return 0;
1638
1639 resize = malloc(sizeof *resize);
1640 if (!resize)
1641 return -1;
1642
1643 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001644 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1645 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646
1647 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001648 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001649
1650 return 0;
1651}
1652
1653static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001654common_surface_resize(struct wl_resource *resource,
1655 struct wl_resource *seat_resource, uint32_t serial,
1656 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001658 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001659 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001660 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001661
Rafael Antognolli03b16592013-12-03 15:35:42 -02001662 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663 return;
1664
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001666 if (seat->pointer->button_count == 0 ||
1667 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001668 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001669 return;
1670
Kristian Høgsberge3148752013-05-06 23:19:49 -04001671 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001672 wl_resource_post_no_memory(resource);
1673}
1674
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001675static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001676shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1677 struct wl_resource *seat_resource, uint32_t serial,
1678 uint32_t edges)
1679{
1680 common_surface_resize(resource, seat_resource, serial, edges);
1681}
1682
1683static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001684end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1685
1686static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001687busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001688{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001689 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001690 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001692 wl_fixed_t sx, sy;
1693
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694 view = weston_compositor_pick_view(pointer->seat->compositor,
1695 pointer->x, pointer->y,
1696 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001697
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001699 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001700}
1701
1702static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001703busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1704 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001705{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001706 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001707}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001708
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001709static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001710busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001711 uint32_t time, uint32_t button, uint32_t state)
1712{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001713 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001714 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001715 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001716
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001717 if (shsurf && button == BTN_LEFT && state) {
1718 activate(shsurf->shell, shsurf->surface, seat);
1719 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001720 } else if (shsurf && button == BTN_RIGHT && state) {
1721 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001722 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001723 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001724}
1725
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001726static void
1727busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1728{
1729 struct shell_grab *grab = (struct shell_grab *) base;
1730
1731 shell_grab_end(grab);
1732 free(grab);
1733}
1734
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001735static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001736 busy_cursor_grab_focus,
1737 busy_cursor_grab_motion,
1738 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001739 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001740};
1741
1742static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001743set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001744{
1745 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001746
1747 grab = malloc(sizeof *grab);
1748 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001749 return;
1750
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001751 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1752 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001753}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001754
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001755static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001756end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001757{
1758 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1759
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001760 if (grab->grab.interface == &busy_cursor_grab_interface &&
1761 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001762 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001765}
1766
Scott Moreau9521d5e2012-04-19 13:06:17 -06001767static void
1768ping_timer_destroy(struct shell_surface *shsurf)
1769{
1770 if (!shsurf || !shsurf->ping_timer)
1771 return;
1772
1773 if (shsurf->ping_timer->source)
1774 wl_event_source_remove(shsurf->ping_timer->source);
1775
1776 free(shsurf->ping_timer);
1777 shsurf->ping_timer = NULL;
1778}
1779
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001780static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001781ping_timeout_handler(void *data)
1782{
1783 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001784 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001785
Scott Moreau9521d5e2012-04-19 13:06:17 -06001786 /* Client is not responding */
1787 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001788
1789 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001790 if (seat->pointer->focus &&
1791 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001792 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001793
1794 return 1;
1795}
1796
1797static void
1798ping_handler(struct weston_surface *surface, uint32_t serial)
1799{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001800 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001801 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001803
1804 if (!shsurf)
1805 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001806 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001807 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001808
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001809 if (shsurf->surface == shsurf->shell->grab_surface)
1810 return;
1811
Scott Moreauff1db4a2012-04-17 19:06:18 -06001812 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001813 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001814 if (!shsurf->ping_timer)
1815 return;
1816
1817 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001818 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1819 shsurf->ping_timer->source =
1820 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1821 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1822
Rafael Antognollie2a34552013-12-03 15:35:45 -02001823 if (shell_surface_is_wl_shell_surface(shsurf))
1824 wl_shell_surface_send_ping(shsurf->resource, serial);
1825 else if (shell_surface_is_xdg_surface(shsurf))
1826 xdg_surface_send_ping(shsurf->resource, serial);
1827 else if (shell_surface_is_xdg_popup(shsurf))
1828 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001829 }
1830}
1831
1832static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001833handle_pointer_focus(struct wl_listener *listener, void *data)
1834{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001835 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001837 struct weston_compositor *compositor;
1838 struct shell_surface *shsurf;
1839 uint32_t serial;
1840
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001842 return;
1843
Jason Ekstranda7af7042013-10-12 22:38:11 -05001844 compositor = view->surface->compositor;
1845 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001847 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848 set_busy_cursor(shsurf, pointer);
1849 } else {
1850 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001851 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001852 }
1853}
1854
1855static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001856create_pointer_focus_listener(struct weston_seat *seat)
1857{
1858 struct wl_listener *listener;
1859
Kristian Høgsberge3148752013-05-06 23:19:49 -04001860 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001861 return;
1862
1863 listener = malloc(sizeof *listener);
1864 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001865 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001866}
1867
1868static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001869xdg_surface_set_transient_for(struct wl_client *client,
1870 struct wl_resource *resource,
1871 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001872{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001873 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02001874 struct weston_surface *parent =
1875 wl_resource_get_user_data(parent_resource);
1876
1877 shell_surface_set_parent(shsurf, parent);
1878}
1879
1880static void
1881surface_pong(struct shell_surface *shsurf, uint32_t serial)
1882{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001884 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001885
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001886 if (shsurf->ping_timer == NULL)
1887 /* Just ignore unsolicited pong. */
1888 return;
1889
Scott Moreauff1db4a2012-04-17 19:06:18 -06001890 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001892 wl_list_for_each(seat, &ec->seat_list, link) {
1893 if(seat->pointer)
1894 end_busy_cursor(shsurf, seat->pointer);
1895 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001896 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001897 }
1898}
1899
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001900static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001901shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1902 uint32_t serial)
1903{
1904 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1905
1906 surface_pong(shsurf, serial);
1907
1908}
1909
1910static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001911set_title(struct shell_surface *shsurf, const char *title)
1912{
1913 free(shsurf->title);
1914 shsurf->title = strdup(title);
1915}
1916
1917static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001918shell_surface_set_title(struct wl_client *client,
1919 struct wl_resource *resource, const char *title)
1920{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001921 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001922
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001923 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001924}
1925
1926static void
1927shell_surface_set_class(struct wl_client *client,
1928 struct wl_resource *resource, const char *class)
1929{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001930 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001931
1932 free(shsurf->class);
1933 shsurf->class = strdup(class);
1934}
1935
Alex Wu4539b082012-03-01 12:57:46 +08001936static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001937restore_output_mode(struct weston_output *output)
1938{
Hardening57388e42013-09-18 23:56:36 +02001939 if (output->current_mode != output->original_mode ||
1940 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001941 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001942 output->original_mode,
1943 output->original_scale,
1944 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001945}
1946
1947static void
1948restore_all_output_modes(struct weston_compositor *compositor)
1949{
1950 struct weston_output *output;
1951
1952 wl_list_for_each(output, &compositor->output_list, link)
1953 restore_output_mode(output);
1954}
1955
Philip Withnallbecb77e2013-11-25 18:01:30 +00001956static int
1957get_output_panel_height(struct desktop_shell *shell,
1958 struct weston_output *output)
1959{
1960 struct weston_view *view;
1961 int panel_height = 0;
1962
1963 if (!output)
1964 return 0;
1965
1966 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1967 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001968 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001969 break;
1970 }
1971 }
1972
1973 return panel_height;
1974}
1975
Philip Withnall07926d92013-11-25 18:01:40 +00001976/* The surface will be inserted into the list immediately after the link
1977 * returned by this function (i.e. will be stacked immediately above the
1978 * returned link). */
1979static struct wl_list *
1980shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1981{
1982 struct workspace *ws;
1983
1984 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00001985 case SHELL_SURFACE_POPUP: {
1986 /* Popups should go at the front of the workspace of their
1987 * parent surface, rather than just in front of the parent. This
1988 * fixes the situation where there are two top-level windows:
1989 * - Above
1990 * - Below
1991 * and a pop-up menu is created for 'Below'. We want:
1992 * - Popup
1993 * - Above
1994 * - Below
1995 * not:
1996 * - Above
1997 * - Popup
1998 * - Below
1999 */
2000 struct shell_surface *parent_shsurf;
2001
2002 parent_shsurf = get_shell_surface(shsurf->parent);
2003 if (parent_shsurf != NULL)
2004 return shell_surface_calculate_layer_link(parent_shsurf);
2005
2006 break;
2007 }
2008
Rafael Antognolli03b16592013-12-03 15:35:42 -02002009 case SHELL_SURFACE_TOPLEVEL: {
Rafael Antognollied207b42013-12-03 15:35:43 -02002010 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002011 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002012 } else if (shsurf->parent) {
2013 /* Move the surface to its parent layer so that
2014 * surfaces which are transient for fullscreen surfaces
2015 * don't get hidden by the fullscreen surfaces.
2016 * However, unlike popups, transient surfaces are
2017 * stacked in front of their parent but not in front of
2018 * other surfaces of the same type. */
2019 struct weston_view *parent;
2020
2021 /* TODO: Handle a parent with multiple views */
2022 parent = get_default_view(shsurf->parent);
2023 if (parent)
2024 return parent->layer_link.prev;
2025 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002026 break;
2027 }
Philip Withnall07926d92013-11-25 18:01:40 +00002028
2029 case SHELL_SURFACE_XWAYLAND:
Philip Withnall07926d92013-11-25 18:01:40 +00002030 case SHELL_SURFACE_NONE:
2031 default:
2032 /* Go to the fallback, below. */
2033 break;
2034 }
2035
2036 /* Move the surface to a normal workspace layer so that surfaces
2037 * which were previously fullscreen or transient are no longer
2038 * rendered on top. */
2039 ws = get_current_workspace(shsurf->shell);
2040 return &ws->layer.view_list;
2041}
2042
Philip Withnall648a4dd2013-11-25 18:01:44 +00002043static void
2044shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2045{
2046 struct shell_surface *child;
2047
2048 /* Move the child layers to the same workspace as shsurf. They will be
2049 * stacked above shsurf. */
2050 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2051 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2052 weston_view_geometry_dirty(child->view);
2053 wl_list_remove(&child->view->layer_link);
2054 wl_list_insert(shsurf->view->layer_link.prev,
2055 &child->view->layer_link);
2056 weston_view_geometry_dirty(child->view);
2057 weston_surface_damage(child->surface);
2058
2059 /* Recurse. We don’t expect this to recurse very far (if
2060 * at all) because that would imply we have transient
2061 * (or popup) children of transient surfaces, which
2062 * would be unusual. */
2063 shell_surface_update_child_surface_layers(child);
2064 }
2065 }
2066}
2067
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002068/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002069 * geometry to ensure the changes are redrawn.
2070 *
2071 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2072 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002073static void
2074shell_surface_update_layer(struct shell_surface *shsurf)
2075{
2076 struct wl_list *new_layer_link;
2077
2078 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2079
2080 if (new_layer_link == &shsurf->view->layer_link)
2081 return;
2082
2083 weston_view_geometry_dirty(shsurf->view);
2084 wl_list_remove(&shsurf->view->layer_link);
2085 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2086 weston_view_geometry_dirty(shsurf->view);
2087 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002088
2089 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002090}
2091
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002092static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002093shell_surface_set_parent(struct shell_surface *shsurf,
2094 struct weston_surface *parent)
2095{
2096 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002097
2098 wl_list_remove(&shsurf->children_link);
2099 wl_list_init(&shsurf->children_link);
2100
2101 /* Insert into the parent surface’s child list. */
2102 if (parent != NULL) {
2103 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2104 if (parent_shsurf != NULL)
2105 wl_list_insert(&parent_shsurf->children_list,
2106 &shsurf->children_link);
2107 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002108}
2109
2110static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002111shell_surface_set_output(struct shell_surface *shsurf,
2112 struct weston_output *output)
2113{
2114 struct weston_surface *es = shsurf->surface;
2115
2116 /* get the default output, if the client set it as NULL
2117 check whether the ouput is available */
2118 if (output)
2119 shsurf->output = output;
2120 else if (es->output)
2121 shsurf->output = es->output;
2122 else
2123 shsurf->output = get_default_output(es->compositor);
2124}
2125
2126static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002127surface_clear_next_states(struct shell_surface *shsurf)
2128{
2129 shsurf->next_state.maximized = false;
2130 shsurf->next_state.fullscreen = false;
2131
2132 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2133 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2134 shsurf->state_changed = true;
2135}
2136
2137static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002138set_toplevel(struct shell_surface *shsurf)
2139{
2140 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002141
2142 /* The layer_link is updated in set_surface_type(),
2143 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002144}
2145
2146static void
2147shell_surface_set_toplevel(struct wl_client *client,
2148 struct wl_resource *resource)
2149{
2150 struct shell_surface *surface = wl_resource_get_user_data(resource);
2151
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002152 shell_surface_set_parent(surface, NULL);
2153
Rafael Antognolli03b16592013-12-03 15:35:42 -02002154 surface_clear_next_states(surface);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002155 set_toplevel(surface);
2156}
2157
2158static void
2159set_transient(struct shell_surface *shsurf,
2160 struct weston_surface *parent, int x, int y, uint32_t flags)
2161{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002162 assert(parent != NULL);
2163
Philip Withnallbecb77e2013-11-25 18:01:30 +00002164 shsurf->transient.x = x;
2165 shsurf->transient.y = y;
2166 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002167
Rafael Antognollied207b42013-12-03 15:35:43 -02002168 shsurf->next_state.relative = true;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002169
2170 /* The layer_link is updated in set_surface_type(),
2171 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002172}
2173
2174static void
2175shell_surface_set_transient(struct wl_client *client,
2176 struct wl_resource *resource,
2177 struct wl_resource *parent_resource,
2178 int x, int y, uint32_t flags)
2179{
2180 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2181 struct weston_surface *parent =
2182 wl_resource_get_user_data(parent_resource);
2183
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002184 shell_surface_set_parent(shsurf, parent);
2185
Rafael Antognolli03b16592013-12-03 15:35:42 -02002186 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002187 set_transient(shsurf, parent, x, y, flags);
2188}
2189
2190static void
2191set_fullscreen(struct shell_surface *shsurf,
2192 uint32_t method,
2193 uint32_t framerate,
2194 struct weston_output *output)
2195{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002196 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002197
2198 shsurf->fullscreen_output = shsurf->output;
2199 shsurf->fullscreen.type = method;
2200 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002201
Rafael Antognolli03b16592013-12-03 15:35:42 -02002202 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002203
2204 shsurf->client->send_configure(shsurf->surface, 0,
2205 shsurf->output->width,
2206 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002207
2208 /* The layer_link is updated in set_surface_type(),
2209 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002210}
2211
2212static void
2213unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002214{
Philip Withnallf85fe842013-11-25 18:01:37 +00002215 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002216 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2217 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002218 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002219 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002220 shsurf->fullscreen_output = NULL;
2221
Alex Wu4539b082012-03-01 12:57:46 +08002222 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2223 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002224
Alex Wu4539b082012-03-01 12:57:46 +08002225 wl_list_remove(&shsurf->fullscreen.transform.link);
2226 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002227
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228 if (shsurf->fullscreen.black_view)
2229 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2230 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002231
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 weston_view_set_position(shsurf->view,
2233 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002234 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002236 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002237 shsurf->saved_rotation_valid = false;
2238 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002239
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002240 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002241}
2242
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002243static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002244shell_surface_set_fullscreen(struct wl_client *client,
2245 struct wl_resource *resource,
2246 uint32_t method,
2247 uint32_t framerate,
2248 struct wl_resource *output_resource)
2249{
2250 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2251 struct weston_output *output;
2252
2253 if (output_resource)
2254 output = wl_resource_get_user_data(output_resource);
2255 else
2256 output = NULL;
2257
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002258 shell_surface_set_parent(shsurf, NULL);
2259
Rafael Antognolli03b16592013-12-03 15:35:42 -02002260 surface_clear_next_states(shsurf);
2261 shsurf->next_state.fullscreen = true;
2262 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002263 set_fullscreen(shsurf, method, framerate, output);
2264}
2265
2266static void
2267set_popup(struct shell_surface *shsurf,
2268 struct weston_surface *parent,
2269 struct weston_seat *seat,
2270 uint32_t serial,
2271 int32_t x,
2272 int32_t y)
2273{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002274 assert(parent != NULL);
2275
Philip Withnallbecb77e2013-11-25 18:01:30 +00002276 shsurf->popup.shseat = get_shell_seat(seat);
2277 shsurf->popup.serial = serial;
2278 shsurf->popup.x = x;
2279 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002280
Philip Withnallb995e1d2013-11-25 18:01:39 +00002281 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002282}
2283
2284static void
2285shell_surface_set_popup(struct wl_client *client,
2286 struct wl_resource *resource,
2287 struct wl_resource *seat_resource,
2288 uint32_t serial,
2289 struct wl_resource *parent_resource,
2290 int32_t x, int32_t y, uint32_t flags)
2291{
2292 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002293 struct weston_surface *parent =
2294 wl_resource_get_user_data(parent_resource);
2295
2296 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002297
Rafael Antognolli03b16592013-12-03 15:35:42 -02002298 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002299 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002300 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002301 wl_resource_get_user_data(seat_resource),
2302 serial, x, y);
2303}
2304
2305static void
2306set_maximized(struct shell_surface *shsurf,
2307 struct weston_output *output)
2308{
2309 struct desktop_shell *shell;
2310 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002311
Philip Withnall352e7ed2013-11-25 18:01:35 +00002312 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002313
2314 shell = shell_surface_get_shell(shsurf);
2315 panel_height = get_output_panel_height(shell, shsurf->output);
2316 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2317
2318 shsurf->client->send_configure(shsurf->surface, edges,
2319 shsurf->output->width,
2320 shsurf->output->height - panel_height);
2321
Rafael Antognolli03b16592013-12-03 15:35:42 -02002322 shsurf->next_type = shsurf->type;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002323}
2324
2325static void
2326unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002327{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002328 /* undo all maximized things here */
2329 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 weston_view_set_position(shsurf->view,
2331 shsurf->saved_x,
2332 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002333
2334 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 wl_list_insert(&shsurf->view->geometry.transformation_list,
2336 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002337 shsurf->saved_rotation_valid = false;
2338 }
2339
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002340 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002341}
2342
Philip Withnallbecb77e2013-11-25 18:01:30 +00002343static void
2344shell_surface_set_maximized(struct wl_client *client,
2345 struct wl_resource *resource,
2346 struct wl_resource *output_resource)
2347{
2348 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2349 struct weston_output *output;
2350
2351 if (output_resource)
2352 output = wl_resource_get_user_data(output_resource);
2353 else
2354 output = NULL;
2355
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002356 shell_surface_set_parent(shsurf, NULL);
2357
Rafael Antognolli03b16592013-12-03 15:35:42 -02002358 surface_clear_next_states(shsurf);
2359 shsurf->next_state.maximized = true;
2360 shsurf->state_changed = true;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002361 set_maximized(shsurf, output);
2362}
2363
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002364/* This is only ever called from set_surface_type(), so there’s no need to
2365 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002366static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002367reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002368{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002369 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002370 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002371 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002372 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002373
2374 surface->type = SHELL_SURFACE_NONE;
2375 return 0;
2376}
2377
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002378static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002379set_full_output(struct shell_surface *shsurf)
2380{
2381 shsurf->saved_x = shsurf->view->geometry.x;
2382 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002383 shsurf->saved_width = shsurf->surface->width;
2384 shsurf->saved_height = shsurf->surface->height;
2385 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002386 shsurf->saved_position_valid = true;
2387
2388 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2389 wl_list_remove(&shsurf->rotation.transform.link);
2390 wl_list_init(&shsurf->rotation.transform.link);
2391 weston_view_geometry_dirty(shsurf->view);
2392 shsurf->saved_rotation_valid = true;
2393 }
2394}
2395
2396static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002397set_surface_type(struct shell_surface *shsurf)
2398{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002399 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002400 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002401
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002403
2404 shsurf->type = shsurf->next_type;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002405 shsurf->state = shsurf->next_state;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002406 shsurf->next_type = SHELL_SURFACE_NONE;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002407 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002408
2409 switch (shsurf->type) {
2410 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002411 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002412 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002413 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002414 weston_view_set_position(shsurf->view,
2415 pev->geometry.x + shsurf->transient.x,
2416 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002417 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002418
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002419 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002420 weston_view_set_position(shsurf->view, shsurf->transient.x,
2421 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002422 break;
2423
Philip Withnall0f640e22013-11-25 18:01:31 +00002424 case SHELL_SURFACE_POPUP:
2425 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002426 default:
2427 break;
2428 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002429
2430 /* Update the surface’s layer. */
2431 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002432}
2433
Tiago Vignattibe143262012-04-16 17:31:41 +03002434static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002435shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002436{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002437 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002438}
2439
Alex Wu21858432012-04-01 20:13:08 +08002440static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002441black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002442
Jason Ekstranda7af7042013-10-12 22:38:11 -05002443static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002444create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002445 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002446 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002447{
2448 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002449 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002450
2451 surface = weston_surface_create(ec);
2452 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002453 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002454 return NULL;
2455 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002456 view = weston_view_create(surface);
2457 if (surface == NULL) {
2458 weston_log("no memory\n");
2459 weston_surface_destroy(surface);
2460 return NULL;
2461 }
Alex Wu4539b082012-03-01 12:57:46 +08002462
Alex Wu21858432012-04-01 20:13:08 +08002463 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002464 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002465 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002466 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002467 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002468 pixman_region32_fini(&surface->input);
2469 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002470
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002471 surface->width = w;
2472 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002473 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002474
2475 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002476}
2477
Philip Withnalled8f1a92013-11-25 18:01:43 +00002478static void
2479shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2480{
2481 struct weston_output *output = shsurf->fullscreen_output;
2482
Rafael Antognolli03b16592013-12-03 15:35:42 -02002483 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002484
2485 if (!shsurf->fullscreen.black_view)
2486 shsurf->fullscreen.black_view =
2487 create_black_surface(shsurf->surface->compositor,
2488 shsurf->surface,
2489 output->x, output->y,
2490 output->width,
2491 output->height);
2492
2493 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2494 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2495 wl_list_insert(&shsurf->view->layer_link,
2496 &shsurf->fullscreen.black_view->layer_link);
2497 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2498 weston_surface_damage(shsurf->surface);
2499}
2500
Alex Wu4539b082012-03-01 12:57:46 +08002501/* Create black surface and append it to the associated fullscreen surface.
2502 * Handle size dismatch and positioning according to the method. */
2503static void
2504shell_configure_fullscreen(struct shell_surface *shsurf)
2505{
2506 struct weston_output *output = shsurf->fullscreen_output;
2507 struct weston_surface *surface = shsurf->surface;
2508 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002509 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002510 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002511
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002512 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2513 restore_output_mode(output);
2514
Philip Withnalled8f1a92013-11-25 18:01:43 +00002515 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002516
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002518 &surf_width, &surf_height);
2519
Alex Wu4539b082012-03-01 12:57:46 +08002520 switch (shsurf->fullscreen.type) {
2521 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002522 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002524 break;
2525 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002526 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002527 if (output->width == surf_width &&
2528 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002529 weston_view_set_position(shsurf->view,
2530 output->x - surf_x,
2531 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002532 break;
2533 }
2534
Alex Wu4539b082012-03-01 12:57:46 +08002535 matrix = &shsurf->fullscreen.transform.matrix;
2536 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002537
Scott Moreau1bad5db2012-08-18 01:04:05 -06002538 output_aspect = (float) output->width /
2539 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002540 /* XXX: Use surf_width and surf_height here? */
2541 surface_aspect = (float) surface->width /
2542 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002543 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002544 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002545 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002546 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002547 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002548 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002549
Alex Wu4539b082012-03-01 12:57:46 +08002550 weston_matrix_scale(matrix, scale, scale, 1);
2551 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002553 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002554 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2555 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002556 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002557
Alex Wu4539b082012-03-01 12:57:46 +08002558 break;
2559 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002560 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002561 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002562 surf_width * surface->buffer_viewport.scale,
2563 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002564 shsurf->fullscreen.framerate};
2565
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002566 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002567 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 weston_view_set_position(shsurf->view,
2569 output->x - surf_x,
2570 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002571 shsurf->fullscreen.black_view->surface->width = output->width;
2572 shsurf->fullscreen.black_view->surface->height = output->height;
2573 weston_view_set_position(shsurf->fullscreen.black_view,
2574 output->x - surf_x,
2575 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002576 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002577 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002578 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002579 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002580 }
Alex Wubd3354b2012-04-17 17:20:49 +08002581 }
Alex Wu4539b082012-03-01 12:57:46 +08002582 break;
2583 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002585 break;
2586 default:
2587 break;
2588 }
2589}
2590
Alex Wu4539b082012-03-01 12:57:46 +08002591static void
2592shell_map_fullscreen(struct shell_surface *shsurf)
2593{
Alex Wubd3354b2012-04-17 17:20:49 +08002594 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002595}
2596
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002597static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002598set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2599{
2600 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002601 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002602 shsurf->transient.x = x;
2603 shsurf->transient.y = y;
2604 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002605
2606 shell_surface_set_parent(shsurf, NULL);
2607
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002608 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2609}
2610
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002611static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002612
2613static void
2614destroy_shell_seat(struct wl_listener *listener, void *data)
2615{
2616 struct shell_seat *shseat =
2617 container_of(listener,
2618 struct shell_seat, seat_destroy_listener);
2619 struct shell_surface *shsurf, *prev = NULL;
2620
2621 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002622 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002623 shseat->popup_grab.client = NULL;
2624
2625 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2626 shsurf->popup.shseat = NULL;
2627 if (prev) {
2628 wl_list_init(&prev->popup.grab_link);
2629 }
2630 prev = shsurf;
2631 }
2632 wl_list_init(&prev->popup.grab_link);
2633 }
2634
2635 wl_list_remove(&shseat->seat_destroy_listener.link);
2636 free(shseat);
2637}
2638
2639static struct shell_seat *
2640create_shell_seat(struct weston_seat *seat)
2641{
2642 struct shell_seat *shseat;
2643
2644 shseat = calloc(1, sizeof *shseat);
2645 if (!shseat) {
2646 weston_log("no memory to allocate shell seat\n");
2647 return NULL;
2648 }
2649
2650 shseat->seat = seat;
2651 wl_list_init(&shseat->popup_grab.surfaces_list);
2652
2653 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2654 wl_signal_add(&seat->destroy_signal,
2655 &shseat->seat_destroy_listener);
2656
2657 return shseat;
2658}
2659
2660static struct shell_seat *
2661get_shell_seat(struct weston_seat *seat)
2662{
2663 struct wl_listener *listener;
2664
2665 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2666 if (listener == NULL)
2667 return create_shell_seat(seat);
2668
2669 return container_of(listener,
2670 struct shell_seat, seat_destroy_listener);
2671}
2672
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002673static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002674popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002676 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002677 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002678 struct shell_seat *shseat =
2679 container_of(grab, struct shell_seat, popup_grab.grab);
2680 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002681 wl_fixed_t sx, sy;
2682
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 view = weston_compositor_pick_view(pointer->seat->compositor,
2684 pointer->x, pointer->y,
2685 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002686
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 if (view && view->surface->resource &&
2688 wl_resource_get_client(view->surface->resource) == client) {
2689 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002690 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002691 weston_pointer_set_focus(pointer, NULL,
2692 wl_fixed_from_int(0),
2693 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002694 }
2695}
2696
2697static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002698popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2699 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002700{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002701 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002702 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002703 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002704
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002705 weston_pointer_move(pointer, x, y);
2706
Neil Roberts96d790e2013-09-19 17:32:00 +01002707 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 weston_view_from_global_fixed(pointer->focus,
2709 pointer->x, pointer->y,
2710 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002711 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002712 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002713}
2714
2715static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002716popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002717 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002718{
2719 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002720 struct shell_seat *shseat =
2721 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002722 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002723 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002724 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002725 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002726
Neil Roberts96d790e2013-09-19 17:32:00 +01002727 resource_list = &grab->pointer->focus_resource_list;
2728 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002729 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002730 wl_resource_for_each(resource, resource_list) {
2731 wl_pointer_send_button(resource, serial,
2732 time, button, state);
2733 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002734 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002735 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002736 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002737 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002738 }
2739
Daniel Stone4dbadb12012-05-30 16:31:51 +01002740 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002741 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002742}
2743
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002744static void
2745popup_grab_cancel(struct weston_pointer_grab *grab)
2746{
2747 popup_grab_end(grab->pointer);
2748}
2749
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002750static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002751 popup_grab_focus,
2752 popup_grab_motion,
2753 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002754 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002755};
2756
2757static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002758shell_surface_send_popup_done(struct shell_surface *shsurf)
2759{
2760 if (shell_surface_is_wl_shell_surface(shsurf))
2761 wl_shell_surface_send_popup_done(shsurf->resource);
2762 else if (shell_surface_is_xdg_popup(shsurf))
2763 xdg_popup_send_popup_done(shsurf->resource,
2764 shsurf->popup.serial);
2765}
2766
2767static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002768popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002769{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002770 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002771 struct shell_seat *shseat =
2772 container_of(grab, struct shell_seat, popup_grab.grab);
2773 struct shell_surface *shsurf;
2774 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002775
2776 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002777 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002778 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002779 shseat->popup_grab.grab.interface = NULL;
2780 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002781 /* Send the popup_done event to all the popups open */
2782 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002783 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002784 shsurf->popup.shseat = NULL;
2785 if (prev) {
2786 wl_list_init(&prev->popup.grab_link);
2787 }
2788 prev = shsurf;
2789 }
2790 wl_list_init(&prev->popup.grab_link);
2791 wl_list_init(&shseat->popup_grab.surfaces_list);
2792 }
2793}
2794
2795static void
2796add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2797{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002798 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002799
2800 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002801 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002802 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002803 /* We must make sure here that this popup was opened after
2804 * a mouse press, and not just by moving around with other
2805 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002806 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002807 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002808
Rob Bradforddfe31052013-06-26 19:49:11 +01002809 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002810 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002811 } else {
2812 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002813 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002814}
2815
2816static void
2817remove_popup_grab(struct shell_surface *shsurf)
2818{
2819 struct shell_seat *shseat = shsurf->popup.shseat;
2820
2821 wl_list_remove(&shsurf->popup.grab_link);
2822 wl_list_init(&shsurf->popup.grab_link);
2823 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002824 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002825 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002826 }
2827}
2828
2829static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002830shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002831{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002832 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002834
Jason Ekstranda7af7042013-10-12 22:38:11 -05002835 shsurf->surface->output = parent_view->output;
2836 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002837
Jason Ekstranda7af7042013-10-12 22:38:11 -05002838 weston_view_set_transform_parent(shsurf->view, parent_view);
2839 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2840 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002841
Kristian Høgsberge3148752013-05-06 23:19:49 -04002842 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002843 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002844 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002845 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002846 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002847 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002848}
2849
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002850static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002851 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002852 shell_surface_move,
2853 shell_surface_resize,
2854 shell_surface_set_toplevel,
2855 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002856 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002857 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002858 shell_surface_set_maximized,
2859 shell_surface_set_title,
2860 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002861};
2862
2863static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002864destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002865{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002866 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2867
Giulio Camuffo5085a752013-03-25 21:42:45 +01002868 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2869 remove_popup_grab(shsurf);
2870 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002871
Alex Wubd3354b2012-04-17 17:20:49 +08002872 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002873 shell_surface_is_top_fullscreen(shsurf))
2874 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002875
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876 if (shsurf->fullscreen.black_view)
2877 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002878
Alex Wubd3354b2012-04-17 17:20:49 +08002879 /* As destroy_resource() use wl_list_for_each_safe(),
2880 * we can always remove the listener.
2881 */
2882 wl_list_remove(&shsurf->surface_destroy_listener.link);
2883 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002884 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002885 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002886
Jason Ekstranda7af7042013-10-12 22:38:11 -05002887 weston_view_destroy(shsurf->view);
2888
Philip Withnall648a4dd2013-11-25 18:01:44 +00002889 wl_list_remove(&shsurf->children_link);
2890
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002891 wl_list_remove(&shsurf->link);
2892 free(shsurf);
2893}
2894
2895static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002896shell_destroy_shell_surface(struct wl_resource *resource)
2897{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002898 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002899
2900 destroy_shell_surface(shsurf);
2901}
2902
2903static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002904shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002905{
2906 struct shell_surface *shsurf = container_of(listener,
2907 struct shell_surface,
2908 surface_destroy_listener);
2909
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002910 if (shsurf->resource)
2911 wl_resource_destroy(shsurf->resource);
2912 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002913 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002914}
2915
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002916static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002917shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002918
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002919struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002920get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002921{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002922 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002923 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002924 else
2925 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002926}
2927
Rafael Antognollie2a34552013-12-03 15:35:45 -02002928static struct shell_surface *
2929create_common_surface(void *shell, struct weston_surface *surface,
2930 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002931{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002932 struct shell_surface *shsurf;
2933
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002934 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002935 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002936 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002937 }
2938
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002939 shsurf = calloc(1, sizeof *shsurf);
2940 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002941 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002942 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002943 }
2944
Jason Ekstranda7af7042013-10-12 22:38:11 -05002945 shsurf->view = weston_view_create(surface);
2946 if (!shsurf->view) {
2947 weston_log("no memory to allocate shell surface\n");
2948 free(shsurf);
2949 return NULL;
2950 }
2951
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002952 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002953 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002954
Tiago Vignattibc052c92012-04-19 16:18:18 +03002955 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002956 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002957 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002958 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002959 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002960 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002961 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2962 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002963 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002964 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002965 wl_list_init(&shsurf->fullscreen.transform.link);
2966
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002967 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002968 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002969 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002970 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002971
2972 /* init link so its safe to always remove it in destroy_shell_surface */
2973 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002974 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002975
Pekka Paalanen460099f2012-01-20 16:48:25 +02002976 /* empty when not in use */
2977 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002978 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002979
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002980 wl_list_init(&shsurf->workspace_transform.link);
2981
Philip Withnall648a4dd2013-11-25 18:01:44 +00002982 wl_list_init(&shsurf->children_link);
2983 wl_list_init(&shsurf->children_list);
2984 shsurf->parent = NULL;
2985
Pekka Paalanen98262232011-12-01 10:42:22 +02002986 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002987 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002988
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002989 shsurf->client = client;
2990
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002991 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002992}
2993
Rafael Antognollie2a34552013-12-03 15:35:45 -02002994static struct shell_surface *
2995create_shell_surface(void *shell, struct weston_surface *surface,
2996 const struct weston_shell_client *client)
2997{
2998 struct shell_surface *shsurf;
2999 shsurf = create_common_surface(shell, surface, client);
3000
3001 shsurf->type = SHELL_SURFACE_NONE;
3002 shsurf->next_type = SHELL_SURFACE_NONE;
3003
3004 return shsurf;
3005}
3006
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007static struct weston_view *
3008get_primary_view(void *shell, struct shell_surface *shsurf)
3009{
3010 return shsurf->view;
3011}
3012
Tiago Vignattibc052c92012-04-19 16:18:18 +03003013static void
3014shell_get_shell_surface(struct wl_client *client,
3015 struct wl_resource *resource,
3016 uint32_t id,
3017 struct wl_resource *surface_resource)
3018{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003019 struct weston_surface *surface =
3020 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003021 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003022 struct shell_surface *shsurf;
3023
3024 if (get_shell_surface(surface)) {
3025 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003026 WL_DISPLAY_ERROR_INVALID_OBJECT,
3027 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003028 return;
3029 }
3030
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003031 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003032 if (!shsurf) {
3033 wl_resource_post_error(surface_resource,
3034 WL_DISPLAY_ERROR_INVALID_OBJECT,
3035 "surface->configure already set");
3036 return;
3037 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003038
Jason Ekstranda85118c2013-06-27 20:17:02 -05003039 shsurf->resource =
3040 wl_resource_create(client,
3041 &wl_shell_surface_interface, 1, id);
3042 wl_resource_set_implementation(shsurf->resource,
3043 &shell_surface_implementation,
3044 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003045}
3046
Rafael Antognollie2a34552013-12-03 15:35:45 -02003047static bool
3048shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3049{
3050 return wl_resource_instance_of(shsurf->resource,
3051 &wl_shell_surface_interface,
3052 &shell_surface_implementation);
3053}
3054
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003055static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003056 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003057};
3058
Rafael Antognollie2a34552013-12-03 15:35:45 -02003059/****************************
3060 * xdg-shell implementation */
3061
3062static void
3063xdg_surface_destroy(struct wl_client *client,
3064 struct wl_resource *resource)
3065{
3066 wl_resource_destroy(resource);
3067}
3068
3069static void
3070xdg_surface_pong(struct wl_client *client,
3071 struct wl_resource *resource,
3072 uint32_t serial)
3073{
3074 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3075
3076 surface_pong(shsurf, serial);
3077}
3078
3079static void
3080xdg_surface_set_app_id(struct wl_client *client,
3081 struct wl_resource *resource,
3082 const char *app_id)
3083{
3084 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3085
3086 free(shsurf->class);
3087 shsurf->class = strdup(app_id);
3088}
3089
3090static void
3091xdg_surface_set_title(struct wl_client *client,
3092 struct wl_resource *resource, const char *title)
3093{
3094 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3095
3096 set_title(shsurf, title);
3097}
3098
3099static void
3100xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3101 struct wl_resource *seat_resource, uint32_t serial)
3102{
3103 common_surface_move(resource, seat_resource, serial);
3104}
3105
3106static void
3107xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3108 struct wl_resource *seat_resource, uint32_t serial,
3109 uint32_t edges)
3110{
3111 common_surface_resize(resource, seat_resource, serial, edges);
3112}
3113
3114static void
3115xdg_surface_set_output(struct wl_client *client,
3116 struct wl_resource *resource,
3117 struct wl_resource *output_resource)
3118{
3119 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3120 struct weston_output *output;
3121
3122 if (output_resource)
3123 output = wl_resource_get_user_data(output_resource);
3124 else
3125 output = NULL;
3126
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003127 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003128}
3129
3130static void
3131xdg_surface_set_fullscreen(struct wl_client *client,
3132 struct wl_resource *resource)
3133{
3134 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3135
3136 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3137 return;
3138
3139 if (!shsurf->next_state.fullscreen) {
3140 shsurf->next_state.fullscreen = true;
3141 shsurf->state_changed = true;
3142 set_fullscreen(shsurf,
3143 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003144 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003145 }
3146}
3147
3148static void
3149xdg_surface_unset_fullscreen(struct wl_client *client,
3150 struct wl_resource *resource)
3151{
3152 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003153 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003154
3155 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3156 return;
3157
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003158 if (!shsurf->next_state.fullscreen)
3159 return;
3160
Rafael Antognollie2a34552013-12-03 15:35:45 -02003161 shsurf->next_state.fullscreen = false;
3162 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003163
3164 if (shsurf->saved_size_valid) {
3165 width = shsurf->saved_width;
3166 height = shsurf->saved_height;
3167 shsurf->saved_size_valid = false;
3168 } else {
3169 width = shsurf->surface->width;
3170 height = shsurf->surface->height;
3171 }
3172
3173 shsurf->client->send_configure(shsurf->surface, 0, width, height);
3174 shsurf->next_type = shsurf->type;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003175}
3176
3177static void
3178xdg_surface_set_maximized(struct wl_client *client,
3179 struct wl_resource *resource)
3180{
3181 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3182
3183 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3184 return;
3185
3186 if (!shsurf->next_state.maximized) {
3187 shsurf->next_state.maximized = true;
3188 shsurf->state_changed = true;
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003189 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003190 }
3191}
3192
3193static void
3194xdg_surface_unset_maximized(struct wl_client *client,
3195 struct wl_resource *resource)
3196{
3197 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003198 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003199
3200 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3201 return;
3202
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003203 if (!shsurf->next_state.maximized)
3204 return;
3205
Rafael Antognollie2a34552013-12-03 15:35:45 -02003206 shsurf->next_state.maximized = false;
3207 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003208
3209 if (shsurf->saved_size_valid) {
3210 width = shsurf->saved_width;
3211 height = shsurf->saved_height;
3212 shsurf->saved_size_valid = false;
3213 } else {
3214 width = shsurf->surface->width;
3215 height = shsurf->surface->height;
3216 }
3217
3218 shsurf->client->send_configure(shsurf->surface, 0, width, height);
3219 shsurf->next_type = shsurf->type;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003220}
3221
3222static const struct xdg_surface_interface xdg_surface_implementation = {
3223 xdg_surface_destroy,
3224 xdg_surface_set_transient_for,
3225 xdg_surface_set_title,
3226 xdg_surface_set_app_id,
3227 xdg_surface_pong,
3228 xdg_surface_move,
3229 xdg_surface_resize,
3230 xdg_surface_set_output,
3231 xdg_surface_set_fullscreen,
3232 xdg_surface_unset_fullscreen,
3233 xdg_surface_set_maximized,
3234 xdg_surface_unset_maximized,
3235 NULL /* set_minimized */
3236};
3237
3238static void
3239xdg_send_configure(struct weston_surface *surface,
3240 uint32_t edges, int32_t width, int32_t height)
3241{
3242 struct shell_surface *shsurf = get_shell_surface(surface);
3243
3244 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3245}
3246
3247static const struct weston_shell_client xdg_client = {
3248 xdg_send_configure
3249};
3250
3251static void
3252xdg_use_unstable_version(struct wl_client *client,
3253 struct wl_resource *resource,
3254 int32_t version)
3255{
3256 if (version > 1) {
3257 wl_resource_post_error(resource,
3258 1,
3259 "xdg-shell:: version not implemented yet.");
3260 return;
3261 }
3262}
3263
3264static struct shell_surface *
3265create_xdg_surface(void *shell, struct weston_surface *surface,
3266 const struct weston_shell_client *client)
3267{
3268 struct shell_surface *shsurf;
3269 shsurf = create_common_surface(shell, surface, client);
3270
3271 shsurf->type = SHELL_SURFACE_NONE;
3272 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
3273
3274 return shsurf;
3275}
3276
3277static void
3278xdg_get_xdg_surface(struct wl_client *client,
3279 struct wl_resource *resource,
3280 uint32_t id,
3281 struct wl_resource *surface_resource)
3282{
3283 struct weston_surface *surface =
3284 wl_resource_get_user_data(surface_resource);
3285 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3286 struct shell_surface *shsurf;
3287
3288 if (get_shell_surface(surface)) {
3289 wl_resource_post_error(surface_resource,
3290 WL_DISPLAY_ERROR_INVALID_OBJECT,
3291 "desktop_shell::get_shell_surface already requested");
3292 return;
3293 }
3294
3295 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3296 if (!shsurf) {
3297 wl_resource_post_error(surface_resource,
3298 WL_DISPLAY_ERROR_INVALID_OBJECT,
3299 "surface->configure already set");
3300 return;
3301 }
3302
3303 shsurf->resource =
3304 wl_resource_create(client,
3305 &xdg_surface_interface, 1, id);
3306 wl_resource_set_implementation(shsurf->resource,
3307 &xdg_surface_implementation,
3308 shsurf, shell_destroy_shell_surface);
3309}
3310
3311static bool
3312shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3313{
3314 return wl_resource_instance_of(shsurf->resource,
3315 &xdg_surface_interface,
3316 &xdg_surface_implementation);
3317}
3318
3319/* xdg-popup implementation */
3320
3321static void
3322xdg_popup_destroy(struct wl_client *client,
3323 struct wl_resource *resource)
3324{
3325 wl_resource_destroy(resource);
3326}
3327
3328static void
3329xdg_popup_pong(struct wl_client *client,
3330 struct wl_resource *resource,
3331 uint32_t serial)
3332{
3333 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3334
3335 surface_pong(shsurf, serial);
3336}
3337
3338static const struct xdg_popup_interface xdg_popup_implementation = {
3339 xdg_popup_destroy,
3340 xdg_popup_pong
3341};
3342
3343static void
3344xdg_popup_send_configure(struct weston_surface *surface,
3345 uint32_t edges, int32_t width, int32_t height)
3346{
3347}
3348
3349static const struct weston_shell_client xdg_popup_client = {
3350 xdg_popup_send_configure
3351};
3352
3353static struct shell_surface *
3354create_xdg_popup(void *shell, struct weston_surface *surface,
3355 const struct weston_shell_client *client,
3356 struct weston_surface *parent,
3357 struct shell_seat *seat,
3358 uint32_t serial,
3359 int32_t x, int32_t y)
3360{
3361 struct shell_surface *shsurf;
3362 shsurf = create_common_surface(shell, surface, client);
3363
3364 shsurf->type = SHELL_SURFACE_NONE;
3365 shsurf->next_type = SHELL_SURFACE_POPUP;
3366 shsurf->popup.shseat = seat;
3367 shsurf->popup.serial = serial;
3368 shsurf->popup.x = x;
3369 shsurf->popup.y = y;
3370 shell_surface_set_parent(shsurf, parent);
3371
3372 return shsurf;
3373}
3374
3375static void
3376xdg_get_xdg_popup(struct wl_client *client,
3377 struct wl_resource *resource,
3378 uint32_t id,
3379 struct wl_resource *surface_resource,
3380 struct wl_resource *parent_resource,
3381 struct wl_resource *seat_resource,
3382 uint32_t serial,
3383 int32_t x, int32_t y, uint32_t flags)
3384{
3385 struct weston_surface *surface =
3386 wl_resource_get_user_data(surface_resource);
3387 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3388 struct shell_surface *shsurf;
3389 struct weston_surface *parent;
3390 struct shell_seat *seat;
3391
3392 if (get_shell_surface(surface)) {
3393 wl_resource_post_error(surface_resource,
3394 WL_DISPLAY_ERROR_INVALID_OBJECT,
3395 "desktop_shell::get_shell_surface already requested");
3396 return;
3397 }
3398
3399 if (!parent_resource) {
3400 wl_resource_post_error(surface_resource,
3401 WL_DISPLAY_ERROR_INVALID_OBJECT,
3402 "xdg_shell::get_xdg_popup requires a parent shell surface");
3403 }
3404
3405 parent = wl_resource_get_user_data(parent_resource);
3406 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3407
3408 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3409 parent, seat, serial, x, y);
3410 if (!shsurf) {
3411 wl_resource_post_error(surface_resource,
3412 WL_DISPLAY_ERROR_INVALID_OBJECT,
3413 "surface->configure already set");
3414 return;
3415 }
3416
3417 shsurf->resource =
3418 wl_resource_create(client,
3419 &xdg_popup_interface, 1, id);
3420 wl_resource_set_implementation(shsurf->resource,
3421 &xdg_popup_implementation,
3422 shsurf, shell_destroy_shell_surface);
3423}
3424
3425static bool
3426shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3427{
3428 return wl_resource_instance_of(shsurf->resource,
3429 &xdg_popup_interface,
3430 &xdg_popup_implementation);
3431}
3432
3433static const struct xdg_shell_interface xdg_implementation = {
3434 xdg_use_unstable_version,
3435 xdg_get_xdg_surface,
3436 xdg_get_xdg_popup
3437};
3438
3439static int
3440xdg_shell_unversioned_dispatch(const void *implementation,
3441 void *_target, uint32_t opcode,
3442 const struct wl_message *message,
3443 union wl_argument *args)
3444{
3445 struct wl_resource *resource = _target;
3446 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3447
3448 if (opcode != 0) {
3449 wl_resource_post_error(resource,
3450 WL_DISPLAY_ERROR_INVALID_OBJECT,
3451 "must call use_unstable_version first");
3452 return 0;
3453 }
3454
3455#define XDG_SERVER_VERSION 1
3456
3457 if (args[0].i != XDG_SERVER_VERSION) {
3458 wl_resource_post_error(resource,
3459 WL_DISPLAY_ERROR_INVALID_OBJECT,
3460 "incompatible version, server is %d "
3461 "client wants %d",
3462 XDG_SERVER_VERSION, args[0].i);
3463 return 0;
3464 }
3465
3466 wl_resource_set_implementation(resource, &xdg_implementation,
3467 shell, NULL);
3468
3469 return 1;
3470}
3471
3472/* end of xdg-shell implementation */
3473/***********************************/
3474
Kristian Høgsberg07937562011-04-12 17:25:42 -04003475static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003476shell_fade(struct desktop_shell *shell, enum fade_type type);
3477
3478static int
3479screensaver_timeout(void *data)
3480{
3481 struct desktop_shell *shell = data;
3482
3483 shell_fade(shell, FADE_OUT);
3484
3485 return 1;
3486}
3487
3488static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003489handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003490{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003491 struct desktop_shell *shell =
3492 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003493
Pekka Paalanen18027e52011-12-02 16:31:49 +02003494 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003495
3496 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003497 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003498}
3499
3500static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003501launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003502{
3503 if (shell->screensaver.binding)
3504 return;
3505
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003506 if (!shell->screensaver.path) {
3507 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003508 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003509 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003510
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003511 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003512 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003513 return;
3514 }
3515
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003516 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003517 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003518 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003519 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003520}
3521
3522static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003523terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003524{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003525 if (shell->screensaver.process.pid == 0)
3526 return;
3527
3528 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003529}
3530
3531static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003532configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003533{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003534 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003535
Jason Ekstranda7af7042013-10-12 22:38:11 -05003536 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3537 if (v->output == ev->output && v != ev) {
3538 weston_view_unmap(v);
3539 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003540 }
3541 }
3542
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003543 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003544
Jason Ekstranda7af7042013-10-12 22:38:11 -05003545 if (wl_list_empty(&ev->layer_link)) {
3546 wl_list_insert(&layer->view_list, &ev->layer_link);
3547 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003548 }
3549}
3550
3551static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003552background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003553{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003554 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003555 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003556
Jason Ekstranda7af7042013-10-12 22:38:11 -05003557 view = container_of(es->views.next, struct weston_view, surface_link);
3558
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003559 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003560}
3561
3562static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003563desktop_shell_set_background(struct wl_client *client,
3564 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003565 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003566 struct wl_resource *surface_resource)
3567{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003568 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003569 struct weston_surface *surface =
3570 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003571 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003572
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003573 if (surface->configure) {
3574 wl_resource_post_error(surface_resource,
3575 WL_DISPLAY_ERROR_INVALID_OBJECT,
3576 "surface role already assigned");
3577 return;
3578 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003579
Jason Ekstranda7af7042013-10-12 22:38:11 -05003580 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3581 weston_view_destroy(view);
3582 view = weston_view_create(surface);
3583
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003584 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003585 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003586 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003587 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003588 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003589 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003590 surface->output->width,
3591 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003592}
3593
3594static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003595panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003596{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003597 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003598 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003599
Jason Ekstranda7af7042013-10-12 22:38:11 -05003600 view = container_of(es->views.next, struct weston_view, surface_link);
3601
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003602 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003603}
3604
3605static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003606desktop_shell_set_panel(struct wl_client *client,
3607 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003608 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003609 struct wl_resource *surface_resource)
3610{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003611 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003612 struct weston_surface *surface =
3613 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003614 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003615
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003616 if (surface->configure) {
3617 wl_resource_post_error(surface_resource,
3618 WL_DISPLAY_ERROR_INVALID_OBJECT,
3619 "surface role already assigned");
3620 return;
3621 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003622
Jason Ekstranda7af7042013-10-12 22:38:11 -05003623 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3624 weston_view_destroy(view);
3625 view = weston_view_create(surface);
3626
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003627 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003628 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003629 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003630 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003631 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003632 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003633 surface->output->width,
3634 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003635}
3636
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003637static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003638lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003639{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003640 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003641 struct weston_view *view;
3642
3643 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003644
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003645 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003646 return;
3647
Jason Ekstranda7af7042013-10-12 22:38:11 -05003648 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003649
3650 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003651 wl_list_insert(&shell->lock_layer.view_list,
3652 &view->layer_link);
3653 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003654 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003655 }
3656}
3657
3658static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003659handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003660{
Tiago Vignattibe143262012-04-16 17:31:41 +03003661 struct desktop_shell *shell =
3662 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003663
Martin Minarik6d118362012-06-07 18:01:59 +02003664 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003665 shell->lock_surface = NULL;
3666}
3667
3668static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003669desktop_shell_set_lock_surface(struct wl_client *client,
3670 struct wl_resource *resource,
3671 struct wl_resource *surface_resource)
3672{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003673 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003674 struct weston_surface *surface =
3675 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003676
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003677 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003678
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003679 if (!shell->locked)
3680 return;
3681
Pekka Paalanen98262232011-12-01 10:42:22 +02003682 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003683
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003684 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003685 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003686 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003687
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003688 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003689 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003690 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003691}
3692
3693static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003694resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003695{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003696 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003697
Pekka Paalanen77346a62011-11-30 16:26:35 +02003698 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003699
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003700 wl_list_remove(&shell->lock_layer.link);
3701 wl_list_insert(&shell->compositor->cursor_layer.link,
3702 &shell->fullscreen_layer.link);
3703 wl_list_insert(&shell->fullscreen_layer.link,
3704 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003705 if (shell->showing_input_panels) {
3706 wl_list_insert(&shell->panel_layer.link,
3707 &shell->input_panel_layer.link);
3708 wl_list_insert(&shell->input_panel_layer.link,
3709 &ws->layer.link);
3710 } else {
3711 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3712 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003713
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003714 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003715
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003716 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003717 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003718 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003719}
3720
3721static void
3722desktop_shell_unlock(struct wl_client *client,
3723 struct wl_resource *resource)
3724{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003725 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003726
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003727 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003728
3729 if (shell->locked)
3730 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003731}
3732
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003733static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003734desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003735 struct wl_resource *resource,
3736 struct wl_resource *surface_resource)
3737{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003738 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003739
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003740 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003741 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003742}
3743
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003744static void
3745desktop_shell_desktop_ready(struct wl_client *client,
3746 struct wl_resource *resource)
3747{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003748 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003749
3750 shell_fade_startup(shell);
3751}
3752
Kristian Høgsberg75840622011-09-06 13:48:16 -04003753static const struct desktop_shell_interface desktop_shell_implementation = {
3754 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003755 desktop_shell_set_panel,
3756 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003757 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003758 desktop_shell_set_grab_surface,
3759 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003760};
3761
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003762static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003763get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003764{
3765 struct shell_surface *shsurf;
3766
3767 shsurf = get_shell_surface(surface);
3768 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003769 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003770 return shsurf->type;
3771}
3772
Kristian Høgsberg75840622011-09-06 13:48:16 -04003773static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003774move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003775{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003776 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003777 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003778 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003779
Pekka Paalanen01388e22013-04-25 13:57:44 +03003780 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003781 if (surface == NULL)
3782 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003783
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003784 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003785 if (shsurf == NULL || shsurf->state.fullscreen ||
3786 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003787 return;
3788
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003789 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003790}
3791
3792static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003793maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3794{
3795 struct weston_surface *focus = seat->pointer->focus->surface;
3796 struct weston_surface *surface;
3797 struct shell_surface *shsurf;
3798
3799 surface = weston_surface_get_main_surface(focus);
3800 if (surface == NULL)
3801 return;
3802
3803 shsurf = get_shell_surface(surface);
3804 if (shsurf == NULL)
3805 return;
3806
3807 if (!shell_surface_is_xdg_surface(shsurf))
3808 return;
3809
3810 if (shsurf->state.maximized)
3811 xdg_surface_send_request_unset_maximized(shsurf->resource);
3812 else
3813 xdg_surface_send_request_set_maximized(shsurf->resource);
3814}
3815
3816static void
3817fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3818{
3819 struct weston_surface *focus = seat->pointer->focus->surface;
3820 struct weston_surface *surface;
3821 struct shell_surface *shsurf;
3822
3823 surface = weston_surface_get_main_surface(focus);
3824 if (surface == NULL)
3825 return;
3826
3827 shsurf = get_shell_surface(surface);
3828 if (shsurf == NULL)
3829 return;
3830
3831 if (!shell_surface_is_xdg_surface(shsurf))
3832 return;
3833
3834 if (shsurf->state.fullscreen)
3835 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3836 else
3837 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3838}
3839
3840static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003841touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3842{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003843 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003844 struct weston_surface *surface;
3845 struct shell_surface *shsurf;
3846
3847 surface = weston_surface_get_main_surface(focus);
3848 if (surface == NULL)
3849 return;
3850
3851 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003852 if (shsurf == NULL || shsurf->state.fullscreen ||
3853 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003854 return;
3855
3856 surface_touch_move(shsurf, (struct weston_seat *) seat);
3857}
3858
3859static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003860resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003861{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003862 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003863 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003864 uint32_t edges = 0;
3865 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003866 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003867
Pekka Paalanen01388e22013-04-25 13:57:44 +03003868 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003869 if (surface == NULL)
3870 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003871
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003872 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003873 if (shsurf == NULL || shsurf->state.fullscreen ||
3874 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003875 return;
3876
Jason Ekstranda7af7042013-10-12 22:38:11 -05003877 weston_view_from_global(shsurf->view,
3878 wl_fixed_to_int(seat->pointer->grab_x),
3879 wl_fixed_to_int(seat->pointer->grab_y),
3880 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003881
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003882 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003883 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003884 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003885 edges |= 0;
3886 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003887 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003888
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003889 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003890 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003891 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003892 edges |= 0;
3893 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003894 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003895
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003896 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003897}
3898
3899static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003900surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003901 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003902{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003903 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003904 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003905 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003906 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003907
Pekka Paalanen01388e22013-04-25 13:57:44 +03003908 /* XXX: broken for windows containing sub-surfaces */
3909 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003910 if (surface == NULL)
3911 return;
3912
3913 shsurf = get_shell_surface(surface);
3914 if (!shsurf)
3915 return;
3916
Jason Ekstranda7af7042013-10-12 22:38:11 -05003917 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003918
Jason Ekstranda7af7042013-10-12 22:38:11 -05003919 if (shsurf->view->alpha > 1.0)
3920 shsurf->view->alpha = 1.0;
3921 if (shsurf->view->alpha < step)
3922 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003923
Jason Ekstranda7af7042013-10-12 22:38:11 -05003924 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003925 weston_surface_damage(surface);
3926}
3927
3928static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003929do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003930 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003931{
Daniel Stone37816df2012-05-16 18:45:18 +01003932 struct weston_seat *ws = (struct weston_seat *) seat;
3933 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003934 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003935 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003936
3937 wl_list_for_each(output, &compositor->output_list, link) {
3938 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003939 wl_fixed_to_double(seat->pointer->x),
3940 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003941 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003942 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003943 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003944 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003945 increment = -output->zoom.increment;
3946 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003947 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003948 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003949 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003950 else
3951 increment = 0;
3952
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003953 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003954
Scott Moreaue6603982012-06-11 13:07:51 -06003955 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003956 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003957 else if (output->zoom.level > output->zoom.max_level)
3958 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003959 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003960 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003961 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003962
Scott Moreaue6603982012-06-11 13:07:51 -06003963 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003964
Jason Ekstranda7af7042013-10-12 22:38:11 -05003965 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003966 }
3967 }
3968}
3969
Scott Moreauccbf29d2012-02-22 14:21:41 -07003970static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003971zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003972 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003973{
3974 do_zoom(seat, time, 0, axis, value);
3975}
3976
3977static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003978zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003979 void *data)
3980{
3981 do_zoom(seat, time, key, 0, 0);
3982}
3983
3984static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003985terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003986 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003987{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003988 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003989
Daniel Stone325fc2d2012-05-30 16:31:58 +01003990 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003991}
3992
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003993struct alt_tab {
3994 struct desktop_shell *shell;
3995 struct weston_keyboard_grab grab;
3996
3997 struct wl_list *current;
3998
3999 struct wl_list preview_list;
4000};
4001
4002struct alt_tab_preview {
4003 struct alt_tab *alt_tab;
4004
4005 struct weston_view *view;
4006 struct weston_transform transform;
4007
4008 struct wl_listener listener;
4009
4010 struct wl_list link;
4011};
4012
4013static void
4014alt_tab_next(struct alt_tab *alt_tab)
4015{
4016 alt_tab->current = alt_tab->current->next;
4017
4018 /* Make sure we're not pointing to the list header e.g. after
4019 * cycling through the whole list. */
4020 if (alt_tab->current->next == alt_tab->preview_list.next)
4021 alt_tab->current = alt_tab->current->next;
4022}
4023
4024static void
4025alt_tab_destroy(struct alt_tab *alt_tab)
4026{
4027 struct alt_tab_preview *preview, *next;
4028 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
4029
4030 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
4031 preview = wl_container_of(alt_tab->current, preview, link);
4032
4033 activate(alt_tab->shell, preview->view->surface,
4034 (struct weston_seat *) keyboard->seat);
4035 }
4036
4037 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
4038 wl_list_remove(&preview->link);
4039 wl_list_remove(&preview->listener.link);
4040 weston_view_destroy(preview->view);
4041 free(preview);
4042 }
4043
4044 weston_keyboard_end_grab(keyboard);
4045 if (keyboard->input_method_resource)
4046 keyboard->grab = &keyboard->input_method_grab;
4047
4048 free(alt_tab);
4049}
4050
4051static void
4052alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
4053{
4054 struct alt_tab_preview *preview =
4055 container_of(listener, struct alt_tab_preview, listener);
4056 struct alt_tab *alt_tab = preview->alt_tab;
4057 int advance = 0;
4058
4059 /* If the preview that we're removing is the currently selected one,
4060 * we want to move to the next one. So we move to ->prev and then
4061 * call _next() after removing the preview. */
4062 if (alt_tab->current == &preview->link) {
4063 alt_tab->current = alt_tab->current->prev;
4064 advance = 1;
4065 }
4066
4067 wl_list_remove(&preview->listener.link);
4068 wl_list_remove(&preview->link);
4069
4070 free(preview);
4071
4072 if (advance)
4073 alt_tab_next(alt_tab);
4074
4075 /* If the last preview goes away, stop the alt-tab */
4076 if (wl_list_empty(alt_tab->current))
4077 alt_tab_destroy(alt_tab);
4078}
4079
4080static void
4081alt_tab_key(struct weston_keyboard_grab *grab,
4082 uint32_t time, uint32_t key, uint32_t state_w)
4083{
4084 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4085 enum wl_keyboard_key_state state = state_w;
4086
4087 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4088 alt_tab_next(alt_tab);
4089}
4090
4091static void
4092alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4093 uint32_t mods_depressed, uint32_t mods_latched,
4094 uint32_t mods_locked, uint32_t group)
4095{
4096 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4097 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4098
4099 if ((seat->modifier_state & MODIFIER_ALT) == 0)
4100 alt_tab_destroy(alt_tab);
4101}
4102
4103static void
4104alt_tab_cancel(struct weston_keyboard_grab *grab)
4105{
4106 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4107
4108 alt_tab_destroy(alt_tab);
4109}
4110
4111static const struct weston_keyboard_grab_interface alt_tab_grab = {
4112 alt_tab_key,
4113 alt_tab_modifier,
4114 alt_tab_cancel,
4115};
4116
4117static int
4118view_for_alt_tab(struct weston_view *view)
4119{
Rafael Antognollied207b42013-12-03 15:35:43 -02004120 struct shell_surface *shsurf = get_shell_surface(view->surface);
4121 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004122 return 0;
4123
Rafael Antognollied207b42013-12-03 15:35:43 -02004124 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004125 return 0;
4126
4127 if (view != get_default_view(view->surface))
4128 return 0;
4129
4130 return 1;
4131}
4132
4133static void
4134alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4135 void *data)
4136{
4137 struct alt_tab *alt_tab;
4138 struct desktop_shell *shell = data;
4139 struct weston_output *output = get_default_output(shell->compositor);
4140 struct workspace *ws;
4141 struct weston_view *view;
4142 int num_surfaces = 0;
4143 int x, y, side, margin;
4144
4145 wl_list_for_each(view, &shell->compositor->view_list, link) {
4146
4147 if (view_for_alt_tab(view))
4148 num_surfaces++;
4149 }
4150
4151 if (!num_surfaces)
4152 return;
4153
4154 alt_tab = malloc(sizeof *alt_tab);
4155 if (!alt_tab)
4156 return;
4157
4158 alt_tab->shell = shell;
4159 wl_list_init(&alt_tab->preview_list);
4160 alt_tab->current = &alt_tab->preview_list;
4161
4162 restore_all_output_modes(shell->compositor);
4163 lower_fullscreen_layer(alt_tab->shell);
4164
4165 alt_tab->grab.interface = &alt_tab_grab;
4166 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
4167 weston_keyboard_set_focus(seat->keyboard, NULL);
4168
4169 ws = get_current_workspace(shell);
4170
4171 /* FIXME: add some visual candy e.g. prelight the selected view
4172 * and/or add a black surrounding background à la gnome-shell */
4173
4174 /* Determine the size for each preview */
4175 side = output->width / num_surfaces;
4176 if (side > 200)
4177 side = 200;
4178 margin = side / 4;
4179 side -= margin;
4180
4181 x = margin;
4182 y = (output->height - side) / 2;
4183
4184 /* Create a view for each surface */
4185 wl_list_for_each(view, &shell->compositor->view_list, link) {
4186 struct alt_tab_preview *preview;
4187 struct weston_view *v;
4188 float scale;
4189
4190 if (!view_for_alt_tab(view))
4191 continue;
4192
4193 preview = malloc(sizeof *preview);
4194 if (!preview) {
4195 alt_tab_destroy(alt_tab);
4196 return;
4197 }
4198
4199 preview->alt_tab = alt_tab;
4200
4201 preview->view = v = weston_view_create(view->surface);
4202 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004203
4204 wl_list_remove(&v->layer_link);
4205 wl_list_insert(&ws->layer.view_list, &v->layer_link);
4206 weston_view_damage_below(v);
4207 weston_surface_damage(v->surface);
4208
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004209 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004210
4211 preview->listener.notify = alt_tab_handle_surface_destroy;
4212 wl_signal_add(&v->destroy_signal, &preview->listener);
4213
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004214 if (view->surface->width > view->surface->height)
4215 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004216 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004217 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004218
4219 wl_list_insert(&v->geometry.transformation_list,
4220 &preview->transform.link);
4221 weston_matrix_init(&preview->transform.matrix);
4222 weston_matrix_scale(&preview->transform.matrix,
4223 scale, scale, 1.0f);
4224
4225 weston_view_geometry_dirty(v);
4226 weston_compositor_schedule_repaint(v->surface->compositor);
4227
4228 /* Insert at the end of the list */
4229 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
4230
4231 x += side + margin;
4232 }
4233
4234 /* Start at the second preview so a simple <alt>tab changes window.
4235 * We set `current' to the first preview and not the second because
4236 * we're going to receive a key press callback for the initial
4237 * <alt>tab which will make `current' point to the second element. */
4238 alt_tab_next(alt_tab);
4239}
4240
4241static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004242rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4243 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004244{
4245 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004246 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004247 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004248 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004249 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004250
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004251 weston_pointer_move(pointer, x, y);
4252
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004253 if (!shsurf)
4254 return;
4255
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004256 cx = 0.5f * shsurf->surface->width;
4257 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004258
Daniel Stone37816df2012-05-16 18:45:18 +01004259 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4260 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004261 r = sqrtf(dx * dx + dy * dy);
4262
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004263 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004264 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004265
4266 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004267 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004268 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004269
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004270 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004271 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004272
4273 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004274 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004275 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004276 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004277 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004278
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004279 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004281 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004282 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004283 wl_list_init(&shsurf->rotation.transform.link);
4284 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004285 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004286 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004287
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004288 /* We need to adjust the position of the surface
4289 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004290 cposx = shsurf->view->geometry.x + cx;
4291 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004292 dposx = rotate->center.x - cposx;
4293 dposy = rotate->center.y - cposy;
4294 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 weston_view_set_position(shsurf->view,
4296 shsurf->view->geometry.x + dposx,
4297 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004298 }
4299
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004300 /* Repaint implies weston_surface_update_transform(), which
4301 * lazily applies the damage due to rotation update.
4302 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004303 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004304}
4305
4306static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004307rotate_grab_button(struct weston_pointer_grab *grab,
4308 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004309{
4310 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004311 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004312 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004313 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004314 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004315
Daniel Stone4dbadb12012-05-30 16:31:51 +01004316 if (pointer->button_count == 0 &&
4317 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004318 if (shsurf)
4319 weston_matrix_multiply(&shsurf->rotation.rotation,
4320 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004321 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004322 free(rotate);
4323 }
4324}
4325
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004326static void
4327rotate_grab_cancel(struct weston_pointer_grab *grab)
4328{
4329 struct rotate_grab *rotate =
4330 container_of(grab, struct rotate_grab, base.grab);
4331
4332 shell_grab_end(&rotate->base);
4333 free(rotate);
4334}
4335
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004336static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004337 noop_grab_focus,
4338 rotate_grab_motion,
4339 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004340 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004341};
4342
4343static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004344surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004345{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004346 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004347 float dx, dy;
4348 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004349
Pekka Paalanen460099f2012-01-20 16:48:25 +02004350 rotate = malloc(sizeof *rotate);
4351 if (!rotate)
4352 return;
4353
Jason Ekstranda7af7042013-10-12 22:38:11 -05004354 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004355 surface->surface->width * 0.5f,
4356 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004358
Daniel Stone37816df2012-05-16 18:45:18 +01004359 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4360 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004361 r = sqrtf(dx * dx + dy * dy);
4362 if (r > 20.0f) {
4363 struct weston_matrix inverse;
4364
4365 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004366 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004367 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004368
4369 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004370 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004371 } else {
4372 weston_matrix_init(&surface->rotation.rotation);
4373 weston_matrix_init(&rotate->rotation);
4374 }
4375
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004376 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4377 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004378}
4379
4380static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004381rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004382 void *data)
4383{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004384 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004385 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004386 struct shell_surface *surface;
4387
Pekka Paalanen01388e22013-04-25 13:57:44 +03004388 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004389 if (base_surface == NULL)
4390 return;
4391
4392 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004393 if (surface == NULL || surface->state.fullscreen ||
4394 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004395 return;
4396
4397 surface_rotate(surface, seat);
4398}
4399
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004400/* Move all fullscreen layers down to the current workspace in a non-reversible
4401 * manner. This should be used when implementing shell-wide overlays, such as
4402 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004403void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004404lower_fullscreen_layer(struct desktop_shell *shell)
4405{
4406 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004408
4409 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004410 wl_list_for_each_reverse_safe(view, prev,
4411 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004412 layer_link) {
4413 wl_list_remove(&view->layer_link);
4414 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4415 weston_view_damage_below(view);
4416 weston_surface_damage(view->surface);
4417 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004418}
4419
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004420void
Tiago Vignattibe143262012-04-16 17:31:41 +03004421activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004422 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004423{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004424 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004425 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004426 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004427 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004428 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004429
Pekka Paalanen01388e22013-04-25 13:57:44 +03004430 main_surface = weston_surface_get_main_surface(es);
4431
Daniel Stone37816df2012-05-16 18:45:18 +01004432 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004433
Jonas Ådahl8538b222012-08-29 22:13:03 +02004434 state = ensure_focus_state(shell, seat);
4435 if (state == NULL)
4436 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004437
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004438 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004439 state->keyboard_focus = es;
4440 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004441 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004442
Rafael Antognolli03b16592013-12-03 15:35:42 -02004443 shsurf = get_shell_surface(main_surface);
4444 if (shsurf->state.fullscreen)
4445 shell_configure_fullscreen(shsurf);
4446 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004447 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004448
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004449 if (shell->focus_animation_type != ANIMATION_NONE) {
4450 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004451 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004452 }
4453
4454 /* Update the surface’s layer. This brings it to the top of the stacking
4455 * order as appropriate. */
4456 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004457}
4458
Alex Wu21858432012-04-01 20:13:08 +08004459/* no-op func for checking black surface */
4460static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004461black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004462{
4463}
4464
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004465static bool
Alex Wu21858432012-04-01 20:13:08 +08004466is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4467{
4468 if (es->configure == black_surface_configure) {
4469 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004470 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004471 return true;
4472 }
4473 return false;
4474}
4475
Kristian Høgsberg75840622011-09-06 13:48:16 -04004476static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004477activate_binding(struct weston_seat *seat,
4478 struct desktop_shell *shell,
4479 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004480{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004481 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004482
Alex Wu9c35e6b2012-03-05 14:13:13 +08004483 if (!focus)
4484 return;
4485
Pekka Paalanen01388e22013-04-25 13:57:44 +03004486 if (is_black_surface(focus, &main_surface))
4487 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004488
Pekka Paalanen01388e22013-04-25 13:57:44 +03004489 main_surface = weston_surface_get_main_surface(focus);
4490 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004491 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004492
Neil Robertsa28c6932013-10-03 16:43:04 +01004493 activate(shell, focus, seat);
4494}
4495
4496static void
4497click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4498 void *data)
4499{
4500 if (seat->pointer->grab != &seat->pointer->default_grab)
4501 return;
4502
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004503 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004504}
4505
4506static void
4507touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4508{
4509 if (seat->touch->grab != &seat->touch->default_grab)
4510 return;
4511
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004512 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004513}
4514
4515static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004516lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004517{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004518 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004519
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004520 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004521 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004522 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004523 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004524
4525 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004526
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004527 /* Hide all surfaces by removing the fullscreen, panel and
4528 * toplevel layers. This way nothing else can show or receive
4529 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004530
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004531 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004532 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004533 if (shell->showing_input_panels)
4534 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004535 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004536 wl_list_insert(&shell->compositor->cursor_layer.link,
4537 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004538
Pekka Paalanen77346a62011-11-30 16:26:35 +02004539 launch_screensaver(shell);
4540
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004541 /* TODO: disable bindings that should not work while locked. */
4542
4543 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004544}
4545
4546static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004547unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004548{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004549 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004550 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004551 return;
4552 }
4553
4554 /* If desktop-shell client has gone away, unlock immediately. */
4555 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004556 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004557 return;
4558 }
4559
4560 if (shell->prepare_event_sent)
4561 return;
4562
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004563 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004564 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004565}
4566
4567static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004568shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004569{
4570 struct desktop_shell *shell = data;
4571
4572 shell->fade.animation = NULL;
4573
4574 switch (shell->fade.type) {
4575 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004576 weston_surface_destroy(shell->fade.view->surface);
4577 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004578 break;
4579 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004580 lock(shell);
4581 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004582 default:
4583 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004584 }
4585}
4586
Jason Ekstranda7af7042013-10-12 22:38:11 -05004587static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004588shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004589{
4590 struct weston_compositor *compositor = shell->compositor;
4591 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004592 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004593
4594 surface = weston_surface_create(compositor);
4595 if (!surface)
4596 return NULL;
4597
Jason Ekstranda7af7042013-10-12 22:38:11 -05004598 view = weston_view_create(surface);
4599 if (!view) {
4600 weston_surface_destroy(surface);
4601 return NULL;
4602 }
4603
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004604 surface->width = 8192;
4605 surface->height = 8192;
4606 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004607 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004608 wl_list_insert(&compositor->fade_layer.view_list,
4609 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004610 pixman_region32_init(&surface->input);
4611
Jason Ekstranda7af7042013-10-12 22:38:11 -05004612 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004613}
4614
4615static void
4616shell_fade(struct desktop_shell *shell, enum fade_type type)
4617{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004618 float tint;
4619
4620 switch (type) {
4621 case FADE_IN:
4622 tint = 0.0;
4623 break;
4624 case FADE_OUT:
4625 tint = 1.0;
4626 break;
4627 default:
4628 weston_log("shell: invalid fade type\n");
4629 return;
4630 }
4631
4632 shell->fade.type = type;
4633
Jason Ekstranda7af7042013-10-12 22:38:11 -05004634 if (shell->fade.view == NULL) {
4635 shell->fade.view = shell_fade_create_surface(shell);
4636 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004637 return;
4638
Jason Ekstranda7af7042013-10-12 22:38:11 -05004639 shell->fade.view->alpha = 1.0 - tint;
4640 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004641 }
4642
4643 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004644 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004645 else
4646 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004647 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004648 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004649 shell_fade_done, shell);
4650}
4651
4652static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004653do_shell_fade_startup(void *data)
4654{
4655 struct desktop_shell *shell = data;
4656
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004657 if (shell->startup_animation_type == ANIMATION_FADE)
4658 shell_fade(shell, FADE_IN);
4659 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004660 weston_surface_destroy(shell->fade.view->surface);
4661 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004662 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004663}
4664
4665static void
4666shell_fade_startup(struct desktop_shell *shell)
4667{
4668 struct wl_event_loop *loop;
4669
4670 if (!shell->fade.startup_timer)
4671 return;
4672
4673 wl_event_source_remove(shell->fade.startup_timer);
4674 shell->fade.startup_timer = NULL;
4675
4676 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4677 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4678}
4679
4680static int
4681fade_startup_timeout(void *data)
4682{
4683 struct desktop_shell *shell = data;
4684
4685 shell_fade_startup(shell);
4686 return 0;
4687}
4688
4689static void
4690shell_fade_init(struct desktop_shell *shell)
4691{
4692 /* Make compositor output all black, and wait for the desktop-shell
4693 * client to signal it is ready, then fade in. The timer triggers a
4694 * fade-in, in case the desktop-shell client takes too long.
4695 */
4696
4697 struct wl_event_loop *loop;
4698
Jason Ekstranda7af7042013-10-12 22:38:11 -05004699 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004700 weston_log("%s: warning: fade surface already exists\n",
4701 __func__);
4702 return;
4703 }
4704
Jason Ekstranda7af7042013-10-12 22:38:11 -05004705 shell->fade.view = shell_fade_create_surface(shell);
4706 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004707 return;
4708
Jason Ekstranda7af7042013-10-12 22:38:11 -05004709 weston_view_update_transform(shell->fade.view);
4710 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004711
4712 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4713 shell->fade.startup_timer =
4714 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4715 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4716}
4717
4718static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004719idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004720{
4721 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004722 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004723
4724 shell_fade(shell, FADE_OUT);
4725 /* lock() is called from shell_fade_done() */
4726}
4727
4728static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004729wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004730{
4731 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004732 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004733
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004734 unlock(shell);
4735}
4736
4737static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004738show_input_panels(struct wl_listener *listener, void *data)
4739{
4740 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004741 container_of(listener, struct desktop_shell,
4742 show_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004743 struct input_panel_surface *ipsurf, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004744
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004745 shell->text_input.surface = (struct weston_surface*)data;
4746
Jan Arne Petersen451a9712013-02-11 15:10:11 +01004747 if (shell->showing_input_panels)
4748 return;
4749
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004750 shell->showing_input_panels = true;
4751
Jan Arne Petersencf18a322012-11-07 15:32:54 +01004752 if (!shell->locked)
4753 wl_list_insert(&shell->panel_layer.link,
4754 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004755
Jason Ekstranda7af7042013-10-12 22:38:11 -05004756 wl_list_for_each_safe(ipsurf, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02004757 &shell->input_panel.surfaces, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004758 if (!ipsurf->surface->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004759 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004760 wl_list_insert(&shell->input_panel_layer.view_list,
4761 &ipsurf->view->layer_link);
4762 weston_view_geometry_dirty(ipsurf->view);
4763 weston_view_update_transform(ipsurf->view);
4764 weston_surface_damage(ipsurf->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004765 weston_slide_run(ipsurf->view, ipsurf->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004767 }
4768}
4769
4770static void
4771hide_input_panels(struct wl_listener *listener, void *data)
4772{
4773 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004774 container_of(listener, struct desktop_shell,
4775 hide_input_panel_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004776 struct weston_view *view, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004777
Jan Arne Petersen61381972013-01-31 15:52:21 +01004778 if (!shell->showing_input_panels)
4779 return;
4780
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004781 shell->showing_input_panels = false;
4782
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01004783 if (!shell->locked)
4784 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004785
Jason Ekstranda7af7042013-10-12 22:38:11 -05004786 wl_list_for_each_safe(view, next,
4787 &shell->input_panel_layer.view_list, layer_link)
4788 weston_view_unmap(view);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004789}
4790
4791static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004792update_input_panels(struct wl_listener *listener, void *data)
4793{
4794 struct desktop_shell *shell =
4795 container_of(listener, struct desktop_shell,
4796 update_input_panel_listener);
4797
4798 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
4799}
4800
4801static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004802center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004803{
Giulio Camuffob8366642013-04-25 13:57:46 +03004804 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004805 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004806
Jason Ekstranda7af7042013-10-12 22:38:11 -05004807 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004808
4809 x = output->x + (output->width - width) / 2 - surf_x / 2;
4810 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004811
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004812 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004813}
4814
4815static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004816weston_view_set_initial_position(struct weston_view *view,
4817 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004818{
4819 struct weston_compositor *compositor = shell->compositor;
4820 int ix = 0, iy = 0;
4821 int range_x, range_y;
4822 int dx, dy, x, y, panel_height;
4823 struct weston_output *output, *target_output = NULL;
4824 struct weston_seat *seat;
4825
4826 /* As a heuristic place the new window on the same output as the
4827 * pointer. Falling back to the output containing 0, 0.
4828 *
4829 * TODO: Do something clever for touch too?
4830 */
4831 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004832 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004833 ix = wl_fixed_to_int(seat->pointer->x);
4834 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004835 break;
4836 }
4837 }
4838
4839 wl_list_for_each(output, &compositor->output_list, link) {
4840 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4841 target_output = output;
4842 break;
4843 }
4844 }
4845
4846 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004847 weston_view_set_position(view, 10 + random() % 400,
4848 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004849 return;
4850 }
4851
4852 /* Valid range within output where the surface will still be onscreen.
4853 * If this is negative it means that the surface is bigger than
4854 * output.
4855 */
4856 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004857 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004858 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004859 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004860
Rob Bradford4cb88c72012-08-13 15:18:44 +01004861 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004862 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004863 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004864 dx = 0;
4865
4866 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004867 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004868 else
4869 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004870
4871 x = target_output->x + dx;
4872 y = target_output->y + dy;
4873
Jason Ekstranda7af7042013-10-12 22:38:11 -05004874 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004875}
4876
4877static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004878map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004879 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004880{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004881 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004882 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004883 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004884 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004885
Pekka Paalanen77346a62011-11-30 16:26:35 +02004886 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004887 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004888 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004889 if (shsurf->state.fullscreen) {
4890 center_on_output(shsurf->view, shsurf->fullscreen_output);
4891 shell_map_fullscreen(shsurf);
4892 } else if (shsurf->state.maximized) {
4893 /* use surface configure to set the geometry */
4894 panel_height = get_output_panel_height(shell, shsurf->output);
4895 surface_subsurfaces_boundingbox(shsurf->surface,
4896 &surf_x, &surf_y, NULL, NULL);
4897 weston_view_set_position(shsurf->view,
4898 shsurf->output->x - surf_x,
4899 shsurf->output->y +
4900 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004901 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004902 weston_view_set_initial_position(shsurf->view, shell);
4903 }
Juan Zhao96879df2012-02-07 08:45:41 +08004904 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004905 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004906 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004907 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004908 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004909 weston_view_set_position(shsurf->view,
4910 shsurf->view->geometry.x + sx,
4911 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004912 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004913 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004914 default:
4915 ;
4916 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004917
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004918 /* Surface stacking order, see also activate(). */
4919 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004920
Jason Ekstranda7af7042013-10-12 22:38:11 -05004921 if (shsurf->type != SHELL_SURFACE_NONE) {
4922 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004923 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004924 shsurf->surface->output = shsurf->output;
4925 shsurf->view->output = shsurf->output;
4926 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004927 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004928
Rafael Antognollied207b42013-12-03 15:35:43 -02004929 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4930 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4931 }
4932
Jason Ekstranda7af7042013-10-12 22:38:11 -05004933 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004934 /* XXX: xwayland's using the same fields for transient type */
4935 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004936 if (shsurf->transient.flags ==
4937 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4938 break;
4939 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004940 if (shsurf->state.relative &&
4941 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4942 break;
4943 if (!shell->locked)
4944 break;
4945 wl_list_for_each(seat, &compositor->seat_list, link)
4946 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004947 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004948 case SHELL_SURFACE_POPUP:
4949 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004950 default:
4951 break;
4952 }
4953
Rafael Antognolli03b16592013-12-03 15:35:42 -02004954 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4955 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004956 {
4957 switch (shell->win_animation_type) {
4958 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004959 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004960 break;
4961 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004962 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004963 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004964 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004965 default:
4966 break;
4967 }
4968 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004969}
4970
4971static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004972configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004973 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004974{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004975 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004976 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004977 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004978
Pekka Paalanen77346a62011-11-30 16:26:35 +02004979 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004980
Rafael Antognolli03b16592013-12-03 15:35:42 -02004981 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004982 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004983 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004984 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004985 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4986 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004987 mx = shsurf->output->x - surf_x;
4988 my = shsurf->output->y +
4989 get_output_panel_height(shell,shsurf->output) - surf_y;
4990 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004991 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004992 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004993 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004994
Alex Wu4539b082012-03-01 12:57:46 +08004995 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004996 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004997 wl_list_for_each(view, &surface->views, surface_link)
4998 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004999
Rafael Antognolli03b16592013-12-03 15:35:42 -02005000 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005001 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005002 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005003}
5004
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005005static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005006shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005007{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005008 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03005009 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01005010
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005011 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005012
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005013 if (!weston_surface_is_mapped(es) &&
5014 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005015 remove_popup_grab(shsurf);
5016 }
5017
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005018 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005019 return;
5020
Rafael Antognolli03b16592013-12-03 15:35:42 -02005021 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
5022 shsurf->type != shsurf->next_type) ||
5023 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005024 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005025 type_changed = 1;
5026 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005027
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005028 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005029 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005030 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005031 shsurf->last_width != es->width ||
5032 shsurf->last_height != es->height) {
5033 shsurf->last_width = es->width;
5034 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005035 float from_x, from_y;
5036 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005037
Jason Ekstranda7af7042013-10-12 22:38:11 -05005038 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5039 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005040 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005041 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005042 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005043 }
5044}
5045
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005046static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005047
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005048static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005049desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005050{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005051 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005052 struct desktop_shell *shell =
5053 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005054
5055 shell->child.process.pid = 0;
5056 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005057
5058 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5059 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005060 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005061 shell->child.deathstamp = time;
5062 shell->child.deathcount = 0;
5063 }
5064
5065 shell->child.deathcount++;
5066 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005067 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005068 return;
5069 }
5070
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005071 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005072 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005073 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005074}
5075
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005076static void
5077launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005078{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005079 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005080
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005081 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005082 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005083 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005084 desktop_shell_sigchld);
5085
5086 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005087 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005088}
5089
5090static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005091bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5092{
Tiago Vignattibe143262012-04-16 17:31:41 +03005093 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005094 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005095
Jason Ekstranda85118c2013-06-27 20:17:02 -05005096 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5097 if (resource)
5098 wl_resource_set_implementation(resource, &shell_implementation,
5099 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005100}
5101
Kristian Høgsberg75840622011-09-06 13:48:16 -04005102static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005103bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5104{
5105 struct desktop_shell *shell = data;
5106 struct wl_resource *resource;
5107
5108 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5109 if (resource)
5110 wl_resource_set_dispatcher(resource,
5111 xdg_shell_unversioned_dispatch,
5112 NULL, shell, NULL);
5113}
5114
5115static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005116unbind_desktop_shell(struct wl_resource *resource)
5117{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005118 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005119
5120 if (shell->locked)
5121 resume_desktop(shell);
5122
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005123 shell->child.desktop_shell = NULL;
5124 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005125}
5126
5127static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005128bind_desktop_shell(struct wl_client *client,
5129 void *data, uint32_t version, uint32_t id)
5130{
Tiago Vignattibe143262012-04-16 17:31:41 +03005131 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005132 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005133
Jason Ekstranda85118c2013-06-27 20:17:02 -05005134 resource = wl_resource_create(client, &desktop_shell_interface,
5135 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005136
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005137 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005138 wl_resource_set_implementation(resource,
5139 &desktop_shell_implementation,
5140 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005141 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005142
5143 if (version < 2)
5144 shell_fade_startup(shell);
5145
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005146 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005147 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005148
5149 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5150 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005151 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005152}
5153
Pekka Paalanen6e168112011-11-24 11:34:05 +02005154static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005155screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005156{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005157 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005158 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005159
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005160 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005161 return;
5162
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005163 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005164 if (!shell->locked)
5165 return;
5166
Jason Ekstranda7af7042013-10-12 22:38:11 -05005167 view = container_of(surface->views.next, struct weston_view, surface_link);
5168 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005169
Jason Ekstranda7af7042013-10-12 22:38:11 -05005170 if (wl_list_empty(&view->layer_link)) {
5171 wl_list_insert(shell->lock_layer.view_list.prev,
5172 &view->layer_link);
5173 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005174 wl_event_source_timer_update(shell->screensaver.timer,
5175 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005176 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005177 }
5178}
5179
5180static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005181screensaver_set_surface(struct wl_client *client,
5182 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005183 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005184 struct wl_resource *output_resource)
5185{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005186 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005187 struct weston_surface *surface =
5188 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005189 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005190 struct weston_view *view, *next;
5191
5192 /* Make sure we only have one view */
5193 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5194 weston_view_destroy(view);
5195 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005196
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005197 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005198 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005199 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005200}
5201
5202static const struct screensaver_interface screensaver_implementation = {
5203 screensaver_set_surface
5204};
5205
5206static void
5207unbind_screensaver(struct wl_resource *resource)
5208{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005209 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005210
Pekka Paalanen77346a62011-11-30 16:26:35 +02005211 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005212}
5213
5214static void
5215bind_screensaver(struct wl_client *client,
5216 void *data, uint32_t version, uint32_t id)
5217{
Tiago Vignattibe143262012-04-16 17:31:41 +03005218 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005219 struct wl_resource *resource;
5220
Jason Ekstranda85118c2013-06-27 20:17:02 -05005221 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005222
Pekka Paalanen77346a62011-11-30 16:26:35 +02005223 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005224 wl_resource_set_implementation(resource,
5225 &screensaver_implementation,
5226 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005227 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005228 return;
5229 }
5230
5231 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5232 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005233 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005234}
5235
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005236static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005237input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005238{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005239 struct input_panel_surface *ip_surface = surface->configure_private;
5240 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005241 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005242 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005243
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005244 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005245 return;
5246
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005247 if (!weston_surface_is_mapped(surface)) {
5248 if (!shell->showing_input_panels)
5249 return;
5250
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005251 show_surface = 1;
5252 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01005253
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005254 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005255
5256 if (ip_surface->panel) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005257 x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2;
5258 y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005259 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005260 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
5261 y = ip_surface->output->y + ip_surface->output->height - surface->height;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005262 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005263
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005264 weston_view_set_position(ip_surface->view, x, y);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005265
5266 if (show_surface) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005267 wl_list_insert(&shell->input_panel_layer.view_list,
5268 &ip_surface->view->layer_link);
5269 weston_view_update_transform(ip_surface->view);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005270 weston_surface_damage(surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005271 weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005272 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005273}
5274
5275static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005276destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005277{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005278 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
5279
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005280 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005281 wl_list_remove(&input_panel_surface->link);
5282
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005283 input_panel_surface->surface->configure = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005284 weston_view_destroy(input_panel_surface->view);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005285
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005286 free(input_panel_surface);
5287}
5288
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005289static struct input_panel_surface *
5290get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005291{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005292 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005293 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005294 } else {
5295 return NULL;
5296 }
5297}
5298
5299static void
5300input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
5301{
5302 struct input_panel_surface *ipsurface = container_of(listener,
5303 struct input_panel_surface,
5304 surface_destroy_listener);
5305
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005306 if (ipsurface->resource) {
5307 wl_resource_destroy(ipsurface->resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005308 } else {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005309 destroy_input_panel_surface(ipsurface);
5310 }
5311}
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005312
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005313static struct input_panel_surface *
5314create_input_panel_surface(struct desktop_shell *shell,
5315 struct weston_surface *surface)
5316{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005317 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005318
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005319 input_panel_surface = calloc(1, sizeof *input_panel_surface);
5320 if (!input_panel_surface)
5321 return NULL;
5322
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04005323 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005324 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005325
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005326 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005327
5328 input_panel_surface->surface = surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005329 input_panel_surface->view = weston_view_create(surface);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005330
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005331 wl_signal_init(&input_panel_surface->destroy_signal);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005332 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005333 wl_signal_add(&surface->destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005334 &input_panel_surface->surface_destroy_listener);
5335
5336 wl_list_init(&input_panel_surface->link);
5337
5338 return input_panel_surface;
5339}
5340
5341static void
5342input_panel_surface_set_toplevel(struct wl_client *client,
5343 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02005344 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005345 uint32_t position)
5346{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005347 struct input_panel_surface *input_panel_surface =
5348 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005349 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02005350
5351 wl_list_insert(&shell->input_panel.surfaces,
5352 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005353
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005354 input_panel_surface->output = wl_resource_get_user_data(output_resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005355 input_panel_surface->panel = 0;
5356}
5357
5358static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005359input_panel_surface_set_overlay_panel(struct wl_client *client,
5360 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005361{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005362 struct input_panel_surface *input_panel_surface =
5363 wl_resource_get_user_data(resource);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005364 struct desktop_shell *shell = input_panel_surface->shell;
5365
5366 wl_list_insert(&shell->input_panel.surfaces,
5367 &input_panel_surface->link);
5368
5369 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005370}
5371
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005372static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005373 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02005374 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005375};
5376
5377static void
5378destroy_input_panel_surface_resource(struct wl_resource *resource)
5379{
Jason Ekstrand51e5b142013-06-14 10:07:58 -05005380 struct input_panel_surface *ipsurf =
5381 wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005382
5383 destroy_input_panel_surface(ipsurf);
5384}
5385
5386static void
5387input_panel_get_input_panel_surface(struct wl_client *client,
5388 struct wl_resource *resource,
5389 uint32_t id,
5390 struct wl_resource *surface_resource)
5391{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005392 struct weston_surface *surface =
5393 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005394 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005395 struct input_panel_surface *ipsurf;
5396
5397 if (get_input_panel_surface(surface)) {
5398 wl_resource_post_error(surface_resource,
5399 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005400 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005401 return;
5402 }
5403
5404 ipsurf = create_input_panel_surface(shell, surface);
5405 if (!ipsurf) {
5406 wl_resource_post_error(surface_resource,
5407 WL_DISPLAY_ERROR_INVALID_OBJECT,
5408 "surface->configure already set");
5409 return;
5410 }
5411
Jason Ekstranda85118c2013-06-27 20:17:02 -05005412 ipsurf->resource =
5413 wl_resource_create(client,
5414 &wl_input_panel_surface_interface, 1, id);
5415 wl_resource_set_implementation(ipsurf->resource,
5416 &input_panel_surface_implementation,
5417 ipsurf,
5418 destroy_input_panel_surface_resource);
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005419}
5420
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02005421static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01005422 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005423};
5424
5425static void
5426unbind_input_panel(struct wl_resource *resource)
5427{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005428 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005429
5430 shell->input_panel.binding = NULL;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005431}
5432
5433static void
5434bind_input_panel(struct wl_client *client,
5435 void *data, uint32_t version, uint32_t id)
5436{
5437 struct desktop_shell *shell = data;
5438 struct wl_resource *resource;
5439
Jason Ekstranda85118c2013-06-27 20:17:02 -05005440 resource = wl_resource_create(client,
5441 &wl_input_panel_interface, 1, id);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005442
5443 if (shell->input_panel.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005444 wl_resource_set_implementation(resource,
5445 &input_panel_implementation,
5446 shell, unbind_input_panel);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005447 shell->input_panel.binding = resource;
5448 return;
5449 }
5450
5451 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5452 "interface object already bound");
5453 wl_resource_destroy(resource);
5454}
5455
Kristian Høgsberg07045392012-02-19 18:52:44 -05005456struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005457 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005458 struct weston_surface *current;
5459 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005460 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005461};
5462
5463static void
5464switcher_next(struct switcher *switcher)
5465{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005466 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005467 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005468 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005469 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005470
Jason Ekstranda7af7042013-10-12 22:38:11 -05005471 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005472 shsurf = get_shell_surface(view->surface);
5473 switch (shsurf->type) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005474 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005475 if (shsurf->parent)
5476 break;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005477 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005478 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005479 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005480 next = view->surface;
5481 prev = view->surface;
5482 view->alpha = 0.25;
5483 weston_view_geometry_dirty(view);
5484 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005485 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005486 case SHELL_SURFACE_POPUP:
5487 case SHELL_SURFACE_XWAYLAND:
5488 case SHELL_SURFACE_NONE:
Kristian Høgsberg07045392012-02-19 18:52:44 -05005489 default:
5490 break;
5491 }
Alex Wu1659daa2012-04-01 20:13:09 +08005492
Jason Ekstranda7af7042013-10-12 22:38:11 -05005493 if (is_black_surface(view->surface, NULL)) {
5494 view->alpha = 0.25;
5495 weston_view_geometry_dirty(view);
5496 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005497 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005498 }
5499
5500 if (next == NULL)
5501 next = first;
5502
Alex Wu07b26062012-03-12 16:06:01 +08005503 if (next == NULL)
5504 return;
5505
Kristian Høgsberg07045392012-02-19 18:52:44 -05005506 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005507 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005508
5509 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005510 wl_list_for_each(view, &next->views, surface_link)
5511 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005512
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005513 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005514 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005515 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005516}
5517
5518static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005519switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005520{
5521 struct switcher *switcher =
5522 container_of(listener, struct switcher, listener);
5523
5524 switcher_next(switcher);
5525}
5526
5527static void
Daniel Stone351eb612012-05-31 15:27:47 -04005528switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005529{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005530 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005531 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005532 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005533
Jason Ekstranda7af7042013-10-12 22:38:11 -05005534 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005535 if (is_focus_view(view))
5536 continue;
5537
Jason Ekstranda7af7042013-10-12 22:38:11 -05005538 view->alpha = 1.0;
5539 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005540 }
5541
Alex Wu07b26062012-03-12 16:06:01 +08005542 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005543 activate(switcher->shell, switcher->current,
5544 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005545 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005546 weston_keyboard_end_grab(keyboard);
5547 if (keyboard->input_method_resource)
5548 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005549 free(switcher);
5550}
5551
5552static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005553switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005554 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005555{
5556 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005557 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005558
Daniel Stonec9785ea2012-05-30 16:31:52 +01005559 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005560 switcher_next(switcher);
5561}
5562
5563static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005564switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005565 uint32_t mods_depressed, uint32_t mods_latched,
5566 uint32_t mods_locked, uint32_t group)
5567{
5568 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005569 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005570
Daniel Stone351eb612012-05-31 15:27:47 -04005571 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5572 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005573}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005574
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005575static void
5576switcher_cancel(struct weston_keyboard_grab *grab)
5577{
5578 struct switcher *switcher = container_of(grab, struct switcher, grab);
5579
5580 switcher_destroy(switcher);
5581}
5582
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005583static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005584 switcher_key,
5585 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005586 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005587};
5588
5589static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005590switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005591 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005592{
Tiago Vignattibe143262012-04-16 17:31:41 +03005593 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005594 struct switcher *switcher;
5595
5596 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005597 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005598 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005599 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005600 wl_list_init(&switcher->listener.link);
5601
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005602 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005603 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005604 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005605 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5606 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005607 switcher_next(switcher);
5608}
5609
Pekka Paalanen3c647232011-12-22 13:43:43 +02005610static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005611backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005612 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005613{
5614 struct weston_compositor *compositor = data;
5615 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005616 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005617
5618 /* TODO: we're limiting to simple use cases, where we assume just
5619 * control on the primary display. We'd have to extend later if we
5620 * ever get support for setting backlights on random desktop LCD
5621 * panels though */
5622 output = get_default_output(compositor);
5623 if (!output)
5624 return;
5625
5626 if (!output->set_backlight)
5627 return;
5628
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005629 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5630 backlight_new = output->backlight_current - 25;
5631 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5632 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005633
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005634 if (backlight_new < 5)
5635 backlight_new = 5;
5636 if (backlight_new > 255)
5637 backlight_new = 255;
5638
5639 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005640 output->set_backlight(output, output->backlight_current);
5641}
5642
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005643struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005644 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005645 struct weston_seat *seat;
5646 uint32_t key[2];
5647 int key_released[2];
5648};
5649
5650static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005651debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005652 uint32_t key, uint32_t state)
5653{
5654 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005655 struct weston_compositor *ec = db->seat->compositor;
5656 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005657 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005658 uint32_t serial;
5659 int send = 0, terminate = 0;
5660 int check_binding = 1;
5661 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005662 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005663
5664 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5665 /* Do not run bindings on key releases */
5666 check_binding = 0;
5667
5668 for (i = 0; i < 2; i++)
5669 if (key == db->key[i])
5670 db->key_released[i] = 1;
5671
5672 if (db->key_released[0] && db->key_released[1]) {
5673 /* All key releases been swalled so end the grab */
5674 terminate = 1;
5675 } else if (key != db->key[0] && key != db->key[1]) {
5676 /* Should not swallow release of other keys */
5677 send = 1;
5678 }
5679 } else if (key == db->key[0] && !db->key_released[0]) {
5680 /* Do not check bindings for the first press of the binding
5681 * key. This allows it to be used as a debug shortcut.
5682 * We still need to swallow this event. */
5683 check_binding = 0;
5684 } else if (db->key[1]) {
5685 /* If we already ran a binding don't process another one since
5686 * we can't keep track of all the binding keys that were
5687 * pressed in order to swallow the release events. */
5688 send = 1;
5689 check_binding = 0;
5690 }
5691
5692 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005693 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5694 key, state)) {
5695 /* We ran a binding so swallow the press and keep the
5696 * grab to swallow the released too. */
5697 send = 0;
5698 terminate = 0;
5699 db->key[1] = key;
5700 } else {
5701 /* Terminate the grab since the key pressed is not a
5702 * debug binding key. */
5703 send = 1;
5704 terminate = 1;
5705 }
5706 }
5707
5708 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005709 serial = wl_display_next_serial(display);
5710 resource_list = &grab->keyboard->focus_resource_list;
5711 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005712 wl_keyboard_send_key(resource, serial, time, key, state);
5713 }
5714 }
5715
5716 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005717 weston_keyboard_end_grab(grab->keyboard);
5718 if (grab->keyboard->input_method_resource)
5719 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005720 free(db);
5721 }
5722}
5723
5724static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005725debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005726 uint32_t mods_depressed, uint32_t mods_latched,
5727 uint32_t mods_locked, uint32_t group)
5728{
5729 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005730 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005731
Neil Roberts96d790e2013-09-19 17:32:00 +01005732 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005733
Neil Roberts96d790e2013-09-19 17:32:00 +01005734 wl_resource_for_each(resource, resource_list) {
5735 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5736 mods_latched, mods_locked, group);
5737 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005738}
5739
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005740static void
5741debug_binding_cancel(struct weston_keyboard_grab *grab)
5742{
5743 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5744
5745 weston_keyboard_end_grab(grab->keyboard);
5746 free(db);
5747}
5748
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005749struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005750 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005751 debug_binding_modifiers,
5752 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005753};
5754
5755static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005756debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005757{
5758 struct debug_binding_grab *grab;
5759
5760 grab = calloc(1, sizeof *grab);
5761 if (!grab)
5762 return;
5763
5764 grab->seat = (struct weston_seat *) seat;
5765 grab->key[0] = key;
5766 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005767 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005768}
5769
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005770static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005771force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005772 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005773{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005774 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005775 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005776 struct desktop_shell *shell = data;
5777 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005778 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005779
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005780 focus_surface = seat->keyboard->focus;
5781 if (!focus_surface)
5782 return;
5783
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005784 wl_signal_emit(&compositor->kill_signal, focus_surface);
5785
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005786 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005787 wl_client_get_credentials(client, &pid, NULL, NULL);
5788
5789 /* Skip clients that we launched ourselves (the credentials of
5790 * the socketpair is ours) */
5791 if (pid == getpid())
5792 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005793
Daniel Stone325fc2d2012-05-30 16:31:58 +01005794 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005795}
5796
5797static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005798workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005799 uint32_t key, void *data)
5800{
5801 struct desktop_shell *shell = data;
5802 unsigned int new_index = shell->workspaces.current;
5803
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005804 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005805 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005806 if (new_index != 0)
5807 new_index--;
5808
5809 change_workspace(shell, new_index);
5810}
5811
5812static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005813workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005814 uint32_t key, void *data)
5815{
5816 struct desktop_shell *shell = data;
5817 unsigned int new_index = shell->workspaces.current;
5818
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005819 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005820 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005821 if (new_index < shell->workspaces.num - 1)
5822 new_index++;
5823
5824 change_workspace(shell, new_index);
5825}
5826
5827static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005828workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005829 uint32_t key, void *data)
5830{
5831 struct desktop_shell *shell = data;
5832 unsigned int new_index;
5833
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005834 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005835 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005836 new_index = key - KEY_F1;
5837 if (new_index >= shell->workspaces.num)
5838 new_index = shell->workspaces.num - 1;
5839
5840 change_workspace(shell, new_index);
5841}
5842
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005843static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005844workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005845 uint32_t key, void *data)
5846{
5847 struct desktop_shell *shell = data;
5848 unsigned int new_index = shell->workspaces.current;
5849
5850 if (shell->locked)
5851 return;
5852
5853 if (new_index != 0)
5854 new_index--;
5855
5856 take_surface_to_workspace_by_seat(shell, seat, new_index);
5857}
5858
5859static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005860workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005861 uint32_t key, void *data)
5862{
5863 struct desktop_shell *shell = data;
5864 unsigned int new_index = shell->workspaces.current;
5865
5866 if (shell->locked)
5867 return;
5868
5869 if (new_index < shell->workspaces.num - 1)
5870 new_index++;
5871
5872 take_surface_to_workspace_by_seat(shell, seat, new_index);
5873}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005874
5875static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005876handle_output_destroy(struct wl_listener *listener, void *data)
5877{
5878 struct shell_output *output_listener =
5879 container_of(listener, struct shell_output, destroy_listener);
5880
5881 wl_list_remove(&output_listener->destroy_listener.link);
5882 wl_list_remove(&output_listener->link);
5883 free(output_listener);
5884}
5885
5886static void
5887create_shell_output(struct desktop_shell *shell,
5888 struct weston_output *output)
5889{
5890 struct shell_output *shell_output;
5891
5892 shell_output = zalloc(sizeof *shell_output);
5893 if (shell_output == NULL)
5894 return;
5895
5896 shell_output->output = output;
5897 shell_output->shell = shell;
5898 shell_output->destroy_listener.notify = handle_output_destroy;
5899 wl_signal_add(&output->destroy_signal,
5900 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005901 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005902}
5903
5904static void
5905handle_output_create(struct wl_listener *listener, void *data)
5906{
5907 struct desktop_shell *shell =
5908 container_of(listener, struct desktop_shell, output_create_listener);
5909 struct weston_output *output = (struct weston_output *)data;
5910
5911 create_shell_output(shell, output);
5912}
5913
5914static void
5915setup_output_destroy_handler(struct weston_compositor *ec,
5916 struct desktop_shell *shell)
5917{
5918 struct weston_output *output;
5919
5920 wl_list_init(&shell->output_list);
5921 wl_list_for_each(output, &ec->output_list, link)
5922 create_shell_output(shell, output);
5923
5924 shell->output_create_listener.notify = handle_output_create;
5925 wl_signal_add(&ec->output_created_signal,
5926 &shell->output_create_listener);
5927}
5928
5929static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005930shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005931{
Tiago Vignattibe143262012-04-16 17:31:41 +03005932 struct desktop_shell *shell =
5933 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005934 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005935 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005936
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005937 if (shell->child.client)
5938 wl_client_destroy(shell->child.client);
5939
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005940 wl_list_remove(&shell->idle_listener.link);
5941 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005942 wl_list_remove(&shell->show_input_panel_listener.link);
5943 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005944
Xiong Zhang6b481422013-10-23 13:58:32 +08005945 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5946 wl_list_remove(&shell_output->destroy_listener.link);
5947 wl_list_remove(&shell_output->link);
5948 free(shell_output);
5949 }
5950
5951 wl_list_remove(&shell->output_create_listener.link);
5952
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005953 wl_array_for_each(ws, &shell->workspaces.array)
5954 workspace_destroy(*ws);
5955 wl_array_release(&shell->workspaces.array);
5956
Pekka Paalanen3c647232011-12-22 13:43:43 +02005957 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005958 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005959 free(shell);
5960}
5961
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005962static void
5963shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5964{
5965 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005966 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005967
5968 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005969 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5970 MODIFIER_CTRL | MODIFIER_ALT,
5971 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005972 weston_compositor_add_key_binding(ec, KEY_TAB,
5973 MODIFIER_ALT,
5974 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005975 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5976 click_to_activate_binding,
5977 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005978 weston_compositor_add_touch_binding(ec, 0,
5979 touch_to_activate_binding,
5980 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005981 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5982 MODIFIER_SUPER | MODIFIER_ALT,
5983 surface_opacity_binding, NULL);
5984 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5985 MODIFIER_SUPER, zoom_axis_binding,
5986 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005987
5988 /* configurable bindings */
5989 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005990 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5991 zoom_key_binding, NULL);
5992 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5993 zoom_key_binding, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02005994 weston_compositor_add_key_binding(ec, KEY_M, mod, maximize_binding,
5995 NULL);
5996 weston_compositor_add_key_binding(ec, KEY_F, mod, fullscreen_binding,
5997 NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005998 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5999 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006000 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006001 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6002 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006003
6004 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6005 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6006 rotate_binding, NULL);
6007
Daniel Stone325fc2d2012-05-30 16:31:58 +01006008 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6009 shell);
6010 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6011 ec);
6012 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6013 backlight_binding, ec);
6014 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6015 ec);
6016 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6017 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006018 weston_compositor_add_key_binding(ec, KEY_K, mod,
6019 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006020 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6021 workspace_up_binding, shell);
6022 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6023 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006024 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6025 workspace_move_surface_up_binding,
6026 shell);
6027 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6028 workspace_move_surface_down_binding,
6029 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006030
Daniel Stonedf8133b2013-11-19 11:37:14 +01006031 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
6032
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006033 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6034 if (shell->workspaces.num > 1) {
6035 num_workspace_bindings = shell->workspaces.num;
6036 if (num_workspace_bindings > 6)
6037 num_workspace_bindings = 6;
6038 for (i = 0; i < num_workspace_bindings; i++)
6039 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6040 workspace_f_binding,
6041 shell);
6042 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006043
6044 /* Debug bindings */
6045 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6046 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006047}
6048
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006049WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006050module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006051 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006052{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006053 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006054 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006055 struct workspace **pws;
6056 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006057 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006058
Peter Huttererf3d62272013-08-08 11:57:05 +10006059 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006060 if (shell == NULL)
6061 return -1;
6062
Kristian Høgsberg75840622011-09-06 13:48:16 -04006063 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006064
6065 shell->destroy_listener.notify = shell_destroy;
6066 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006067 shell->idle_listener.notify = idle_handler;
6068 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6069 shell->wake_listener.notify = wake_handler;
6070 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006071 shell->show_input_panel_listener.notify = show_input_panels;
6072 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
6073 shell->hide_input_panel_listener.notify = hide_input_panels;
6074 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006075 shell->update_input_panel_listener.notify = update_input_panels;
6076 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06006077 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006078 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006079 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006080 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006081 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006082 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05006083 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006084 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04006085 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006086 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006087 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006088
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006089 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02006090
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006091 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6092 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006093 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6094 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006095 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006096
6097 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006098 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006099
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006100 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006101
Daniel Stonedf8133b2013-11-19 11:37:14 +01006102 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6103 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6104
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006105 for (i = 0; i < shell->workspaces.num; i++) {
6106 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6107 if (pws == NULL)
6108 return -1;
6109
6110 *pws = workspace_create();
6111 if (*pws == NULL)
6112 return -1;
6113 }
6114 activate_workspace(shell, 0);
6115
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006116 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006117 wl_list_init(&shell->workspaces.animation.link);
6118 shell->workspaces.animation.frame = animate_workspace_change_frame;
6119
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006120 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006121 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006122 return -1;
6123
Rafael Antognollie2a34552013-12-03 15:35:45 -02006124 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6125 shell, bind_xdg_shell) == NULL)
6126 return -1;
6127
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006128 if (wl_global_create(ec->wl_display,
6129 &desktop_shell_interface, 2,
6130 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006131 return -1;
6132
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006133 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6134 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006135 return -1;
6136
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006137 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006138 shell, bind_input_panel) == NULL)
6139 return -1;
6140
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006141 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6142 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006143 return -1;
6144
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006145 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006146
Xiong Zhang6b481422013-10-23 13:58:32 +08006147 setup_output_destroy_handler(ec, shell);
6148
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006149 loop = wl_display_get_event_loop(ec->wl_display);
6150 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006151
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006152 shell->screensaver.timer =
6153 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6154
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006155 wl_list_for_each(seat, &ec->seat_list, link)
6156 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006157
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006158 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006159
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006160 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006161
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006162 return 0;
6163}